การโต้ตอบกับ Gymnasium environments

Reinforcement Learning with Gymnasium ใน Python

Fouad Trad

Machine Learning Engineer

Gymnasium

  • ไลบรารีมาตรฐานสำหรับงาน RL
  • ลดความซับซ้อนของปัญหา RL
  • มี RL environments ให้เลือกใช้มากมาย

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

Reinforcement Learning with Gymnasium ใน Python

Gymnasium environments หลัก

CartPole: Agent ต้องทรงตัวเสาบนรถเข็นที่กำลังเคลื่อนที่ GIF representing the CartPole environment.

MountainCar: Agent ต้องขับรถขึ้นเนินชัน GIF representing the MountainCar environment.

FrozenLake: Agent ต้องเดินข้ามทะเลสาบน้ำแข็งที่มีหลุมอยู่ GIF representing the FrozenLake gymnasium environment.

Taxi: รับและส่งผู้โดยสาร GIF representing the Taxi environment.

Reinforcement Learning with Gymnasium ใน Python

Gymnasium interface

 

  • ใช้งานได้เหมือนกันทุก environment
  • มีฟังก์ชันและเมธอดสำหรับ:
    • เริ่มต้น environment
    • แสดงผล environment
    • ดำเนินการ action
    • สังเกตผลลัพธ์

GIF representing the CartPole environment.

Reinforcement Learning with Gymnasium ใน Python

การสร้างและเริ่มต้น 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 ใน Python

การแสดงผล 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 ใน Python

การแสดงผล 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 ใน Python

การดำเนินการ action

  • 0: เคลื่อนที่ไปทางซ้าย
  • 1: เคลื่อนที่ไปทางขวา
action = 1 
state, reward, terminated, truncated, info = env.step(action)




Reinforcement Learning with Gymnasium ใน Python

การดำเนินการ action

  • 0: เคลื่อนที่ไปทางซ้าย
  • 1: เคลื่อนที่ไปทางขวา
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 ใน 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 ใน Python

มาฝึกกันเถอะ!

Reinforcement Learning with Gymnasium ใน Python

Preparing Video For Download...