Passed
Push — master ( accb4c...87b46e )
by Simon
01:44 queued 13s
created

split()   A

Complexity

Conditions 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nop 2
dl 0
loc 14
rs 9.85
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import math
6
import numpy as np
7
8
from ..core_optimizer import CoreOptimizer
9
10
11
def split(positions_l, population):
12
    div_int = math.ceil(len(positions_l) / population)
13
    dist_init_positions = []
14
15
    for nth_indiv in range(population):
16
        indiv_pos = []
17
        for nth_indiv_pos in range(div_int):
18
            idx = nth_indiv + nth_indiv_pos * population
19
            if idx < len(positions_l):
20
                indiv_pos.append(positions_l[idx])
21
22
        dist_init_positions.append(indiv_pos)
23
24
    return dist_init_positions
25
26
27
class BasePopulationOptimizer(CoreOptimizer):
28
    def __init__(self, *args, **kwargs):
29
        super().__init__(*args, **kwargs)
30
31
        self.eval_times = []
32
        self.iter_times = []
33
34
        self.init_done = False
35
36
    def _iterations(self, positioners):
37
        nth_iter = 0
38
        for p in positioners:
39
            nth_iter = nth_iter + len(p.pos_new_list)
40
41
        return nth_iter
42
43
    def sort_pop_best_score(self):
44
        scores_list = []
45
        for _p_ in self.optimizers:
46
            scores_list.append(_p_.score_current)
47
48
        scores_np = np.array(scores_list)
49
        idx_sorted_ind = list(scores_np.argsort()[::-1])
50
51
        self.pop_sorted = [self.optimizers[i] for i in idx_sorted_ind]
52
53
    def _create_population(self, Optimizer):
54
        if isinstance(self.population, int):
55
            pop_size = self.population
56
        else:
57
            pop_size = len(self.population)
58
        diff_init = pop_size - self.init.n_inits
59
60
        if diff_init > 0:
61
            self.init.add_n_random_init_pos(diff_init)
62
63
        if isinstance(self.population, int):
64
            distributed_init_positions = split(
65
                self.init.init_positions_l, self.population
66
            )
67
68
            population = []
69
            for init_positions in distributed_init_positions:
70
                init_values = self.conv.positions2values(init_positions)
71
                init_paras = self.conv.values2paras(init_values)
72
73
                population.append(
74
                    Optimizer(
75
                        self.conv.search_space,
76
                        rand_rest_p=self.rand_rest_p,
77
                        initialize={"warm_start": init_paras},
78
                    )
79
                )
80
        else:
81
            population = self.population
82
83
        return population
84
85
    @CoreOptimizer.track_new_score
86
    def evaluate_init(self, score_new):
87
        self.p_current.evaluate_init(score_new)
88
89
    def finish_initialization(self):
90
        self.search_state = "iter"
91