<jupyter_start><jupyter_text>OpenAI Gym Introduction This is brief introctuion on Gym's API with `'CartPolev1'`for more detailed explanation, please use [Open AI GYM CartPole Wiki]("https://github.com/openai/gym/wiki")<jupyter_code>import gym


#Create gym CartPolev1 environment
env = gym.make("CartPole-v1")
obs=env.reset() #Returns the observation<jupyter_output><empty_output><jupyter_text>CartPole outputs 4 different observations | Num | Observation          | Min   | Max  ||-----|----------------------|-------|------|| 0   | Cart Position        | -2.4  | 2.4  || 1   | Cart Velocity        | -Inf  | Inf  || 2   | Pole Angle           | -41.8 | 41.8 || 3   | Pole Velocity at Tip | -Inf  | Inf  |<jupyter_code>observation_space = env.observation_space.shape


print(observation_space) #Print Possible Action
print(obs) #Print Observation
env.action_space #Print Possible Action<jupyter_output><empty_output><jupyter_text>Plotting the Obsesrvation`env.render()` opens a current screen Must be Closed with `env.close()`, if on Jupyter to prevent Stopping"`env.render(mode='rgb_array')`can output the screen as numpy<jupyter_code>env.render()
env.close()
import matplotlib.pyplot as plt
image=env.render(mode='rgb_array')
env.close()
plt.imshow(image)<jupyter_output><empty_output><jupyter_text>Choosing an Action`env.step('action')`, has one input 'action' and outputs 'observation','reward', 'done (bool)', 'info' (useful for debugging), We can use `env.render()` to check the changes<jupyter_code># Action = {0,1 } pushes to left/right
env.reset()
start=env.render(mode='rgb_array')
for i in range(10):
    env.step(1)
end=env.render(mode='rgb_array')

env.close()
plt.figure(figsize=(16, 16))
plt.subplot(1, 2, 1)
plt.xticks([])
plt.yticks([])
plt.imshow(start)
plt.subplot(1,2,2)
plt.tight_layout(0)
plt.imshow(end)
plt.xticks([])
plt.yticks([])


asdas
import gym # loading the Gym library
 
env = gym.make("FrozenLake-v0")
env.reset()                    
env.render()
from itertools import chain 
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
def render(env):
    def index_fun(a,b,height):
        return a*height+b
    def state_to_color(argument):
        func=switcher.get(argument,"nothing")
        return func()
    def frozen():
        return 'c'
    def hole():
        return 'k'
    def start():
        return "y"
    def goal():
        return 'g'
    switcher = {
        'F': frozen,
        'S': start,
        'H': hole,
        'G': goal
    }
    desc = env.desc.tolist()
    desc = [[c.decode('utf-8') for c in line] for line in desc]
    desc_flatten = list(chain.from_iterable(desc)) 
    height=len(desc)
    width=len(desc[0])
    fig=plt.figure(figsize=(4, 4))

    ax = [plt.subplot(height,width,i+1) for i in range(height*width)]
    plt.tick_params(labelcolor="none", bottom=False, left=False)
    plt.setp(ax, xlim=(-1,1), ylim=(-1,1))



    def state_to_color(argument):
        func=switcher.get(argument,"nothing")
        return func()
    for index,(axes,state) in enumerate(zip(ax,desc_flatten)):

        axes.set_facecolor(state_to_color(state))
        if index==env.s:
            axes.set_facecolor((1,0,0))

        axes.get_yaxis().set_visible(False)
        axes.get_xaxis().set_visible(False)


    plt.subplots_adjust(wspace=0, hspace=0)

    fig.canvas.draw()
    img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
#    plt.close()
    return img<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15107319757842597, 'fraction_numerical': 0.014309301045679693, 'mean_word_length': 3.618805590851334, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '608722', 'license': 'MIT', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1256, 'n_words': 282, 'path': 'Intro to Gym Environment.ipynb', 'repo_name': 'Cognitive-Systems-Laboratory/Reinforcement-Learning-Tutorial', 'size': '3634'}