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 .candidate import Candidate |
11
|
|
|
from .verbosity import set_verbosity |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class BaseOptimizer: |
15
|
|
|
def __init__(self, _main_args_, _opt_args_): |
16
|
|
|
self._main_args_ = _main_args_ |
17
|
|
|
self._opt_args_ = _opt_args_ |
18
|
|
|
|
19
|
|
|
self._info_, _pbar_ = set_verbosity(_main_args_.verbosity) |
20
|
|
|
self._pbar_ = _pbar_() |
21
|
|
|
|
22
|
|
|
def search(self, nth_process=0, rayInit=False): |
23
|
|
|
self.start_time = time.time() |
24
|
|
|
self.results = {} |
25
|
|
|
self.eval_times = {} |
26
|
|
|
self.iter_times = {} |
27
|
|
|
self.best_scores = {} |
28
|
|
|
self.pos_list = {} |
29
|
|
|
self.score_list = {} |
30
|
|
|
|
31
|
|
|
if rayInit: |
32
|
|
|
self._run_job(nth_process) |
33
|
|
|
elif self._main_args_.n_jobs == 1: |
34
|
|
|
self._run_job(nth_process) |
35
|
|
|
else: |
36
|
|
|
self._run_multiple_jobs() |
37
|
|
|
|
38
|
|
|
return ( |
39
|
|
|
self.results, |
40
|
|
|
self.pos_list, |
41
|
|
|
self.score_list, |
42
|
|
|
self.eval_times, |
43
|
|
|
self.iter_times, |
44
|
|
|
self.best_scores, |
45
|
|
|
) |
46
|
|
|
|
47
|
|
|
def _search_multiprocessing(self): |
48
|
|
|
"""Wrapper for the parallel search. Passes integer that corresponds to process number""" |
49
|
|
|
pool = multiprocessing.Pool(self._main_args_.n_jobs) |
50
|
|
|
_cand_list, _p_list = zip( |
51
|
|
|
*pool.map(self._search, self._main_args_._n_process_range) |
52
|
|
|
) |
53
|
|
|
|
54
|
|
|
return _cand_list, _p_list |
55
|
|
|
|
56
|
|
|
def _run_job(self, nth_process): |
57
|
|
|
_cand_, _p_ = self._search(nth_process) |
58
|
|
|
self._get_attributes(_cand_, _p_) |
59
|
|
|
|
60
|
|
|
def _get_attributes(self, _cand_, _p_): |
61
|
|
|
self.results[_cand_.func_] = _cand_._process_results(self._opt_args_) |
62
|
|
|
self.eval_times[_cand_.func_] = _cand_.eval_time |
63
|
|
|
self.iter_times[_cand_.func_] = _cand_.iter_times |
64
|
|
|
self.best_scores[_cand_.func_] = _cand_.score_best |
65
|
|
|
|
66
|
|
|
if isinstance(_p_, list): |
67
|
|
|
self.pos_list[_cand_.func_] = [np.array(p.pos_new_list) for p in _p_] |
68
|
|
|
self.score_list[_cand_.func_] = [np.array(p.score_new_list) for p in _p_] |
69
|
|
|
else: |
70
|
|
|
self.pos_list[_cand_.func_] = [np.array(_p_.pos_new_list)] |
71
|
|
|
self.score_list[_cand_.func_] = [np.array(_p_.score_new_list)] |
72
|
|
|
|
73
|
|
|
def _run_multiple_jobs(self): |
74
|
|
|
_cand_list, _p_list = self._search_multiprocessing() |
75
|
|
|
|
76
|
|
|
for _ in range(int(self._main_args_.n_jobs / 2) + 2): |
77
|
|
|
print("\n") |
78
|
|
|
|
79
|
|
|
for _cand_, _p_ in zip(_cand_list, _p_list): |
80
|
|
|
self._get_attributes(_cand_, _p_) |
81
|
|
|
|
82
|
|
|
def _search(self, nth_process): |
83
|
|
|
_cand_, _p_ = self._initialize_search( |
84
|
|
|
self._main_args_, nth_process, self._info_ |
85
|
|
|
) |
86
|
|
|
|
87
|
|
|
for i in range(self._main_args_.n_iter - 1): |
88
|
|
|
c_time = time.time() |
89
|
|
|
|
90
|
|
|
_cand_.i = i |
91
|
|
|
_cand_ = self._iterate(i, _cand_, _p_) |
92
|
|
|
self._pbar_.update_p_bar(1, _cand_) |
93
|
|
|
|
94
|
|
|
run_time = time.time() - self.start_time |
95
|
|
|
if self._main_args_.max_time and run_time > self._main_args_.max_time: |
96
|
|
|
break |
97
|
|
|
|
98
|
|
|
_cand_.iter_times.append(time.time() - c_time) |
99
|
|
|
|
100
|
|
|
self._pbar_.close_p_bar() |
101
|
|
|
return _cand_, _p_ |
102
|
|
|
|
103
|
|
|
def _initialize_search(self, _main_args_, nth_process, _info_): |
104
|
|
|
_main_args_._set_random_seed(nth_process) |
105
|
|
|
_cand_ = Candidate(nth_process, _main_args_, _info_) |
106
|
|
|
self._pbar_.init_p_bar(_cand_, self._main_args_) |
107
|
|
|
|
108
|
|
|
_p_ = self._init_opt_positioner(_cand_) |
109
|
|
|
self._pbar_.update_p_bar(1, _cand_) |
110
|
|
|
|
111
|
|
|
return _cand_, _p_ |
112
|
|
|
|
113
|
|
|
def _init_base_positioner(self, _cand_, positioner=None): |
114
|
|
|
if positioner: |
115
|
|
|
_p_ = positioner(**self._opt_args_.kwargs_opt) |
116
|
|
|
else: |
117
|
|
|
_p_ = BasePositioner(**self._opt_args_.kwargs_opt) |
118
|
|
|
|
119
|
|
|
_p_.pos_current = _cand_.pos_best |
120
|
|
|
_p_.score_current = _cand_.score_best |
121
|
|
|
|
122
|
|
|
return _p_ |
123
|
|
|
|
124
|
|
|
def _update_pos(self, _cand_, _p_): |
125
|
|
|
_cand_.pos_best = _p_.pos_new |
126
|
|
|
_cand_.score_best = _p_.score_new |
127
|
|
|
|
128
|
|
|
_p_.pos_current = _p_.pos_new |
129
|
|
|
_p_.score_current = _p_.score_new |
130
|
|
|
|
131
|
|
|
self._pbar_.best_since_iter = _cand_.i |
132
|
|
|
|
133
|
|
|
return _cand_, _p_ |
134
|
|
|
|