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