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

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:

主站蜘蛛池模板: 安塞县| 柘城县| 苍山县| 景东| 广昌县| 莎车县| 北京市| 南和县| 阳高县| 云浮市| 吉木乃县| 休宁县| 大关县| 阜平县| 襄汾县| 临朐县| 什邡市| 东丽区| 铜山县| 濮阳县| 甘德县| 榆社县| 南岸区| 和田县| 普兰县| 怀仁县| 玉门市| 黔江区| 松阳县| 西盟| 确山县| 荣成市| 林甸县| 辛集市| 特克斯县| 巩义市| 洞头县| 永春县| 安平县| 新津县| 黄浦区|