1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import abc |
6
|
|
|
from tqdm.auto import tqdm |
7
|
|
|
|
8
|
|
|
from .util import sort_for_best |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Verbosity(metaclass=abc.ABCMeta): |
12
|
|
|
def __init__(self): |
13
|
|
|
pass |
14
|
|
|
|
15
|
|
|
@abc.abstractmethod |
16
|
|
|
def print_start_point(self, _cand_): |
17
|
|
|
pass |
18
|
|
|
|
19
|
|
|
@abc.abstractmethod |
20
|
|
|
def print_start_points(self, _cand_): |
21
|
|
|
pass |
22
|
|
|
|
23
|
|
|
def init_p_bar(self, _cand_, _core_): |
24
|
|
|
pass |
25
|
|
|
|
26
|
|
|
def update_p_bar(self, n, _cand_): |
27
|
|
|
pass |
28
|
|
|
|
29
|
|
|
def close_p_bar(self): |
30
|
|
|
pass |
31
|
|
|
|
32
|
|
|
def _tqdm_dict(self, _cand_): |
33
|
|
|
pass |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
class VerbosityLVL0(Verbosity): |
37
|
|
|
def __init__(self): |
38
|
|
|
pass |
39
|
|
|
|
40
|
|
|
def print_start_point(self, _cand_): |
41
|
|
|
return _cand_._get_warm_start() |
42
|
|
|
|
43
|
|
|
def print_start_points(self, _cand_list, _core_): |
44
|
|
|
start_point_list = [] |
45
|
|
|
score_best_list = [] |
46
|
|
|
model_best_list = [] |
47
|
|
|
results = {} |
48
|
|
|
|
49
|
|
|
for _cand_ in _cand_list: |
50
|
|
|
model_best = _cand_.model_best |
51
|
|
|
score_best = _cand_.score_best |
52
|
|
|
start_point = _cand_._get_warm_start() |
53
|
|
|
|
54
|
|
|
results[score_best] = start_point |
55
|
|
|
|
56
|
|
|
start_point_list.append(start_point) |
57
|
|
|
score_best_list.append(score_best) |
58
|
|
|
model_best_list.append(model_best) |
59
|
|
|
|
60
|
|
|
start_point_sorted, score_best_sorted = sort_for_best( |
61
|
|
|
start_point_list, score_best_list |
62
|
|
|
) |
63
|
|
|
|
64
|
|
|
model_best_sorted, score_best_sorted = sort_for_best( |
65
|
|
|
model_best_list, score_best_list |
66
|
|
|
) |
67
|
|
|
|
68
|
|
|
return score_best_sorted, model_best_sorted, results |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
class VerbosityLVL1(VerbosityLVL0): |
72
|
|
|
def __init__(self): |
73
|
|
|
pass |
74
|
|
|
|
75
|
|
|
def print_start_point(self, _cand_): |
76
|
|
|
start_point = _cand_._get_warm_start() |
77
|
|
|
print("\nbest para =", start_point) |
78
|
|
|
print("score =", _cand_.score_best) |
79
|
|
|
|
80
|
|
|
return start_point |
81
|
|
|
|
82
|
|
|
def print_start_points(self, _cand_list, _core_): |
83
|
|
|
start_point_list = [] |
84
|
|
|
score_best_list = [] |
85
|
|
|
model_best_list = [] |
86
|
|
|
results = {} |
87
|
|
|
|
88
|
|
|
for _cand_ in _cand_list: |
89
|
|
|
model_best = _cand_.model_best |
90
|
|
|
score_best = _cand_.score_best |
91
|
|
|
start_point = _cand_._get_warm_start() |
92
|
|
|
|
93
|
|
|
results[score_best] = start_point |
94
|
|
|
|
95
|
|
|
start_point_list.append(start_point) |
96
|
|
|
score_best_list.append(score_best) |
97
|
|
|
model_best_list.append(model_best) |
98
|
|
|
|
99
|
|
|
start_point_sorted, score_best_sorted = sort_for_best( |
100
|
|
|
start_point_list, score_best_list |
101
|
|
|
) |
102
|
|
|
|
103
|
|
|
model_best_sorted, score_best_sorted = sort_for_best( |
104
|
|
|
model_best_list, score_best_list |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
for i in range(int(_core_.n_jobs / 2)): |
108
|
|
|
print("\n") |
109
|
|
|
print("\nList of start points (best first):\n") |
110
|
|
|
for start_point, score_best in zip(start_point_sorted, score_best_sorted): |
111
|
|
|
print("best para =", start_point) |
112
|
|
|
print("score =", score_best, "\n") |
113
|
|
|
|
114
|
|
|
return score_best_sorted, model_best_sorted, results |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
class VerbosityLVL2(VerbosityLVL1): |
118
|
|
|
def __init__(self): |
119
|
|
|
pass |
120
|
|
|
|
121
|
|
|
def init_p_bar(self, _cand_, _core_): |
122
|
|
|
self.p_bar = tqdm(**self._tqdm_dict(_cand_, _core_)) |
123
|
|
|
|
124
|
|
|
def update_p_bar(self, n, _cand_): |
125
|
|
|
self.p_bar.update(n) |
126
|
|
|
self.p_bar.set_postfix(best_score=str(_cand_.score_best)) |
127
|
|
|
|
128
|
|
|
def close_p_bar(self): |
129
|
|
|
self.p_bar.close() |
130
|
|
|
|
131
|
|
|
def _tqdm_dict(self, _cand_, _core_): |
132
|
|
|
"""Generates the parameter dict for tqdm in the iteration-loop of each optimizer""" |
133
|
|
|
return { |
134
|
|
|
"total": _core_.n_iter, |
135
|
|
|
"desc": "Thread " |
136
|
|
|
+ str(_cand_.nth_process) |
137
|
|
|
+ " -> " |
138
|
|
|
+ _cand_._model_.func_.__name__, |
139
|
|
|
"position": _cand_.nth_process, |
140
|
|
|
"leave": True, |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
class VerbosityLVL10(VerbosityLVL0): |
145
|
|
|
def __init__(self): |
146
|
|
|
pass |
147
|
|
|
|
148
|
|
|
def start_search(self): |
149
|
|
|
print("") |
150
|
|
|
|
151
|
|
|
def get_search_path(self): |
152
|
|
|
pass |
153
|
|
|
|