forked from aoiang/LaMOO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis_obj_split.py
349 lines (247 loc) · 11.4 KB
/
vis_obj_split.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import matplotlib.patches as mpatches
import pickle
import numpy as np
import matplotlib.pyplot as plt
from utils import latin_hypercube, from_unit_cube, convert_dtype
from botorch.test_functions.multi_objective import VehicleSafety, BraninCurrin, DTLZ2
from botorch.utils.multi_objective.pareto import is_non_dominated
import torch
import json
import os
tkwargs = {
"dtype": torch.double,
"device": torch.device("cuda" if torch.cuda.is_available() else "cpu"),
}
f = BraninCurrin(negate=True).to(**tkwargs)
init_cands = latin_hypercube(300000, f.dim)
init_cands = from_unit_cube(init_cands, f.bounds[0].data.numpy(), f.bounds[1].data.numpy())
init_cands = torch.tensor(init_cands)
if not os.path.exists('./state/samples.json'):
init_points = latin_hypercube(6200, f.dim)
samples = from_unit_cube(init_points, lb=f.bounds[0].data.numpy(), ub=f.bounds[1].data.numpy())
sams = {}
sams['samples'] = samples.tolist()
with open('./state/samples.json', 'w') as file:
json.dump(sams, file)
else:
with open('./state/samples.json', 'r') as file:
samples = json.load(file)['samples']
samples = torch.tensor(samples)
print(type(samples))
print()
objs = f(samples)
objs = torch.tensor(objs)
pareto_mask = is_non_dominated(objs)
x_pareto = samples[pareto_mask]
# print(x_pareto)
x_pareto = x_pareto.cpu().data.numpy()
new_x = []
for x in x_pareto:
if x[0] < 0.4:
new_x.append(x.tolist())
x_pareto = torch.tensor(new_x, dtype=torch.float64)
y_pareto = f(x_pareto)
# print(x_pareto)
def plot1(t=26, spec=None):
samples = []
for iter in range(t):
if spec is not None:
if iter < spec:
continue
with open('./state/selfnodes_' + 'iter_' + str(iter) + '.pkl', 'rb') as f:
selfnodes = pickle.load(f)
cur_bag = []
for node in selfnodes:
if node.is_leaf():
cur_bag.extend(node.bag[1].cpu().data.numpy())
if iter == 0:
samples = torch.tensor(cur_bag)
else:
valid_bag = []
for s1 in cur_bag:
valid = True
for s2 in samples:
if torch.equal(torch.tensor(s1), s2):
valid = False
break
if valid:
# print(s1)
valid_bag.append(s1)
valid_bag = torch.tensor(valid_bag)
# print(valid_bag)
samples = torch.cat([samples, valid_bag])
print('this is iter ', iter)
print(samples)
from matplotlib import rcParams
rcParams['font.family'] = 'sans-serif'
from matplotlib.cm import ScalarMappable
plt.rc('font', size=20)
plt.rc('axes', labelsize=20)
plt.rc('xtick', labelsize=20)
plt.rc('ytick', labelsize=20)
plt.rc('legend', fontsize=22.5)
cm = plt.cm.get_cmap('viridis')
# plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8)
fig, ax = plt.subplots(figsize=(7, 5))
color_id = 0
color_list = ['#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22',
'#17becf', 'silver', 'plum', 'sienna', 'chartreuse', 'black', 'ivory']
batch_number = torch.cat(
[torch.zeros(10), torch.arange(1, 10 + 1).repeat(2, 1).t().reshape(-1)]
).numpy()
plt.scatter(
samples[:, 0].cpu().numpy(), samples[:, 1].cpu().numpy(), c=batch_number, alpha=0.8, cmap='RdBu_r',
)
ax.set_xlabel("Objective 1")
# plt.xlim(-260, 5)
# plt.ylim(-15, 0)
ax.set_ylabel("Objective 2")
norm = plt.Normalize(batch_number.min(), batch_number.max())
# sm = ScalarMappable(norm=norm, cmap=cm)
sm = ScalarMappable(norm=norm, cmap='RdBu_r')
# sm.set_array([])
# fig.subplots_adjust(right=0.9)
cbar_ax = fig.add_axes([0.935, 0.15, 0.01, 0.7])
cbar = fig.colorbar(sm, cax=cbar_ax)
cbar.ax.set_title("Iter")
plt.show()
# fig.savefig('./figs/' + 'iter_' + str(iter) +'_region_split_.png', bbox_inches='tight')
def plot2(t=26, spec=None):
for iter in range(t):
if spec is not None:
if iter < spec:
continue
print('this is iter--------------------------------', iter)
with open('./state/selfnodes_' + 'iter_' + str(iter) + '.pkl', 'rb') as f:
selfnodes = pickle.load(f)
f = BraninCurrin(negate=True).to(**tkwargs)
s = latin_hypercube(1, f.dim)
s = from_unit_cube(s, f.bounds[0].data.numpy(), f.bounds[1].data.numpy())
s = torch.tensor(s)
for node in selfnodes:
s = torch.cat([s, node.bag[0]])
x1o_min, x1o_max = -300.0, 0.0
x2o_min, x2o_max = -15.0, -0.0
h1 = 1.5
h2 = 0.075
xx_obj, yy_obj = np.meshgrid(np.arange(x1o_min, x1o_max, h1),
np.arange(x2o_min, x2o_max, h2))
x1_min, x1_max = 0.0, 1.0
x2_min, x2_max = 0.0, 1.0
h = 0.005
xx, yy = np.meshgrid(np.arange(x1_min, x1_max, h),
np.arange(x2_min, x2_max, h))
mesh_samples = np.c_[xx.ravel(), yy.ravel()]
mesh_samples_obj = f(torch.tensor(mesh_samples)).cpu().data.numpy()
node_samples_index = {}
whole_index = []
for i in range(len(mesh_samples)):
whole_index.append(i)
for node in selfnodes:
node_samples_index[node.get_name()] = []
node_samples_index[selfnodes[0].get_name()] = whole_index
node_mesh_samples = {}
node_mesh_samples[selfnodes[0].get_name()] = mesh_samples
nodes = {}
node_cands = {}
node_cands[selfnodes[0].get_name()] = s
# node_cands[selfnodes[0].get_name()] = init_cands
for node in selfnodes:
nodes[node.get_name()] = node
for node in selfnodes:
path = [node]
cur_node = node
while cur_node.parent:
path.insert(0, nodes[cur_node.get_parent_str()])
cur_node = nodes[cur_node.get_parent_str()]
for p in range(len(path)):
if path[p].get_name() not in node_cands:
if path[p].is_good_kid():
boundary = path[p - 1].classifier.svm
cands = node_cands[path[p - 1].get_name()][
boundary.predict(node_cands[path[p - 1].get_name()]) == 0]
node_cands[path[p].get_name()] = cands
else:
boundary = path[p - 1].classifier.svm
cands = node_cands[path[p - 1].get_name()][
boundary.predict(node_cands[path[p - 1].get_name()]) == 1]
node_cands[path[p].get_name()] = cands
total = 0
for node in selfnodes:
if not node.is_leaf():
index_0 = (node.classifier.svm.predict(node_mesh_samples[node.get_name()]) == 0)
index_1 = (node.classifier.svm.predict(node_mesh_samples[node.get_name()]) == 1)
node_mesh_samples[node.kids[0].get_name()] = node_mesh_samples[node.get_name()][index_0]
node_mesh_samples[node.kids[1].get_name()] = node_mesh_samples[node.get_name()][index_1]
# print('index_0 is', index_0)
# print('index_1 is', index_1)
assert len(index_0) == len(node_samples_index[node.get_name()])
for i in range(len(index_0)):
if index_0[i]:
node_samples_index[node.kids[0].get_name()].append(node_samples_index[node.get_name()][i])
else:
node_samples_index[node.kids[1].get_name()].append(node_samples_index[node.get_name()][i])
assert len(node_samples_index[node.kids[0].get_name()]) == len(node_mesh_samples[node.kids[0].get_name()])
assert len(node_samples_index[node.kids[1].get_name()]) == len(node_mesh_samples[node.kids[1].get_name()])
else:
total += len(node_samples_index[node.get_name()])
from matplotlib import rcParams
rcParams['font.family'] = 'sans-serif'
# rcParams['image.cmap'] = 'viridis'
plt.rc('font', size=20)
plt.rc('axes', labelsize=20)
plt.rc('xtick', labelsize=20)
plt.rc('ytick', labelsize=20)
plt.rc('legend', fontsize=20)
fig, ax = plt.subplots(figsize=(7, 5))
color_id = 0
color_list = ['#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22',
'#17becf', 'silver', 'plum', 'sienna', 'chartreuse', 'black', 'ivory']
plt.scatter(y_pareto[:, 0], y_pareto[:, 1], color='red', marker='*', label='Pareto frontier')
for node in selfnodes:
if node.is_leaf():
best = True
cur_node = node
while cur_node.get_name() != 'node0':
if not cur_node.is_good_kid():
best = False
break
else:
cur_node = cur_node.parent
if best:
print('best node is', node.get_name())
print('cur_color id is', 0)
# print('x is', node_cands[node.get_name()][:, 0])
# plt.scatter(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1],
# cmap=plt.cm.coolwarm, label=node.get_name() + '_is_selected_node')
# plt.scatter(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1],
# label=node.get_name() + '(selected)', c='#1f77b4', alpha=0.0)
obj_space_samples = f(node_cands[node.get_name()])
plt.scatter(obj_space_samples[:, 0], obj_space_samples[:, 1], facecolor='#1f77b4', lw=0, alpha=1)
# plt.scatter(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1],
# label=node.get_name() + '(selected)', c='#1f77b4', alpha=1.0)
# plt.scatter(nodes[node.get_name()].bag[0][:, 0], nodes[node.get_name()].bag[0][:, 1], c='#1f77b4')
else:
# print('x is', node_cands[node.get_name()][:, 0])
print('cur_node is', node.get_name())
print('cur_color id is', color_id+1)
obj_space_samples = f(node_cands[node.get_name()]).cpu().data.numpy()
plt.scatter(obj_space_samples[:, 0], obj_space_samples[:, 1],
c=color_list[color_id], lw=0, alpha=1)
# plt.scatter(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1],
# c=color_list[color_id], label=node.get_name(), alpha=0.5)
# plt.scatter(nodes[node.get_name()].bag[0][:, 0], nodes[node.get_name()].bag[0][:, 1], c=color_list[color_id],
# alpha=1)
color_id += 1
# plt.scatter(self.samples[0][:, 0], self.samples[0][:, 1], c='black', marker='*')
plt.legend()
# plt.xlim(0.0, 1.0)
# plt.ylim(0.0, 1.0)
# plt.xticks(())
# plt.yticks(())
plt.xlabel('objective1')
plt.ylabel('objective2')
fig.show()
# fig.savefig('./figs/' + 'iter_' + str(iter) + 'leaf_objs.png', bbox_inches='tight')
plot1(11)
# plot2(11, spec=10)