|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import time |
|
6
|
|
|
|
|
7
|
|
|
import numpy as np |
|
8
|
|
|
from multiprocessing import Pool |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class Search: |
|
12
|
|
|
def __init__(self, search_processes): |
|
13
|
|
|
self.search_processes = search_processes |
|
14
|
|
|
self.n_processes = len(search_processes) |
|
15
|
|
|
self._n_process_range = range(0, self.n_processes) |
|
16
|
|
|
|
|
17
|
|
|
print("self.n_processes", self.n_processes) |
|
18
|
|
|
|
|
19
|
|
|
def run(self, max_time): |
|
20
|
|
|
self.start_time = time.time() |
|
21
|
|
|
self.results = {} |
|
22
|
|
|
self.eval_times = {} |
|
23
|
|
|
self.iter_times = {} |
|
24
|
|
|
self.best_scores = {} |
|
25
|
|
|
self.pos_list = {} |
|
26
|
|
|
self.score_list = {} |
|
27
|
|
|
|
|
28
|
|
|
if len(self.search_processes) == 1: |
|
29
|
|
|
self._run_job(0) |
|
30
|
|
|
else: |
|
31
|
|
|
self._run_multiple_jobs() |
|
32
|
|
|
|
|
33
|
|
|
return ( |
|
34
|
|
|
self.results, |
|
35
|
|
|
self.pos_list, |
|
36
|
|
|
self.score_list, |
|
37
|
|
|
self.eval_times, |
|
38
|
|
|
self.iter_times, |
|
39
|
|
|
self.best_scores, |
|
40
|
|
|
) |
|
41
|
|
|
|
|
42
|
|
|
def _search_multiprocessing(self): |
|
43
|
|
|
"""Wrapper for the parallel search. Passes integer that corresponds to process number""" |
|
44
|
|
|
pool = Pool(self.n_processes) |
|
45
|
|
|
_p_list = zip(*pool.map(self._run, self._n_process_range)) |
|
46
|
|
|
|
|
47
|
|
|
return _p_list |
|
48
|
|
|
|
|
49
|
|
|
def _run_job(self, nth_process): |
|
50
|
|
|
_p_ = self._run(nth_process) |
|
51
|
|
|
# self._get_attributes(_p_) |
|
52
|
|
|
|
|
53
|
|
|
def _get_attributes(self, _p_): |
|
54
|
|
|
self.results[self.process.obj_func] = self.process._process_results() |
|
55
|
|
|
self.eval_times[self.process.obj_func] = self.process.eval_time |
|
56
|
|
|
self.iter_times[self.process.obj_func] = self.process.iter_times |
|
57
|
|
|
self.best_scores[self.process.obj_func] = self.process.score_best |
|
58
|
|
|
|
|
59
|
|
|
if isinstance(_p_, list): |
|
60
|
|
|
self.pos_list[self.process.obj_func] = [np.array(p.pos_list) for p in _p_] |
|
61
|
|
|
self.score_list[self.process.obj_func] = [ |
|
62
|
|
|
np.array(p.score_list) for p in _p_ |
|
63
|
|
|
] |
|
64
|
|
|
else: |
|
65
|
|
|
self.pos_list[self.process.obj_func] = [np.array(_p_.pos_list)] |
|
66
|
|
|
self.score_list[self.process.obj_func] = [np.array(_p_.score_list)] |
|
67
|
|
|
|
|
68
|
|
|
def _run_multiple_jobs(self): |
|
69
|
|
|
_p_list = self._search_multiprocessing() |
|
70
|
|
|
for _ in range(int(self.n_processes / 2) + 2): |
|
71
|
|
|
print("\n") |
|
72
|
|
|
|
|
73
|
|
|
""" |
|
74
|
|
|
for self.process, _p_ in zip(self.processlist, _p_list): |
|
75
|
|
|
self._get_attributes(_p_) |
|
76
|
|
|
""" |
|
77
|
|
|
|
|
78
|
|
|
def _run(self, nth_process): |
|
79
|
|
|
process = self.search_processes[nth_process] |
|
80
|
|
|
return process.search(nth_process) |
|
81
|
|
|
|
|
82
|
|
|
""" |
|
83
|
|
|
|
|
84
|
|
|
def _time_exceeded(self): |
|
85
|
|
|
run_time = time.time() - self.start_time |
|
86
|
|
|
return self.study_para.max_time and run_time > self.study_para.max_time |
|
87
|
|
|
|
|
88
|
|
|
def _initialize_search(self, study_para, nth_process, _info_): |
|
89
|
|
|
study_para._set_random_seed(nth_process) |
|
90
|
|
|
|
|
91
|
|
|
self.process = SearchProcess(nth_process, study_para, _info_) |
|
92
|
|
|
self._pbar_.init_p_bar(nth_process, self.study_para) |
|
93
|
|
|
""" |
|
94
|
|
|
|