目录

Gymnasium

misaraty 更新 | 2024-12-25

前言
OpenAI开发的强化学习库Gym不再更新,后续社区版Gymnasium接手了。
1

山地车

2

直接渲染

1
2
3
4
5
6
7
8
9
import gymnasium as gym
env = gym.make("MountainCarContinuous-v0", render_mode="human", goal_velocity=0.1)
obs, _ = env.reset(seed=123, options={"low": -0.7, "high": -0.5})
for _ in range(200):
    action = env.action_space.sample()
    obs, reward, done, _, _ = env.step(action)
    if done:
        obs, _ = env.reset()
env.close()

直接保存gif

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import gymnasium as gym
import imageio
env = gym.make("MountainCarContinuous-v0", render_mode="rgb_array", goal_velocity=0.1)
obs, _ = env.reset(seed=123, options={"low": -0.7, "high": -0.5})
frames = []
for _ in range(200):
    action = env.action_space.sample()
    obs, reward, done, _, _ = env.step(action)
    frame = env.render()
    frames.append(frame)
    if done:
        break
imageio.mimsave("mountaincar_direct.gif", frames, duration=0.05)

./mountaincar.gif

Matplotlib保存gif

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation, PillowWriter
import gymnasium as gym
env = gym.make("MountainCarContinuous-v0", render_mode="rgb_array", goal_velocity=0.1)
obs, _ = env.reset(seed=123, options={"low": -0.7, "high": -0.5})
frames = []
for _ in range(200):
    action = env.action_space.sample()
    obs, reward, done, _, _ = env.step(action)
    frame = env.render()
    frames.append(frame)
    if done:
        break
fig, ax = plt.subplots()
im = ax.imshow(frames[0])
def update(frame):
    im.set_array(frame)
    return [im]
ani = FuncAnimation(fig, update, frames=frames, interval=5, blit=True)
plt.axis('off')
ani.save("mountaincar_animation.gif", writer=PillowWriter(fps=30))
plt.show()
注意
Matplotlib生成的gif边框空白太多。

OpenCV保存gif

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import cv2
import imageio
import gymnasium as gym
env = gym.make("MountainCarContinuous-v0", render_mode="rgb_array", goal_velocity=0.1)
obs, _ = env.reset(seed=123, options={"low": -0.7, "high": -0.5})
frames = []
for _ in range(200):
    action = env.action_space.sample()
    obs, reward, done, _, _ = env.step(action)
    frame = env.render()
    frames.append(frame)
    cv2.imshow("MountainCarContinuous", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
    if cv2.waitKey(50) & 0xFF == ord('q'):
        break
    if done:
        obs, _ = env.reset()
env.close()
cv2.destroyAllWindows()
imageio.mimsave("mountaincar_cv2.gif", frames, duration=0.05)

双足步行者

3

直接渲染

1
2
3
4
5
6
7
8
9
import gymnasium as gym
env = gym.make("BipedalWalker-v3", render_mode="human")
obs, _ = env.reset(seed=123)
for _ in range(200):
    action = env.action_space.sample()
    obs, reward, done, _, _ = env.step(action)
    if done:
        obs, _ = env.reset()
env.close()

直接保存gif

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import gymnasium as gym
import imageio
env = gym.make("BipedalWalker-v3", render_mode="rgb_array")
obs, _ = env.reset(seed=123)
frames = []
for _ in range(200):
    action = env.action_space.sample()
    obs, reward, done, _, _ = env.step(action)
    frame = env.render()
    frames.append(frame)
    if done:
        obs, _ = env.reset() 
env.close()
imageio.mimsave("bipedalwalker_direct.gif", frames, duration=0.05)

./bipedalwalker.gif


  1. Announcing The Farama Foundation ↩︎

  2. Mountain Car Continuous ↩︎

  3. Bipedal Walker ↩︎