-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerations.py
115 lines (94 loc) · 3.78 KB
/
generations.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
from car import Player
from position import pos
from geneticAlgorithms import ga
import pygame
from colors import WHITE
class Generation():
def __init__(self,screen,race_track,offsprings):
self.screen,self.track=screen,race_track
self.scoresDict={}
if (offsprings==[]):
popNumber=25
self.randomInitialise( popNumber )
self.popNumber=0
def randomInitialise(self,popNumber):
population=[]
for i in range(popNumber):
population.append( Player(self.screen,self.track,position=pos(64+5*i, 54)) )
self.population=population
self.alive_agents=population.copy()
def update(self):
indices_to_pop=[]
for i,player in enumerate(self.alive_agents):
#player.handle_keys()
player.updateDynamics()
player.collision_check()
player.getDistances()
player.draw()
player.updateScore()
player.handleAIOutput()
if player.collision_check():
indices_to_pop.append(i)
counter=0
for index in indices_to_pop:
dead=self.alive_agents.pop(index-counter)
counter+=1
self.scoresDict[dead.score]=dead.AI.getWeightsVector()
#self.scoresDict[dead.score]='mlkia'
self.drawGenerationInfo()
def isPopulationDead(self):
return len(self.alive_agents)==0
def selectKBestParents(self,K=10):
sorted_keys=sorted(self.scoresDict.keys(),reverse=True)
parentsDict={}
for key in sorted_keys[:K]:
parentsDict[key]=self.scoresDict[key]
self.parents=parentsDict
def getMates(self,n_offsprings=20):
parentsDict=self.parents
mates=[]
while len(mates)<n_offsprings:
index1=ga.rouletteWheelParentSelection(parentsDict)
index2=index1
while index1==index2:
index2=ga.rouletteWheelParentSelection(parentsDict)
mate=(index1,index2)
if not mate in mates:
mates.append(mate)
self.mates= mates
def combineMates(self):
mates=self.mates
offsprings=[]
offsprings.append(self.parents[max(self.parents.keys())])
for par1,par2 in mates:
par1,par2=self.parents[par1],self.parents[par2]
x=ga.uniform_crossover(par1,par2)
y=ga.addMutationstoArray(x,n_mutations=10)
offsprings.append(y)
self.offsprings=offsprings
def updatePopulation(self,offsprings):
player=Player(self.screen,self.track,position=pos(64, 54))
player.AI.getWeightsVector()
weightsShapes=player.AI.weightsShapes
print("population",len(self.population))
for i,offspring in enumerate(offsprings):
#player=self.population[i]
print(i)
self.population[i].AI.weightsShapes=weightsShapes
self.population[i].AI.weightsVector=offspring
self.population[i].AI.setWeightsFromVector()
self.population[i].score=0
self.population[i].rect.x=64
self.population[i].rect.y=54
self.population[i].old_center=self.population[i].rect.center
self.population[i].orientation=0
self.population[i].canMove=True
self.alive_agents=self.population.copy()
self.popNumber+=1
def drawGenerationInfo(self):
font = pygame.font.Font('freesansbold.ttf', 20)
string="Generation #"+str(int(self.popNumber))
text = font.render( string , True, WHITE, None)
textRect = text.get_rect()
textRect.center = (1000,10)
self.screen.blit(text, textRect)