1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import time |
6
|
|
|
|
7
|
|
|
from importlib import import_module |
8
|
|
|
|
9
|
|
|
import multiprocessing |
10
|
|
|
from .verbosity import Verbosity |
11
|
|
|
|
12
|
|
|
from .checks import check_args |
13
|
|
|
|
14
|
|
|
search_process_dict = { |
15
|
|
|
False: "SearchProcessNoMem", |
16
|
|
|
"short": "SearchProcessShortMem", |
17
|
|
|
"long": "SearchProcessLongMem", |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
search_dict = { |
21
|
|
|
False: "Search", |
22
|
|
|
"short": "Search", |
23
|
|
|
"long": "SearchLongTermMemory", |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def set_n_jobs(n_jobs): |
28
|
|
|
"""Sets the number of jobs to run in parallel""" |
29
|
|
|
num_cores = multiprocessing.cpu_count() |
30
|
|
|
if n_jobs == -1 or n_jobs > num_cores: |
31
|
|
|
return num_cores |
32
|
|
|
else: |
33
|
|
|
return n_jobs |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def get_class(file_path, class_name): |
37
|
|
|
module = import_module(file_path, "hyperactive") |
38
|
|
|
return getattr(module, class_name) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
class Optimizer: |
42
|
|
|
def __init__( |
43
|
|
|
self, |
44
|
|
|
random_state=None, |
45
|
|
|
verbosity=3, |
46
|
|
|
warnings=False, |
47
|
|
|
ext_warnings=False, |
48
|
|
|
hyperactive=False, |
49
|
|
|
): |
50
|
|
|
self.verb = Verbosity(verbosity, warnings) |
51
|
|
|
self.random_state = random_state |
52
|
|
|
self.hyperactive = hyperactive |
53
|
|
|
self.search_processes = [] |
54
|
|
|
|
55
|
|
|
def _add_process( |
56
|
|
|
self, |
57
|
|
|
nth_process, |
58
|
|
|
objective_function, |
59
|
|
|
search_space, |
60
|
|
|
n_iter, |
61
|
|
|
function_parameter, |
62
|
|
|
optimizer, |
63
|
|
|
n_jobs, |
64
|
|
|
init_para, |
65
|
|
|
memory, |
66
|
|
|
): |
67
|
|
|
search_process_kwargs = { |
68
|
|
|
"nth_process": nth_process, |
69
|
|
|
"verb": self.verb, |
70
|
|
|
"objective_function": objective_function, |
71
|
|
|
"search_space": search_space, |
72
|
|
|
"n_iter": n_iter, |
73
|
|
|
"function_parameter": function_parameter, |
74
|
|
|
"optimizer": optimizer, |
75
|
|
|
"n_jobs": n_jobs, |
76
|
|
|
"init_para": init_para, |
77
|
|
|
"memory": memory, |
78
|
|
|
"hyperactive": self.hyperactive, |
79
|
|
|
"random_state": self.random_state, |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
SearchProcess = get_class(".search_process", search_process_dict[memory]) |
83
|
|
|
new_search_process = SearchProcess(**search_process_kwargs) |
84
|
|
|
self.search_processes.append(new_search_process) |
85
|
|
|
|
86
|
|
|
def add_search( |
87
|
|
|
self, |
88
|
|
|
objective_function, |
89
|
|
|
search_space, |
90
|
|
|
n_iter=10, |
91
|
|
|
function_parameter=None, |
92
|
|
|
optimizer="RandomSearch", |
93
|
|
|
n_jobs=1, |
94
|
|
|
init_para=[], |
95
|
|
|
memory="short", |
96
|
|
|
): |
97
|
|
|
|
98
|
|
|
check_args( |
99
|
|
|
objective_function, |
100
|
|
|
search_space, |
101
|
|
|
n_iter, |
102
|
|
|
function_parameter, |
103
|
|
|
optimizer, |
104
|
|
|
n_jobs, |
105
|
|
|
init_para, |
106
|
|
|
memory, |
107
|
|
|
) |
108
|
|
|
|
109
|
|
|
n_jobs = set_n_jobs(n_jobs) |
110
|
|
|
|
111
|
|
|
for nth_job in range(n_jobs): |
112
|
|
|
self._add_process( |
113
|
|
|
nth_job, |
114
|
|
|
objective_function, |
115
|
|
|
search_space, |
116
|
|
|
n_iter, |
117
|
|
|
function_parameter, |
118
|
|
|
optimizer, |
119
|
|
|
n_jobs, |
120
|
|
|
init_para, |
121
|
|
|
memory, |
122
|
|
|
) |
123
|
|
|
|
124
|
|
|
Search = get_class(".search", search_dict[memory]) |
125
|
|
|
self.search = Search(function_parameter, self.search_processes) |
126
|
|
|
|
127
|
|
|
def run(self, max_time=None, distribution=None): |
128
|
|
|
if max_time is not None: |
129
|
|
|
max_time = max_time * 60 |
130
|
|
|
|
131
|
|
|
start_time = time.time() |
132
|
|
|
|
133
|
|
|
self.search.run(start_time, max_time) |
134
|
|
|
|
135
|
|
|
self.position_results = self.search.position_results |
136
|
|
|
self.eval_times = self.search.eval_times |
137
|
|
|
self.iter_times = self.search.iter_times |
138
|
|
|
self.best_para = self.search.results |
139
|
|
|
self.best_score = self.search.results |
140
|
|
|
|
141
|
|
|
|