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 functools import partial |
10
|
|
|
|
11
|
|
|
from .base_positioner import BasePositioner |
12
|
|
|
from .verb import VerbosityLVL0, VerbosityLVL1, VerbosityLVL2 |
13
|
|
|
from .util import init_candidate, init_eval |
14
|
|
|
from .candidate import Candidate |
15
|
|
|
|
16
|
|
|
# from meta_learn import HyperactiveWrapper |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class BaseOptimizer: |
20
|
|
|
def __init__(self, _main_args_, _opt_args_): |
21
|
|
|
self._main_args_ = _main_args_ |
22
|
|
|
self._opt_args_ = _opt_args_ |
23
|
|
|
|
24
|
|
|
self._meta_ = None |
25
|
|
|
""" |
26
|
|
|
self.search_config = self._main_args_.search_config |
27
|
|
|
self.n_iter = self._main_args_.n_iter |
28
|
|
|
|
29
|
|
|
if self._main_args_.memory == "long": |
30
|
|
|
self._meta_ = HyperactiveWrapper(self._main_args_.search_config) |
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
verbs = [VerbosityLVL0, VerbosityLVL1, VerbosityLVL2] |
34
|
|
|
self._verb_ = verbs[_main_args_.verbosity]() |
35
|
|
|
|
36
|
|
|
self.pos_list = [] |
37
|
|
|
self.score_list = [] |
38
|
|
|
|
39
|
|
|
def _init_base_positioner(self, _cand_, positioner=None): |
40
|
|
|
if positioner: |
41
|
|
|
_p_ = positioner(**self._opt_args_.kwargs_opt) |
42
|
|
|
else: |
43
|
|
|
_p_ = BasePositioner(**self._opt_args_.kwargs_opt) |
44
|
|
|
|
45
|
|
|
_p_.pos_current = _cand_.pos_best |
46
|
|
|
_p_.score_current = _cand_.score_best |
47
|
|
|
|
48
|
|
|
return _p_ |
49
|
|
|
|
50
|
|
|
def _update_pos(self, _cand_, _p_): |
51
|
|
|
_cand_.pos_best = _p_.pos_new |
52
|
|
|
_cand_.score_best = _p_.score_new |
53
|
|
|
|
54
|
|
|
_p_.pos_current = _p_.pos_new |
55
|
|
|
_p_.score_current = _p_.score_new |
56
|
|
|
|
57
|
|
|
self._verb_.best_since_iter = _cand_.i |
58
|
|
|
|
59
|
|
|
return _cand_, _p_ |
60
|
|
|
|
61
|
|
|
def _initialize_search(self, _main_args_, nth_process): |
62
|
|
|
_cand_ = init_candidate(_main_args_, nth_process, Candidate) |
63
|
|
|
_cand_ = init_eval(_cand_, nth_process) |
64
|
|
|
_p_ = self._init_opt_positioner(_cand_) |
65
|
|
|
self._verb_.init_p_bar(_cand_, self._main_args_) |
66
|
|
|
|
67
|
|
|
if self._meta_: |
68
|
|
|
meta_data = self._meta_.get_func_metadata(_cand_.func_) |
69
|
|
|
|
70
|
|
|
# self._meta_.retrain(_cand_) |
71
|
|
|
# para, score = self._meta_.search(X, y, _cand_) |
72
|
|
|
_cand_._space_.load_memory(*meta_data) |
73
|
|
|
|
74
|
|
|
return _cand_, _p_ |
75
|
|
|
|
76
|
|
|
def _finish_search(self, _main_args_, _cand_): |
77
|
|
|
_cand_.eval_pos(_cand_.pos_best, force_eval=True) |
78
|
|
|
self.eval_time = _cand_.eval_time_sum |
79
|
|
|
self._verb_.close_p_bar() |
80
|
|
|
|
81
|
|
|
return _cand_ |
82
|
|
|
|
83
|
|
|
def _search(self, nth_process): |
84
|
|
|
_cand_, _p_ = self._initialize_search(self._main_args_, nth_process) |
85
|
|
|
|
86
|
|
|
for i in range(self._main_args_.n_iter): |
87
|
|
|
_cand_.i = i |
88
|
|
|
_cand_ = self._iterate(i, _cand_, _p_) |
89
|
|
|
self._verb_.update_p_bar(1, _cand_) |
90
|
|
|
|
91
|
|
|
run_time = time.time() - self.start_time |
92
|
|
|
if self._main_args_.max_time and run_time > self._main_args_.max_time: |
93
|
|
|
break |
94
|
|
|
|
95
|
|
|
if self._main_args_.get_search_path: |
96
|
|
|
self._monitor_search_path(_p_) |
97
|
|
|
|
98
|
|
|
_cand_ = self._finish_search(self._main_args_, _cand_) |
99
|
|
|
|
100
|
|
|
return _cand_ |
101
|
|
|
|
102
|
|
|
def _monitor_search_path(self, _p_): |
103
|
|
|
pos_list = [] |
104
|
|
|
score_list = [] |
105
|
|
|
if isinstance(_p_, list): |
106
|
|
|
for p in _p_: |
107
|
|
|
pos_list.append(p.pos_new) |
108
|
|
|
score_list.append(p.score_new) |
109
|
|
|
|
110
|
|
|
pos_list_ = np.array(pos_list) |
111
|
|
|
score_list_ = np.array(score_list) |
112
|
|
|
|
113
|
|
|
self.pos_list.append(pos_list_) |
|
|
|
|
114
|
|
|
self.score_list.append(score_list_) |
|
|
|
|
115
|
|
|
else: |
116
|
|
|
pos_list.append(_p_.pos_new) |
117
|
|
|
score_list.append(_p_.score_new) |
118
|
|
|
|
119
|
|
|
pos_list_ = np.array(pos_list) |
120
|
|
|
score_list_ = np.array(score_list) |
121
|
|
|
|
122
|
|
|
self.pos_list.append(pos_list_) |
123
|
|
|
self.score_list.append(score_list_) |
124
|
|
|
|
125
|
|
|
def _process_results(self, _cand_): |
126
|
|
|
start_point = self._verb_.print_start_point(_cand_) |
127
|
|
|
self.results_params[_cand_.func_] = start_point |
128
|
|
|
self.results_models[_cand_.func_] = _cand_.model_best |
129
|
|
|
|
130
|
|
|
""" |
131
|
|
|
if self._main_args_.memory == "long": |
132
|
|
|
self._meta_.collect(X, y, _cand_) |
133
|
|
|
""" |
134
|
|
|
|
135
|
|
|
def _search_multiprocessing(self): |
136
|
|
|
"""Wrapper for the parallel search. Passes integer that corresponds to process number""" |
137
|
|
|
pool = multiprocessing.Pool(self._main_args_.n_jobs) |
138
|
|
|
_search = partial(self._search) |
139
|
|
|
|
140
|
|
|
_cand_list = pool.map(_search, self._main_args_._n_process_range) |
141
|
|
|
|
142
|
|
|
return _cand_list |
143
|
|
|
|
144
|
|
|
def _run_job(self, nth_process): |
145
|
|
|
_cand_ = self._search(nth_process) |
146
|
|
|
self._process_results(_cand_) |
147
|
|
|
|
148
|
|
|
def _run_multiple_jobs(self): |
149
|
|
|
_cand_list = self._search_multiprocessing() |
150
|
|
|
|
151
|
|
|
for _ in range(int(self._main_args_.n_jobs / 2) + 2): |
152
|
|
|
print("\n") |
153
|
|
|
|
154
|
|
|
for _cand_ in _cand_list: |
155
|
|
|
self._process_results(_cand_) |
156
|
|
|
|
157
|
|
|
def search(self, nth_process=0, ray_=False): |
158
|
|
|
"""Public method for starting the search with the training data (X, y) |
159
|
|
|
|
160
|
|
|
Parameters |
161
|
|
|
---------- |
162
|
|
|
X : array-like or sparse matrix of shape = [n_samples, n_features] |
163
|
|
|
|
164
|
|
|
y : array-like, shape = [n_samples] or [n_samples, n_outputs] |
165
|
|
|
|
166
|
|
|
Returns |
167
|
|
|
------- |
168
|
|
|
None |
169
|
|
|
""" |
170
|
|
|
|
171
|
|
|
self.start_time = time.time() |
172
|
|
|
self.results_params = {} |
173
|
|
|
self.results_models = {} |
174
|
|
|
|
175
|
|
|
if ray_: |
176
|
|
|
self._run_job(nth_process) |
177
|
|
|
elif self._main_args_.n_jobs == 1: |
178
|
|
|
self._run_job(nth_process) |
179
|
|
|
else: |
180
|
|
|
self._run_multiple_jobs() |
181
|
|
|
|