Running an environment

Any gym environment can be initialized and run by using a simple interface. Let's start by importing the gym library, as follows:

  1. First we import the gym library:
import gym 
  1. Next, create an environment by passing an argument to gym.make. In the following code, CartPole is used as an example:
environment = gym.make('CartPole-v0') 
  1. Next, reset the environment:
  1. Then, start an iteration and render the environment, as follows:
for dummy in range(100):    environment.render()     environment.step(environment.action_space.sample())

Also, change the action space at every step, to see CartPole moving. Running the preceding program should produce a visualization. The scene should start with a visualization, ...

Get Python Reinforcement Learning Projects now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.