<jupyter_start><jupyter_text><jupyter_code># In colab please uncomment this to install Atari 
# Box2d is a 2D physics engine.
!pip install box2d-py
# And for visualization on Colab install
!pip install pyglet
!apt-get install -y xvfb python-opengl > /dev/null 2>&1
!pip install gym pyvirtualdisplay > /dev/null 2>&1

import gym
import matplotlib.pyplot as plt
import pandas as pd
from IPython import display as ipythondisplay  ## Needed on colab
## Need to set a virtual Display on colab, else ipythondisplay would not work
from pyvirtualdisplay import Display
display = Display(visible=0, size=(400, 300))
display.start()<jupyter_output><empty_output><jupyter_text>Physics Engine<jupyter_code>env = gym.make('LunarLander-v2')
obs = env.reset() 
img = env.render(mode='rgb_array')
env.close()
plt.imshow(img)
#ipythondisplay.clear_output(wait=True)  #Colab<jupyter_output><empty_output><jupyter_text>Classic Control<jupyter_code>env = gym.make('CartPole-v0')
env.reset()
img = env.render(mode='rgb_array')
env.close()
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>Atari Games<jupyter_code>!wget http://www.atarimania.com/roms/Roms.rar
!mkdir /content/ROM/
!unrar e /content/Roms.rar /content/ROM/
!python -m atari_py.import_roms /content/ROM/
env = gym.make('SpaceInvaders-v0')
env.reset()
img = env.render(mode='rgb_array')
env.close()
plt.imshow(img)
from gym import envs

envall = envs.registry.all()
len(envall)
from tqdm import tqdm
List = []
for e in tqdm(envall):
    try:
        env = e.make()
        List.append([e.id, env.observation_space, env.action_space, env.reward_range])
        env.close() 
    except:
        continue     
    

df = pd.DataFrame(List, columns= ['Environment', 'Observation Space', 'Action Space', 'Reward'])
df.sample(20)
env = gym.make('MountainCar-v0')
print(f"The Observation space is        {env.observation_space}" )

print(f"Upper Bound for Env Observation {env.observation_space.high}")
print(f"Lower Bound for Env Observation {env.observation_space.low}")
print(f"Action Space                    {env.action_space}")

env.seed(0)
obs = env.reset()
print(f"The initial observation is      {obs}")
# # Take a random actionget the new observation space
new_obs, reward, done, info = env.step(env.action_space.sample())
print(f"The new observation is          {new_obs}")
env.close()<jupyter_output>The Observation space is        Box(-1.2000000476837158, 0.6000000238418579, (2,), float32)
Upper Bound for Env Observation [0.6  0.07]
Lower Bound for Env Observation [-1.2  -0.07]
Action Space                    Discrete(3)
The initial observation is      [-0.58912799  0.        ]
The new observation is          [-5.88639679e-01  4.88309600e-04]<jupyter_text>Working with Wrappers<jupyter_code>class ClippedRewards(gym.RewardWrapper):
    def __init__(self, env):
        gym.RewardWrapper.__init__(self, env)
        self.reward_range = (-10,10)

    def reward(self, reward):
        """Clip to {+10, 0, -10} by its sign."""
        return reward if reward >= -10 and reward <= 10 else 10 * np.sign(reward)
env = gym.make("CartPole-v0")
print("Original Rewards:", env.reward_range)
env.close()
env = ClippedRewards(gym.make("CartPole-v0"))
print(f'Clipped reward range: {env.reward_range}')
env.close()<jupyter_output>Original Rewards: (-inf, inf)
Clipped reward range: (-10, 10)<jupyter_text>Working with Wrappers<jupyter_code>from collections import deque
from gym import spaces
import numpy as np

class ConcatObservations(gym.Wrapper):
    def __init__(self, env, n):
        gym.Wrapper.__init__(self, env)
        shape = env.observation_space.shape
        self.n = n
        self.frames = deque([], maxlen=n)
        self.observation_space = \
            spaces.Box(low=0, high=255, shape=((n,) + shape), dtype=env.observation_space.dtype)
    def reset(self):
        obs = self.env.reset()
        for _ in range(self.n):
            self.frames.append(obs)
        return self._get_obs()

    def step(self, action):
        obs, reward, done, info = self.env.step(action)
        self.frames.append(obs)
        return self._get_obs(), reward, done, info

    def _get_obs(self):
        return np.array(self.frames)
env = gym.make("BreakoutNoFrameskip-v4")
print(f"The original observation space is  {env.observation_space}")
env = ConcatObservations(env, 4)
print(f"The new observation space is  {env.observation_space}")

env.reset()
obs, _, _, _  = env.step(env.action_space.sample())
print(f"Observation after taking a random step is  {obs.shape}")
env.close()<jupyter_output>Observation after taking a random step is  (4, 210, 160, 3)<jupyter_text>Monitors for saving videos<jupyter_code>import gym
from gym.wrappers import Monitor
#env = gym.make('CartPole-v0')
env = Monitor(gym.make('CartPole-v0'), '../video', force=True)
state = env.reset()
done = False

while not done:
    action = env.action_space.sample()
    state_next, reward, done, info = env.step(action)
    
env.close()

# On Colab
from IPython.display import HTML
from base64 import b64encode
mp4 = open('/video/openaigym.video.0.62.video000000.mp4','rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
HTML("""
<video width=400 controls>
      <source src="%s" type="video/mp4">
</video>
""" % data_url)

# from IPython.display import HTML

# HTML("""
#     <video alt="test" controls>
#         <source src="../video/old.mp4" type="video/mp4">
#     </video>
# """)<jupyter_output><empty_output><jupyter_text>Visualize Animation<jupyter_code>import gym
import matplotlib.pyplot as plt
import matplotlib.animation as animation

env = gym.make('Pong-v0')
obs = env.reset()
frames = [] # array to store state space at each step 
for _ in range(300):
    frames.append(env.render(mode='rgb_array')) 
    obs,reward,done, _ = env.step(env.action_space.sample()) 
    if done:
        break

patch = plt.imshow(frames[0])
plt.axis('off')
def animate(i):
    patch.set_data(frames[i])
anim = animation.FuncAnimation(plt.gcf(), animate, \
                               frames=len(frames), interval=100)
#anim.save('random_agent.gif', writer='imagemagick')<jupyter_output><empty_output><jupyter_text>![](../images/random_agent.gif)<jupyter_code># For Colab
from IPython.display import HTML
HTML(anim.to_html5_video())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13627000475360482, 'fraction_numerical': 0.027570907938520045, 'mean_word_length': 4.140065146579804, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '75981', 'license': 'MIT', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2206, 'n_words': 510, 'path': 'Module2/Module_2.1_Introduction_to_gym.ipynb', 'repo_name': 'Educat8n/Reinforcement-Learning-for-Game-Playing-and-More', 'size': '6311'}