source: framspy/evolalg/statistics/statistics_deap.py @ 1163

Last change on this file since 1163 was 1146, checked in by Maciej Komosinski, 3 years ago

Cosmetic

File size: 907 bytes
Line 
1from deap import tools
2
3from evolalg.statistics.statistics import Statistics
4
5
6class 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.