-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrl_algorithms.py
171 lines (134 loc) · 5.16 KB
/
rl_algorithms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import Sokoban_env
from Sokoban_env import Sokoban_v2
import numpy as np
import matplotlib.pyplot as plt
from IPython import display
import pandas as pd
import pygame
import os
#Q-learning algorithm
def Q_learning(env,learning_rate = 0.1, discount_factor = 0.9, epsilon = 0.6, num_episodes = 5000):
# Setting Q
state_space = env.num_states.n
action_space = env.action_space.n
q_table = np.zeros((state_space, action_space))
eva = []
for episode in range(num_episodes):
print(f'---- Episode: {episode}')
state = env.reset()
done = False
Return = 0
while not done:
# Choose an action using epsilon-greedy policy
if np.random.uniform(0, 1) < epsilon:
action = env.action_space.sample() # Explore
else:
action = np.argmax(q_table[np.argmax(q_table[state, :])]) # Exploit
next_state, reward, done, _ = env.step(action)
# Update Q-table using the Q-learning formula
q_table[state, action] = (1 - learning_rate) * q_table[state, action] + \
learning_rate * (reward + discount_factor * np.max(q_table[next_state, :]))
state = next_state
Return += reward
eva.append(Return)
display.clear_output()
return q_table, eva
# SARSA
def sarsa(env, learning_rate=0.1, discount_factor=0.9, epsilon=0.1, num_episodes = 1000):
state_space = env.num_states.n
action_space = env.action_space.n
# Initialize the Q-table
q_table = np.zeros((state_space, action_space))
eva = []
Return = 0
# SARSA algorithm
for episode in range(num_episodes):
print(f'---- Episode: {episode}')
state = env.reset()
done = False
# Choose an action using epsilon-greedy policy
if np.random.uniform(0, 1) < epsilon:
action = env.action_space.sample() # Explore
else:
action = np.argmax(q_table[state, :]) # Exploit
while not done:
Return = 0
next_state, reward, done, _ = env.step(action)
# Choose the next action using epsilon-greedy policy
if np.random.uniform(0, 1) < epsilon:
next_action = env.action_space.sample() # Explore
else:
next_action = np.argmax(q_table[next_state, :]) # Exploit
# Update Q-table using SARSA formula
q_table[state, action] = q_table[state, action] + learning_rate * (
reward + discount_factor * q_table[next_state, next_action] - q_table[state, action]
)
state = next_state
action = next_action
Return += reward
eva.append(Return)
display.clear_output()
return q_table, eva
def first_visit_monte_carlo(env, num_episodes, epsilon=0.1):
state_space = env.num_states.n
action_space = env.action_space.n
q_table = np.zeros((state_space, action_space))
returns = np.zeros((state_space, action_space))
counts = np.zeros((state_space, action_space))
eva = []
def epsilon_greedy(state):
if np.random.uniform(0, 1) < epsilon:
return env.action_space.sample() # Explore
else:
return np.argmax(q_table[state, :]) # Exploit
for episode in range(num_episodes):
print(f'---- Episode: {episode}')
episode_data = []
state = env.reset()
done = False
while not done:
action = epsilon_greedy(state)
next_state, reward, done, _ = env.step(action)
episode_data.append((state, action, reward))
state = next_state
# Update Q-values using Monte Carlo method
G = 0
for t in reversed(range(len(episode_data))):
state, action, reward = episode_data[t]
G = reward + G
if (state, action) not in episode_data[:t]:
counts[state, action] += 1
returns[state, action] += G
q_table[state, action] = returns[state, action] / counts[state, action]
eva.append(G)
return q_table, eva
def running_alg(env, q_table):
#Evaluate the trained Q-table
os.environ['SDL_AUDIODRIVER'] = 'alsa'
pygame.mixer.init()
pygame.mixer.music.load('/home/duy/Documents/Reinforcement Learning/Sokoban RL/Code/surface/troll.mp3')
total_rewards = 0
num_episodes_eval = 1
display.clear_output(wait=True)
env.reset()
state = env.reset()
done = False
while not done:
plt.imshow(env.render())
action = np.argmax(q_table[state, :])
next_state, reward, done, _ = env.step(action)
display.display(plt.gcf())
display.clear_output(wait=True)
total_rewards += reward
state = next_state
plt.imshow(env.render())
display.display(plt.gcf())
display.clear_output(wait=True)
if done:
pygame.mixer.music.play()
average_reward = total_rewards / num_episodes_eval
print(f"Reward over {num_episodes_eval} episodes: {average_reward:}")
def eva_graph(eva):
plt.xlabel('Episodes')
plt.ylabel('Reward')
plt.plot(eva)