-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver6.py
364 lines (282 loc) · 7.89 KB
/
driver6.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
import numpy as np
import math as m
from kivy.clock import Clock
import _thread
import time
import random
from dis import dis
try:
from matplotlib import pyplot as plt
except:
pass
# dummy bot turn tu direction off error
class driver6:
fps = 30.0
work = False
episodeLen = fps*60.00
EPISODES = 500000
SHOW_EVERY = 1000
epsilon = 0.998
LEARNING_RATE = 0.1
DISCOUNT = 0.95
q_table = []
def __init__(self, simEng):
self.sim = simEng
def run(self,render = True):
self.render = render
self.work = True
self.timeOfLastMove = 0.0
self.timeOfWork = 0.0
self.ep_rewards = []
_thread.start_new(self.startIt,())
def startIt(self):
for episode in range(self.EPISODES):
if episode >= 1:
self.episodeNow = episode
if not episode%100:
nm = episode*(self.episodeLen/self.fps)/60.0/60.0*self.sim.boat['sog']
print("episode: %s \texpiriance: %s [nm] memory size [%s]"%(episode,round(nm,2), len(self.q_table[self.qnet])))
print("res:%s",self.ep_rewards[-1])
self.itersLeft = self.episodeLen
self.sim.reset()
self.qnet = 0
for n in range(0,10,1):
self.q_table.append({})
self.mainLoop()
if not (episode+1)%self.SHOW_EVERY:
_thread.start_new(self.buildPlot,())
def buildPlot(self):
r = self.ep_rewards
onGoal = []
moves = []
on = []
off = []
offCorse = []
avg = []
for i in r:
onGoal.append(i['onGoal'])
moves.append(i['moves'])
on.append(i['on'])
off.append(i['off'])
offCorse.append(int(i['offCourse']))
avg.append(i['avg'])
try:
#plt.plot(onGoal,label="onGoal")
#plt.plot(moves, label="moves")
#plt.plot(on, label="on")
#plt.plot(off, label="off")
#plt.plot(offCorse, label="offCourse")
plt.plot(avg, label="reward avg")
plt.legend(loc=1)
plt.show()
except:
pass
def get_discrete_state(self,b):
ce = b['cogError']
gr = self.sim.boat['gRot']*100.00
if self.qnetOld == 1:
ce*=10.0
gr*=10.0
tola = int(self.sim.boat['ruderPos'])//5
divs = []
cefoldIn = None
grfoldIn = None
tolafoldIn = None
res = 0.0
sufix = 0
base = 40.0
if ce >= base:
ce = base
elif ce <= -base:
ce = -base
if gr >= base:
gr = base
elif gr <= -base:
gr = -base
if tola >= base:
tola = base
elif tola <= -base:
tola = -base
divsCount = 6
if ce < -(base*0.5) and ce >=-base:
cefoldIn = divsCount
if gr < -(base*0.5) and gr >=-base:
grfoldIn = divsCount
if tola < -(base*0.5) and tola >=-base:
tolafoldIn = divsCount
for doff in range(divsCount):
d = doff-(divsCount*0.5)
if d < 0.0:
res = base*0.5
resOld = res
sufix+=1
divs.append(round(res,3))
if base >= ce and ce >= res:
cefoldIn = d
if base >= gr and gr >= res:
grfoldIn = d
if base >= tola and tola >= res:
tolafoldIn = d
else:
res = -divs[sufix-1]
divs.append(res)
sufix-=1
#print(res,resOld)
if res <= ce and ce <= resOld:
cefoldIn = d
if res <= gr and gr <= resOld:
grfoldIn = d
if res <= tola and tola <= resOld:
tolafoldIn = d
resOld = res
base = res
if cefoldIn == None:
print(res,resOld)
print("CE None foldIn: %s"%ce)
print(divs)
if grfoldIn == None:
print(res,resOld)
print("GR None foldIn: %s"%gr)
print(divs)
if tolafoldIn == None:
print(res,resOld)
print("tola None foldIn: %s"%tola)
print(divs)
n = "%s_%s"%(cefoldIn,grfoldIn)
return self.chkQtable(n)
def chkQtable(self,discrete_state):
try:
self.q_table[self.qnetOld][discrete_state]
except:
print("making new entry to qtable [%s] %s"%(self.qnetOld,discrete_state))
new = [-random.random(),-random.random(),-random.random()]
self.q_table[self.qnetOld][discrete_state] = new
return discrete_state
def mainLoop(self):
s = self.sim
b = s.boat
offCorseSum = 0.0
movesc = 0
offCourse = False
action = 0
actionOld = 0
self.mh = []
self.discreteHistory = []
self.goal = 0.0
iterNo = 0
timeOfWork = 0.0
timeOfOff = 0.0
directionChangeCount = 0
randomDesision = 0
randomDesisionCount = 0
rewardMem = []
qnetOld = 0
self.qnetOld = 0
discrete_state = self.get_discrete_state(b)
while self.itersLeft > 0:
randomAllowed = False
try:
self.directionChangeCount = 0
self.timeOfWork = 0.0
self.timeOfOff = 0.0
self.timeOfLastMove = 0.0
dirOld = None
if len(self.mh)>(2.0*self.fps):
self.mh.pop(0)
self.discreteHistory.pop()
if len(self.mh)>4:
for d in self.mh:
if dirOld == None:
dirOld = dir
if d != dirOld:
self.directionChangeCount+=1
self.timeOfLastMove = 0.0
if d != 0:
self.timeOfWork+=1.0/self.fps
if d == 0:
self.timeOfOff+=1.0 / self.fps
self.timeOfLastMove+=1.0/self.fps
dirOld = d
if not iterNo % ( 2.0*self.fps ):
timeOfOff+= self.timeOfOff
timeOfWork+= self.timeOfWork
directionChangeCount+= self.directionChangeCount
except:
pass
reward = -1.0
self.qnet = 0
if len(self.mh) > 5:
if self.qnet == 0 and m.fabs(b['cogError'])<5.0 and self.directionChangeCount<=3 and m.fabs(b['gRot']) < 0.05:
reward = -1.0/(movesc+1.0)
self.qnet = 1
#self.itersLeft = 0
print("0in moves ",movesc)
movesc = 0
if self.qnet == 1 and m.fabs(b['cogError'])<0.5 and self.directionChangeCount<=3 and m.fabs(b['gRot']) < 0.01:
reward = -1.0/(movesc+1.0)
self.qnet = 2
print("1in moves ",movesc)
self.itersLeft = 0
if random.random() > self.epsilon and randomAllowed:
keys = list(self.q_table[qnetOld].keys())
keysLen = len(keys)
random_discrete_state = keys[random.randint(0,keysLen-1)]
aa = max(self.q_table[qnetOld][random_discrete_state])
action = self.q_table[qnetOld][random_discrete_state].index(aa)
randomDesision = True
randomDesisionCount+=1
else:
self.chkQtable(discrete_state)
aa = max(self.q_table[qnetOld][discrete_state])
action = self.q_table[qnetOld][discrete_state].index(aa)
self.mh.append(action-1)
if self.qnet != 2:
self.sim.iter(action-1)
#print( action-1 )
if action != actionOld:
movesc+=1
self.discreteHistory.append([discrete_state,0])
if self.qnet != 2:
self.discreteHistory[-1][1] = action
new_discrete_state = self.get_discrete_state(b)
max_future_q = max(self.q_table[qnetOld][new_discrete_state])
current_q = self.q_table[qnetOld][discrete_state][int(action)]
new_q = (1 - self.LEARNING_RATE) * current_q + self.LEARNING_RATE * (reward + self.DISCOUNT * max_future_q)
self.q_table[qnetOld][discrete_state][int(action)] = new_q
#self.q_table[self.discreteHistory[0][0]][int(self.discreteHistory[0][1])] = new_q
#if self.itersLeft == 1:
# print("new_q %s"%new_q)
rewardMem.append(reward)
discrete_state = new_discrete_state
actionOld = action
offCorseSum+= m.fabs(b['cogError'])
if self.render and not self.episodeNow%self.SHOW_EVERY:
self.sim.renderFrame()
#print('discrete_state',discrete_state,' timeOfLastWork',self.timeOfLastMove)
#print(reward)
print("qnet",self.qnet)
time.sleep(1.00/self.fps)
if randomDesision:
print("random desision")
randomDesision = False
self.itersLeft-=1
iterNo+=1
qnetOld = self.qnet
self.qnetOld = self.qnet
self.get_discrete_state(b)
self.ep_rewards.append({
'offCorseSum': round(offCorseSum,2),
'reward': reward,
'onGoal': round(self.goal,3),
'moves': movesc,
'off':round(timeOfOff,2),
'on':round(timeOfWork,2),
'changes': directionChangeCount,
'offCourse': offCourse,
'randDesisions': randomDesisionCount,
'min': min(rewardMem),
'max': max(rewardMem),
'avg': sum(rewardMem)/len(rewardMem),
'qnet': self.qnet
})
#print( self.ep_rewards[-1])