-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_autoTrainer.py
453 lines (359 loc) · 13.4 KB
/
main_autoTrainer.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
import pygame
import neat
import time
import os
import random
import pickle
import math
import visualize
pygame.font.init()
STAT_FONT = pygame.font.SysFont("comicsans",50)
WIN_WIDTH = 700
WIN_HEIGHT = 700
CAR_IMG = pygame.transform.rotate(pygame.transform.scale(pygame.image.load(os.path.join("imgs","mustang.png")),(50,100)),-90)
TRACK1_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs","Track1.png")),(WIN_WIDTH,WIN_HEIGHT))
TRACK2_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs","Track2.png")),(WIN_WIDTH,WIN_HEIGHT))
TRACK3_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs","Track3.png")),(WIN_WIDTH,WIN_HEIGHT))
TRACK4_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs","Track4.png")),(WIN_WIDTH,WIN_HEIGHT))
TRACK5_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs","Track5.png")),(WIN_WIDTH,WIN_HEIGHT))
TRACK6_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs","Track6.png")),(WIN_WIDTH,WIN_HEIGHT))
END_OF_LINE_COLOR =(0,255,0,255)
MAX_OUT_OF_LINE = 10
OUT_OF_LINE_PENALTY = 100
FINISH_BONUS = 1000
MAX_TIME_IN_SECONDS = 10
FPS = 30
MAX_TIME = MAX_TIME_IN_SECONDS*(FPS)
GENERATION_COUNT =0
class Car:
def __init__(self,x,y):
self.x =x
self.y =y
self.length = 100
self.SensorsWidth = 50
self.trackWidth = 50
self.rotation = 0
self.img = CAR_IMG
self.originalImg = self.img
self.carSensorPositions=[self.getFrontLeftCarSensorPosition(),self.getFrontCenterLeftCarSensorPosition()
,self.getFrontCenterCarSensorPosition(),self.getFrontCenterRightCarSensorPosition(),self.getFrontRightCarSensorPosition()]
self.lastDistance =0
self.nbOfOutOfLineDetection = 0
self.isActive = True
self.isAlive = True
def restart(self,x,y):
self.x =x
self.y =y
self.img = CAR_IMG
self.rotation = 0
self.lastDistance =0
self.nbOfOutOfLineDetection = 0
self.isActive = True
self.isAlive = True
self.carSensorPositions=[self.getFrontLeftCarSensorPosition(),self.getFrontCenterLeftCarSensorPosition()
,self.getFrontCenterCarSensorPosition(),self.getFrontCenterRightCarSensorPosition(),self.getFrontRightCarSensorPosition()]
def outOfTrack(self,currentTrack):
trackRect = currentTrack.get_rect(topleft =(0,0))
carRect = self.img.get_rect(topleft = ((self.x),(self.y)))
carRect = carRect.inflate(12,12)
if trackRect.contains(carRect) == True:
return False
else:
return True
def incrementOutOfLineDetection(self):
self.nbOfOutOfLineDetection +=1
def getOutOfLineDetection(self):
return self.nbOfOutOfLineDetection
def isTheCarActive(self):
return self.isActive
def deactivate(self):
self.isActive = False
def isTheCarAlive(self):
return self.isAlive
def kill(self):
self.deactivate()
self.isAlive = False
def rotateCar(self,rotAngle):
self.rotation += rotAngle
if (self.rotation >0):
self.rotation = self.rotation % 360
else :
self.rotation = -self.rotation
self.rotation = self.rotation % 360
self.rotation = -self.rotation
rotated_image = pygame.transform.rotate(self.originalImg, -self.rotation)
old_rect = self.img.get_rect(topleft = ((self.x),(self.y)))
new_rect = rotated_image.get_rect(center=old_rect.center)
self.img = rotated_image
self.x = float(new_rect.topleft[0])
self.y = float(new_rect.topleft[1])
def getCenter(self):
return self.img.get_rect(topleft = (int(self.x),int(self.y))).center
def getFrontLeftCarSensorPosition(self):
returnVal=(0,0)
x= (self.length)/2
y= -(self.SensorsWidth)/2
teta = math.radians(self.rotation)
x1=(x*math.cos(teta)-y*math.sin(teta))
y1=(x*math.sin(teta)+y*math.cos(teta))
x1+=self.getCenter()[0]
y1+=self.getCenter()[1]
returnVal=int(x1),int(y1)
return returnVal
def getFrontCenterLeftCarSensorPosition(self):
returnVal=(0,0)
x= (self.length)/2
y= -(self.SensorsWidth)*1/4
teta = math.radians(self.rotation)
x1=(x*math.cos(teta)-y*math.sin(teta))
y1=(x*math.sin(teta)+y*math.cos(teta))
x1+=self.getCenter()[0]
y1+=self.getCenter()[1]
returnVal=int(x1),int(y1)
return returnVal
def getFrontRightCarSensorPosition(self):
returnVal=(0,0)
x= (self.length)/2
y= (self.SensorsWidth)/2
teta = math.radians(self.rotation)
x1=(x*math.cos(teta)-y*math.sin(teta))
y1=(x*math.sin(teta)+y*math.cos(teta))
x1+=self.getCenter()[0]
y1+=self.getCenter()[1]
returnVal=int(x1),int(y1)
return returnVal
def getFrontCenterRightCarSensorPosition(self):
returnVal=(0,0)
x= (self.length)/2
y= (self.SensorsWidth)*1/4
teta = math.radians(self.rotation)
x1=(x*math.cos(teta)-y*math.sin(teta))
y1=(x*math.sin(teta)+y*math.cos(teta))
x1+=self.getCenter()[0]
y1+=self.getCenter()[1]
returnVal=int(x1),int(y1)
return returnVal
def getFrontCenterCarSensorPosition(self):
returnVal=(0,0)
x= (self.length)/2
y= 0
teta = math.radians(self.rotation)
x1=(x*math.cos(teta)-y*math.sin(teta))
y1=(x*math.sin(teta)+y*math.cos(teta))
x1+=self.getCenter()[0]
y1+=self.getCenter()[1]
returnVal=int(x1),int(y1)
return returnVal
def getCarSensorValue(self,sensorPosition,currentTrack):
returnVal=0
startX = sensorPosition[0]-3
endX = sensorPosition[0]+3
startY = sensorPosition[1]-3
endY = sensorPosition[1]+3
for x in range(startX,endX):
for y in range(startY,endY):
if (currentTrack.get_at((x,y))==(0,0,0,255)):
returnVal = 1
break
return returnVal
def isCarIsOutOfLine(self,currentTrack):
returnVal = True
for p in self.carSensorPositions:
if self.getCarSensorValue(p,currentTrack) == 1:
returnVal = False
break
return returnVal
def isCarIsAtTheFinishLine(self,currentTrack):
returnVal = False
for p in self.carSensorPositions:
if (currentTrack.get_at(p)== END_OF_LINE_COLOR):
returnVal = True
break
return returnVal
def getLastDistance(self):
return self.lastDistance
def advance(self,distance):
dx= distance* (math.cos(math.radians(self.rotation)))
dy= distance* (math.sin(math.radians(self.rotation)))
if (abs(dx)<0.3):
dx = 0
elif (dx >0):
dx = math.ceil(dx)
else:
dx = (math.floor(dx))
if (abs(dy)<0.3):
dy = 0
elif (dy >0):
dy = math.ceil(dy)
else:
dy = (math.floor(dy))
self.x = round(self.x,2) + round(dx,2)
self.y = round(self.y,2) + round(dy,2)
def move(self,dLeft,dRight):
d= (dLeft+dRight)/2
if abs(dRight-dLeft)<0.0001:
turningRadius = 9999999999
else:
turningRadius = -(self.trackWidth/2)*((dRight+dLeft)/(dRight-dLeft))
deltaAngle = math.degrees(-(dRight-dLeft)/self.trackWidth)
self.rotateCar(deltaAngle)
self.advance(d)
self.lastDistance = d
self.carSensorPositions=[self.getFrontLeftCarSensorPosition(),self.getFrontCenterLeftCarSensorPosition()
,self.getFrontCenterCarSensorPosition(),self.getFrontCenterRightCarSensorPosition(),self.getFrontRightCarSensorPosition()]
def drawSensors(self,win):
pygame.draw.line(win,(255,25,0),self.getFrontLeftCarSensorPosition(),self.getFrontRightCarSensorPosition(),10)
pygame.draw.circle(win,(0,0,255),self.getFrontLeftCarSensorPosition(),3,3)
pygame.draw.circle(win,(0,0,255),self.getFrontRightCarSensorPosition(),3,3)
pygame.draw.circle(win,(0,0,255),self.getFrontCenterCarSensorPosition(),3,3)
pygame.draw.circle(win,(0,0,255),self.getFrontCenterLeftCarSensorPosition(),3,3)
pygame.draw.circle(win,(0,0,255),self.getFrontCenterRightCarSensorPosition(),3,3)
def draw(self,win):
win.blit(self.img, (self.x, self.y))
self.drawSensors(win)
def draw_window(win,cars,timeValue,gen,currentTrack,trackIndex):
text = STAT_FONT.render("Time in ms: "+str(int(timeValue*1000/FPS)),1,(0,0,0))
textgen = STAT_FONT.render("Gen: "+str(gen),1,(0,0,0))
textTrack = STAT_FONT.render("track: "+str(trackIndex+1),1,(0,0,0))
win.blit(currentTrack, (0, 0))
win.blit(text, (10,10))
win.blit(textTrack, (text.get_width()+20,10))
win.blit(textgen, (WIN_WIDTH-10 - textgen.get_width(),10))
for car in cars:
if car.isTheCarActive() == True:
car.draw(win)
pygame.display.update()
def trainingFunction(genomes, config):
global GENERATION_COUNT
GENERATION_COUNT +=1
win = pygame.display.set_mode((WIN_WIDTH,WIN_HEIGHT))
clock = pygame.time.Clock()
tracks = [TRACK1_IMG,TRACK2_IMG,TRACK3_IMG,TRACK4_IMG,TRACK5_IMG,TRACK6_IMG]
trackIndex = 0
run = True
ge=[]
nets=[]
cars=[]
tempGe=[]
tempNets=[]
timeCounter = 0
for _,g in genomes:
net = neat.nn.FeedForwardNetwork.create(g, config)
nets.append(net)
cars.append(Car(30,50))
g.fitness = 0
ge.append(g)
tempGe.append(g)
tempNets.append(net)
while ((run == True) and (numberOfAliveCars(cars)>0) and (trackIndex <len(tracks))):
clock.tick(FPS)
timeCounter+=1
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
keyPressed = pygame.key.get_pressed()
if (keyPressed[pygame.K_ESCAPE] ==True):
run = False
if (timeCounter>=MAX_TIME):
for car in cars:
if car.isTheCarActive():
car.kill()
if (numberOfActiveCars(cars)>0):
for indexC,car in enumerate(cars):
if car.isTheCarActive()== True:
if(car.outOfTrack(tracks[trackIndex])==True):
car.kill()
else:
output = nets[indexC].activate((car.getCarSensorValue(car.carSensorPositions[0],tracks[trackIndex]),
car.getCarSensorValue(car.carSensorPositions[1],tracks[trackIndex]),
car.getCarSensorValue(car.carSensorPositions[2],tracks[trackIndex]),
car.getCarSensorValue(car.carSensorPositions[3],tracks[trackIndex]),
car.getCarSensorValue(car.carSensorPositions[4],tracks[trackIndex])))
car.move(output[0]*10,output[1]*10)
ge[indexC].fitness += car.getLastDistance()
if(car.outOfTrack(tracks[trackIndex])==True):
car.kill()
elif (car.isCarIsAtTheFinishLine(tracks[trackIndex])== True):
ge[indexC].fitness += (FINISH_BONUS*(trackIndex+1))+(pow(MAX_TIME-timeCounter,2))
car.deactivate()
elif (car.isCarIsOutOfLine(tracks[trackIndex]) == True ):
car.incrementOutOfLineDetection()
ge[indexC].fitness -= OUT_OF_LINE_PENALTY
if(car.getOutOfLineDetection()>MAX_OUT_OF_LINE):
car.kill()
else:
#do nothing
pass
for indexC,car in enumerate(cars):
if (car.isTheCarAlive() == False):
cars.pop(indexC)
nets.pop(indexC)
ge.pop(indexC)
draw_window(win,cars,timeCounter,GENERATION_COUNT,tracks[trackIndex],trackIndex)
else:
if (numberOfAliveCars(cars)>0):
run = True
timeCounter =0
trackIndex+=1
for car in cars:
car.restart(30,50)
else:
run = False
bestFitness = tempGe[0].fitness
indexBestOfGen = 0
for tempIndex,gg in enumerate(tempGe):
if gg.fitness > bestFitness:
bestFitness = gg.fitness
indexBestOfGen = tempIndex
saveNet(tempNets[indexBestOfGen],GENERATION_COUNT)
def run(config_file):
# Load configuration.
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_file)
# Create the population, which is the top-level object for a NEAT run.
p = neat.Population(config)
# Add a stdout reporter to show progress in the terminal.
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
p.add_reporter(neat.Checkpointer(5))
# Run for up to 50 generations.
winner = p.run(trainingFunction, 50)
# Display the winning genome.
print('\nBest genome:\n{!s}'.format(winner))
visualize.draw_net(config, winner, True)
visualize.plot_stats(stats, ylog=False, view=True)
visualize.plot_species(stats, view=True)
winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
with open ('backupFile','wb') as backupFile :
pickle.dump( winner_net, backupFile )
def saveNet(net,genCount):
isDir =os.path.isdir("generationBackups")
if isDir == False:
os.mkdir("generationBackups")
fileName="backupFile"+str(genCount)
fullFileName = os.path.join("generationBackups", fileName)
with open (fullFileName,'wb') as backupFile :
pickle.dump( net, backupFile )
def numberOfActiveCars(cars):
returnCounter = 0
for car in cars:
if car.isTheCarActive()==True:
returnCounter+=1
return (returnCounter)
def numberOfAliveCars(cars):
returnCounter = 0
for car in cars:
if car.isTheCarAlive()==True:
returnCounter+=1
return (returnCounter)
if __name__ == '__main__':
# Determine path to configuration file. This path manipulation is
# here so that the script will run successfully regardless of the
# current working directory.
local_dir = os.path.dirname(__file__)
config_file = os.path.join(local_dir, 'config-feedforward.txt')
run(config_file)