Week 2 value-based reinforcement learning approach

English translation

[Key objectives]

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

Learn a value function or Q-function without directly parameterizing the policy

1. QQ function — Q(s,a)Q(s, a)

2. QQ-learning (QQ-learning)

3. Prepare to implement a neural network model in PyTorch

[Q-learning example: Escape a maze with Q-learning]

1. Environment setup

Define the maze

Represent a 5x5 maze as a NumPy array

import numpy as np

# Set up a 5x5 maze (0: path, 1: wall, 2: start, 3: goal)
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]
])

Action definition

Define the actions available to the agent

# 0: Move up 1 cell (-1,0)
# 1: Move down 1 cell (1,0)
# 2: Move left 1 cell (0,-1)
# 3: Move right 1 cell (0,1))
actions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

Initialize the Q-table

Initialize the Q-table to 0 (5x5x4 actions).

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

Hyperparameter settings

Set the hyperparameters needed for training.

learning_rate = 0.1
discount_factor = 0.9
epsilon = 0.1  # Exploration rate
episodes = 1000

2. Q-learning algorithm

Obtain the next state

Function that takes the current state and action and returns the next state

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

  # Check boundaries and detect wall collisions
  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

Obtain reward

A function that takes the current state and returns a reward

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

Q-learning training

Update the Q-table using Q-learning.

# Q-learning algorithm
for episode in range(episodes):
  state = (0, 0)  # Starting position
  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

    # Print progress (example: every 100 episodes)
  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-value update formula

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. Check results

Find the final path

Use the learned Q-table to find the optimal path from the initial state to the goal state.

# Find the final path
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)

Path visualization

Print a text visualization of the path found

# Visualize the path
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=" ")  # Path taken
        elif maze[i, j] == 1:
            print("#", end=" ")  # Mark walls
        else:
            print(" ", end=" ")
    print()
미로 탈출 경로:
S         
* # #     
* * * #   
# # * #   
  # * G