-
Notifications
You must be signed in to change notification settings - Fork 0
/
anothergamebackup.py
489 lines (392 loc) · 10.9 KB
/
anothergamebackup.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
import turtle
import time
import math
#import keyboard
import random
import numpy as np
import copy
class Connection:
def __init__(self, connectedNeuron):
self.connectedNeuron = connectedNeuron
self.weight = np.random.normal()
self.dWeight = 0.0
class Neuron:
eta = 0.001
alpha = 0.01
def __init__(self, layer):
self.dendrons = []
self.error = 0.0
self.gradient = 0.0
self.output = 0.0
if layer is None:
pass
else:
for neuron in layer:
con = Connection(neuron)
self.dendrons.append(con)
def addError(self, err):
self.error = self.error + err
def sigmoid(self, x):
return 1 / (1 + math.exp(-x * 1.0))
def dSigmoid(self, x):
return x * (1.0 - x)
def setError(self, err):
self.error = err
def setOutput(self, output):
self.output = output
def getOutput(self):
return self.output
def feedForword(self):
sumOutput = 0
if len(self.dendrons) == 0:
return
for dendron in self.dendrons:
sumOutput = sumOutput + dendron.connectedNeuron.getOutput() * dendron.weight
self.output = self.sigmoid(sumOutput)
def backPropagate(self):
self.gradient = self.error * self.dSigmoid(self.output);
for dendron in self.dendrons:
dendron.dWeight = Neuron.eta * (
self.gradient * dendron.connectedNeuron.output) + self.alpha * dendron.dWeight;
dendron.weight = dendron.weight + dendron.dWeight;
dendron.connectedNeuron.addError(dendron.weight * self.gradient);
self.error = 0;
def mutate(self):
#if len(self.dendrons) == 0:
# return
for dendron in self.dendrons:
randomnumber = random.randint(0,2)
if randomnumber == 1:
anotherrandomnumber = random.randint(0, 2)
if anotherrandomnumber == 0:
kerroin = 1
else:
kerroin = -1
dendron.weight = dendron.weight +(kerroin*(100/random.randint(1,200)))
class Network:
def __init__(self, topology):
self.layers = []
for numNeuron in topology:
layer = []
for i in range(numNeuron):
if (len(self.layers) == 0):
layer.append(Neuron(None))
else:
layer.append(Neuron(self.layers[-1]))
layer.append(Neuron(None))
layer[-1].setOutput(1)
self.layers.append(layer)
def setInput(self, inputs):
for i in range(len(inputs)):
self.layers[0][i].setOutput(inputs[i])
def feedForword(self):
for layer in self.layers[1:]:
for neuron in layer:
neuron.feedForword();
def backPropagate(self, target):
for i in range(len(target)):
self.layers[-1][i].setError(self.layers[-1][i].getOutput()- target[i])
for layer in self.layers[::-1]:
for neuron in layer:
neuron.backPropagate()
def getError(self, target):
err = 0
for i in range(len(target)):
e = (target[i] - self.layers[-1][i].getOutput())
err = err + e ** 2
err = err / len(target)
err = math.sqrt(err)
return err
def getResults(self):
output = []
for neuron in self.layers[-1]:
output.append(neuron.getOutput())
output.pop()
return output
def getThResults(self):
output = []
for neuron in self.layers[-1]:
o = neuron.getOutput()
print(o)
if (o > 0.5):
o = 1
else:
o = 0
output.append(o)
output.pop()
return output
def mutatenetwork(self):
for layer in self.layers[1:]:
for neuron in layer:
neuron.mutate();
class Car:
def __init__(self, angle, x, y, xwidth, ywidth):
self.nn = Network([5,4,4])
self.magnitudeofvelocity = 0
self.x = x
self.y = y
self.fitness = 0
self.angle = angle
self.terminalstate = False
self.xwidth = xwidth
self.ywidth = ywidth
self.deltax = self.magnitudeofvelocity*math.cos(self.angle/57.2958)
self.deltay = self.magnitudeofvelocity*math.sin(self.angle/57.2958)
self.deltax = round(deltax, 2)
self.deltay = round(deltay, 2)
turtle.penup()
turtle.goto(self.x, self.y)
turtle.pendown()
turtle.goto(0,0)
def drawcar(self, angle, x, y, xwidth, ywidth):
"""
paskat = turtle.pos()
turtle.speed(0)
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
turtle.speed(0)
turtle.setheading(angle)
turtle.forward(xwidth)
turtle.left(90)
turtle.forward(ywidth)
turtle.left(90)
turtle.forward(xwidth)
turtle.left(90)
turtle.forward(ywidth)
turtle.left(90)
turtle.penup()
turtle.goto(paskat)
"""
turtle.speed(0)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.dot()
turtle.penup()
def checkcollision(self):
if self.x > -240:
if self.y < 200:
return True
if self.x < -400 and self.y > 340 and self.x > -500:
return True
if self.x > 400:
return True
if self.y > 400:
return True
if self.y < -500:
return True
return False
"""
was before this
def checkcollision(self):
if self.x > -240:
if self.x < 260:
if self.y > -100:
if self.y < 200:
return True
if self.x < -400:
return True
if self.x > 400:
return True
if self.y > 400:
return True
if self.y < -400:
return True
return False
"""
def checkcollisiongeneric(ax,ay):
if ax > -240:
if ay < 200:
return True
if ax < -400 and ay > 340 and ax > -500:
return True
if ax > 400:
return True
if ay > 400:
return True
if ay < -400:
return True
if ax < -500:
return True
return False
"""
Was before this:
def checkcollisiongeneric(ax,ay):
if ax > -240:
if ax < 260:
if ay > -100:
if ay < 200:
return True
if ax < -400:
return True
if ax > 400:
return True
if ay > 400:
return True
if ay < -400:
return True
return False
"""
def drawmap():
paska = turtle.Turtle()
paska.speed(0)
paska.color("grey","grey")
paska.penup()
paska.setposition(-240,-100)
paska.begin_fill()
paska.pendown()
paska.forward(500)
paska.left(90)
paska.forward(300)
paska.left(90)
paska.forward(500)
paska.left(90)
paska.forward(300)
def drawbestcar():
try:
bescar.drawcar(bestcar.angle, bestcar.x, bestcar.y, 30, 10)
except:
pass
turtle.screensize(960, 960)
drawmap()
magnitudeofvelocity = 1
deltax = 1
deltay = 0
angle = 0
turtle.tracer(0,0)
drawmap()
brakingconstant = 1
cars = []
terminalstate = False
bestcar = None
numofcars = 600
emphasisontime = 0
#for i in range(numofcars):
# cars.append(Car(0, random.randint(0, 100), random.randint(0,1000), 30, 10))
while True:
cars = []
results = []
terminalstate = False
bestfitness = 0
if bestcar != None:
bestcar.x = 300
bestcar.y = 300
bestcar.deltax = 0
bestcar.deltay = 0
bestcar.magnitudeofvelocity = 0
thingycar = Car(0, 300, 300, 30, 10)
if bestcar:
thingycar.nn = bestcar.nn
for i in range(numofcars):
cars.append(copy.deepcopy(thingycar))
if bestcar == None:
cars = []
for i in range(numofcars):
cars.append(Car(0, 300, 300, 30, 10))
for car in cars:
car.nn.mutatenetwork()
car.angle = random.randint(-110, -70)
for k in range(len(cars)):
for i in range(len(cars)-1):
if cars[i].nn == cars[i+1].nn:
print("fuck this")
for juttu in range(1000):
turtle.clear()
for car in cars:
"""
turtle.penup()
turtle.goto(car.x, car.y)
turtle.pendown()
turtle.goto(0, 0)
"""
if car.magnitudeofvelocity < 0.1 and juttu > 50:
cars.remove(car)
continue
car.deltax = car.magnitudeofvelocity*math.cos(car.angle/57.2958)
car.deltay = car.magnitudeofvelocity*math.sin(car.angle/57.2958)
car.deltax = round(car.deltax, 6)
car.deltay = round(car.deltay, 6)
if (math.sqrt(((-300-car.x)**2)+((-300-car.y)**2))) < 200:
bestcar = car
bestfitness = 1/(math.sqrt(((-300-car.x)**2)+((-300-car.y)**2))) + car.fitness*(1/(juttu+1))*emphasisontime
cars.remove(car)
continue
if car.checkcollision():
car.fitness = 1/(math.sqrt(((-300-car.x)**2)+((-300-car.y)**2)))
car.fitness = car.fitness + car.fitness*(1/(juttu+1))*emphasisontime
car.deltax = 0
car.deltay = 0
car.terminalstate = True
if car.fitness > bestfitness:
bestcar = copy.deepcopy(car)
bestfitness = bestcar.fitness
cars.remove(car)
continue
car.x = car.x + car.deltax
car.y = car.y + car.deltay
car.drawcar(car.angle, car.x, car.y, 30, 10)
"""
if keyboard.is_pressed("w"):
print("you pressed forward")
car.magnitudeofvelocity = car.magnitudeofvelocity + 0.02
print(car.magnitudeofvelocity)
if keyboard.is_pressed("a"):
car.angle = car.angle + magnitudeofvelocity*brakingconstant
if keyboard.is_pressed("d"):
car.angle = car.angle - magnitudeofvelocity*brakingconstant
if keyboard.is_pressed("s"):
car.magnitudeofvelocity = car.magnitudeofvelocity - 0.01
"""
car.magnitudeofvelocity = car.magnitudeofvelocity - 0.001
if car.magnitudeofvelocity < 0:
car.magnitudeofvelocity = 0
inputti = [0,0,0,0,0]
anglejuttu = -20+car.angle
for i in range(5):
for kakka in range(100):
if checkcollisiongeneric((car.x + kakka*math.cos(anglejuttu/57.2958)), (car.y + kakka*math.sin(anglejuttu/57.2958))):
inputti[i] = 1
"""
turtle.setheading(anglejuttu)
turtle.penup()
turtle.goto((car.x + 100*math.cos(anglejuttu/57.2958)), (car.y + 100*math.sin(anglejuttu/57.2958)))
turtle.pendown()
turtle.forward(100)
turtle.penup()
"""
anglejuttu = anglejuttu + 10
drawbestcar()
car.nn.setInput(inputti)
car.nn.feedForword()
results = car.nn.getResults()
if results[0] > 0.5:
car.magnitudeofvelocity = car.magnitudeofvelocity + 0.01
if results[1] > 0.5:
car.magnitudeofvelocity = car.magnitudeofvelocity - 0.01
if results[2] > 0.5:
car.angle = car.angle + magnitudeofvelocity*brakingconstant
if results[3] > 0.5:
car.angle = car.angle - magnitudeofvelocity*brakingconstant
paskaa = True
for i in range(len(cars)):
if cars[i].terminalstate == True:
paskaa = True
else:
paskaa = False
break
drawbestcar()
turtle.update()
for car in cars:
car.fitness = 1/(math.sqrt(((-300-car.x)**2)+((-300-car.y)**2)))
car.fitness = car.fitness + car.fitness*(1/(juttu+1))*emphasisontime
car.deltax = 0
car.deltay = 0
car.terminalstate = True
if car.fitness > bestfitness:
bestcar = copy.deepcopy(car)
bestfitness = bestcar.fitness
cars.remove(car)
#for i in range():
#for i in range(6)
#print(bestcar.nn.layers[1][0].dendrons[i].weight)