1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
from ..search_space import SearchSpace |
6
|
|
|
from ..model import Model |
7
|
|
|
from ..init_position import InitSearchPosition |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class Candidate: |
11
|
|
|
def __init__(self, nth_process, _config_): |
12
|
|
|
self.search_config = _config_.search_config |
13
|
|
|
self.memory = _config_.memory |
14
|
|
|
|
15
|
|
|
self._score_best = -1000 |
16
|
|
|
self.pos_best = None |
17
|
|
|
|
18
|
|
|
self.model = None |
19
|
|
|
self._space_ = SearchSpace(_config_) |
20
|
|
|
|
21
|
|
|
self.nth_process = nth_process |
22
|
|
|
self.func_ = list(_config_.search_config.keys())[0] |
23
|
|
|
|
24
|
|
|
self._space_.create_kerasSearchSpace() |
25
|
|
|
self._model_ = Model(_config_) |
26
|
|
|
|
27
|
|
|
self._init_ = InitSearchPosition( |
28
|
|
|
self._space_, self._model_, _config_.warm_start, _config_.scatter_init |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
def create_start_point(self, para): |
32
|
|
|
start_point = {} |
33
|
|
|
|
34
|
|
|
temp_dict = {} |
35
|
|
|
for para_key in para: |
36
|
|
|
temp_dict[para_key] = [para[para_key]] |
37
|
|
|
|
38
|
|
|
start_point[self.func_] = temp_dict |
39
|
|
|
|
40
|
|
|
return start_point |
41
|
|
|
|
42
|
|
|
def _get_warm_start(self): |
43
|
|
|
para_best = self._space_.pos2para(self.pos_best) |
44
|
|
|
warm_start = self.create_start_point(para_best) |
45
|
|
|
|
46
|
|
|
return warm_start |
47
|
|
|
|
48
|
|
|
@property |
49
|
|
|
def score_best(self): |
50
|
|
|
return self._score_best |
51
|
|
|
|
52
|
|
|
@score_best.setter |
53
|
|
|
def score_best(self, value): |
54
|
|
|
# self.model_best = self.model |
55
|
|
|
self._score_best = value |
56
|
|
|
|
57
|
|
|
def eval_pos(self, pos, X, y, force_eval=False): |
58
|
|
|
pos_str = pos.tostring() |
59
|
|
|
|
60
|
|
|
if pos_str in self._space_.memory and self.memory and not force_eval: |
61
|
|
|
return self._space_.memory[pos_str] |
62
|
|
|
else: |
63
|
|
|
para = self._space_.pos2para(pos) |
64
|
|
|
score, self.model = self._model_.train_model(para, X, y) |
65
|
|
|
self._space_.memory[pos_str] = score |
66
|
|
|
|
67
|
|
|
return score |
68
|
|
|
|