- Python Reinforcement Learning
- Sudharsan Ravichandiran Sean Saito Rajalingappaa Shanmugamani Yang Wenzhuo
- 180字
- 2021-06-24 15:17:27
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:

推薦閱讀
- Effective Amazon Machine Learning
- Access 2007數據庫應用上機指導與練習
- MySQL基礎教程
- 軟件成本度量國家標準實施指南:理論、方法與實踐
- Python金融數據分析(原書第2版)
- ZeroMQ
- 數據庫技術實用教程
- Oracle PL/SQL實例精解(原書第5版)
- 圖數據實戰:用圖思維和圖技術解決復雜問題
- SQL應用及誤區分析
- 數據分析師養成寶典
- Spring MVC Beginner’s Guide
- Hands-On System Programming with C++
- Visual Studio 2012 and .NET 4.5 Expert Development Cookbook
- ECharts數據可視化:入門、實戰與進階