官术网_书友最值得收藏!

Basic simulations

Let's see how to simulate a basic cart pole environment:

  1. First, let's import the library:
import gym
  1. The next step is to create a simulation instance using the make function:
env = gym.make('CartPole-v0')
  1. Then we should initialize the environment using the reset method:
env.reset()
  1. 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:

主站蜘蛛池模板: 永康市| 镇平县| 宁阳县| 天水市| 海口市| 三门县| 彰化市| 武鸣县| 临城县| 崇仁县| 澎湖县| 林西县| 永新县| 成都市| 苗栗县| 乌拉特中旗| 且末县| 新晃| 灯塔市| 晋江市| 湛江市| 吴旗县| 富裕县| 思茅市| 扎兰屯市| 云霄县| 赣州市| 咸丰县| 湖北省| 常熟市| 西乡县| 临武县| 平武县| 精河县| 张家口市| 新晃| 通州区| 浑源县| 临夏县| 闸北区| 荃湾区|