-
Notifications
You must be signed in to change notification settings - Fork 4
/
BreakoutRA.py
500 lines (399 loc) · 17.3 KB
/
BreakoutRA.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#
# Breakout game with reward automa for non-Markovian rewards
#
# Luca Iocchi 2017
#
import pygame, sys
import numpy as np
import atexit
import random
import time
import math
from math import fabs
from Breakout import *
np.set_printoptions(precision=3)
# Reward automa
class RewardAutoma(object):
def __init__(self, brick_cols=0, left_right=True): # brick_cols=0 -> RA disabled
# RA states
self.brick_cols = brick_cols
if (self.brick_cols>0):
self.nRAstates = brick_cols+2 # number of RA states
self.RAGoal = self.nRAstates-2
self.RAFail = self.nRAstates-1
else: # RA disabled
self.nRAstates = 2 # number of RA states
self.RAGoal = 1 # never reached
self.RAFail = 2 # never reached
self.STATES = {
'RAGoalStep':100, # goal step of reward automa
'RAGoal':1000, # goal of reward automa
'RAFail':0, # fail of reward automa
'GoodBrick':0, # good brick removed for next RA state
'WrongBrick':0 # wrong brick removed for next RA state
}
self.left_right = left_right
self.goalreached = 0 # number of RA goals reached for statistics
self.visits = {} # number of visits for each state
self.success = {} # number of good transitions for each state
self.reset()
def init(self, game):
self.game = game
def reset(self):
self.current_node = 0
self.last_node = self.current_node
self.countupdates = 0 # count state transitions (for the score)
if (self.current_node in self.visits):
self.visits[self.current_node] += 1
else:
self.visits[self.current_node] = 1
# check if a column is free (used by RA)
def check_free_cols(self, cols):
cond = True
for c in cols:
for j in range(0,self.game.brick_rows):
if (self.game.bricksgrid[c][j]==1):
cond = False
break
return cond
# RewardAutoma Transition
def update(self):
reward = 0
state_changed = False
# RA disabled
if (self.brick_cols==0):
return (reward, state_changed)
for b in self.game.last_brikcsremoved:
if b.i == self.current_node:
reward += self.STATES['GoodBrick']
#print 'Hit right brick for next RA state'
else:
reward += self.STATES['WrongBrick']
#print 'Hit wrong brick for next RA state'
f = np.zeros(self.brick_cols)
for c in range(0,self.brick_cols):
f[c] = self.check_free_cols([c]) # vector of free columns
if (self.current_node<self.brick_cols): # 0 ... brick_cols
if self.left_right:
goal_column = self.current_node
cbegin = goal_column + 1
cend = self.brick_cols
cinc = 1
else:
goal_column = self.brick_cols - self.current_node - 1
cbegin = goal_column - 1
cend = -1
cinc = -1
if f[goal_column]:
state_changed = True
self.countupdates += 1
self.last_node = self.current_node
self.current_node += 1
reward += self.STATES['RAGoal'] * self.countupdates / self.brick_cols
#print(" -- RA state transition to %d, " %(self.current_node))
if (self.current_node==self.RAGoal):
# print(" <<< RA GOAL >>>")
reward += self.STATES['RAGoal']
self.goalreached += 1
else:
for c in range(cbegin, cend, cinc):
if f[c]:
self.last_node = self.current_node
self.current_node = self.RAFail # FAIL
reward += self.STATES['RAFail']
#print(" *** RA FAIL *** ")
break
elif (self.current_node==self.RAGoal): # GOAL
pass
elif (self.current_node==self.RAFail): # FAIL
pass
if (state_changed):
if (self.current_node in self.visits):
self.visits[self.current_node] += 1
else:
self.visits[self.current_node] = 1
if (self.current_node != self.RAFail):
#print "Success for last_node ",self.last_node
if (self.last_node in self.success):
self.success[self.last_node] += 1
else:
self.success[self.last_node] = 1
return (reward, state_changed)
def current_successrate(self):
s = 0.0
v = 1.0
if (self.current_node in self.success):
s = float(self.success[self.current_node])
if (self.current_node in self.visits):
v = float(self.visits[self.current_node])
#print " -- success rate: ",s," / ",v
return s/v
def print_successrate(self):
r = []
for i in range(len(self.success)):
r.append(float(self.success[i])/self.visits[i])
print('RA success: %s' %str(r))
class BreakoutSRA(BreakoutS):
def __init__(self, brick_rows=3, brick_cols=3, trainsessionname='test'):
BreakoutS.__init__(self,brick_rows, brick_cols, trainsessionname)
self.RA = RewardAutoma(brick_cols)
self.RA.init(self)
self.STATES = {
'Init':0,
'Alive':0,
'Dead':-1,
'PaddleNotMoving':0,
'Scores':0, # brick removed
'Hit':0, # paddle hit
'Goal':0, # level completed
}
self.RA_exploration_enabled = False # Use options to speed-up learning process
self.report_str = ''
def savedata(self):
return [self.iteration, self.hiscore, self.hireward, self.elapsedtime, self.RA.visits, self.RA.success, self.agent.SA_failure, random.getstate(),np.random.get_state()]
def loaddata(self,data):
self.iteration = data[0]
self.hiscore = data[1]
self.hireward = data[2]
self.elapsedtime = data[3]
self.RA.visits = data[4]
self.RA.success = data[5]
if (len(data)>6):
self.agent.SA_failure = data[6]
if (len(data)>7):
print('Set random generator state from file.')
random.setstate(data[7])
np.random.set_state(data[8])
def setStateActionSpace(self):
super(BreakoutSRA, self).setStateActionSpace()
self.nstates *= self.RA.nRAstates
print('Number of states: %d' %self.nstates)
print('Number of actions: %d' %self.nactions)
def getstate(self):
x = super(BreakoutSRA, self).getstate()
return x + (self.n_diff_paddle_ball) * self.RA.current_node
def reset(self):
super(BreakoutSRA, self).reset()
self.RA.reset()
self.RA_exploration()
def update(self, a):
super(BreakoutSRA, self).update(a)
(RAr, state_changed) = self.RA.update()
if (state_changed):
self.RA_exploration()
self.current_reward += RAr
if (self.RA.current_node==self.RA.RAFail):
self.finished = True
def goal_reached(self):
return self.RA.current_node==self.RA.RAGoal
def getreward(self):
r = self.current_reward
#for b in self.last_brikcsremoved:
# if b.i == self.RA.current_node:
# r += self.STATES['GoodBrick']
#print 'Hit right brick for next RA state'
#if (self.current_reward>0 and self.RA.current_node>0 and self.RA.current_node<=self.RA.RAGoal):
#r *= (self.RA.current_node+1)
#print "MAXI REWARD ",r
if (self.current_reward>0 and self.RA.current_node==self.RA.RAFail): # FAIL RA state
r = 0
self.cumreward += self.gamman * r
self.gamman *= self.agent.gamma
return r
def print_report(self, printall=False):
toprint = printall
ch = ' '
if (self.agent.optimal):
ch = '*'
toprint = True
RAnode = self.RA.current_node
if (RAnode==self.RA.RAFail):
RAnode = self.RA.last_node
s = 'Iter %6d, b_hit: %3d, p_hit: %3d, na: %4d, r: %5d, RA: %d, mem: %d/%d %c' %(self.iteration, self.score, self.paddle_hit_count,self.numactions, self.cumreward, RAnode, len(self.agent.Q), len(self.agent.SA_failure), ch)
if self.score > self.hiscore:
self.hiscore = self.score
s += ' HISCORE '
toprint = True
if self.cumreward > self.hireward:
self.hireward = self.cumreward
s += ' HIREWARD '
toprint = True
if (toprint):
print(s)
self.cumreward100 += self.cumreward
self.cumscore100 += RAnode
numiter = 100
if (self.iteration%numiter==0):
#self.doSave()
self.report_str = "%s %6d/%4d avg last 100: reward %.1f | RA %.2f | p goals %.1f %% <<<" %(self.trainsessionname, self.iteration, self.elapsedtime, float(self.cumreward100/100), float(self.cumscore100)/100, float(self.RA.goalreached*100)/numiter)
print('-----------------------------------------------------------------------')
print(self.report_str)
self.RA.print_successrate()
print('-----------------------------------------------------------------------')
self.cumreward100 = 0
self.cumscore100 = 0
self.RA.goalreached = 0
sys.stdout.flush()
self.vscores.append(self.score)
#self.resfile.write("%d,%d,%d,%d,%d\n" % (RAnode, self.cumreward, self.goal_reached(),self.numactions,self.agent.optimal))
self.resfile.write("%d,%d,%d,%d,%d,%d,%d\n" % (self.iteration, self.elapsedtime, RAnode, self.cumreward, self.goal_reached(),self.numactions,self.agent.optimal))
self.resfile.flush()
def RA_exploration(self):
if not self.RA_exploration_enabled:
return
#print("RA state: ",self.RA.current_node)
success_rate = max(min(self.RA.current_successrate(),0.9),0.1)
#print("RA exploration policy: current state success rate ",success_rate)
er = random.random()
self.agent.option_enabled = (er<success_rate)
#print("RA exploration policy: optimal ",self.agent.partialoptimal, "\n")
class BreakoutNRA(BreakoutN):
def __init__(self, brick_rows=3, brick_cols=3, trainsessionname='test', RAenabled=True):
BreakoutN.__init__(self,brick_rows, brick_cols, trainsessionname)
RA_cols = 0
if (RAenabled):
RA_cols = brick_cols
self.RA_exploration_enabled = False # Use options to speed-up learning process
self.RA = RewardAutoma(RA_cols)
self.RA.init(self)
self.STATES = {
'Init':0,
'Alive':0,
'Dead':-1,
'PaddleNotMoving':0,
'Scores':0, # brick removed
'Hit':0, # paddle hit
'Goal':0, # level completed
}
def savedata(self):
return [self.iteration, self.hiscore, self.hireward, self.elapsedtime, self.RA.visits, self.RA.success, self.agent.SA_failure, random.getstate(),np.random.get_state()]
def loaddata(self,data):
self.iteration = data[0]
self.hiscore = data[1]
self.hireward = data[2]
self.elapsedtime = data[3]
self.RA.visits = data[4]
self.RA.success = data[5]
if (len(data)>6):
self.agent.SA_failure = data[6]
if (len(data)>7):
print('Set random generator state from file.')
random.setstate(data[7])
np.random.set_state(data[8])
def setStateActionSpace(self):
super(BreakoutNRA, self).setStateActionSpace()
self.nstates *= self.RA.nRAstates
print('Number of states: %d' %self.nstates)
print('Number of actions: %d' %self.nactions)
def getstate(self):
x = super(BreakoutNRA, self).getstate()
return x + (self.n_ball_x*self.n_ball_y*self.n_ball_dir*self.n_paddle_x) * self.RA.current_node
def reset(self):
super(BreakoutNRA, self).reset()
self.RA.reset()
self.RA_exploration()
def update(self, a):
super(BreakoutNRA, self).update(a)
(RAr, state_changed) = self.RA.update()
if (state_changed):
self.RA_exploration()
self.current_reward += RAr
if (self.RA.current_node==self.RA.RAFail):
self.finished = True
def goal_reached(self):
return self.RA.current_node==self.RA.RAGoal
def getreward(self):
r = self.current_reward
#if (self.current_reward>0 and self.RA.current_node>0 and self.RA.current_node<=self.RA.RAGoal):
# r *= (self.RA.current_node+1)
#print "MAXI REWARD ",r
if (self.current_reward>0 and self.RA.current_node==self.RA.RAFail): # FAIL RA state
r = 0
self.cumreward += self.gamman * r
self.gamman *= self.agent.gamma
#if (r<0):
# print("Neg reward: %.1f" %r)
return r
def print_report(self, printall=False):
toprint = printall
ch = ' '
if (self.agent.optimal):
ch = '*'
toprint = True
RAnode = self.RA.current_node
if (RAnode==self.RA.RAFail):
RAnode = self.RA.last_node
s = 'Iter %6d, b_hit: %3d, p_hit: %3d, na: %4d, r: %5d, RA: %d, mem: %d/%d %c' %(self.iteration, self.score, self.paddle_hit_count,self.numactions, self.cumreward, RAnode, len(self.agent.Q), len(self.agent.SA_failure), ch)
if self.score > self.hiscore:
self.hiscore = self.score
s += ' HISCORE '
toprint = True
if self.cumreward > self.hireward:
if self.agent.optimal:
self.hireward = self.cumreward
s += ' HIREWARD '
toprint = True
if (toprint):
print(s)
self.cumreward100 += self.cumreward
self.cumscore100 += RAnode
numiter = 100
if (self.iteration%numiter==0):
#self.doSave()
print('----------------------------------------------------------------------------------')
print("%s %6d/%4d avg last 100: reward %.1f | RA %.2f | p goals %.1f %%" %(self.trainsessionname, self.iteration, self.elapsedtime, float(self.cumreward100/100), float(self.cumscore100)/100, float(self.RA.goalreached*100)/numiter))
self.RA.print_successrate()
print('----------------------------------------------------------------------------------')
self.cumreward100 = 0
self.cumscore100 = 0
self.RA.goalreached = 0
sys.stdout.flush()
self.vscores.append(self.score)
#self.resfile.write("%d,%d,%d,%d,%d\n" % (RAnode, self.cumreward, self.goal_reached(),self.numactions,self.agent.optimal))
self.resfile.write("%d,%d,%d,%d,%d,%d,%d\n" % (self.iteration, self.elapsedtime, RAnode, self.cumreward, self.goal_reached(),self.numactions,self.agent.optimal))
self.resfile.flush()
def RA_exploration(self):
if not self.RA_exploration_enabled:
return
#print("RA state: ",self.RA.current_node)
success_rate = max(min(self.RA.current_successrate(),0.9),0.1)
#print("RA exploration policy: current state success rate ",success_rate)
er = random.random()
self.agent.option_enabled = (er<success_rate)
#print("RA exploration policy: optimal ",self.agent.partialoptimal, "\n")
class BreakoutSRAExt(BreakoutSRA):
def setStateActionSpace(self):
super(BreakoutSRAExt, self).setStateActionSpace()
self.nstates *= math.pow(2,self.brick_rows*self.brick_cols)
print('Number of states: %d' %self.nstates)
print('Number of actions: %d' %self.nactions)
def getstate(self):
x = super(BreakoutSRAExt, self).getstate()
return x + (self.n_diff_paddle_ball * self.RA.nRAstates) * self.encodebricks(self.bricksgrid)
def encodebricks(self,brickgrid):
b = 1
r = 0
for i in range(0,self.brick_cols):
for j in range(0,self.brick_rows):
if self.bricksgrid[i][j]==1:
r += b
b *= 2
return r
class BreakoutNRAExt(BreakoutNRA):
def setStateActionSpace(self):
super(BreakoutNRAExt, self).setStateActionSpace()
self.nstates *= math.pow(2,self.brick_rows*self.brick_cols)
print('Number of states: %d' %self.nstates)
print('Number of actions: %d' %self.nactions)
def getstate(self):
x = super(BreakoutNRAExt, self).getstate()
return x + (self.n_ball_x * self.n_ball_y * self.n_ball_dir * self.n_paddle_x * self.RA.nRAstates) * self.encodebricks(self.bricksgrid)
def encodebricks(self,brickgrid):
b = 1
r = 0
for i in range(0,self.brick_cols):
for j in range(0,self.brick_rows):
if self.bricksgrid[i][j]==1:
r += b
b *= 2
return r