-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevo_mass.py
88 lines (67 loc) · 2.67 KB
/
evo_mass.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
from __future__ import print_function
from re import L
import sys
import os.path
import os
import neat
from neat.graphs import feed_forward_layers
from neat.six_util import itervalues
from manimlib import *
sys.path.insert(0, '')
from evolution_anim import *
from game_mass import *
DISPLAY_ONCE_X_GENERATION = 3
nr_tries = 5
class NetworkScene(Scene):
def construct(self):
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'config-feedforward')
self.generation = 0
self.prev_net = None
self.runnn(config_path)
def eval_genomes(self, genomes, config):
max_genome = None
max_net = None
nets = []
for genome_id, genome in genomes:
genome.fitness = 0
net = neat.nn.FeedForwardNetwork.create(genome, config)
self.export_structure(net, genome, config)
nets.append(net)
snakes = GameMass(self, nets, self.generation)
snakes.play_all()
scores = snakes.get_scores()
i=0
for genome_id, genome in genomes:
genome.fitness = scores[i]
i+=1
self.generation+=1
def eval_genome(self, genome, config):
net = neat.nn.FeedForwardNetwork.create(genome, config)
fitness = 0
for i in range(0,nr_tries):
game = Game(self, net, self.generation, False)
fitness += game.play()
return fitness / nr_tries
def runnn(self,config_file):
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_file)
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
p.add_reporter(neat.Checkpointer(50, filename_prefix='checkpoints\\neat-checkpoint-'))
winner = p.run(self.eval_genomes, 3000)
def export_structure(self, net, genome, config):
connections = [cg.key for cg in itervalues(genome.connections) if cg.enabled]
net.layers = [config.genome_config.input_keys] + [sorted(list(x)) for x in feed_forward_layers(config.genome_config.input_keys, config.genome_config.output_keys, connections) ]
for l in range(0,len(net.layers)-1):
to_remove_nodes = []
for nod in net.layers[l]:
if nod in net.output_nodes:
to_remove_nodes.append(nod)
for nod in to_remove_nodes:
net.layers[l].remove(nod)
net.layers[-1].append(nod)
sorted(net.layers[-1])