-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis_region_split.py
539 lines (434 loc) · 21.4 KB
/
vis_region_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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
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(100000, f.dim)
init_cands = from_unit_cube(init_cands, f.bounds[0].data.numpy(), f.bounds[1].data.numpy())
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)
print(x_pareto)
def plot1(t=26):
for iter in range(t):
with open('./state/selfnodes_' + 'iter_' + str(iter) + '.pkl', 'rb') as f:
selfnodes=pickle.load(f)
x1_min, x1_max = 0.0, 1.0
x2_min, x2_max = 0.0, 1.0
h = 0.001
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()]
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()] = init_cands
# 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()])
Z = []
for i in range(len(mesh_samples)):
Z.append(-1)
color = 1
# if iter == 10:
# print(node_samples_index['node4'])
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
# print('length is', len(node_samples_index[node.get_name()]))
if best:
# print('region node name is', node.get_name())
# print('color is', color) if not best else print('color is', 0)
# for index in node_samples_index[node.get_name()]:
# Z[index] = 0
cur_node = node
while cur_node.get_name() != 'node0':
fig, ax = plt.subplots(figsize=(7, 5))
Z = []
for i in range(len(mesh_samples)):
Z.append(-1)
color = 1
print('region node name is', cur_node.get_name())
print('color is', color) if not best else print('color is', 0)
for index in node_samples_index[cur_node.get_name()]:
Z[index] = 0
Z = np.array(Z)
Z = Z.reshape(xx.shape)
# print(Z)
if iter > 1:
plt.contourf(xx, yy, Z, color,
colors=['white', '#1f77b4'], alpha=0.5)
else:
plt.contourf(xx, yy, Z, color,
colors=['#1f77b4', 'white'], alpha=0.5)
plt.scatter(x_pareto[:, 0], x_pareto[:, 1], color='red', marker='*',
label='Pareto frontier')
plt.xlim(0.0, 1.0)
plt.ylim(0.0, 1.0)
if iter == 10 and cur_node.get_name() == 'node1':
patch = mpatches.Patch(color='#1f77b4', label='Selected space', alpha=0.5)
plt.legend(handles=[patch, a], loc='lower left')
fig.savefig('./figs/' + 'iter_' + str(iter) + cur_node.get_name() + '_.png',
bbox_inches='tight')
cur_node = cur_node.parent
from matplotlib import rcParams
rcParams['font.family'] = 'sans-serif'
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)
# 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']
Z = np.array(Z)
Z = Z.reshape(xx.shape)
# print(Z)
if iter > 1:
plt.contourf(xx, yy, Z, color,
colors=['white', '#1f77b4'], alpha=0.5)
else:
plt.contourf(xx, yy, Z, color,
colors=['#1f77b4', 'white'], alpha=0.5)
# 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:
#
# cur_node = node
# while cur_node.get_name() != 'node0':
#
# fig, ax = plt.subplots(figsize=(7, 5))
# ax.set_facecolor('khaki')
# plt.scatter(node_cands[cur_node.get_name()][:, 0], node_cands[cur_node.get_name()][:, 1],
# c='#1f77b4')
# plt.scatter(x_pareto[:, 0], x_pareto[:, 1], color='red', marker='*',
# label='Pareto frontier')
# plt.xlim(0.0, 1.0)
# plt.ylim(0.0, 1.0)
#
# fig.savefig('./figs/' + 'iter_' + str(iter) + cur_node.get_name() + '_.png',
# bbox_inches='tight')
# cur_node = cur_node.parent
#
#
# fig, ax = plt.subplots(figsize=(7, 5))
#
# # ax.set_facecolor('khaki')
# plt.scatter(node_cands[cur_node.get_name()][:, 0], node_cands[cur_node.get_name()][:, 1],
# c='#1f77b4')
# plt.scatter(x_pareto[:, 0], x_pareto[:, 1], color='red', marker='*',
# label='Pareto frontier')
# plt.xlim(0.0, 1.0)
# plt.ylim(0.0, 1.0)
#
# fig.savefig('./figs/' + 'iter_' + str(iter) + cur_node.get_name() + '_.png',
# bbox_inches='tight')
#
#
#
#
#
# print('best node is', node.get_name())
# 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')
# Z = np.array(Z)
# fig, ax = plt.subplots(figsize=(7, 5))
# Z = Z.reshape(xx.shape)
# ax.set_facecolor('khaki')
# plt.scatter(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1], c='#1f77b4')
# else:
# fig2, ax2 = plt.subplots(figsize=(7, 5))
# ax2.set_facecolor('khaki')
# plt.scatter(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1], cmap=plt.cm.coolwarm, label=node.get_name())
# plt.scatter(init_cands[:, 0], init_cands[:, 1], cmap=plt.cm.coolwarm)
if iter == 0:
# print("imddfdfd")
a = plt.scatter(x_pareto[:, 0], x_pareto[:, 1], color='red', marker='*', label='Pareto frontier')
patch = mpatches.Patch(color='#1f77b4', label='Selected space', alpha=0.5)
plt.legend(handles=[patch, a], loc='lower left')
else:
plt.scatter(x_pareto[:, 0], x_pareto[:, 1], color='red', marker='*')
# plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xlim(0.0, 1.0)
plt.ylim(0.0, 1.0)
# plt.xticks(())
# plt.yticks(())
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]])
x1_min, x1_max = 0.0, 1.0
x2_min, x2_max = 0.0, 1.0
h = 0.0005
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()]
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()])
Z = []
for i in range(len(mesh_samples)):
Z.append(-1)
color = 1
# if iter == 10:
# print(node_samples_index['node4'])
# 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
# # print('length is', len(node_samples_index[node.get_name()]))
# if best:
# print('region node name is', node.get_name())
# print('color is', color) if not best else print('color is', 0)
# for index in node_samples_index[node.get_name()]:
# Z[index] = 0
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
# print('length is', len(node_samples_index[node.get_name()]))
if not best:
print('region node name is', node.get_name())
print('color is', color) if not best else print('color is', 0)
for index in node_samples_index[node.get_name()]:
Z[index] = color
# if not best:
color += 1
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']
Z = np.array(Z)
Z = Z.reshape(xx.shape)
# print(Z)
plt.contourf(xx, yy, Z, color-1, colors=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22',
'#17becf', 'silver', 'plum', 'sienna', 'chartreuse', 'black', 'ivory'], alpha=0.5)
plt.scatter(x_pareto[:, 0], x_pareto[:, 1], color='black', 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)
# 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(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1],
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)
# if len(node_cands[node.get_name()][:, 0]) > 0:
# plt.scatter(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1],
# label=node.get_name(), c=color_list[color_id], alpha=1.0)
# plt.scatter(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1],
# c=color_list[color_id], label=node.get_name(), alpha=1.0)
plt.scatter(node_cands[node.get_name()][:, 0], node_cands[node.get_name()][:, 1],
c=color_list[color_id], alpha=1.0)
# 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(loc='upper left', bbox_to_anchor=(-0.03, 1.396), ncol=2)
plt.legend()
plt.xlim(0.0, 1.0)
plt.ylim(0.0, 1.0)
# plt.xticks(())
# plt.yticks(())
ax.set_xlabel("x1")
ax.set_ylabel("x2")
plt.show()
# fig.savefig('./figs/' + 'iter_' + str(iter) + 'leaf3.png', bbox_inches='tight')
# plot1(11)
# plot2(11, spec=10)