Passed
Push — master ( 723823...a4cb74 )
by Simon
04:20
created

BasePopulationOptimizer._create_population()   B

Complexity

Conditions 6

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nop 2
dl 0
loc 24
rs 8.5666
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
from ...converter import Converter
6
from ...results_manager import ResultsManager
7
8
9
class BasePopulationOptimizer:
10
    def __init__(
11
        self,
12
        search_space,
13
        initialize={"grid": 4, "random": 2, "vertices": 4},
14
    ):
15
        super().__init__()
16
        self.conv = Converter(search_space)
17
        self.results_mang = ResultsManager(self.conv)
18
        self.initialize = initialize
19
20
        self.eval_times = []
21
        self.iter_times = []
22
23
    def _iterations(self, positioners):
24
        nth_iter = 0
25
        for p in positioners:
26
            nth_iter = nth_iter + len(p.pos_new_list)
27
28
        return nth_iter
29
30
    def _create_population(self, Optimizer):
31
        if isinstance(self.population, int):
32
            population = []
33
            for pop_ in range(self.population):
34
                population.append(
35
                    Optimizer(self.conv.search_space, rand_rest_p=self.rand_rest_p)
36
                )
37
        else:
38
            population = self.population
39
40
        n_inits = 0
41
        for key_ in self.initialize.keys():
42
            init_value = self.initialize[key_]
43
            if isinstance(init_value, int):
44
                n_inits += init_value
45
            else:
46
                n_inits += len(init_value)
47
48
        if n_inits < len(population):
49
            print("\n Warning: Not enough initial positions for population size")
50
            print(" Population size is reduced to", n_inits)
51
            population = population[:n_inits]
52
53
        return population
54
55
    def finish_initialization(self):
56
        pass
57