1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import logging |
6
|
|
|
import numpy as np |
7
|
|
|
|
8
|
|
|
indent = " " |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class PrintResults: |
12
|
|
|
def __init__(self, opt_pros, verbosity): |
13
|
|
|
self.opt_pros = opt_pros |
14
|
|
|
self.verbosity = verbosity |
15
|
|
|
|
16
|
|
|
def _print_times(self, eval_time, iter_time, n_iter): |
17
|
|
|
opt_time = iter_time - eval_time |
18
|
|
|
iterPerSec = n_iter / iter_time |
19
|
|
|
|
20
|
|
|
print( |
21
|
|
|
indent, |
22
|
|
|
"Evaluation time :", |
23
|
|
|
eval_time, |
24
|
|
|
"sec", |
25
|
|
|
indent, |
26
|
|
|
"[{} %]".format(round(eval_time / iter_time * 100, 2)), |
27
|
|
|
) |
28
|
|
|
print( |
29
|
|
|
indent, |
30
|
|
|
"Optimization time :", |
31
|
|
|
opt_time, |
32
|
|
|
"sec", |
33
|
|
|
indent, |
34
|
|
|
"[{} %]".format(round(opt_time / iter_time * 100, 2)), |
35
|
|
|
) |
36
|
|
|
if iterPerSec >= 1: |
37
|
|
|
print( |
38
|
|
|
indent, |
39
|
|
|
"Iteration time :", |
40
|
|
|
iter_time, |
41
|
|
|
"sec", |
42
|
|
|
indent, |
43
|
|
|
"[{} iter/sec]".format(round(iterPerSec, 2)), |
44
|
|
|
) |
45
|
|
|
else: |
46
|
|
|
secPerIter = iter_time / n_iter |
47
|
|
|
print( |
48
|
|
|
indent, |
49
|
|
|
"Iteration time :", |
50
|
|
|
iter_time, |
51
|
|
|
"sec", |
52
|
|
|
indent, |
53
|
|
|
"[{} sec/iter]".format(round(secPerIter, 2)), |
54
|
|
|
) |
55
|
|
|
print(" ") |
56
|
|
|
|
57
|
|
|
def align_para_names(self, para_names): |
58
|
|
|
str_lengths = [len(str_) for str_ in para_names] |
59
|
|
|
max_length = max(str_lengths) |
60
|
|
|
|
61
|
|
|
para_names_align = {} |
62
|
|
|
for para_name, str_length in zip(para_names, str_lengths): |
63
|
|
|
added_spaces = max_length - str_length |
64
|
|
|
para_names_align[para_name] = " " * added_spaces |
65
|
|
|
|
66
|
|
|
return para_names_align |
67
|
|
|
|
68
|
|
|
def _print_results( |
69
|
|
|
self, |
70
|
|
|
objective_function, |
71
|
|
|
best_score, |
72
|
|
|
best_para, |
73
|
|
|
best_iter, |
74
|
|
|
best_additional_results, |
75
|
|
|
random_seed, |
76
|
|
|
): |
77
|
|
|
print("\nResults: '{}'".format(objective_function.__name__), " ") |
78
|
|
|
if best_para is None: |
79
|
|
|
print(indent, "Best score:", best_score, " ") |
80
|
|
|
print(indent, "Best parameter set:", best_para, " ") |
81
|
|
|
print(indent, "Best iteration:", best_iter, " ") |
82
|
|
|
|
83
|
|
|
else: |
84
|
|
|
print(indent, "Best score:", best_score, " ") |
85
|
|
|
|
86
|
|
|
if best_additional_results: |
87
|
|
|
print(indent, "Best additional results:") |
88
|
|
|
add_results_names = list(best_additional_results.keys()) |
89
|
|
|
add_results_names_align = self.align_para_names(add_results_names) |
90
|
|
|
|
91
|
|
|
for best_additional_result in best_additional_results.keys(): |
92
|
|
|
added_spaces = add_results_names_align[best_additional_result] |
93
|
|
|
print( |
94
|
|
|
indent, |
95
|
|
|
indent, |
96
|
|
|
"'{}'".format(best_additional_result), |
97
|
|
|
"{}:".format(added_spaces), |
98
|
|
|
best_additional_results[best_additional_result], |
99
|
|
|
" ", |
100
|
|
|
) |
101
|
|
|
|
102
|
|
|
if best_para: |
103
|
|
|
print(indent, "Best parameter set:") |
104
|
|
|
para_names = list(best_para.keys()) |
105
|
|
|
para_names_align = self.align_para_names(para_names) |
106
|
|
|
|
107
|
|
|
for para_key in best_para.keys(): |
108
|
|
|
added_spaces = para_names_align[para_key] |
109
|
|
|
print( |
110
|
|
|
indent, |
111
|
|
|
indent, |
112
|
|
|
"'{}'".format(para_key), |
113
|
|
|
"{}:".format(added_spaces), |
114
|
|
|
best_para[para_key], |
115
|
|
|
" ", |
116
|
|
|
) |
117
|
|
|
|
118
|
|
|
print(indent, "Best iteration:", best_iter, " ") |
119
|
|
|
|
120
|
|
|
print(" ") |
121
|
|
|
print(indent, "Random seed:", random_seed, " ") |
122
|
|
|
print(" ") |
123
|
|
|
|
124
|
|
|
def print_process(self, results, nth_process): |
125
|
|
|
verbosity = self.verbosity |
126
|
|
|
objective_function = self.opt_pros[nth_process].objective_function |
127
|
|
|
search_space = self.opt_pros[nth_process].s_space.search_space |
128
|
|
|
|
129
|
|
|
search_data = results["search_data"] |
130
|
|
|
|
131
|
|
|
try: |
132
|
|
|
best_sample = search_data.iloc[search_data["score"].idxmax()] |
133
|
|
|
|
134
|
|
|
except TypeError: |
135
|
|
|
logging.warning( |
136
|
|
|
"Warning: Cannot index by location index with a non-integer key" |
137
|
|
|
) |
138
|
|
|
|
139
|
|
|
else: |
140
|
|
|
best_score = best_sample["score"] |
141
|
|
|
best_values = best_sample[list(search_space.keys())] |
142
|
|
|
best_para = dict(zip(list(search_space.keys()), best_values)) |
143
|
|
|
best_additional_results_df = best_sample.drop( |
144
|
|
|
["score"] + list(search_space.keys()) |
145
|
|
|
) |
146
|
|
|
best_additional_results = best_additional_results_df.to_dict() |
147
|
|
|
|
148
|
|
|
best_iter = results["best_iter"] |
149
|
|
|
eval_times = results["eval_times"] |
150
|
|
|
iter_times = results["iter_times"] |
151
|
|
|
random_seed = results["random_seed"] |
152
|
|
|
|
153
|
|
|
n_iter = self.opt_pros[nth_process].n_iter |
154
|
|
|
|
155
|
|
|
eval_time = np.array(eval_times).sum() |
156
|
|
|
iter_time = np.array(iter_times).sum() |
157
|
|
|
|
158
|
|
|
if "print_results" in verbosity: |
159
|
|
|
self._print_results( |
160
|
|
|
objective_function, |
161
|
|
|
best_score, |
162
|
|
|
best_para, |
163
|
|
|
best_iter, |
164
|
|
|
best_additional_results, |
165
|
|
|
random_seed, |
166
|
|
|
) |
167
|
|
|
|
168
|
|
|
if "print_times" in verbosity: |
169
|
|
|
self._print_times(eval_time, iter_time, n_iter) |
170
|
|
|
|