-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenetics.py
More file actions
30 lines (23 loc) · 802 Bytes
/
Genetics.py
File metadata and controls
30 lines (23 loc) · 802 Bytes
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
from copy import deepcopy # <- Might create some issues
import NeuralNetwork as NN
PERCENTAGE_NEW=0.2
def generate_new_brains(birds, score_last_gen):
chance = 0.5
if score_last_gen > 10:
chance = 0.1
elif score_last_gen > 3:
chance = 0.2
if len(birds) == 0:
return
birds = sorted(birds, key=lambda bird: bird.score)
best_bird = birds[-1]
new_brains = []
nb_new=int(len(birds)*PERCENTAGE_NEW)
nb_from_ancestores=len(birds)-nb_new
for _ in range(nb_new):
new_brains.append(NN.NeuralNetwork(4, 1))
for _ in range(nb_from_ancestores):
new_brain = deepcopy(best_bird.brain) # TODO: Fix performance hit
new_brain.mutate(NN.mutate(chance))
new_brains.append(new_brain)
return new_brains