-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogReporting.py
102 lines (82 loc) · 4.4 KB
/
logReporting.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
from __future__ import division, print_function
from neat.reporting import BaseReporter
import time
import datetime
from neat.math_util import mean, stdev
from neat.six_util import itervalues, iterkeys
class LoggingReporter(BaseReporter):
def __init__(self, filename, show_species_detail):
self.show_species_detail = show_species_detail
self.generation = None
self.generation_start_time = None
self.generation_times = []
self.num_extinctions = 0
self.filename = filename;
def start_generation(self, generation):
self.generation = generation
self.output('\n ****** Running generation {0} ****** \n'.format(generation))
self.generation_start_time = time.time()
def checkpoint_restored(self,generation):
self.generation = generation;
def post_speciation(self, config, population, species_set):
if self.show_species_detail:
ng = len(population)
ns = len(species_set.species)
outputString = 'Population of {0:d} members in {1:d} species:'.format(ng, ns) + '\n';
sids = list(iterkeys(species_set.species))
sids.sort()
outputString += (" ID age size fitness adj fit stag") + '\n';
outputString += (" ==== === ==== ======= ======= ====") + '\n';
for sid in sids:
s = species_set.species[sid]
a = self.generation - s.created
n = len(s.members)
f = "--" if s.fitness is None else "{:.1f}".format(s.fitness)
af = "--" if s.adjusted_fitness is None else "{:.3f}".format(s.adjusted_fitness)
st = self.generation - s.last_improved
outputString += (" {: >4} {: >3} {: >4} {: >7} {: >7} {: >4}".format(sid, a, n, f, af, st)) + '\n';
self.output(outputString);
def end_generation(self, config, population, species_set):
ng = len(population)
ns = len(species_set.species)
outputString = '';
outputString += ('Population of {0:d} members in {1:d} species'.format(ng, ns)) + '\n';
elapsed = time.time() - self.generation_start_time;
self.generation_times.append(elapsed)
self.generation_times = self.generation_times[-10:]
average = sum(self.generation_times) / len(self.generation_times)
outputString += ('Total extinctions: {0:d}'.format(self.num_extinctions)) + '\n';
if len(self.generation_times) > 1:
outputString += ("Generation time: {0:.3f} sec ({1:.3f} average)".format(elapsed, average)) + '\n';
else:
outputString += ("Generation time: {0:.3f} sec".format(elapsed)) + '\n';
outputString += "Generation Commpletion time: {0}\n".format(datetime.datetime.now())
self.output(outputString);
def post_evaluate(self, config, population, species, best_genome):
# pylint: disable=no-self-use
fitnesses = [c.fitness for c in itervalues(population)]
fit_mean = mean(fitnesses)
fit_std = stdev(fitnesses)
best_species_id = species.get_species_id(best_genome.key)
outputString = '';
outputString += ('Population\'s average fitness: {0:3.5f} stdev: {1:3.5f}'.format(fit_mean, fit_std)) + '\n'
outputString += ('Best fitness: {0:3.5f} - size: {1!r} - species {2} - id {3}'.format(best_genome.fitness,
best_genome.size(),
best_species_id,
best_genome.key)) + '\n';
self.output(outputString);
def complete_extinction(self):
self.num_extinctions += 1
self.output('All species extinct.')
def found_solution(self, config, generation, best):
self.output('\nBest individual in generation {0} meets fitness threshold - complexity: {1!r}'.format(
self.generation, best.size()))
def species_stagnant(self, sid, species):
if self.show_species_detail:
self.output("\nSpecies {0} with {1} members is stagnated: removing it".format(sid, len(species.members)))
def info(self, msg):
self.output(msg)
def output(self,txt):
f = open(self.filename,'a');
f.write('\n' + txt);
f.close();