Completed
Push — master ( 9ccc17...a45661 )
by Simon
04:00 queued 22s
created

hyperactive.base_optimizer.BaseOptimizer._search()   A

Complexity

Conditions 5

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 18
rs 9.2833
c 0
b 0
f 0
cc 5
nop 2
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import time
6
import numpy as np
7
import multiprocessing
8
9
from .base_positioner import BasePositioner
10
from .verb import VerbosityLVL0, VerbosityLVL1, VerbosityLVL2, VerbosityLVL3
11
from .util import init_candidate
12
from .candidate import Candidate
13
14
15
class BaseOptimizer:
16
    def __init__(self, _main_args_, _opt_args_):
17
        self._main_args_ = _main_args_
18
        self._opt_args_ = _opt_args_
19
20
        verbs = [VerbosityLVL0, VerbosityLVL1, VerbosityLVL2, VerbosityLVL3]
21
        self._verb_ = verbs[_main_args_.verbosity]()
22
23
        self.pos_list = []
24
        self.score_list = []
25
26
    def _init_base_positioner(self, _cand_, positioner=None):
27
        if positioner:
28
            _p_ = positioner(**self._opt_args_.kwargs_opt)
29
        else:
30
            _p_ = BasePositioner(**self._opt_args_.kwargs_opt)
31
32
        _p_.pos_current = _cand_.pos_best
33
        _p_.score_current = _cand_.score_best
34
35
        return _p_
36
37
    def _update_pos(self, _cand_, _p_):
38
        _cand_.pos_best = _p_.pos_new
39
        _cand_.score_best = _p_.score_new
40
41
        _p_.pos_current = _p_.pos_new
42
        _p_.score_current = _p_.score_new
43
44
        self._verb_.best_since_iter = _cand_.i
45
46
        return _cand_, _p_
47
48
    def _initialize_search(self, _main_args_, nth_process):
49
        _cand_ = init_candidate(_main_args_, nth_process, Candidate)
50
        self._verb_.init_p_bar(_cand_, self._main_args_)
51
52
        _p_ = self._init_opt_positioner(_cand_)
53
        self._verb_.update_p_bar(1, _cand_)
54
55
        return _cand_, _p_
56
57
    def _search(self, nth_process):
58
        _cand_, _p_ = self._initialize_search(self._main_args_, nth_process)
59
60
        for i in range(self._main_args_.n_iter - 1):
61
            _cand_.i = i
62
            _cand_ = self._iterate(i, _cand_, _p_)
63
            self._verb_.update_p_bar(1, _cand_)
64
65
            run_time = time.time() - self.start_time
66
            if self._main_args_.max_time and run_time > self._main_args_.max_time:
67
                break
68
69
            if self._main_args_.get_search_path:
70
                self._monitor_search_path(_p_)
71
72
        self._verb_.close_p_bar()
73
74
        return _cand_
75
76
    def _monitor_search_path(self, _p_):
77
        pos_list = []
78
        score_list = []
79
        if isinstance(_p_, list):
80
            for p in _p_:
81
                pos_list.append(p.pos_new)
82
                score_list.append(p.score_new)
83
84
                pos_list_ = np.array(pos_list)
85
                score_list_ = np.array(score_list)
86
87
            self.pos_list.append(pos_list_)
0 ignored issues
show
introduced by
The variable pos_list_ does not seem to be defined in case the for loop on line 80 is not entered. Are you sure this can never be the case?
Loading history...
88
            self.score_list.append(score_list_)
0 ignored issues
show
introduced by
The variable score_list_ does not seem to be defined in case the for loop on line 80 is not entered. Are you sure this can never be the case?
Loading history...
89
        else:
90
            pos_list.append(_p_.pos_new)
91
            score_list.append(_p_.score_new)
92
93
            pos_list_ = np.array(pos_list)
94
            score_list_ = np.array(score_list)
95
96
            self.pos_list.append(pos_list_)
97
            self.score_list.append(score_list_)
98
99
    def _search_multiprocessing(self):
100
        """Wrapper for the parallel search. Passes integer that corresponds to process number"""
101
        pool = multiprocessing.Pool(self._main_args_.n_jobs)
102
        _cand_list = pool.map(self._search, self._main_args_._n_process_range)
103
104
        return _cand_list
105
106
    def _run_job(self, nth_process):
107
        _cand_ = self._search(nth_process)
108
        self.results_params[_cand_.func_] = _cand_._process_results(
109
            self._verb_, self._opt_args_
110
        )
111
112
    def _run_multiple_jobs(self):
113
        _cand_list = self._search_multiprocessing()
114
115
        for _ in range(int(self._main_args_.n_jobs / 2) + 2):
116
            print("\n")
117
118
        for _cand_ in _cand_list:
119
            pass
120
            self.results_params[_cand_.func_] = _cand_._process_results(
121
                self._verb_, self._opt_args_
122
            )
123
124
    def search(self, nth_process=0, rayInit=False):
125
        self.start_time = time.time()
126
        self.results_params = {}
127
128
        if rayInit:
129
            self._run_job(nth_process)
130
        elif self._main_args_.n_jobs == 1:
131
            self._run_job(nth_process)
132
        else:
133
            self._run_multiple_jobs()
134
135
        return (self.results_params, self.pos_list, self.score_list)
136