|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import numpy as np |
|
6
|
|
|
import pandas as pd |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class Converter: |
|
10
|
|
|
def __init__(self, search_space): |
|
11
|
|
|
self.search_space = search_space |
|
12
|
|
|
self.para_names = list(search_space.keys()) |
|
13
|
|
|
self.dim_sizes = np.array( |
|
14
|
|
|
[len(array) - 1 for array in search_space.values()] |
|
15
|
|
|
) |
|
16
|
|
|
|
|
17
|
|
|
def position2value(self, position): |
|
18
|
|
|
value = [] |
|
19
|
|
|
for n, space_dim in enumerate(self.search_space.values()): |
|
20
|
|
|
value.append(space_dim[position[n]]) |
|
21
|
|
|
|
|
22
|
|
|
return np.array(value) |
|
23
|
|
|
|
|
24
|
|
|
def value2position(self, value): |
|
25
|
|
|
position = [] |
|
26
|
|
|
for n, space_dim in enumerate(self.search_space.values()): |
|
27
|
|
|
pos = np.abs(value[n] - space_dim).argmin() |
|
28
|
|
|
position.append(pos) |
|
29
|
|
|
|
|
30
|
|
|
return np.array(position).astype(int) |
|
31
|
|
|
|
|
32
|
|
|
def value2para(self, value): |
|
33
|
|
|
para = {} |
|
34
|
|
|
for key, p_ in zip(self.para_names, value): |
|
35
|
|
|
para[key] = p_ |
|
36
|
|
|
|
|
37
|
|
|
return para |
|
38
|
|
|
|
|
39
|
|
|
def para2value(self, para): |
|
40
|
|
|
value = np.concatenate(list(para.values())) |
|
41
|
|
|
return value |
|
42
|
|
|
|
|
43
|
|
|
def values2positions(self, values): |
|
44
|
|
|
positions_temp = [] |
|
45
|
|
|
values_np = np.array(values) |
|
46
|
|
|
|
|
47
|
|
|
for n, space_dim in enumerate(self.search_space.values()): |
|
48
|
|
|
values_1d = values_np[:, n] |
|
49
|
|
|
m_conv = np.abs(values_1d - space_dim[:, np.newaxis]) |
|
50
|
|
|
pos_list = m_conv.argmin(0) |
|
51
|
|
|
|
|
52
|
|
|
positions_temp.append(pos_list) |
|
53
|
|
|
|
|
54
|
|
|
positions = list(np.array(positions_temp).T.astype(int)) |
|
55
|
|
|
|
|
56
|
|
|
return positions |
|
57
|
|
|
|
|
58
|
|
|
def positions2values(self, positions): |
|
59
|
|
|
values_temp = [] |
|
60
|
|
|
positions_np = np.array(positions) |
|
61
|
|
|
|
|
62
|
|
|
for n, space_dim in enumerate(self.search_space.values()): |
|
63
|
|
|
pos_1d = positions_np[:, n] |
|
64
|
|
|
value_ = np.take(space_dim, pos_1d, axis=0) |
|
65
|
|
|
values_temp.append(value_) |
|
66
|
|
|
|
|
67
|
|
|
values = list(np.array(values_temp).T) |
|
68
|
|
|
return values |
|
69
|
|
|
|
|
70
|
|
|
def positions_scores2memory_dict(self, positions, scores): |
|
71
|
|
|
value_tuple_list = list(map(tuple, positions)) |
|
72
|
|
|
memory_dict = dict(zip(value_tuple_list, scores)) |
|
73
|
|
|
|
|
74
|
|
|
return memory_dict |
|
75
|
|
|
|
|
76
|
|
|
def memory_dict2positions_scores(self, memory_dict): |
|
77
|
|
|
positions = [ |
|
78
|
|
|
np.array(pos).astype(int) for pos in list(memory_dict.keys()) |
|
79
|
|
|
] |
|
80
|
|
|
scores = list(memory_dict.values()) |
|
81
|
|
|
|
|
82
|
|
|
return positions, scores |
|
83
|
|
|
|
|
84
|
|
|
def dataframe2memory_dict(self, dataframe): |
|
85
|
|
|
parameter = set(self.search_space.keys()) |
|
86
|
|
|
memory_para = set(dataframe.columns) |
|
87
|
|
|
|
|
88
|
|
|
if parameter <= memory_para: |
|
89
|
|
|
values = list(dataframe[self.para_names].values) |
|
90
|
|
|
positions = self.values2positions(values) |
|
91
|
|
|
scores = dataframe["score"] |
|
92
|
|
|
|
|
93
|
|
|
memory_dict = self.positions_scores2memory_dict(positions, scores) |
|
94
|
|
|
|
|
95
|
|
|
return memory_dict |
|
96
|
|
|
else: |
|
97
|
|
|
missing = parameter - memory_para |
|
98
|
|
|
|
|
99
|
|
|
print( |
|
100
|
|
|
"\nWarning:", |
|
101
|
|
|
'"{}"'.format(*missing), |
|
102
|
|
|
"is in search_space but not in memory dataframe", |
|
103
|
|
|
) |
|
104
|
|
|
print("Optimization run will continue without memory warm start\n") |
|
105
|
|
|
|
|
106
|
|
|
return {} |
|
107
|
|
|
|
|
108
|
|
|
def memory_dict2dataframe(self, memory_dict): |
|
109
|
|
|
positions, score = self.memory_dict2positions_scores(memory_dict) |
|
110
|
|
|
values = self.positions2values(positions) |
|
111
|
|
|
|
|
112
|
|
|
dataframe = pd.DataFrame(values, columns=self.para_names) |
|
113
|
|
|
dataframe["score"] = score |
|
114
|
|
|
|
|
115
|
|
|
return dataframe |
|
116
|
|
|
|