Passed
Push — master ( 7ebd63...8bd9a3 )
by Simon
06:11
created

gradient_free_optimizers._progress_bar   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 67
dl 0
loc 105
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A ProgressBarLVL1._tqdm_dict() 0 17 2
A ProgressBarBase.best_since_iter() 0 3 1
A ProgressBarLVL0.update() 0 3 1
A ProgressBarBase.score_best() 0 3 1
A ProgressBarLVL0.close() 0 2 1
A ProgressBarLVL1.__init__() 0 3 1
A ProgressBarLVL1.update() 0 15 2
A ProgressBarLVL1.close() 0 3 1
A ProgressBarBase._new2best() 0 6 2
A ProgressBarLVL0.__init__() 0 2 1
A ProgressBarBase.__init__() 0 13 1
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.pos_best = None
12
        self._score_best = -np.inf
13
        self.score_best_list = []
14
15
        self.convergence_data = []
16
17
        self._best_since_iter = 0
18
        self.best_since_iter_list = []
19
20
        self.objective_function = objective_function
21
22
        self.n_iter_current = 0
23
24
    @property
25
    def score_best(self):
26
        return self._score_best
27
28
    @score_best.setter
29
    def score_best(self, score):
30
        self.score_best_list.append(score)
31
        self._score_best = score
32
33
    @property
34
    def best_since_iter(self):
35
        return self._best_since_iter
36
37
    @best_since_iter.setter
38
    def best_since_iter(self, nth_iter):
39
        self.best_since_iter_list.append(nth_iter)
40
        self._best_since_iter = nth_iter
41
42
    def _new2best(self, score_new, pos_new, nth_iter):
43
        if score_new > self.score_best:
44
            self.score_best = score_new
45
            self.pos_best = pos_new
46
47
        self.convergence_data.append(self.score_best)
48
49
50
class ProgressBarLVL0(ProgressBarBase):
51
    def __init__(self, nth_process, n_iter, objective_function):
52
        super().__init__(nth_process, n_iter, objective_function)
53
54
    def update(self, score_new, pos_new, nth_iter):
55
        self.n_iter_current = nth_iter
56
        self._new2best(score_new, pos_new, nth_iter)
57
58
    def close(self):
59
        pass
60
61
62
class ProgressBarLVL1(ProgressBarBase):
63
    def __init__(self, nth_process, n_iter, objective_function):
64
        super().__init__(nth_process, n_iter, objective_function)
65
        self._tqdm = tqdm(**self._tqdm_dict(nth_process, n_iter, objective_function))
66
67
    def update(self, score_new, pos_new, nth_iter):
68
        self.n_iter_current = nth_iter
69
70
        if score_new > self.score_best:
71
            self.best_since_iter = nth_iter
72
73
            self._tqdm.set_postfix(
74
                best_score=str(score_new),
75
                best_pos=str(pos_new),
76
                best_iter=str(self._best_since_iter),
77
            )
78
79
        self._new2best(score_new, pos_new, nth_iter)
80
81
        self._tqdm.update(1)
82
        # self._tqdm.refresh()
83
84
    def close(self):
85
        self._tqdm.close()
86
        del self._tqdm
87
88
    def _tqdm_dict(self, nth_process, n_iter, objective_function):
89
        """
90
        Generates the parameter dict for tqdm in the iteration-loop of each optimizer
91
        """
92
93
        self.objective_function = objective_function
94
95
        if nth_process is None:
96
            process_str = ""
97
        else:
98
            process_str = str(objective_function.__name__)
99
100
        return {
101
            "total": n_iter,
102
            "desc": process_str,
103
            "position": nth_process,
104
            "leave": False,
105
            # "smoothing": 1.0,
106
        }
107