Passed
Push — master ( b3cc5b...cdf567 )
by Simon
01:59
created

BaseOptimizer._process_results()   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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