Interacting with Gymnasium environments

Reinforcement Learning with Gymnasium in Python

Fouad Trad

Machine Learning Engineer

Gymnasium

  • Standard library for RL tasks
  • Abstracts complexity of RL problems
  • Provides a plethora of RL environments

Image showing the gymnasium logo along with some environments it provides.

Reinforcement Learning with Gymnasium in Python

Key Gymnasium environments

CartPole: Agent must balance a pole on moving cart GIF representing the CartPole environment.

MountainCar: Agent must drive a car up a steep hill GIF representing the MountainCar environment.

FrozenLake: Agent must navigate a frozen lake with holes GIF representing the FrozenLake gymnasium environment.

Taxi: Picking up and dropping off passengers GIF representing the Taxi environment.

Reinforcement Learning with Gymnasium in Python

Gymnasium interface

 

  • Unified for all environments
  • Includes functions and methods to:
    • Initialize environment
    • Visually represent environment
    • Execute actions
    • Observe outcomes

GIF representing the CartPole environment.

Reinforcement Learning with Gymnasium in Python

Creating and initializing the environment

import gymnasium as gym

env = gym.make('CartPole', render_mode='rgb_array')
state, info = env.reset(seed=42) print(state)
[-0.04405273  0.0242996  -0.04377224 -0.01767325]
1 https://gymnasium.farama.org/environments/classic_control/cart_pole/
Reinforcement Learning with Gymnasium in Python

Visualizing the state

 

import matplotlib.pyplot as plt

state_image = env.render()
plt.imshow(state_image)

plt.show()

Plot of the initial state in CartPole.

Reinforcement Learning with Gymnasium in Python

Visualizing the state

 

import matplotlib.pyplot as plt

def render(): state_image = env.render() plt.imshow(state_image) plt.show()
# Call function render()

Plot of the initial state in CartPole.

Reinforcement Learning with Gymnasium in Python

Performing actions

  • 0: moving left
  • 1: moving right
action = 1 
state, reward, terminated, truncated, info = env.step(action)




Reinforcement Learning with Gymnasium in Python

Performing actions

  • 0: moving left
  • 1: moving right
action = 1
state, reward, terminated, _, _ = env.step(action)


print("State: ", state) print("Reward: ", reward) print("Terminated: ", terminated)
State:  [-0.04356674  0.22002107 -0.0441257  -0.3238392 ]
Reward:  1.0
Terminated:  False
Reinforcement Learning with Gymnasium in Python

Interaction loops

while not terminated:
    action = 1 # Move to the right
    state, reward, terminated, _, _ = env.step(action)
    render()

Image showing four different states in the CartPole environment, captured during the interaction loop.

Reinforcement Learning with Gymnasium in Python

Let's practice!

Reinforcement Learning with Gymnasium in Python

Preparing Video For Download...