-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.py
230 lines (190 loc) · 8.24 KB
/
map.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
import pygame
import math
from neuronalNetwork import *
hexagonImage = pygame.image.load(HEXAGON_FILE)
hexagonImageResized = pygame.transform.scale(hexagonImage, (HEXAGON_WIDTH, HEXAGON_HEIGHT))
hexagonsPositions = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
weightsOfCars = []
def measureDistanceBetweenTwoPoints(p1X, p1Y, p2X, p2Y):
xDelta = p1X - p2X
yDelta = p1Y - p2Y
distance = math.sqrt(xDelta ** 2 + yDelta ** 2)
return distance
def getSecondPointForReferenceCar(car, typePoint):
hypothenuse = 50
if typePoint == 45:
if car.isGrowingLineLeft:
hypothenuse = car.longitudeLineLeft + .1
else:
hypothenuse = car.longitudeLineLeft
if typePoint == 0:
if car.isGrowingLineCenter:
hypothenuse = car.longitudeLineCenter + .1
else:
hypothenuse = car.longitudeLineCenter
if typePoint == -45:
if car.isGrowingLineRight:
hypothenuse = car.longitudeLineRight + .1
else:
hypothenuse = car.longitudeLineRight
centerP1X = car.posX
centerP1Y = car.posY
angleAndSection = car.getAngle(car.angle + typePoint)
xComponent = math.cos(car.getAngleInRadians(angleAndSection[0])) * hypothenuse
yComponent = math.sin(car.getAngleInRadians(angleAndSection[0])) * hypothenuse
if angleAndSection[1] == 1:
centerP1X += xComponent
centerP1Y -= yComponent
elif angleAndSection[1] == 2:
centerP1X -= xComponent
centerP1Y -= yComponent
elif angleAndSection[1] == 3:
centerP1X -= xComponent
centerP1Y += yComponent
elif angleAndSection[1] == 4:
centerP1X += xComponent
centerP1Y += yComponent
if typePoint == 45:
car.coordenatesLineLeft = [centerP1X, centerP1Y]
car.longitudeLineLeft = hypothenuse
if typePoint == 0:
car.coordenatesLineCenter = [centerP1X, centerP1Y]
car.longitudeLineCenter = hypothenuse
if typePoint == -45:
car.coordenatesLineRight = [centerP1X, centerP1Y]
car.longitudeLineRight = hypothenuse
class Map:
def __init__(self):
self.generation = 1
self.numberOfCar = 1
@staticmethod
def sortWeights():
global weightsOfCars
# print(weightsOfCars)
weightsOfCars.sort(key=lambda x: x[2], reverse=True)
cutOff = int(len(weightsOfCars) * MUTATION_CUT_OFF)
goodCars = weightsOfCars[0: cutOff]
badCars = weightsOfCars[cutOff:]
numBadToTake = int(len(weightsOfCars) * MUTATION_BAD_TO_KEEP)
counterBadCars = 0
for car in badCars:
newCar = NNet.modifyWeights(car)
badCars[counterBadCars] = newCar
counterBadCars += 1
newCarsFinalArray = []
idxBadToTake = numpy.random.choice(numpy.arange(len(badCars)), numBadToTake, replace=False)
for index in idxBadToTake:
newCarsFinalArray.append(badCars[index])
newCarsFinalArray.extend(goodCars)
while len(newCarsFinalArray) < len(weightsOfCars):
idxGoodToTake = numpy.random.choice(numpy.arange(len(goodCars)), 2, replace=False)
if idxGoodToTake[0] != idxGoodToTake[1]:
newSuperCar = NNet.create_mixed_weights(
[],
goodCars[idxGoodToTake[0]],
goodCars[idxGoodToTake[1]])
if random.random() < MUTATION_MODIFY_CHANCE_LIMIT:
newSuperCar = NNet.modifyWeights(newSuperCar)
newCarsFinalArray.append(newSuperCar)
weightsOfCars = newCarsFinalArray
"""print("")
print("")
print("")
print("")
print("")
print(weightsOfCars)"""
def createMap(self, window, carsRect, cars):
numberOfRow = 0
for car in cars:
car.lineLeftRect = pygame.draw.line(window, car.linesColor, (car.posX, car.posY),
(car.coordenatesLineLeft), car.linesThickness)
car.lineCenterRect = pygame.draw.line(window, car.linesColor, (car.posX, car.posY),
(car.coordenatesLineCenter), car.linesThickness)
car.lineRightRect = pygame.draw.line(window, car.linesColor, (car.posX, car.posY),
(car.coordenatesLineRight), car.linesThickness)
dead = False
for row in hexagonsPositions:
numberOfColumn = 0
for column in row:
counter = 0
for car in cars:
isDead = self.draw(window, column, numberOfColumn, numberOfRow, carsRect[counter], car,
car.lineLeftRect,
car.lineCenterRect,
car.lineRightRect)
if isDead:
if len(weightsOfCars) == GENERATION_SIZE:
weightsOfCars[self.numberOfCar - 1] = [
[car.nnet.weightsInputHidden],
[car.nnet.weightsHiddenOutput],
[car.fitness]
]
else:
weightsOfCars.append([
[car.nnet.weightsInputHidden],
[car.nnet.weightsHiddenOutput],
[car.fitness]
])
if self.numberOfCar == GENERATION_SIZE:
self.generation += 1
self.numberOfCar = 1
Map.sortWeights()
else:
self.numberOfCar += 1
car.reset()
car.initLines()
dead = True
break
counter += 1
if dead: break
if dead: break
numberOfColumn += 1
if dead: break
numberOfRow += 1
def checkCollision(self, hexagonRect, carRect):
if hexagonRect.colliderect(carRect):
return True
else:
return False
def draw(self, window, column, numberOfColumn, numberOfRow, carRect, car,
lineLeft,
lineCenter,
lineRight
):
leftGap = 300
upperGap = 22.5
if column == 1:
x = HEXAGON_WIDTH * numberOfColumn + leftGap
if numberOfColumn % 2 == 0:
y = HEXAGON_HEIGHT * numberOfRow + upperGap
else:
y = HEXAGON_HEIGHT * numberOfRow + upperGap
hexagonRect = window.blit(hexagonImageResized, (x, y))
isDead = self.checkCollision(hexagonRect, carRect)
isContactLineLeft = self.checkCollision(hexagonRect, lineLeft)
isContactLineCenter = self.checkCollision(hexagonRect, lineCenter)
isContactLineRight = self.checkCollision(hexagonRect, lineRight)
if isContactLineLeft:
car.isGrowingLineLeft = False
if isContactLineCenter:
car.isGrowingLineCenter = False
if isContactLineRight:
car.isGrowingLineRight = False
getSecondPointForReferenceCar(car, 45)
getSecondPointForReferenceCar(car, 0)
getSecondPointForReferenceCar(car, -45)
return isDead