|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
import numpy as np |
|
7
|
|
|
import pandas as pd |
|
8
|
|
|
|
|
9
|
|
|
from multiprocessing import Pool |
|
10
|
|
|
from importlib import import_module |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class SearchBase: |
|
14
|
|
|
def __init__(self, function_parameter, search_processes): |
|
15
|
|
|
self.function_parameter = function_parameter |
|
16
|
|
|
self.search_processes = search_processes |
|
17
|
|
|
self.n_processes = len(search_processes) |
|
18
|
|
|
self._n_process_range = range(0, self.n_processes) |
|
19
|
|
|
|
|
20
|
|
|
self.obj_functions = self._uniques_obj_func(search_processes) |
|
21
|
|
|
|
|
22
|
|
|
self.results = {} |
|
23
|
|
|
self.eval_times = {} |
|
24
|
|
|
self.iter_times = {} |
|
25
|
|
|
self.best_scores = {} |
|
26
|
|
|
self.pos_list = {} |
|
27
|
|
|
self.score_list = {} |
|
28
|
|
|
self.position_results = {} |
|
29
|
|
|
|
|
30
|
|
|
def _uniques_obj_func(self, search_processes): |
|
31
|
|
|
self.obj_func_list = [] |
|
32
|
|
|
for process in search_processes: |
|
33
|
|
|
self.obj_func_list.append(process.objective_function) |
|
34
|
|
|
|
|
35
|
|
|
return set(self.obj_func_list) |
|
36
|
|
|
|
|
37
|
|
|
def _get_results(self): |
|
38
|
|
|
position_results_dict = {} |
|
39
|
|
|
|
|
40
|
|
|
eval_times_dict = {} |
|
41
|
|
|
iter_times_dict = {} |
|
42
|
|
|
|
|
43
|
|
|
score_best_dict = {} |
|
44
|
|
|
para_best_dict = {} |
|
45
|
|
|
|
|
46
|
|
|
score_best = -np.inf |
|
47
|
|
|
para_best = None |
|
48
|
|
|
for objective_function in self.obj_func_set: |
|
49
|
|
|
for process in self.search_processes: |
|
50
|
|
|
if objective_function != process.objective_function: |
|
51
|
|
|
continue |
|
52
|
|
|
|
|
53
|
|
|
if score_best < process.cand.score_best: |
|
54
|
|
|
score_best = process.cand.score_best |
|
55
|
|
|
para_best = process.cand.para_best |
|
56
|
|
|
|
|
57
|
|
|
score_best_dict[objective_function] = score_best |
|
58
|
|
|
para_best_dict[objective_function] = para_best |
|
59
|
|
|
|
|
60
|
|
|
def _print_best_para(self): |
|
61
|
|
|
for _ in range(int(self.n_processes / 2) + 2): |
|
62
|
|
|
print("\n") # make room in cmd for prints |
|
63
|
|
|
for process in self.search_processes: |
|
64
|
|
|
process.print_best_para() |
|
65
|
|
|
|
|
66
|
|
|
def _run_job(self, nth_process): |
|
67
|
|
|
self.process = self.search_processes[nth_process] |
|
68
|
|
|
return self.process.search(self.start_time, self.max_time, nth_process) |
|
69
|
|
|
|
|
70
|
|
|
def _run_multiple_jobs(self): |
|
71
|
|
|
"""Wrapper for the parallel search. Passes integer that corresponds to process number""" |
|
72
|
|
|
pool = Pool(self.n_processes) |
|
73
|
|
|
results_list = pool.map(self._run_job, self._n_process_range) |
|
74
|
|
|
|
|
75
|
|
|
return results_list |
|
76
|
|
|
|
|
77
|
|
|
def _memory_dict2dataframe(self, results_dict): |
|
78
|
|
|
memory_dict = results_dict["memory"] |
|
79
|
|
|
tuple_list = list(memory_dict.keys()) |
|
80
|
|
|
result_list = list(memory_dict.values()) |
|
81
|
|
|
|
|
82
|
|
|
results_df = pd.DataFrame(result_list) |
|
83
|
|
|
np_pos = np.array(tuple_list) |
|
84
|
|
|
|
|
85
|
|
|
columns = list(results_dict["search_space"].keys()) |
|
86
|
|
|
columns = [col + ".index" for col in columns] |
|
87
|
|
|
pd_pos = pd.DataFrame(np_pos, columns=columns) |
|
88
|
|
|
|
|
89
|
|
|
results = pd.concat([pd_pos, results_df], axis=1) |
|
90
|
|
|
|
|
91
|
|
|
return results |
|
92
|
|
|
|
|
93
|
|
|
def _run(self, start_time, max_time): |
|
94
|
|
|
self.start_time = start_time |
|
95
|
|
|
self.max_time = max_time |
|
96
|
|
|
|
|
97
|
|
|
if len(self.search_processes) == 1: |
|
98
|
|
|
results_list = [self._run_job(0)] |
|
99
|
|
|
else: |
|
100
|
|
|
results_list = self._run_multiple_jobs() |
|
101
|
|
|
|
|
102
|
|
|
return results_list |
|
103
|
|
|
|
|
104
|
|
|
""" |
|
105
|
|
|
for result in results_list: |
|
106
|
|
|
# print("\n result \n", result) |
|
107
|
|
|
|
|
108
|
|
|
self._memory_dict2dataframe(result) |
|
109
|
|
|
|
|
110
|
|
|
# self._store_memory(result["memory"]) |
|
111
|
|
|
# self._print_best_para() |
|
112
|
|
|
""" |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
class Search(SearchBase): |
|
116
|
|
|
def __init__(self, function_parameter, search_processes): |
|
117
|
|
|
super().__init__(function_parameter, search_processes) |
|
118
|
|
|
|
|
119
|
|
|
def run(self, start_time, max_time): |
|
120
|
|
|
self._run(start_time, max_time) |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
class SearchLongTermMemory(Search): |
|
124
|
|
|
def __init__(self, function_parameter, search_processes): |
|
125
|
|
|
super().__init__(function_parameter, search_processes) |
|
126
|
|
|
self._load_memory() |
|
127
|
|
|
|
|
128
|
|
|
def _load_memory(self): |
|
129
|
|
|
for process in self.search_processes: |
|
130
|
|
|
process.cand.memory_dict = process.res.load_long_term_memory() |
|
131
|
|
|
|
|
132
|
|
|
def _save_memory(self): |
|
133
|
|
|
for process in self.search_processes: |
|
134
|
|
|
process.res.save_long_term_memory() |
|
135
|
|
|
|
|
136
|
|
|
def run(self, start_time, max_time): |
|
137
|
|
|
self._run(start_time, max_time) |
|
138
|
|
|
self._save_memory() |
|
139
|
|
|
|
|
140
|
|
|
|