2주차 Value-Based Reinforcement Learning 방식

[핵심 목표]

[Value-Based Reinforcement Learning (Value-based Approach)]

정책을 직접 파라미터화하지 않고, Value Function 또는 Q 함수를 학습

1. QQ 함수 - Q(s,a)Q(s, a)

2. QQ-러닝 (QQ-learning)

3. PyTorch Neural Network 모델 구현 준비

[Q-러닝 실습 예제: Q-러닝으로 미로 탈출하기]

1. 환경 설정

미로 정의

5x5 크기의 미로를 numpy 배열로 표현

import numpy as np

# 5x5 미로 환경 설정 (0: 길, 1: 벽, 2: 시작, 3: 목표)
maze = np.array([
    [2, 0, 0, 0, 0],
    [0, 1, 1, 0, 0],
    [0, 0, 0, 1, 0],
    [1, 1, 0, 1, 0],
    [0, 1, 0, 3, 0]
])

행동 정의

에이전트가 취할 수 있는 행동을 정의

# 0: 위로 1칸 이동(-1,0)
# 1: 아래로 1칸 이동(1,0)
# 2: 왼쪽으로 1칸 이동(0,-1)
# 3: 오른쪽으로 1칸 이동(0,1))
actions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

Q테이블 초기화

Q테이블을 0으로 초기화 (5x5x4개행동)

q_table = np.zeros((5, 5, 4))

하이퍼파라미터 설정

학습에 필요한 하이퍼파라미터를 설정

learning_rate = 0.1
discount_factor = 0.9
epsilon = 0.1  # 탐험 비율
episodes = 1000

2. Q-러닝 알고리즘

다음 상태 얻기

현재 상태와 행동을 입력받아 다음 상태를 반환하는 함수

def get_next_state(state, action):
  next_state = (state[0] + actions[action][0], state[1] + actions[action][1])

  # 경계 확인 및 벽 충돌 감지
  if 0 <= next_state[0] < 5 and 0 <= next_state[1] < 5 and maze[next_state[0], next_state[1]] != 1:
    return next_state
  else:
    return state

보상 얻기

현재 상태를 입력받아 보상을 반환하는 함수

def get_reward(state):
  if maze[state[0], state[1]] == 3:
    return 10
  else:
    return -1

Q-러닝 학습

Q-러닝 알고리즘을 사용하여 Q-테이블을 업데이트

# Q-러닝 알고리즘
for episode in range(episodes):
  state = (0, 0)  # 시작 위치
  while maze[state[0], state[1]] != 3 :
    if np.random.rand() < epsilon:
        action = np.random.randint(4)
    else:
        action = np.argmax(q_table[state[0], state[1], :])

    next_state = get_next_state(state, action)
    reward = get_reward(next_state)

    q_table[state[0], state[1], action] += learning_rate * (
            reward + discount_factor * np.max(q_table[next_state[0], next_state[1], :])
            - q_table[state[0], state[1], action]
    )

    state = next_state

    # 중간 프로그래스 출력 (예시: 100번째 에피소드마다 출력)
  if (episode + 1) % 100 == 0:
    print(f"Episode {episode + 1} 완료")
Episode 100 완료
Episode 200 완료
Episode 300 완료
Episode 400 완료
Episode 500 완료
Episode 600 완료
Episode 700 완료
Episode 800 완료
Episode 900 완료
Episode 1000 완료
Q-값 업데이트 수식

Q(s,a)←Q(s,a)+α[r+γmaxa′Q(s′,a′)−Q(s,a)]Q(s, a) \leftarrow Q(s, a) + \alpha [r + \gamma \max_{a'} Q(s', a') - Q(s, a)]

3. 결과 확인

최종 경로 찾기

학습된 Q-테이블을 사용하여 시작 상태에서 목표 상태까지의 최적 경로를 찾기

# 최종 경로 찾기
state = (0, 0)
path = [state]
while maze[state[0], state[1]] != 3:
    action = np.argmax(q_table[state[0], state[1], :])
    state = get_next_state(state, action)
    path.append(state)

경로 시각화

찾은 경로를 text로 시각화하여 출력

# 경로 시각화
print("미로 탈출 경로:")
for i in range(5):
    for j in range(5):
        if (i, j) == (0, 0):
          print("S", end=" ")
        elif (i,j) == (4,3):
          print("G", end=" ")
        elif (i,j) in path:
          print("*", end=" ")  # 지나간 경로
        elif maze[i, j] == 1:
            print("#", end=" ")  # 벽 표시
        else:
            print(" ", end=" ")
    print()
미로 탈출 경로:
S         
* # #     
* * * #   
# # * #   
  # * G