1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
import numpy as np |
7
|
|
|
|
8
|
|
|
from ..core_optimizer import CoreOptimizer |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class BasePopulationOptimizer(CoreOptimizer): |
12
|
|
|
def __init__(self, *args, **kwargs): |
13
|
|
|
super().__init__(*args, **kwargs) |
14
|
|
|
|
15
|
|
|
self.eval_times = [] |
16
|
|
|
self.iter_times = [] |
17
|
|
|
|
18
|
|
|
self.init_done = False |
19
|
|
|
|
20
|
|
|
def _iterations(self, positioners): |
21
|
|
|
nth_iter = 0 |
22
|
|
|
for p in positioners: |
23
|
|
|
nth_iter = nth_iter + len(p.pos_new_list) |
24
|
|
|
|
25
|
|
|
return nth_iter |
26
|
|
|
|
27
|
|
|
def sort_pop_best_score(self): |
28
|
|
|
scores_list = [] |
29
|
|
|
for _p_ in self.optimizers: |
30
|
|
|
scores_list.append(_p_.score_current) |
31
|
|
|
|
32
|
|
|
scores_np = np.array(scores_list) |
33
|
|
|
idx_sorted_ind = list(scores_np.argsort()[::-1]) |
34
|
|
|
|
35
|
|
|
self.pop_sorted = [self.optimizers[i] for i in idx_sorted_ind] |
36
|
|
|
|
37
|
|
|
def _create_population(self, Optimizer): |
38
|
|
|
if isinstance(self.population, int): |
39
|
|
|
pop_size = self.population |
40
|
|
|
else: |
41
|
|
|
pop_size = len(self.population) |
42
|
|
|
diff_init = pop_size - self.init.n_inits |
43
|
|
|
|
44
|
|
|
if diff_init > 0: |
45
|
|
|
self.init.add_n_random_init_pos(diff_init) |
46
|
|
|
|
47
|
|
|
if isinstance(self.population, int): |
48
|
|
|
population = [] |
49
|
|
|
for init_position in self.init.init_positions_l: |
50
|
|
|
init_value = self.conv.position2value(init_position) |
51
|
|
|
init_para = self.conv.value2para(init_value) |
52
|
|
|
|
53
|
|
|
population.append( |
54
|
|
|
Optimizer( |
55
|
|
|
self.conv.search_space, |
56
|
|
|
rand_rest_p=self.rand_rest_p, |
57
|
|
|
initialize={"warm_start": [init_para]}, |
58
|
|
|
) |
59
|
|
|
) |
60
|
|
|
else: |
61
|
|
|
population = self.population |
62
|
|
|
|
63
|
|
|
return population |
64
|
|
|
|
65
|
|
|
@CoreOptimizer.track_new_score |
66
|
|
|
def evaluate_init(self, score_new): |
67
|
|
|
self.p_current.evaluate_init(score_new) |
68
|
|
|
|
69
|
|
|
def finish_initialization(self): |
70
|
|
|
self.search_state = "iter" |
71
|
|
|
|