Reinforcement Learning with Gymnasium in Python
Fouad Trad
Machine Learning Engineer
CartPole:
Agent must balance a pole on moving cart
MountainCar: Agent must drive a car up a steep hill
FrozenLake:
Agent must navigate a frozen lake with holes
Taxi:
Picking up and dropping off passengers
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]
import matplotlib.pyplot as plt state_image = env.render() plt.imshow(state_image)
plt.show()
import matplotlib.pyplot as plt
def render(): state_image = env.render() plt.imshow(state_image) plt.show()
# Call function render()
action = 1
state, reward, terminated, truncated, info = env.step(action)
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
while not terminated:
action = 1 # Move to the right
state, reward, terminated, _, _ = env.step(action)
render()
Reinforcement Learning with Gymnasium in Python