Passed
Push — master ( 19dcde...f6c7e6 )
by Simon
04:25
created

gradient_free_optimizers.optimizers.population.base_population_optimizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A BasePopulationOptimizer.__init__() 0 12 1
A BasePopulationOptimizer._iterations() 0 6 2
A BasePopulationOptimizer.finish_initialization() 0 2 1
A BasePopulationOptimizer._create_population() 0 18 4
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
from ...optimizers.base_optimizer import get_n_inits
8
9
10
class BasePopulationOptimizer:
11
    def __init__(
12
        self,
13
        search_space,
14
        initialize={"grid": 4, "random": 2, "vertices": 4},
15
    ):
16
        super().__init__()
17
        self.conv = Converter(search_space)
18
        self.results_mang = ResultsManager(self.conv)
19
        self.initialize = initialize
20
21
        self.eval_times = []
22
        self.iter_times = []
23
24
    def _iterations(self, positioners):
25
        nth_iter = 0
26
        for p in positioners:
27
            nth_iter = nth_iter + len(p.pos_new_list)
28
29
        return nth_iter
30
31
    def _create_population(self, Optimizer):
32
        if isinstance(self.population, int):
33
            population = []
34
            for pop_ in range(self.population):
35
                population.append(
36
                    Optimizer(self.conv.search_space, rand_rest_p=self.rand_rest_p)
37
                )
38
        else:
39
            population = self.population
40
41
        n_inits = get_n_inits(self.initialize)
42
43
        if n_inits < len(population):
44
            print("\n Warning: Not enough initial positions for population size")
45
            print(" Population size is reduced to", n_inits)
46
            population = population[:n_inits]
47
48
        return population
49
50
    def finish_initialization(self):
51
        pass
52