|
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
|
|
|
experiment, |
|
71
|
|
|
best_score, |
|
72
|
|
|
best_para, |
|
73
|
|
|
best_iter, |
|
74
|
|
|
best_additional_results, |
|
75
|
|
|
random_seed, |
|
76
|
|
|
): |
|
77
|
|
|
print("\nResults: '{}'".format(experiment.__class__.__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( |
|
90
|
|
|
add_results_names |
|
91
|
|
|
) |
|
92
|
|
|
|
|
93
|
|
|
for best_additional_result in best_additional_results.keys(): |
|
94
|
|
|
added_spaces = add_results_names_align[ |
|
95
|
|
|
best_additional_result |
|
96
|
|
|
] |
|
97
|
|
|
print( |
|
98
|
|
|
indent, |
|
99
|
|
|
indent, |
|
100
|
|
|
"'{}'".format(best_additional_result), |
|
101
|
|
|
"{}:".format(added_spaces), |
|
102
|
|
|
best_additional_results[best_additional_result], |
|
103
|
|
|
" ", |
|
104
|
|
|
) |
|
105
|
|
|
|
|
106
|
|
|
if best_para: |
|
107
|
|
|
print(indent, "Best parameter set:") |
|
108
|
|
|
para_names = list(best_para.keys()) |
|
109
|
|
|
para_names_align = self.align_para_names(para_names) |
|
110
|
|
|
|
|
111
|
|
|
for para_key in best_para.keys(): |
|
112
|
|
|
added_spaces = para_names_align[para_key] |
|
113
|
|
|
print( |
|
114
|
|
|
indent, |
|
115
|
|
|
indent, |
|
116
|
|
|
"'{}'".format(para_key), |
|
117
|
|
|
"{}:".format(added_spaces), |
|
118
|
|
|
best_para[para_key], |
|
119
|
|
|
" ", |
|
120
|
|
|
) |
|
121
|
|
|
|
|
122
|
|
|
print(indent, "Best iteration:", best_iter, " ") |
|
123
|
|
|
|
|
124
|
|
|
print(" ") |
|
125
|
|
|
print(indent, "Random seed:", random_seed, " ") |
|
126
|
|
|
print(" ") |
|
127
|
|
|
|
|
128
|
|
|
def print_process(self, results, nth_process): |
|
129
|
|
|
verbosity = self.verbosity |
|
130
|
|
|
experiment = self.opt_pros[nth_process].experiment |
|
131
|
|
|
search_space = self.opt_pros[nth_process].s_space.search_space |
|
132
|
|
|
|
|
133
|
|
|
search_data = results["search_data"] |
|
134
|
|
|
|
|
135
|
|
|
try: |
|
136
|
|
|
best_sample = search_data.iloc[search_data["score"].idxmax()] |
|
137
|
|
|
|
|
138
|
|
|
except TypeError: |
|
139
|
|
|
logging.warning( |
|
140
|
|
|
"Warning: Cannot index by location index with a non-integer key" |
|
141
|
|
|
) |
|
142
|
|
|
|
|
143
|
|
|
else: |
|
144
|
|
|
best_score = best_sample["score"] |
|
145
|
|
|
best_values = best_sample[list(search_space.keys())] |
|
146
|
|
|
best_para = dict(zip(list(search_space.keys()), best_values)) |
|
147
|
|
|
best_additional_results_df = best_sample.drop( |
|
148
|
|
|
["score"] + list(search_space.keys()) |
|
149
|
|
|
) |
|
150
|
|
|
best_additional_results = best_additional_results_df.to_dict() |
|
151
|
|
|
|
|
152
|
|
|
best_iter = results["best_iter"] |
|
153
|
|
|
eval_times = results["eval_times"] |
|
154
|
|
|
iter_times = results["iter_times"] |
|
155
|
|
|
random_seed = results["random_seed"] |
|
156
|
|
|
|
|
157
|
|
|
n_iter = self.opt_pros[nth_process].n_iter |
|
158
|
|
|
|
|
159
|
|
|
eval_time = np.array(eval_times).sum() |
|
160
|
|
|
iter_time = np.array(iter_times).sum() |
|
161
|
|
|
|
|
162
|
|
|
if "print_results" in verbosity: |
|
163
|
|
|
self._print_results( |
|
164
|
|
|
experiment, |
|
165
|
|
|
best_score, |
|
166
|
|
|
best_para, |
|
167
|
|
|
best_iter, |
|
168
|
|
|
best_additional_results, |
|
169
|
|
|
random_seed, |
|
170
|
|
|
) |
|
171
|
|
|
|
|
172
|
|
|
if "print_times" in verbosity: |
|
173
|
|
|
self._print_times(eval_time, iter_time, n_iter) |
|
174
|
|
|
|