|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import time |
|
6
|
|
|
import numpy as np |
|
7
|
|
|
import multiprocessing |
|
8
|
|
|
|
|
9
|
|
|
from .verbosity import set_verbosity |
|
10
|
|
|
|
|
11
|
|
|
from .search_process import SearchProcess |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
from gradient_free_optimizers import ( |
|
15
|
|
|
HillClimbingOptimizer, |
|
16
|
|
|
StochasticHillClimbingOptimizer, |
|
17
|
|
|
TabuOptimizer, |
|
18
|
|
|
RandomSearchOptimizer, |
|
19
|
|
|
RandomRestartHillClimbingOptimizer, |
|
20
|
|
|
RandomAnnealingOptimizer, |
|
21
|
|
|
SimulatedAnnealingOptimizer, |
|
22
|
|
|
StochasticTunnelingOptimizer, |
|
23
|
|
|
ParallelTemperingOptimizer, |
|
24
|
|
|
ParticleSwarmOptimizer, |
|
25
|
|
|
EvolutionStrategyOptimizer, |
|
26
|
|
|
BayesianOptimizer, |
|
27
|
|
|
TreeStructuredParzenEstimators, |
|
28
|
|
|
DecisionTreeOptimizer, |
|
29
|
|
|
) |
|
30
|
|
|
|
|
31
|
|
|
optimizer_dict = { |
|
32
|
|
|
"HillClimbing": HillClimbingOptimizer, |
|
33
|
|
|
"StochasticHillClimbing": StochasticHillClimbingOptimizer, |
|
34
|
|
|
"TabuSearch": TabuOptimizer, |
|
35
|
|
|
"RandomSearch": RandomSearchOptimizer, |
|
36
|
|
|
"RandomRestartHillClimbing": RandomRestartHillClimbingOptimizer, |
|
37
|
|
|
"RandomAnnealing": RandomAnnealingOptimizer, |
|
38
|
|
|
"SimulatedAnnealing": SimulatedAnnealingOptimizer, |
|
39
|
|
|
"StochasticTunneling": StochasticTunnelingOptimizer, |
|
40
|
|
|
"ParallelTempering": ParallelTemperingOptimizer, |
|
41
|
|
|
"ParticleSwarm": ParticleSwarmOptimizer, |
|
42
|
|
|
"EvolutionStrategy": EvolutionStrategyOptimizer, |
|
43
|
|
|
"Bayesian": BayesianOptimizer, |
|
44
|
|
|
"TPE": TreeStructuredParzenEstimators, |
|
45
|
|
|
"DecisionTree": DecisionTreeOptimizer, |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
class Search: |
|
50
|
|
|
def __init__(self, _main_args_): |
|
51
|
|
|
self._main_args_ = _main_args_ |
|
52
|
|
|
|
|
53
|
|
|
self._info_, _pbar_ = set_verbosity(_main_args_.verbosity) |
|
54
|
|
|
self._pbar_ = _pbar_() |
|
55
|
|
|
|
|
56
|
|
|
def search(self, nth_process=0, rayInit=False): |
|
57
|
|
|
self.start_time = time.time() |
|
58
|
|
|
self.results = {} |
|
59
|
|
|
self.eval_times = {} |
|
60
|
|
|
self.iter_times = {} |
|
61
|
|
|
self.best_scores = {} |
|
62
|
|
|
self.pos_list = {} |
|
63
|
|
|
self.score_list = {} |
|
64
|
|
|
|
|
65
|
|
|
if rayInit: |
|
66
|
|
|
self._run_job(nth_process) |
|
67
|
|
|
elif self._main_args_.n_jobs == 1: |
|
68
|
|
|
self._run_job(nth_process) |
|
69
|
|
|
else: |
|
70
|
|
|
self._run_multiple_jobs() |
|
71
|
|
|
|
|
72
|
|
|
return ( |
|
73
|
|
|
self.results, |
|
74
|
|
|
self.pos_list, |
|
75
|
|
|
self.score_list, |
|
76
|
|
|
self.eval_times, |
|
77
|
|
|
self.iter_times, |
|
78
|
|
|
self.best_scores, |
|
79
|
|
|
) |
|
80
|
|
|
|
|
81
|
|
|
def _search_multiprocessing(self): |
|
82
|
|
|
"""Wrapper for the parallel search. Passes integer that corresponds to process number""" |
|
83
|
|
|
pool = multiprocessing.Pool(self._main_args_.n_jobs) |
|
84
|
|
|
self.processlist, _p_list = zip( |
|
85
|
|
|
*pool.map(self._search, self._main_args_._n_process_range) |
|
86
|
|
|
) |
|
87
|
|
|
|
|
88
|
|
|
return self.processlist, _p_list |
|
89
|
|
|
|
|
90
|
|
|
def _run_job(self, nth_process): |
|
91
|
|
|
self.process, _p_ = self._search(nth_process) |
|
92
|
|
|
self._get_attributes(_p_) |
|
93
|
|
|
|
|
94
|
|
|
def _get_attributes(self, _p_): |
|
95
|
|
|
self.results[self.process.func_] = self.process._process_results() |
|
96
|
|
|
self.eval_times[self.process.func_] = self.process.eval_time |
|
97
|
|
|
self.iter_times[self.process.func_] = self.process.iter_times |
|
98
|
|
|
self.best_scores[self.process.func_] = self.process.score_best |
|
99
|
|
|
|
|
100
|
|
|
if isinstance(_p_, list): |
|
101
|
|
|
self.pos_list[self.process.func_] = [np.array(p.pos_list) for p in _p_] |
|
102
|
|
|
self.score_list[self.process.func_] = [np.array(p.score_list) for p in _p_] |
|
103
|
|
|
else: |
|
104
|
|
|
self.pos_list[self.process.func_] = [np.array(_p_.pos_list)] |
|
105
|
|
|
self.score_list[self.process.func_] = [np.array(_p_.score_list)] |
|
106
|
|
|
|
|
107
|
|
|
def _run_multiple_jobs(self): |
|
108
|
|
|
self.processlist, _p_list = self._search_multiprocessing() |
|
109
|
|
|
|
|
110
|
|
|
for _ in range(int(self._main_args_.n_jobs / 2) + 2): |
|
111
|
|
|
print("\n") |
|
112
|
|
|
|
|
113
|
|
|
for self.process, _p_ in zip(self.processlist, _p_list): |
|
|
|
|
|
|
114
|
|
|
self._get_attributes(_p_) |
|
115
|
|
|
|
|
116
|
|
|
def _search(self, nth_process): |
|
117
|
|
|
self._initialize_search(self._main_args_, nth_process, self._info_) |
|
118
|
|
|
|
|
119
|
|
|
n_positions = 10 |
|
120
|
|
|
|
|
121
|
|
|
init_positions = self.process.init_pos(n_positions) |
|
122
|
|
|
|
|
123
|
|
|
self.opt = optimizer_dict[self._main_args_.optimizer]( |
|
124
|
|
|
init_positions, self.process._space_.dim, self._main_args_.opt_para |
|
125
|
|
|
) |
|
126
|
|
|
|
|
127
|
|
|
# loop to initialize N positions |
|
128
|
|
|
for nth_init in range(len(init_positions)): |
|
129
|
|
|
pos_new = self.opt.init_pos(nth_init) |
|
130
|
|
|
score_new = self._get_score(pos_new, 0) |
|
131
|
|
|
self.opt.evaluate(score_new) |
|
132
|
|
|
|
|
133
|
|
|
# loop to do the iterations |
|
134
|
|
|
for nth_iter in range(len(init_positions), self._main_args_.n_iter): |
|
135
|
|
|
pos_new = self.opt.iterate(nth_iter) |
|
136
|
|
|
score_new = self._get_score(pos_new, nth_iter) |
|
137
|
|
|
self.opt.evaluate(score_new) |
|
138
|
|
|
|
|
139
|
|
|
self._pbar_.close_p_bar() |
|
140
|
|
|
|
|
141
|
|
|
return self.process, self.opt.p_list |
|
142
|
|
|
|
|
143
|
|
|
def _get_score(self, pos_new, nth_iter): |
|
144
|
|
|
score_new = self.process.eval_pos(pos_new, self._pbar_, nth_iter) |
|
145
|
|
|
self._pbar_.update_p_bar(1, self.process) |
|
146
|
|
|
|
|
147
|
|
|
if score_new > self.process.score_best: |
|
148
|
|
|
self.process.score = score_new |
|
149
|
|
|
self.process.pos = pos_new |
|
150
|
|
|
|
|
151
|
|
|
return score_new |
|
152
|
|
|
|
|
153
|
|
|
def _time_exceeded(self): |
|
154
|
|
|
run_time = time.time() - self.start_time |
|
155
|
|
|
return self._main_args_.max_time and run_time > self._main_args_.max_time |
|
156
|
|
|
|
|
157
|
|
|
def _initialize_search(self, _main_args_, nth_process, _info_): |
|
158
|
|
|
_main_args_._set_random_seed(nth_process) |
|
159
|
|
|
|
|
160
|
|
|
self.process = SearchProcess(nth_process, _main_args_, _info_) |
|
161
|
|
|
self._pbar_.init_p_bar(nth_process, self._main_args_) |
|
162
|
|
|
|