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

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:

主站蜘蛛池模板: 田东县| 陇川县| 崇礼县| 清河县| 六枝特区| 京山县| 历史| 龙川县| 凤庆县| 突泉县| 墨竹工卡县| 化隆| 衡东县| 威远县| 玛曲县| 博乐市| 枣强县| 杭锦后旗| 海盐县| 麻城市| 宁都县| 青岛市| 罗甸县| 长兴县| 文水县| 柏乡县| 丹巴县| 连州市| 新田县| 伊川县| 应城市| 玉龙| 乐山市| 浮山县| 改则县| 汉川市| 固安县| 天峻县| 门源| 兴国县| 奎屯市|