Last change
on this file since 1185 was
1185,
checked in by Maciej Komosinski, 22 months ago
|
Renamed a module; new name is "evolalg_steps"
|
File size:
913 bytes
|
Line | |
---|
1 | from deap import tools |
---|
2 | |
---|
3 | from evolalg_steps.statistics.statistics import Statistics |
---|
4 | |
---|
5 | |
---|
6 | class StatisticsDeap(Statistics): |
---|
7 | def __init__(self, stats, extract_fn=lambda ind: ind.fitness, verbose=True, *args, **kwargs): |
---|
8 | super(StatisticsDeap, self).__init__(*args, **kwargs) |
---|
9 | self.stats = tools.Statistics(extract_fn) |
---|
10 | for name, fn in stats: |
---|
11 | self.stats.register(name, fn) |
---|
12 | |
---|
13 | self.logbook = tools.Logbook() |
---|
14 | self.logbook.header = ['gen'] + (self.stats.fields if self.stats else []) |
---|
15 | |
---|
16 | self.gen = 0 |
---|
17 | self.verbose = verbose |
---|
18 | |
---|
19 | def init(self): |
---|
20 | self.gen = 0 |
---|
21 | |
---|
22 | def collect(self, population): |
---|
23 | record = self.stats.compile(population) |
---|
24 | self.logbook.record(gen=self.gen, **record) |
---|
25 | self.gen += 1 |
---|
26 | if self.verbose: |
---|
27 | print(self.logbook.stream) |
---|
28 | |
---|
29 | def compile(self, data): |
---|
30 | return self.stats.compile(data) |
---|
31 | |
---|
32 | |
---|
Note: See
TracBrowser
for help on using the repository browser.