- Hands-On Reinforcement Learning with Python
- Sudharsan Ravichandiran
- 180字
- 2021-06-18 19:12:07
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:

推薦閱讀
- Learning Scala Programming
- GAE編程指南
- DevOps with Kubernetes
- 零基礎(chǔ)PHP學(xué)習(xí)筆記
- INSTANT Weka How-to
- Building an RPG with Unity 2018
- Learning SciPy for Numerical and Scientific Computing(Second Edition)
- Kotlin編程實戰(zhàn):創(chuàng)建優(yōu)雅、富于表現(xiàn)力和高性能的JVM與Android應(yīng)用程序
- D3.js By Example
- Java EE企業(yè)級應(yīng)用開發(fā)教程(Spring+Spring MVC+MyBatis)
- 響應(yīng)式Web設(shè)計:HTML5和CSS3實戰(zhàn)(第2版)
- Image Processing with ImageJ
- Python程序設(shè)計開發(fā)寶典
- C++程序設(shè)計教程
- Java從入門到精通(視頻實戰(zhàn)版)