Thursday, July 23, 2020

Constructing an Environment with Python for building reinforcement learning agent

For building reinforcement learning agent, we will be using the OpenAI Gym package
which can be installed with the help of the following command:

pip install gym

There are various environments in OpenAI gym which can be used for various purposes.
Few of them are Cartpole-v0, Hopper-v1, and MsPacman-v0. They require different
engines. The detail documentation of OpenAI Gym can be found on

https://gym.openai.com/docs/#environments.

The following code shows an example of Python code for cartpole-v0 environment:

import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
    env.render()
    env.step(env.action_space.sample())


Python – Balancing CartPole with Machine Learning – Useful code
We can construct other environments in a similar way.

For building reinforcement learning agent, we will be using the OpenAI Gym package as shown:

import gym
env = gym.make('CartPole-v0')
for _ in range(20):
observation = env.reset()
for i in range(100):
env.render()
print(observation)
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
    print("Episode finished after {} timesteps".format(i+1))
    break

Bytepawn - Marton Trencseni – Solving the CartPole Reinforcement ...
You may observe that the cartpole can balance itself.
Share:

0 comments:

Post a Comment