上QQ阅读APP看书,第一时间看更新
Basic simulations
Let's see how to simulate a basic cart pole environment:
- First, let's import the library:
import gym
- The next step is to create a simulation instance using the make function:
env = gym.make('CartPole-v0')
- Then we should initialize the environment using the reset method:
env.reset()
- Then we can loop for some time steps and render the environment at each step:
for _ in range(1000):
env.render()
env.step(env.action_space.sample())
The complete code is as follows:
import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
env.render()
env.step(env.action_space.sample())
If you run the preceding program, you can see the output, which shows the cart pole environment:
OpenAI Gym provides a lot of simulation environments for training, evaluating, and building our agents. We can check the available environments by either checking their website or simply typing the following, which will list the available environments:
from gym import envs
print(envs.registry.all())
Since Gym provides different interesting environments, let's simulate a car racing environment, shown as follows:
import gym
env = gym.make('CarRacing-v0')
env.reset()
for _ in range(1000):
env.render()
env.step(env.action_space.sample())
You will get the output as follows: