How to use
The environments in the Stable Gym package can be imported like any other
gymnasium environments. You can then use the gym.vector.make()
function to create an instance of
the environment. Here’s a bare minimum example of using one of the Stable Gym environment. This will run an instance of the
Oscillator-v1 environment for 1000 timesteps. You should see the observations
being printed to the console. More examples can be found in the Stable Gym examples folder.
1"""A simple example on how to use the Stable Gym gymnasium environments."""
2
3import gymnasium as gym
4
5# ENV_NAME = "stable_gym:Oscillator-v1"
6# ENV_NAME = "stable_gym:CartPoleCost-v1"
7# ENV_NAME = "stable_gym:SwimmerCost-v1"
8# ENV_NAME = "stable_gym:FetchReachCost-v1"
9# ENV_NAME = "stable_gym:MinitaurBulletCost-v1"
10ENV_NAME = "stable_gym:QuadXHoverCost-v1"
11
12if __name__ == "__main__":
13 env = gym.make(ENV_NAME, render_mode="human")
14
15 # Define a policy function.
16 # NOTE: Can be any function that takes an observation and returns an action.
17 def policy(*args, **kwargs):
18 """A simple policy that samples random actions."""
19 return env.action_space.sample()
20
21 # Run training loop.
22 observation, info = env.reset(seed=42)
23 for _ in range(1000):
24 action = policy(observation) # User-defined policy function
25 observation, reward, terminated, truncated, info = env.step(action)
26
27 if terminated or truncated:
28 print("Environment terminated or truncated. Resetting.")
29 observation, info = env.reset()
30 env.close()
Important
Some of the environments in this package do not have a render method.