-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.py
64 lines (52 loc) · 2.39 KB
/
ga.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
import random
from bird import Bird
class GA:
def __init__(self, game):
self.game = game
def nextGeneration(self):
self.calculateFitness()
if (self.game.foundBestBird):
child = Bird(self.game)
child.brain = self.game.bestBirdBrain
self.game.birds.append(child)
else:
self.game.birds.append(self.game.savedBirds[random.randrange(self.game.population)])
for i in range(self.game.population - 1):
self.game.birds.append(self.pickOne())
self.game.savedBirds = []
def calculateFitness(self):
summ = 0
for bird in self.game.savedBirds:
bird.calculateFitness()
summ += bird.fitness
for bird in self.game.savedBirds:
bird.fitness /= summ
def pickOne(self):
r1 = random.uniform(0, 1)
index = 0
while r1 > 0:
r1 -= self.game.savedBirds[index].fitness
index += 1
index -= 1
bird1 = self.game.savedBirds[index]
r2 = random.uniform(0, 1)
index_2 = 0
while r2 > 0:
r2 -= self.game.savedBirds[index_2].fitness
index_2 += 1
index_2 -= 1
bird2 = self.game.savedBirds[index_2]
child = Bird(self.game)
child.brain.in_hidden1_weights = bird1.brain.crossover(bird1.brain.in_hidden1_weights,
bird2.brain.in_hidden1_weights)
child.brain.in_hidden1_biases = bird1.brain.crossover(bird1.brain.in_hidden1_biases,
bird2.brain.in_hidden1_biases)
child.brain.hidden1_output_weights = bird1.brain.crossover(bird1.brain.hidden1_output_weights,
bird2.brain.hidden1_output_weights)
child.brain.hidden1_output_biases = bird1.brain.crossover(bird1.brain.hidden1_output_biases,
bird2.brain.hidden1_output_biases)
child.brain.mutate(child.brain.in_hidden1_weights, 0.3)
child.brain.mutate(child.brain.in_hidden1_biases, 0.3)
child.brain.mutate(child.brain.hidden1_output_weights, 0.3)
child.brain.mutate(child.brain.hidden1_output_biases, 0.3)
return child