You are on page 1of 2

ASSIGNMENT - 1

In [1]:

import gym

In [2]:
env = gym.make('MountainCar-v0',render_mode="rgb_array")

In [3]:
# Observation and action space
obs_space = env.observation_space
action_space = env.action_space
print("The observation space: {} ".format(obs_space))
print("The action space: {} ".format(action_space))

The observation space: Box([-1.2 -0.07], [0.6 0.07], (2,), float32)


The action space: Discrete(3)

In [4]:

# pip install gym[classic_control]

In [5]:
import matplotlib.pyplot as plt

# reset the environment and see the initial observation


obs = env.reset()
print("The initial observation is {} ".format(obs))

# Sample a random action from the entire action space


random_action = env.action_space.sample()

# # Take the action and get the new observation space


new_obs, reward, done, info, a = env.step(random_action)
print("The new observation is {} ".format(new_obs))

The initial observation is (array([-0.5884122, 0. ], dtype=float32), {})


The new observation is [-5.8892918e-01 -5.1695626e-04]

In [6]:
env_screen = env.render()
env.close()

import matplotlib.pyplot as plt


plt.imshow(env_screen)
Out[6]:

<matplotlib.image.AxesImage at 0x266f66029e0>
In [7]:

import time

# Number of steps you run the agent for


num_steps = 150

obs = env.reset()

for step in range(num_steps):


# take random action, but you can also do something more intelligent
# action = my_intelligent_agent_fn(obs)
action = env.action_space.sample()

# apply the action


obs, reward, done, info ,a= env.step(action)

# Render the env


env.render()

# Wait a bit before the next frame unless you want to see a crazy fast video
time.sleep(0.001)
plt.imshow(env.render())
# If the epsiode is up, then start another one
if done:
env.reset()

# Close the env


env.close()

You might also like