|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import numpy as np |
|
6
|
|
|
from tqdm import tqdm |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class ProgressBarBase: |
|
10
|
|
|
def __init__(self, nth_process, n_iter, objective_function): |
|
11
|
|
|
self.best_since_iter = 0 |
|
12
|
|
|
self.score_best = -np.inf |
|
13
|
|
|
self.values_best = None |
|
14
|
|
|
|
|
15
|
|
|
self.objective_function = objective_function |
|
16
|
|
|
|
|
17
|
|
|
def _new2best(self, score_new, values_new): |
|
18
|
|
|
if score_new > self.score_best: |
|
19
|
|
|
self.score_best = score_new |
|
20
|
|
|
self.values_best = values_new |
|
21
|
|
|
|
|
22
|
|
|
def _print_results(self, print_results): |
|
23
|
|
|
if print_results: |
|
24
|
|
|
print("\nResults: '{}'".format(self.objective_function.__name__), " ") |
|
25
|
|
|
print(" Best values", np.array(self.values_best), " ") |
|
26
|
|
|
print(" Best score", self.score_best, " ") |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class ProgressBarLVL0(ProgressBarBase): |
|
30
|
|
|
def __init__(self, nth_process, n_iter, objective_function): |
|
31
|
|
|
super().__init__(nth_process, n_iter, objective_function) |
|
32
|
|
|
|
|
33
|
|
|
def update(self, score_new, values_new): |
|
34
|
|
|
self._new2best(score_new, values_new) |
|
35
|
|
|
|
|
36
|
|
|
def close(self, print_results): |
|
37
|
|
|
self._print_results(print_results) |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class ProgressBarLVL1(ProgressBarBase): |
|
41
|
|
|
def __init__(self, nth_process, n_iter, objective_function): |
|
42
|
|
|
self.best_since_iter = 0 |
|
43
|
|
|
self.score_best = -np.inf |
|
44
|
|
|
self.values_best = None |
|
45
|
|
|
|
|
46
|
|
|
self._tqdm = tqdm(**self._tqdm_dict(nth_process, n_iter, objective_function)) |
|
47
|
|
|
|
|
48
|
|
|
def update(self, score_new, values_new): |
|
49
|
|
|
self._tqdm.update() |
|
50
|
|
|
self._new2best(score_new, values_new) |
|
51
|
|
|
|
|
52
|
|
|
if score_new > self.score_best: |
|
53
|
|
|
self.best_since_iter = self._tqdm.n - 1 |
|
54
|
|
|
|
|
55
|
|
|
self._tqdm.set_postfix( |
|
56
|
|
|
best_score=str(score_new), best_iter=str(self.best_since_iter) |
|
57
|
|
|
) |
|
58
|
|
|
self._tqdm.refresh() |
|
59
|
|
|
|
|
60
|
|
|
def close(self, print_results): |
|
61
|
|
|
self._tqdm.close() |
|
62
|
|
|
self._print_results(print_results) |
|
63
|
|
|
|
|
64
|
|
|
def _tqdm_dict(self, nth_process, n_iter, objective_function): |
|
65
|
|
|
"""Generates the parameter dict for tqdm in the iteration-loop of each optimizer""" |
|
66
|
|
|
|
|
67
|
|
|
self.objective_function = objective_function |
|
68
|
|
|
|
|
69
|
|
|
if nth_process is None: |
|
70
|
|
|
process_str = "" |
|
71
|
|
|
else: |
|
72
|
|
|
process_str = "Process " + str(nth_process) + " -> " |
|
73
|
|
|
|
|
74
|
|
|
return { |
|
75
|
|
|
"total": n_iter, |
|
76
|
|
|
"desc": process_str + objective_function.__name__, |
|
77
|
|
|
"position": nth_process, |
|
78
|
|
|
"leave": True, |
|
79
|
|
|
"smoothing": 1.0, |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|