정책 반복과 가치 반복

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Fouad Trad

Machine Learning Engineer

정책 반복

  • 최적 정책을 찾는 반복 절차

1단계: 정책 초기화 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

정책 반복

  • 최적 정책을 찾는 반복 절차

두 단계: 정책 초기화와 평가 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

정책 반복

  • 최적 정책을 찾는 반복 절차

세 단계: 정책 초기화, 평가, 개선 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

정책 반복

  • 최적 정책을 찾는 반복 절차

정책 평가와 개선을 반복하여 정책이 더 이상 변하지 않을 때까지 수행하는 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

정책 반복

  • 최적 정책을 찾는 반복 절차

정책 반복 흐름: 정책 초기화 후 평가와 개선을 번갈아 수행하여 최적 정책에 도달하는 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

그리드 월드

policy = {
    0:1, 1:2, 2:1, 
    3:1, 4:3, 5:1,
    6:2, 7:3
}

각 상태에서 이동을 화살표로 나타낸 정책 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

정책 평가

def policy_evaluation(policy):

V = {state: compute_state_value(state, policy) for state in range(num_states)}
return V
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

정책 개선

def policy_improvement(policy):

improved_policy = {s: 0 for s in range(num_states-1)}
Q = {(state, action): compute_q_value(state, action, policy) for state in range(num_states) for action in range(num_actions)}
for state in range(num_states-1): max_action = max(range(num_actions), key=lambda action: Q[(state, action)]) improved_policy[state] = max_action
return improved_policy
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

정책 반복

def policy_iteration():

policy = {0:1, 1:2, 2:1, 3:1, 4:3, 5:1, 6:2, 7:3}
while True: V = policy_evaluation(policy) improved_policy = policy_improvement(policy)
if improved_policy == policy: break policy = improved_policy
return policy, V
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

최적 정책

policy, V = policy_iteration()
print(policy, V)
{0: 2, 1: 2, 2: 1, 
 3: 1, 4: 2, 5: 1, 
 6: 2, 7: 2} 

{0: 7, 1: 8, 2: 9, 
 3: 7, 4: 9, 5: 10, 
 6: 8, 7: 10, 8: 0}

optimal.png

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

가치 반복

  • 한 단계에서 정책 평가와 개선을 결합
    • 최적 상태가치 함수 계산
    • 그로부터 정책 도출

1단계: 상태가치 V를 0으로 초기화하는 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

가치 반복

  • 한 단계에서 정책 평가와 개선을 결합
    • 최적 상태가치 함수 계산
    • 그로부터 정책 도출

V 표를 사용해 Q-값을 계산하는 추가 단계 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

가치 반복

  • 한 단계에서 정책 평가와 개선을 결합
    • 최적 상태가치 함수 계산
    • 그로부터 정책 도출

각 상태에서 최적 행동을 선택해 V를 갱신하는 추가 단계 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

가치 반복

  • 한 단계에서 정책 평가와 개선을 결합
    • 최적 상태가치 함수 계산
    • 그로부터 정책 도출

V로 Q-값을 계산하고 V를 갱신하는 과정을 V가 수렴할 때까지 반복하는 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

가치 반복

  • 한 단계에서 정책 평가와 개선을 결합
    • 최적 상태가치 함수 계산
    • 그로부터 정책 도출

반복이 끝나면 최적 정책과 V를 얻는 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

가치 반복 구현

V = {state: 0 for state in range(num_states)}
policy = {state:0 for state in range(num_states-1)}
threshold = 0.001

while True: new_V = {state: 0 for state in range(num_states)}
for state in range(num_states-1): max_action, max_q_value = get_max_action_and_value(state, V)
new_V[state] = max_q_value policy[state] = max_action
if all(abs(new_V[state] - V[state]) < thresh for state in V): break V = new_V
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

최적 행동과 값 구하기

def get_max_action_and_value(state, V):
    Q_values = [compute_q_value(state, action, V) for action in range(num_actions)]

max_action = max(range(num_actions), key=lambda a: Q_values[a])
max_q_value = Q_values[max_action]
return max_action, max_q_value
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Q-값 계산

def compute_q_value(state, action, V):
    if state == terminal_state:
        return None
    _, next_state, reward, _ = env.P[state][action][0]
    return reward + gamma * V[next_state]
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

최적 정책

print(policy, V)
{0: 2, 1: 2, 2: 1, 
 3: 1, 4: 2, 5: 1, 
 6: 2, 7: 2} 

{0: 7, 1: 8, 2: 9, 
 3: 7, 4: 9, 5: 10, 
 6: 8, 7: 10, 8: 0}

최적 정책의 상태가치 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Ayo berlatih!

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Preparing Video For Download...