Python 中的 Gymnasium 强化学习
Fouad Trad
Machine Learning Engineer

CartPole:
智能体需在移动小车上平衡杆

MountainCar: 智能体需驾驶小车爬上陡坡

FrozenLake:
智能体需在有洞的冰湖上导航

Taxi:
接送乘客


import gymnasium as gymenv = 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 pltdef render(): state_image = env.render() plt.imshow(state_image) plt.show()# 调用函数 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 # 向右移动
state, reward, terminated, _, _ = env.step(action)
render()

Python 中的 Gymnasium 强化学习