-
Notifications
You must be signed in to change notification settings - Fork 1
/
ql.py
334 lines (313 loc) · 13.8 KB
/
ql.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
'''
ql.py
Desperate tourist - Q-learning example
Copyright (C) 2019 Alexey "FoxyLab" Voronin
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Credits:
Icon mades by Freepik, Skyclick, xnimrodx, surang from www.flaticon.com
'''
import math
import random
from dataclasses import dataclass
import numpy as np
import pygame
import sys
import time
from settings import *
def action(x, y, a):
''' Calculates new position of agent
Arguments:
x - current X-coordinate of agent
y - current Y-coordinate of agent
a - action of agent
Returns:
dx - X-coordinate offset
dy - Y-coordinate offset
bonus - reward for action
flag - event flag for action
'''
flag = NONE_FLAG # flag reset
# deltas initialization
dx = 0
dy = 0
if (a == 0): # N
if (y > 0):
dy = -1
elif (a == 1): # NE
if ((y > 0) and (x < (SIZE-1))):
dy = -1
dx = 1
elif (a == 2): # E
if (x < (SIZE-1)):
dx = 1
elif (a == 3): # SE
if ((y < (SIZE-1)) and (x < (SIZE-1))):
dy = 1
dx = 1
elif (a == 4): # S
if (y < (SIZE-1)):
dy = 1
elif (a == 5): # SW
if ((y < (SIZE-1)) and (x > 0)):
dy = 1
dx = -1
elif (a == 6): # W
if (x > 0):
dx = -1
elif (a == 7): # NW
if ((y > 0) and (x >0)):
dy = -1
dx = -1
if ((dx == 0) and (dy == 0)): # border
flag = BORDER_FLAG
if ((x+dx, y+dy) in WALLS): # wall
dx = 0
dy = 0
flag = WALL_FLAG
if ((x+dx, y+dy) in TRAPS): # trap
dx = X_START - x
dy = Y_START - y
x = X_START
y = Y_START
flag = TRAP_FLAG
# bonus calculation
bonus = STEP_PENALTY # step penalty
if (((x + dx) == X_FINISH) and ((y + dy) == Y_FINISH)): # check next position for finish
bonus = bonus + FINISH_BONUS
flag = FINISH_FLAG
return dx, dy, bonus, flag
def state(x, y):
''' Calculates number of cell
Arguments:
x - X-coordinate of cell
y - Y-coordinate of cell
a - action of agent
Returns:
number of cell
'''
return x + y*SIZE
@dataclass
class Pos:
''' Position
x - X-coordinate
y - Y-coordinate
'''
x: int
y: int
def main():
print("Q-learning")
np.random.seed(SEED) # PRNG initialization
episodes_max = int(input("Episodes number?")) # episode request
q = np.empty((ACTIONS, SIZE*SIZE), dtype = float) # creating an empty table Q
q[:] = np.NINF # initialization of elements of table Q
pygame.init() # Pygame initialization
pygame.display.set_caption("Q-learning")
canvas = pygame.display.set_mode((SIZE*CELL_SIZE+(SIZE-1)*BORDER_SIZE, SIZE*CELL_SIZE+(SIZE-1)*BORDER_SIZE+STATUS_SIZE))
canvas.fill(WHITE)
font = pygame.font.SysFont("comicsansms", FONT_SIZE) # font assignment
# image upload
icon = pygame.image.load("walk.png")
footprint = pygame.image.load("footprints.png")
bricks = pygame.image.load("wall.png")
sand = pygame.image.load("sand.png")
bomb = pygame.image.load("bomb.png")
start = pygame.image.load("start.png")
finish = pygame.image.load("finish.png")
walk = pygame.image.load("walk.png")
icon.convert()
pygame.display.set_icon(icon) # window icon assignment
footprint.convert()
bricks.convert()
sand.convert()
bomb.convert()
start.convert()
finish.convert()
walk.convert()
# drawing cell borders
i = 1
while (i < SIZE):
pygame.draw.line(canvas, BLACK, (i * CELL_SIZE + (i-1) * BORDER_SIZE + 1, 0), (i * CELL_SIZE + (i-1) * BORDER_SIZE + 1, SIZE*CELL_SIZE+(SIZE-1)*BORDER_SIZE - 1), BORDER_SIZE)
i += 1
i = 1
while (i < SIZE):
pygame.draw.line(canvas, BLACK, (0, i * CELL_SIZE + (i-1) * BORDER_SIZE + 1), (SIZE*CELL_SIZE+(SIZE-1)*BORDER_SIZE - 1, i * CELL_SIZE + (i-1) * BORDER_SIZE + 1), BORDER_SIZE)
i += 1
episode = 0 # episode counter reset
while (episode < episodes_max): # episode cycle
print("EPISODE: " + str(episode + 1)) # episode number output
# epsilon calculation
if (episode == (episodes_max - 1)):
# testing (last episode)
epsilon = 0.0
else:
# training
epsilon = ((episodes_max-1) - episode)/(episodes_max - 1) *(EPSILON_START-EPSILON_FINISH) + EPSILON_FINISH
print("EPSILON: ", format(epsilon, '.2f')) # вывод значения эпсилон
# cell fill
i = 0
while (i < SIZE):
j = 0
while (j < SIZE):
canvas.blit(sand, (j*(CELL_SIZE+BORDER_SIZE) , i*(CELL_SIZE+BORDER_SIZE)))
j += 1
i += 1
# drawing walls
for wall in WALLS:
canvas.blit(bricks, (wall[0]*(CELL_SIZE+BORDER_SIZE) , wall[1]*(CELL_SIZE+BORDER_SIZE)))
# drawing traps
for trap in TRAPS:
canvas.blit(bomb, (trap[0]*(CELL_SIZE+BORDER_SIZE) , trap[1]*(CELL_SIZE+BORDER_SIZE)))
moving = True # moving flag up
pos = Pos(X_START, Y_START) # agent position initialization
canvas.blit(walk, (pos.x*(CELL_SIZE+BORDER_SIZE) , pos.y*(CELL_SIZE+BORDER_SIZE))) # drawing agent
f = NONE_FLAG
step = 0 # step counter reset
scores = 0.0 # reward counter reset
canvas.blit(finish, (X_FINISH*(CELL_SIZE+BORDER_SIZE) , Y_FINISH*(CELL_SIZE+BORDER_SIZE))) # drawing end position
while (moving):
pygame.event.pump()
# drawing
canvas.blit(sand, (pos.x*(CELL_SIZE+BORDER_SIZE) , pos.y*(CELL_SIZE+BORDER_SIZE)))
canvas.blit(walk, (pos.x*(CELL_SIZE+BORDER_SIZE) , pos.y*(CELL_SIZE+BORDER_SIZE)))
canvas.fill(WHITE, pygame.Rect(0, SIZE*CELL_SIZE+SIZE*BORDER_SIZE, SIZE*CELL_SIZE+(SIZE-1)*BORDER_SIZE, STATUS_SIZE))
text = font.render("E:"+str(episode + 1),True,(BLUE))
canvas.blit(text,(1, SIZE*CELL_SIZE+SIZE*BORDER_SIZE+5))
text = font.render("S:"+str(step),True,(BLUE))
canvas.blit(text,((SIZE*CELL_SIZE+SIZE*BORDER_SIZE) // 5, SIZE*CELL_SIZE+SIZE*BORDER_SIZE+5))
text = font.render("X:"+str(pos.x)+" Y:"+str(pos.y),True,(BLUE))
canvas.blit(text,((SIZE*CELL_SIZE+SIZE*BORDER_SIZE) * 2 // 5, SIZE*CELL_SIZE+SIZE*BORDER_SIZE+5))
text = font.render(format(scores, '.2f'),True,(RED))
canvas.blit(text,((SIZE*CELL_SIZE+SIZE*BORDER_SIZE) * 3 // 5, SIZE*CELL_SIZE+SIZE*BORDER_SIZE+5))
if ((episode == (episodes_max-1)) and ((f == BORDER_FLAG) or (f == WALL_FLAG))):
text = font.render("LOCK",True,(RED))
canvas.blit(text,((SIZE*CELL_SIZE+SIZE*BORDER_SIZE) *4 // 5, SIZE*CELL_SIZE+SIZE*BORDER_SIZE+5))
pygame.display.flip() # display update
print("LOCK") # total scores
break
if (f == FINISH_FLAG): # episode is over
# optimal actions display
i = 0
while (i < SIZE*SIZE):
zero = True
# search for the first explored action
j = 0
while (j < ACTIONS):
if (np.isneginf(q[j, i]) == False):
zero = False # explored action found
break
j += 1 # next action
if (zero == False): # explored action exist
max = q[j, i] # current max q-value
k = j # current optimal action
j += 1 # next action
while (j < ACTIONS):
# check for not nan
if (np.isneginf(q[j, i]) == False):
# check for max
if (q[j, i] > max):
max = q[j, i] # new max q-value
k = j # new optimal action
j += 1 # next action
print(DIRS[k], end = '')
else:
print("-", end = '')
i += 1
if ((i % SIZE) == 0):
print("")
text = font.render("FINISH",True,(BLUE))
canvas.blit(text,((SIZE*CELL_SIZE+SIZE*BORDER_SIZE) *4 // 5, SIZE*CELL_SIZE+SIZE*BORDER_SIZE+5))
pygame.display.flip() # display update
print("SCORES = " + format(scores, '.2f')) # total scores
time.sleep(EPISODE_PAUSE)
break
if (f == WALL_FLAG):
text = font.render("WALL",True,(RED))
canvas.blit(text,((SIZE*CELL_SIZE+SIZE*BORDER_SIZE) *4 // 5, SIZE*CELL_SIZE+SIZE*BORDER_SIZE+5))
elif (f == TRAP_FLAG):
text = font.render("TRAP",True,(RED))
canvas.blit(text,((SIZE*CELL_SIZE+SIZE*BORDER_SIZE) *4 // 5, SIZE*CELL_SIZE+SIZE*BORDER_SIZE+5))
elif (f == BORDER_FLAG):
text = font.render("BORDER",True,(RED))
canvas.blit(text,((SIZE*CELL_SIZE+SIZE*BORDER_SIZE) *4 // 5, SIZE*CELL_SIZE+SIZE*BORDER_SIZE+5))
pygame.display.flip() # display update
pygame.event.pump()
# задержка шага
if (episode == (episodes_max-1)):
time.sleep(STEP_PAUSE) # testing pause
else:
time.sleep(LRN_PAUSE) # learning pause
# random strategy selection
if (np.random.rand() < epsilon):
# explore
a = np.random.randint(0, ACTIONS)
else:
# exploit
zero = True
# find first not nan
j = 0
while (j < ACTIONS):
if (np.isneginf(q[j, state(pos.x, pos.y)]) == False):
zero = False # explored action found
break
j += 1 # next action
if (zero == False): # explored action exist
max = q[j, state(pos.x, pos.y)] # current max q-value
k = j # current optimal action
j += 1 # next action
while (j < ACTIONS):
# check for not nan
if (np.isneginf(q[j, state(pos.x, pos.y)]) == False):
# check for max
if (q[j, state(pos.x, pos.y)] > max):
max = q[j, state(pos.x, pos.y)] # new max q-value
k = j # new optimal action
j += 1 # next action
# exploit
a = k # optimal action select
else: # no action explored
# explore
a = np.random.randint(0, ACTIONS) # random action select
#
dx, dy, r, f = action(pos.x, pos.y, a)
if ((dx != 0) or (dy != 0)): # agent moves
canvas.blit(sand, (pos.x*(CELL_SIZE+BORDER_SIZE) , pos.y*(CELL_SIZE+BORDER_SIZE))) # clean source cell
canvas.blit(footprint, (pos.x*(CELL_SIZE+BORDER_SIZE) , pos.y*(CELL_SIZE+BORDER_SIZE))) # footprint drawing
# q-table update
if (episode != (episodes_max-1)):
if (np.isneginf(q[a, state(pos.x, pos.y)]) == False):
if (np.isneginf(np.max(q[:, state(pos.x+dx, pos.y+dy)])) == False):
q[a, state(pos.x, pos.y)] = q[a, state(pos.x, pos.y)] + ALPHA*(r + GAMMA*np.max(q[:, state(pos.x+dx, pos.y+dy)]) - q[a, state(pos.x, pos.y)])
else:
q[a, state(pos.x, pos.y)] = q[a, state(pos.x, pos.y)] + ALPHA*(r - q[a, state(pos.x, pos.y)])
else:
if (np.isneginf(np.max(q[:, state(pos.x+dx, pos.y+dy)])) == False):
q[a, state(pos.x, pos.y)] = ALPHA*(r + GAMMA*np.max(q[:, state(pos.x+dx, pos.y+dy)]))
else:
q[a, state(pos.x, pos.y)] = ALPHA*r
scores = scores + r # scores update
# moving an agent to a new cell
pos.x = pos.x + dx
pos.y = pos.y + dy
step += 1 # increment step counter
pygame.event.pump()
for event in pygame.event.get(): # event checking
if event.type == pygame.QUIT:
sys.exit(0) # exit from the program
episode += 1 # increment episode counter
while (True): # waiting for window to close
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0) # exit from the program
if (__name__ == "__main__"):
main()