Gymnasium 환경과 상호작용

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Fouad Trad

Machine Learning Engineer

Gymnasium

  • RL 작업을 위한 표준 라이브러리
  • RL 문제의 복잡성을 추상화
  • 다양한 RL 환경 제공

Gymnasium 로고와 제공 환경 예시 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

주요 Gymnasium 환경

CartPole: 에이전트가 움직이는 카트 위 막대를 균형 잡음 CartPole 환경을 보여주는 GIF.

MountainCar: 에이전트가 가파른 언덕을 오름 MountainCar 환경을 보여주는 GIF.

FrozenLake: 구멍이 있는 얼어붙은 호수를 탐색 FrozenLake Gymnasium 환경을 보여주는 GIF.

Taxi: 승객을 태우고 하차시킴 Taxi 환경을 보여주는 GIF.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Gymnasium 인터페이스

 

  • 모든 환경에 공통 인터페이스
  • 다음을 위한 함수/메서드 포함:
    • 환경 초기화
    • 환경 시각화
    • 행동 실행
    • 결과 관찰

CartPole 환경을 보여주는 GIF.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

환경 생성 및 초기화

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/
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

상태 시각화

 

import matplotlib.pyplot as plt

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

plt.show()

CartPole의 초기 상태 플롯.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

상태 시각화

 

import matplotlib.pyplot as plt

def render(): state_image = env.render() plt.imshow(state_image) plt.show()
# 함수 호출 render()

CartPole의 초기 상태 플롯.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

행동 수행

  • 0: 왼쪽으로 이동
  • 1: 오른쪽으로 이동
action = 1 
state, reward, terminated, truncated, info = env.step(action)




Python으로 배우는 Gymnasium 기반 Reinforcement Learning

행동 수행

  • 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
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

상호작용 루프

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

상호작용 루프 중 캡처된 CartPole의 네 가지 상태 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Vamos praticar!

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Preparing Video For Download...