Passed
Push — master ( b0b7f3...e3d174 )
by Simon
04:31
created

ProgressBarBase.__init__()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
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.pos_best = None
14
15
        self.objective_function = objective_function
16
17
    def _new2best(self, score_new, pos_new):
18
        if score_new > self.score_best:
19
            self.score_best = score_new
20
            self.pos_best = pos_new
21
22
23
class ProgressBarLVL0(ProgressBarBase):
24
    def __init__(self, nth_process, n_iter, objective_function):
25
        super().__init__(nth_process, n_iter, objective_function)
26
27
    def update(self, score_new, pos_new):
28
        self._new2best(score_new, pos_new)
29
30
    def close(self):
31
        pass
32
33
34
class ProgressBarLVL1(ProgressBarBase):
35
    def __init__(self, nth_process, n_iter, objective_function):
36
        super().__init__(nth_process, n_iter, objective_function)
37
        self._tqdm = tqdm(
38
            **self._tqdm_dict(nth_process, n_iter, objective_function)
39
        )
40
41
    def update(self, score_new, pos_new):
42
        if score_new > self.score_best:
43
            self.best_since_iter = self._tqdm.n - 1
44
            self._tqdm.set_postfix(
45
                best_score=str(score_new),
46
                best_pos=str(pos_new),
47
                best_iter=str(self.best_since_iter),
48
            )
49
50
        self._new2best(score_new, pos_new)
51
        self._tqdm.update(1)
52
        # self._tqdm.refresh()
53
54
    def close(self):
55
        self._tqdm.close()
56
57
    def _tqdm_dict(self, nth_process, n_iter, objective_function):
58
        """Generates the parameter dict for tqdm in the iteration-loop of each optimizer"""
59
60
        self.objective_function = objective_function
61
62
        if nth_process is None:
63
            process_str = ""
64
        else:
65
            process_str = "Process " + str(nth_process)
66
67
        return {
68
            "total": n_iter,
69
            "desc": process_str,
70
            "position": nth_process,
71
            "leave": False,
72
            # "smoothing": 1.0,
73
        }
74
75