-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolve_ann.py
81 lines (61 loc) · 2.63 KB
/
evolve_ann.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
"""
A chromosome will have the following structure. Say there are 2 inputs, 3 hidden and 1 output.
Structure of chromosome weights is then:
(i1 -> h1), (i1 -> h2), (i1 -> h3), (i2 -> h1), ..., (i3 -> h3),
(h1 -> o1), (h2 -> o1), (h3 -> o1)
"""
from generation import Generation
from ann import GenFFANN
def evolve_generations_count(simulation_func, max_num_generations, pop_size=60, num_fittest=5, num_random=10, num_elites=3):
# Create start generation
# While not done
# Simulate generation
# Check if should be done
# Spawn next generation
Generator = GenFFANN
num_params = (GenFFANN.INPUTSIZE * GenFFANN.HIDDENSIZE) + (GenFFANN.HIDDENSIZE * GenFFANN.OUTPUTSIZE)
print num_params
current_gen = Generation(Generator, simulation_func, pop_size, num_fittest, num_random, num_elites, num_params)
current_gen.spawn_random_generation()
best_fitness = -100000
gen_num = 0
while True:
gen_num += 1
print "Running generation {0}".format(gen_num)
fitness = current_gen.run()
print "Fitness is {0} compared to {1}".format(fitness, best_fitness)
if fitness > best_fitness:
best_generator = current_gen.fittest[0]
best_fitness = best_generator.fitness
if gen_num >= max_num_generations:
return best_generator
current_gen = current_gen.spawn_next_generation()
def evolve_generations_plateau(simulation_func, negligible, max_bad_gen_count, pop_size=60, num_fittest=5, num_random=10, num_elites=3):
# Create start generation
# While not done
# Simulate generation
# Check if should be done
# Spawn next generation
Generator = GenFFANN
num_params = (GenFFANN.INPUTSIZE * GenFFANN.HIDDENSIZE) + (GenFFANN.HIDDENSIZE * GenFFANN.OUTPUTSIZE)
print num_params
current_gen = Generation(Generator, simulation_func, pop_size, num_fittest, num_random, num_elites, num_params)
current_gen.spawn_random_generation()
best_fitness = -100000
bad_gen_count = 0
gen_num = 0
while True:
gen_num += 1
print "Running generation {0}".format(gen_num)
fitness = current_gen.run()
print "Fitness is {0} compared to {1}".format(fitness, best_fitness)
if fitness - best_fitness <= negligible:
bad_gen_count += 1
print "Bad Generation #{0}".format(bad_gen_count)
else:
bad_gen_count = 0
best_generator = current_gen.fittest[0]
best_fitness = best_generator.fitness
if bad_gen_count >= max_bad_gen_count:
return best_generator
current_gen = current_gen.spawn_next_generation()