1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import random |
6
|
|
|
import numpy as np |
7
|
|
|
from .search_tracker import SearchTracker |
8
|
|
|
from ..converter import Converter |
9
|
|
|
from ..results_manager import ResultsManager |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def get_n_inits(initialize): |
13
|
|
|
n_inits = 0 |
14
|
|
|
for key_ in initialize.keys(): |
15
|
|
|
init_value = initialize[key_] |
16
|
|
|
if isinstance(init_value, int): |
17
|
|
|
n_inits += init_value |
18
|
|
|
else: |
19
|
|
|
n_inits += len(init_value) |
20
|
|
|
return n_inits |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class BaseOptimizer(SearchTracker): |
24
|
|
|
def __init__( |
25
|
|
|
self, |
26
|
|
|
search_space, |
27
|
|
|
initialize={"grid": 4, "random": 2, "vertices": 4}, |
28
|
|
|
): |
29
|
|
|
super().__init__() |
30
|
|
|
self.conv = Converter(search_space) |
31
|
|
|
self.results_mang = ResultsManager(self.conv) |
32
|
|
|
self.initialize = initialize |
33
|
|
|
|
34
|
|
|
self.optimizers = [self] |
35
|
|
|
|
36
|
|
|
self.n_inits = get_n_inits(initialize) |
37
|
|
|
|
38
|
|
|
def move_random(self): |
39
|
|
|
position = [] |
40
|
|
|
for search_space_pos in self.conv.search_space_positions: |
41
|
|
|
pos_ = random.choice(search_space_pos) |
42
|
|
|
position.append(pos_) |
43
|
|
|
|
44
|
|
|
return np.array(position) |
45
|
|
|
|
46
|
|
|
def track_nth_iter(func): |
47
|
|
|
def wrapper(self, *args, **kwargs): |
48
|
|
|
self.nth_iter = len(self.pos_new_list) |
49
|
|
|
pos = func(self, *args, **kwargs) |
50
|
|
|
self.pos_new = pos |
51
|
|
|
return pos |
52
|
|
|
|
53
|
|
|
return wrapper |
54
|
|
|
|
55
|
|
|
def random_restart(func): |
56
|
|
|
def wrapper(self, *args, **kwargs): |
57
|
|
|
if self.rand_rest_p > random.uniform(0, 1): |
58
|
|
|
return self.move_random() |
59
|
|
|
else: |
60
|
|
|
return func(self, *args, **kwargs) |
61
|
|
|
|
62
|
|
|
return wrapper |
63
|
|
|
|
64
|
|
|
def conv2pos(self, pos): |
65
|
|
|
# position to int |
66
|
|
|
r_pos = np.rint(pos) |
67
|
|
|
|
68
|
|
|
n_zeros = [0] * len(self.conv.max_positions) |
69
|
|
|
# clip into search space boundaries |
70
|
|
|
pos = np.clip(r_pos, n_zeros, self.conv.max_positions).astype(int) |
71
|
|
|
|
72
|
|
|
return pos |
73
|
|
|
|
74
|
|
|
def init_pos(self, pos): |
75
|
|
|
self.pos_new = pos |
76
|
|
|
self.nth_iter = len(self.pos_new_list) |
77
|
|
|
|
78
|
|
|
def finish_initialization(self): |
79
|
|
|
pass |
80
|
|
|
|
81
|
|
|
def evaluate(self, score_new): |
82
|
|
|
self.score_new = score_new |
83
|
|
|
|
84
|
|
|
if self.pos_best is None: |
85
|
|
|
self.pos_best = self.pos_new |
86
|
|
|
self.pos_current = self.pos_new |
87
|
|
|
|
88
|
|
|
self.score_best = score_new |
89
|
|
|
self.score_current = score_new |
90
|
|
|
|
91
|
|
|
# self._evaluate_new2current(score_new) |
92
|
|
|
# self._evaluate_current2best() |
93
|
|
|
|