-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
235 lines (171 loc) · 6.84 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import gym
import gym_maze
import numpy as np
import time
import matplotlib.pyplot as plt
from matplotlib.table import Table
from matplotlib.patches import Polygon
import matplotlib.image as mpimg
# Create an environment
env = gym.make("maze-random-10x10-plus-v0")
observation = env.reset()
def exploration_exploitation(state, epsilon, q_table):
random = np.random.rand()
if random < epsilon: # exploration
action = env.action_space.sample()
else: # exploitation
x, y = state
action = np.argmax(q_table[int(x), int(y), :])
return action
def update_q_table(q_table, cur_state, action, next_state, reward, alpha, gamma):
cur_x, cur_y = cur_state
next_x, next_y = next_state
# Q(S, A) <- (1 - α) Q(S, A) + [α * (r + (γ * max(Q(S', A*))))]
sample = reward + (gamma * np.max(q_table[next_x, next_y, :]))
q_table[int(cur_x), int(cur_y), action] += alpha * (sample - q_table[int(cur_x), int(cur_y), action])
def q_learning(num_episodes, q_table, epsilon, alpha, gamma, render_mode=False):
actions = np.zeros((env.maze_size[0], env.maze_size[1]))
episode_steps = []
episode_rewards = []
limit_steps = 500
for i in range(num_episodes):
# start from the initial state
cur_state = env.reset()
game_over = False
num_actions = 0
total_reward = 0
while not game_over:
env.render()
# time.sleep(0.05)
if num_actions > limit_steps:
break
# choose an action
action = exploration_exploitation(cur_state, epsilon, q_table)
if epsilon > 0 and num_actions != 0:
epsilon -= ((0.00001 * i) / (num_actions))
elif num_actions != 0:
epsilon = 0
num_actions += 1
# Perform the action and receive feedback from the environment
next_state, reward, done, truncated = env.step(action)
if truncated:
break
if done:
game_over = True
else:
total_reward += reward
# update Q_table
update_q_table(q_table, cur_state, action, next_state, reward, alpha, gamma)
episode_steps.append(num_actions)
episode_rewards.append(total_reward)
# update state
cur_state = next_state
print("episode:", i, " steps:", num_actions, " reward:", total_reward, " epsilon:", epsilon)
return q_table, actions, episode_steps, episode_rewards
num_actions = env.action_space.n
num_row = env.maze_size[0]
num_column = env.maze_size[1]
# initialize Q-Table
Q_table = np.zeros((num_row, num_column, num_actions))
# Define the maximum number of iterations
NUM_EPISODES = 1000
# Define explroration explotation trade-off
epsilon = 1
# Define learning rate
alpha = 0.1
# Define discount factor
gamma = 0.9
# Q-Learning Algorithm
q_table, actions, episode_steps, episode_rewards = q_learning(NUM_EPISODES, Q_table, epsilon, alpha, gamma)
#-----------------------------------------------------------------------------------------------------------------------
fig, ax1 = plt.subplots(figsize=(10, 5))
ax2 = ax1.twinx()
ax1.plot(episode_steps, label='Steps', color='blue')
ax1.set_xlabel('Episode')
ax1.set_ylabel('Number of Steps', color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
ax2.plot(episode_rewards, label='Total Reward', color='green')
ax2.set_ylabel('Total Reward', color='green')
ax2.tick_params(axis='y', labelcolor='green')
plt.show()
def show_q_table(q_table):
rows, cols = q_table.shape[:2]
fig, ax_array = plt.subplots(rows, cols, figsize=(cols, rows))
plt.subplots_adjust(wspace=0.1, hspace=0.1)
for i in range(rows):
for j in range(cols):
values = q_table[i, j, :]
ax = ax_array[i, j]
center = (0.5, 0.5)
triangle1 = Polygon([
[center[0] - 0.5, center[1] + 0.5],
[center[0] + 0.5, center[1] + 0.5],
[center[0], center[1]],
], closed=True, edgecolor='black', facecolor='none')
triangle2 = Polygon([
[center[0] - 0.5, center[1] + 0.5],
[center[0] - 0.5, center[1] - 0.5],
[center[0], center[1]],
], closed=True, edgecolor='black', facecolor='none')
triangle3 = Polygon([
[center[0] - 0.5, center[1] - 0.5],
[center[0] + 0.5, center[1] - 0.5],
[center[0], center[1]],
], closed=True, edgecolor='black', facecolor='none')
triangle4 = Polygon([
[center[0] + 0.5, center[1] - 0.5],
[center[0] + 0.5, center[1] + 0.5],
[center[0], center[1]],
], closed=True, edgecolor='black', facecolor='none')
ax.text(center[0], center[1] + 0.25, f'{values[0]:.4f}', ha='center', va='center', fontsize=6)
ax.text(center[0], center[1] - 0.25, f'{values[1]:.4f}', ha='center', va='center', fontsize=6)
ax.text(center[0] + 0.25, center[1], f'{values[2]:.4f}', ha='center', va='center', fontsize=6)
ax.text(center[0] - 0.25, center[1], f'{values[3]:.4f}', ha='center', va='center', fontsize=6)
ax.add_patch(triangle1)
ax.add_patch(triangle2)
ax.add_patch(triangle3)
ax.add_patch(triangle4)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.axis('off')
plt.tight_layout()
plt.show()
def show_actions(actions, ax):
actions = actions.astype(int)
table = ax.table(cellText=actions, loc='center', cellLoc='center', edges='open')
ax.axis('off')
for i in range(actions.shape[0]):
for j in range(actions.shape[1]):
value = actions[i, j]
if value == 0:
arrow = u'$\u2191$' # Up arrow
elif value == 1:
arrow = u'$\u2193$' # Down arrow
elif value == 2:
arrow = u'$\u2192$' # Right arrow
elif value == 3:
arrow = u'$\u2190$' # Left arrow
else:
arrow = ''
table[i, j].get_text().set_text(arrow)
# show results
show_q_table(q_table)
max_values = np.max(q_table, axis=2)
fig, axs = plt.subplots(1, 3, figsize=(15, 5))
axs[0].imshow(max_values, cmap='viridis', interpolation='none')
axs[0].set_title('Q-table Visualization')
axs[0].axis('off')
for i in range(max_values.shape[0]):
for j in range(max_values.shape[1]):
axs[0].text(j, i, f'{max_values[i, j]:.2f}', ha='center', va='center', color='black')
show_actions(actions, axs[2])
axs[2].set_title('Actions')
image_path = 'env.PNG'
img = mpimg.imread(image_path)
axs[1].imshow(img)
axs[1].set_title('Image')
axs[1].axis('off')
# Adjust layout to prevent clipping of titles
plt.tight_layout()
# Show the plot
plt.show()