1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
from .base_positioner import BasePositioner |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class BaseOptimizer: |
9
|
|
|
def __init__(self, _opt_args_): |
10
|
|
|
self._opt_args_ = _opt_args_ |
11
|
|
|
self.p_list = [] |
12
|
|
|
|
13
|
|
|
def iterate(self, i, _cand_): |
14
|
|
|
self.i = i |
15
|
|
|
|
16
|
|
|
if i < self.n_positioners: |
17
|
|
|
p = self._init_iteration(_cand_) |
18
|
|
|
self.p_list.append(p) |
19
|
|
|
|
20
|
|
|
else: |
21
|
|
|
self._iterate(i, _cand_) |
22
|
|
|
|
23
|
|
|
return _cand_ |
24
|
|
|
|
25
|
|
|
def _finish_search(self): |
26
|
|
|
self._pbar_.close_p_bar() |
27
|
|
|
|
28
|
|
|
def _update_pos(self, _cand_, _p_): |
29
|
|
|
if _p_.score_new > _p_.score_best: |
30
|
|
|
_p_.pos_best = _p_.pos_new |
31
|
|
|
_p_.score_best = _p_.score_new |
32
|
|
|
|
33
|
|
|
if _p_.score_new > _cand_.score_best: |
34
|
|
|
_p_.pos_current = _p_.pos_new |
35
|
|
|
_p_.score_current = _p_.score_new |
36
|
|
|
|
37
|
|
|
_cand_.pos_best = _p_.pos_new |
38
|
|
|
_cand_.score_best = _p_.score_new |
39
|
|
|
|
40
|
|
|
self._pbar_.best_since_iter = _cand_.i |
41
|
|
|
|
42
|
|
|
def _optimizer_eval(self, _cand_, _p_): |
43
|
|
|
_p_.score_new = _cand_.eval_pos(_p_.pos_new) |
44
|
|
|
self._pbar_.update_p_bar(1, _cand_) |
45
|
|
|
|
46
|
|
|
def _init_base_positioner(self, _cand_, positioner=None): |
47
|
|
|
if positioner: |
48
|
|
|
_p_ = positioner(**self._opt_args_.kwargs_opt) |
49
|
|
|
else: |
50
|
|
|
_p_ = BasePositioner(**self._opt_args_.kwargs_opt) |
51
|
|
|
|
52
|
|
|
_p_.pos_new = _cand_.pos_best |
53
|
|
|
_p_.score_new = _cand_.score_best |
54
|
|
|
|
55
|
|
|
return _p_ |
56
|
|
|
|