source: framspy/evolalg/utils/stable_generation.py @ 1139

Last change on this file since 1139 was 1113, checked in by Maciej Komosinski, 4 years ago

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

File size: 1.1 KB
Line 
1from collections import Iterable
2
3from evolalg.base.step import Step
4import copy
5
6from evolalg.base.union_step import UnionStep
7
8
9class StableGeneration(Step):
10
11    def __init__(self, selection, steps, population_size=None):
12        self.selection = selection
13        self.steps = UnionStep(steps)
14        self.population_size = population_size
15
16    def generate_new(self, population, missing_count):
17        selected = self.selection(population, missing_count)
18        selected = copy.deepcopy(selected)
19        selected = self.steps(selected)
20        return selected
21
22    def init(self):
23        self.selection.init()
24        self.steps.init()
25
26    def call(self, population):
27        population_size = self.population_size
28        if population_size is None:
29            population_size = len(population)
30        generated = []
31        while len(generated) < population_size:
32            gen = self.generate_new(population, population_size - len(generated))
33            if isinstance(gen, Iterable):
34                generated.extend(gen)
35            else:
36                generated.append(gen)
37        return generated[:len(population)]
Note: See TracBrowser for help on using the repository browser.