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

Last change on this file since 1113 was 1113, checked in by Maciej Komosinski, 19 months ago

Added a framework for evolutionary algorithms cooperating with FramsticksLib?.py

File size: 755 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):
8        self.stats = tools.Statistics(extract_fn)
9        for name, fn in stats:
10            self.stats.register(name, fn)
11
12        self.logbook = tools.Logbook()
13        self.logbook.header = ['gen'] + (self.stats.fields if self.stats else [])
14
15        self.gen = 0
16        self.verbose = verbose
17
18    def init(self):
19        self.gen = 0
20
21    def collect(self, population):
22        record = self.stats.compile(population)
23        self.logbook.record(gen=self.gen, **record)
24        self.gen += 1
25        if self.verbose:
26            print(self.logbook.stream)
Note: See TracBrowser for help on using the repository browser.