-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmwis_base_call_v2.py
executable file
·657 lines (587 loc) · 25.3 KB
/
mwis_base_call_v2.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# python3
# Make this standard template for testing and training
from __future__ import division
from __future__ import print_function
import sys
import os
import shutil
sys.path.append('%s/gcn' % os.path.dirname(os.path.realpath(__file__)))
# add the libary path for graph reduction and local search
# sys.path.append( '%s/kernel' % os.path.dirname(os.path.realpath(__file__)) )
import time
import random
import scipy.io as sio
import numpy as np
import scipy.sparse as sp
from multiprocessing import Queue
from copy import deepcopy
import networkx as nx
import tensorflow as tf
from collections import deque
from gcn.utils import *
# import the libary for graph reduction and local search
# from reduce_lib import reducelib
import warnings
warnings.filterwarnings('ignore')
from runtime_config import flags, FLAGS
# Settings (FLAGS)
from heuristics import *
if not hasattr(flags.FLAGS, 'epsilon'):
flags.DEFINE_float('epsilon', 1.0, 'initial exploration rate')
if not hasattr(flags.FLAGS, 'epsilon_min'):
flags.DEFINE_float('epsilon_min', 0.001, 'minimal exploration rate')
if not hasattr(flags.FLAGS, 'epsilon_decay'):
flags.DEFINE_float('epsilon_decay', 0.985, 'exploration rate decay per replay')
if not hasattr(flags.FLAGS, 'gamma'):
flags.DEFINE_float('gamma', 1.0, 'gamma')
# Some preprocessing
num_supports = 1 + FLAGS.max_degree
nsr = np.power(10.0, -FLAGS.snr_db / 20.0)
class MWISSolver(object):
def __init__(self, input_flags, memory_size):
self.feature_size = input_flags.feature_size
self.memory = deque(maxlen=memory_size)
self.reward_mem = deque(maxlen=memory_size)
self.flags = input_flags
self.delta = 0.000001 # prevent empty solution
self.gamma = self.flags.gamma # discount rate
self.epsilon = self.flags.epsilon # exploration rate
self.epsilon_min = self.flags.epsilon_min
self.epsilon_decay = self.flags.epsilon_decay
self.learning_rate = self.flags.learning_rate
self.sess = None
# self.writer = tf.summary.create_file_writer('./logs/metrics', max_queue=10000)
self.saver = None
def _build_model(self):
raise NotImplementedError
def makestate(self, adj, wts_nn):
reduced_nn = wts_nn.shape[0]
norm_wts = np.amax(wts_nn) + 1e-9
if self.flags.predict == 'mwis':
features = np.ones([reduced_nn, self.flags.feature_size])
else:
features = np.multiply(np.ones([reduced_nn, self.flags.feature_size]), wts_nn / norm_wts)
features_raw = features.copy()
features = sp.lil_matrix(features)
if self.flags.predict == 'mwis':
features = preprocess_features(features)
else:
features = sparse_to_tuple(features)
support = simple_polynomials(adj, self.flags.max_degree)
state = {"features": features, "support": support, "features_raw": features_raw}
return state
def act(self, state, train):
raise NotImplementedError
def predict(self, state):
raise NotImplementedError
def memorize(self, state, act_vals, solu, next_state, reward):
self.memory.append((state.copy(), act_vals.copy(), solu.copy(), next_state.copy(), reward))
self.reward_mem.append(reward)
def load(self, name):
ckpt = tf.train.get_checkpoint_state(name)
if ckpt:
with self.sess.as_default():
self.saver.restore(self.sess, ckpt.model_checkpoint_path)
print('loaded ' + ckpt.model_checkpoint_path)
def save(self, name):
with self.sess.as_default():
self.saver.save(self.sess, os.path.join(name, "model.ckpt"))
def copy_model_parameters(self, estimator1, estimator2):
"""
Copies the model parameters of one estimator to another.
Args:
sess: Tensorflow session instance
estimator1: Estimator to copy the paramters from
estimator2: Estimator to copy the parameters to
"""
e1_params = [t for t in tf.compat.v1.trainable_variables() if t.name.startswith(estimator1)]
e1_params = sorted(e1_params, key=lambda v: v.name)
e2_params = [t for t in tf.compat.v1.trainable_variables() if t.name.startswith(estimator2)]
e2_params = sorted(e2_params, key=lambda v: v.name)
update_ops = []
for e1_v, e2_v in zip(e1_params, e2_params):
op = e2_v.assign(e1_v)
update_ops.append(op)
self.sess.run(update_ops)
def mellowmax(self, q_vec, omega, beta):
c = np.max(q_vec)
a_size = np.size(q_vec)
mellow = c + np.log(np.sum(np.exp(omega * (q_vec - c))) / a_size) / omega
# ans = np.sum(np.exp((q_vec-mellow)*beta)*(q_vec-mellow))
return mellow
def utility(self, adj_0, wts_0, train=False):
"""
GCN for per utility function
"""
adj = adj_0.copy()
wts_nn = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
# GCN
state = self.makestate(adj, wts_nn)
act_vals = self.act(state, train)
gcn_wts = act_vals
return gcn_wts, state
def schedule(self, adj_0, wts_0, train=False):
"""
GCN followed by LGS
"""
adj = adj_0.copy()
wts_nn = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
# GCN
state = self.makestate(adj, wts_nn)
act_vals_t, act = self.act(state, train)
act_vals = act_vals_t.numpy()
if self.flags.predict == 'mwis':
# gcn_wts = np.divide(wts_nn.flatten(), act_vals.flatten()+1e-8)
gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
# gcn_wts = act_vals.flatten()+100
else:
gcn_wts = act_vals.flatten()
# gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
# gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten()) + wts_nn.flatten()
mwis, _ = local_greedy_search(adj, gcn_wts)
# mwis, _ = greedy_search(adj, gcn_wts)
solu = list(mwis)
mwis_rt = mwis
total_wt = np.sum(wts_nn[solu, 0])
return mwis_rt, total_wt, state, act_vals
def topology_encode(self, adj_0, wts_0, train=False):
# buffer = deque(maxlen=20)
adj = adj_0.copy()
wts_nn = np.reshape(wts_0, (wts_0.size, 1))
# GCN
state = self.makestate(adj, wts_nn)
act_vals, action = self.act(state, train)
return act_vals.numpy(), action
def solve_mwis(self, adj_0, wts_0, train=False, grd=1.0):
"""
GCN followed by LGS
"""
adj = adj_0.copy()
wts_nn = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
# GCN
state = self.makestate(adj, wts_nn)
act_vals_t, act = self.act(state, train)
act_vals = act_vals_t.numpy()
if self.flags.predict == 'mwis':
# gcn_wts = np.divide(wts_nn.flatten(), act_vals.flatten()+1e-8)
gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
# gcn_wts = act_vals.flatten()+100
else:
gcn_wts = act_vals.flatten()
# gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
# gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten()) + wts_nn.flatten()
mwis, _ = local_greedy_search(adj, gcn_wts)
# mwis, _ = greedy_search(adj, gcn_wts)
solu = list(mwis)
mwis_rt = mwis
total_wt = np.sum(wts_nn[solu, 0])
if train:
# wts_norm = wts_nn[list(sol_gd), :]/greedy_util.flatten()
# self.memorize(state.copy(), act_vals.copy(), list(sol_gd), wts_norm, 1.0)
# reward = (total_wt + self.smallconst) / (greedy_util.flatten()[0] + self.smallconst)
reward = total_wt / (grd + 1e-6)
# reward = reward if reward > 0 else 0
wts_norm = wts_nn / np.amax(wts_nn)
if not np.isnan(reward):
self.memorize(state.copy(), act_vals.copy(), list(mwis), {}, reward)
return mwis_rt, total_wt
return mwis_rt, total_wt
def solve_mwis_util(self, adj_0, wts_0, wts_u, train=False, grd=1.0):
"""
This function is to be compatible for utility learning
"""
# buffer = deque(maxlen=20)
adj = adj_0.copy()
wts_nn = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
# GCN
state = self.makestate(adj, wts_nn)
act_vals, act = self.act(state, train)
if self.flags.predict == 'mwis':
# gcn_wts = np.divide(wts_nn.flatten(), act_vals.flatten()+1e-8)
gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
# gcn_wts = act_vals.flatten()+100
else:
gcn_wts = act_vals.flatten()
# gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
# gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten()) + wts_nn.flatten()
mwis, _ = local_greedy_search(adj, gcn_wts)
# mwis, _ = greedy_search(adj, gcn_wts)
solu = list(mwis)
mwis_rt = mwis
total_wt = np.sum(wts_u[solu])
if train:
# wts_norm = wts_nn[list(sol_gd), :]/greedy_util.flatten()
# self.memorize(state.copy(), act_vals.copy(), list(sol_gd), wts_norm, 1.0)
# reward = (total_wt + self.smallconst) / (greedy_util.flatten()[0] + self.smallconst)
reward = total_wt / (grd + 1e-6)
# reward = reward if reward > 0 else 0
if self.flags.predict == 'mwis':
wts_norm = wts_nn / grd
else:
wts_norm = wts_nn
if not np.isnan(reward):
self.memorize(state.copy(), act_vals.copy(), list(mwis), wts_u, reward)
return mwis_rt, total_wt
return mwis_rt, total_wt
def solve_mwis_dit(self, adj_0, wts_0, train=False, grd=1.0):
"""
GCN embedded into LGS iteration
"""
wts = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
it_cnt = 0
best_IS_util = np.array([0.0])
reduced_nn = adj_0.shape[0]
nIS_vec = -np.ones_like(wts).flatten()
while reduced_nn > 0:
it_cnt += 1
remain_vec = (nIS_vec == -1)
reverse_mapping = np.argwhere((nIS_vec == -1))
reverse_mapping = reverse_mapping[:, 0]
adj_nn = adj_0.copy()
adj_nn = adj_nn[remain_vec, :]
adj_nn = adj_nn[:, remain_vec]
wts_nn = wts.copy()
wts_nn = wts_nn[remain_vec, :]
if np.sum(wts_nn) <= 0:
break
# GCN
state = self.makestate(adj_nn, wts_nn)
act_vals, act = self.act(state, train)
if self.flags.predict == 'mwis':
gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
else:
gcn_wts = act_vals.flatten()
# 1-step LGS
sol_part, _, nb_is = local_greedy_search_nstep(adj_nn, gcn_wts, nstep=1)
# post proc
nIS_vec[reverse_mapping[list(sol_part)]] = 1
nIS_vec[reverse_mapping[list(nb_is)]] = 0
best_IS_util = np.dot(nIS_vec, wts)
reduced_nn = np.sum((nIS_vec == -1))
solu = np.argwhere((nIS_vec == 1))
mwis = set(solu.flatten())
return mwis, best_IS_util
def solve_mwis_cit_wrap(self, adj_0, wts_0, train=False, grd=1.0):
wts = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
g = nx.from_scipy_sparse_matrix(adj_0)
subgraphs = list(nx.connected_components(g))
best_IS_util = np.array([0.0])
nIS_vec = -np.ones_like(wts).flatten()
for subgraph in subgraphs:
subgraph = list(subgraph)
sub_vec = np.zeros_like(wts, dtype=np.bool).flatten()
sub_vec[subgraph] = True
adj_sub = adj_0.copy()
adj_sub = adj_sub[sub_vec, :]
adj_sub = adj_sub[:, sub_vec]
wts_sub = wts.copy()
wts_sub = wts_sub[sub_vec, :]
mwis_sub, util_sub = self.solve_mwis_cit(adj_sub, wts_sub, train=train, grd=grd)
best_IS_util += util_sub
mwis_idx = list(mwis_sub)
mwis_map = [subgraph[i] for i in mwis_idx]
nIS_vec[mwis_map] = 1
solu = np.argwhere((nIS_vec == 1))
mwis = set(solu.flatten())
return mwis, best_IS_util
def solve_mwis_cit(self, adj_0, wts_0, train=False, grd=1.0):
"""
GCN in combining with centralized greedy iteration
"""
wts = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
it_cnt = 0
best_IS_util = np.array([0.0])
reduced_nn = adj_0.shape[0]
nIS_vec = -np.ones_like(wts).flatten()
while reduced_nn > 0:
it_cnt += 1
remain_vec = (nIS_vec == -1)
reverse_mapping = np.argwhere((nIS_vec == -1))
reverse_mapping = reverse_mapping[:, 0]
adj_nn = adj_0.copy()
adj_nn = adj_nn[remain_vec, :]
adj_nn = adj_nn[:, remain_vec]
wts_nn = wts.copy()
wts_nn = wts_nn[remain_vec, :]
if np.sum(wts_nn) <= 0:
break
# GCN
state = self.makestate(adj_nn, wts_nn)
act_vals, act = self.act(state, train)
if self.flags.predict == 'mwis':
gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
else:
gcn_wts = act_vals.flatten()
# 1-step CGS
sol_part = np.argmax(gcn_wts)
_, nb_v = np.nonzero(adj_nn[sol_part])
nIS_vec[reverse_mapping[sol_part]] = 1
nIS_vec[reverse_mapping[nb_v]] = 0
best_IS_util = np.dot(nIS_vec, wts)
reduced_nn = np.sum((nIS_vec == -1))
solu = np.argwhere((nIS_vec == 1))
mwis = set(solu.flatten())
return mwis, best_IS_util
def solve_mwis_rollout_wrap(self, adj_0, wts_0, train=False, grd=1.0, b=16):
wts = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
g = nx.from_scipy_sparse_matrix(adj_0)
subgraphs = list(nx.connected_components(g))
best_IS_util = np.array([0.0])
nIS_vec = -np.ones_like(wts).flatten()
for subgraph in subgraphs:
subgraph = list(subgraph)
sub_vec = np.zeros_like(wts, dtype=np.bool).flatten()
sub_vec[subgraph] = True
adj_sub = adj_0.copy()
adj_sub = adj_sub[sub_vec, :]
adj_sub = adj_sub[:, sub_vec]
wts_sub = wts.copy()
wts_sub = wts_sub[sub_vec, :]
mwis_sub, util_sub = self.solve_mwis_rollout1(adj_sub, wts_sub, train=train, grd=grd, b=b)
best_IS_util += util_sub
mwis_idx = list(mwis_sub)
mwis_map = [subgraph[i] for i in mwis_idx]
nIS_vec[mwis_map] = 1
solu = np.argwhere((nIS_vec == 1))
mwis = set(solu.flatten())
return mwis, best_IS_util
def solve_mwis_rollout00(self, adj_0, wts_0, train=False, grd=1.0, b=16):
""" Use top b nodes as branches run rollout """
wts = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
it_cnt = 0
best_IS_util = np.array([0.0])
reduced_nn = adj_0.shape[0]
nIS_vec = -np.ones_like(wts).flatten()
# GCN
state = self.makestate(adj_0, wts)
act_vals, act = self.act(state, train)
while reduced_nn > 0:
it_cnt += 1
remain_vec = (nIS_vec == -1)
reverse_mapping = np.argwhere((nIS_vec == -1))
reverse_mapping = reverse_mapping[:, 0]
adj_nn = adj_0.copy()
adj_nn = adj_nn[remain_vec, :]
adj_nn = adj_nn[:, remain_vec]
wts_nn = wts.copy()
wts_nn = wts_nn[remain_vec, :]
if np.sum(wts_nn) <= 0:
break
if self.flags.predict == 'mwis':
gcn_wts = np.multiply(act_vals[remain_vec, :], wts_nn)
else:
gcn_wts = act_vals[remain_vec, :]
# # Rollout branches
ranks = np.argsort(-gcn_wts.flatten())
# ranks = np.argsort(-act_vals.flatten())
children = ranks[0:b]
scores = wts_nn[children]
if len(scores) > 1:
for i in range(len(children)):
child = children[i]
remain_rollout = np.ones((reduced_nn,), dtype=np.bool)
remain_rollout[child] = False
_, nb_v = np.nonzero(adj_nn[child])
remain_rollout[nb_v] = False
adj_ro = adj_nn[remain_rollout, :]
adj_ro = adj_ro[:, remain_rollout]
wts_ro = wts_nn[remain_rollout]
gw_ro = gcn_wts[remain_rollout]
ps, ss_eval = greedy_search(adj_ro, wts_ro)
ss_eval = np.sum(wts_ro[list(ps)])
scores[i] += ss_eval
# 1-step CGS
i_best = np.random.choice(np.flatnonzero(scores == scores.max()))
# i_best = np.argmax(scores)
sol_part = children[i_best]
_, nb_v = np.nonzero(adj_nn[sol_part])
nIS_vec[reverse_mapping[sol_part]] = 1
nIS_vec[reverse_mapping[nb_v]] = 0
best_IS_util = np.dot(nIS_vec, wts_0)
reduced_nn = np.sum((nIS_vec == -1))
solu = np.argwhere((nIS_vec == 1))
mwis = set(solu.flatten())
return mwis, best_IS_util
def solve_mwis_rollout0(self, adj_0, wts_0, train=False, grd=1.0, b=16):
""" Use top b nodes as branches run rollout """
wts = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
it_cnt = 0
best_IS_util = np.array([0.0])
reduced_nn = adj_0.shape[0]
nIS_vec = -np.ones_like(wts).flatten()
# GCN
state = self.makestate(adj_0, wts)
act_vals, act = self.act(state, train)
while reduced_nn > 0:
it_cnt += 1
remain_vec = (nIS_vec == -1)
reverse_mapping = np.argwhere((nIS_vec == -1))
reverse_mapping = reverse_mapping[:, 0]
adj_nn = adj_0.copy()
adj_nn = adj_nn[remain_vec, :]
adj_nn = adj_nn[:, remain_vec]
wts_nn = wts.copy()
wts_nn = wts_nn[remain_vec, :]
if np.sum(wts_nn) <= 0:
break
if self.flags.predict == 'mwis':
gcn_wts = np.multiply(act_vals[remain_vec, :], wts_nn)
else:
gcn_wts = act_vals[remain_vec, :]
# # Rollout branches
ranks = np.argsort(-gcn_wts.flatten())
# ranks = np.argsort(-act_vals.flatten())
children = ranks[0:b]
scores = wts_nn[children]
if len(scores) > 1:
for i in range(len(children)):
child = children[i]
remain_rollout = np.ones((reduced_nn,), dtype=np.bool)
remain_rollout[child] = False
_, nb_v = np.nonzero(adj_nn[child])
remain_rollout[nb_v] = False
adj_ro = adj_nn[remain_rollout, :]
adj_ro = adj_ro[:, remain_rollout]
wts_ro = wts_nn[remain_rollout]
gw_ro = gcn_wts[remain_rollout]
ps, ss_eval = greedy_search(adj_ro, gw_ro)
ss_eval = np.sum(wts_ro[list(ps)])
scores[i] += ss_eval
# 1-step CGS
i_best = np.random.choice(np.flatnonzero(scores == scores.max()))
# i_best = np.argmax(scores)
sol_part = children[i_best]
_, nb_v = np.nonzero(adj_nn[sol_part])
nIS_vec[reverse_mapping[sol_part]] = 1
nIS_vec[reverse_mapping[nb_v]] = 0
best_IS_util = np.dot(nIS_vec, wts_0)
reduced_nn = np.sum((nIS_vec == -1))
solu = np.argwhere((nIS_vec == 1))
mwis = set(solu.flatten())
return mwis, best_IS_util
def solve_mwis_rollout1(self, adj_0, wts_0, train=False, grd=1.0, b=16):
""" Use top b nodes as branches run rollout """
wts = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
it_cnt = 0
best_IS_util = np.array([0.0])
reduced_nn = adj_0.shape[0]
nIS_vec = -np.ones_like(wts_0).flatten()
# GCN
while reduced_nn > 0:
it_cnt += 1
remain_vec = (nIS_vec == -1)
reverse_mapping = np.argwhere((nIS_vec == -1))
reverse_mapping = reverse_mapping[:, 0]
adj_nn = adj_0.copy()
adj_nn = adj_nn[remain_vec, :]
adj_nn = adj_nn[:, remain_vec]
wts_nn = wts.copy()
wts_nn = wts_nn[remain_vec, :]
if np.sum(wts_nn) <= 0:
break
state = self.makestate(adj_nn, wts_nn)
act_vals, act = self.act(state, train)
if self.flags.predict == 'mwis':
gcn_wts = np.multiply(act_vals, wts_nn)
else:
gcn_wts = act_vals
# # Rollout branches
ranks = np.argsort(-gcn_wts.flatten())
# ranks = np.argsort(-act_vals.flatten())
children = ranks[0:b]
scores = wts_nn[children]
if len(scores) > 1:
for i in range(len(children)):
child = children[i]
remain_rollout = np.ones((reduced_nn,), dtype=np.bool)
remain_rollout[child] = False
_, nb_v = np.nonzero(adj_nn[child])
remain_rollout[nb_v] = False
adj_ro = adj_nn[remain_rollout, :]
adj_ro = adj_ro[:, remain_rollout]
wts_ro = wts_nn[remain_rollout]
gw_ro = gcn_wts[remain_rollout]
ps, ss_eval = greedy_search(adj_ro, gw_ro)
ss_eval = np.sum(wts_ro[list(ps)])
scores[i] += ss_eval
# 1-step CGS
i_best = np.random.choice(np.flatnonzero(scores == scores.max()))
# i_best = np.argmax(scores)
sol_part = children[i_best]
_, nb_v = np.nonzero(adj_nn[sol_part])
nIS_vec[reverse_mapping[sol_part]] = 1
nIS_vec[reverse_mapping[nb_v]] = 0
best_IS_util = np.dot(nIS_vec, wts_0)
reduced_nn = np.sum((nIS_vec == -1))
solu = np.argwhere((nIS_vec == 1))
mwis = set(solu.flatten())
return mwis, best_IS_util
def solve_mwis_rollout(self, adj_0, wts_0, train=False, grd=1.0, b=16):
""" Use top b nodes as branches run rollout """
wts = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
it_cnt = 0
best_IS_util = np.array([0.0])
reduced_nn = adj_0.shape[0]
nIS_vec = -np.ones_like(wts).flatten()
while reduced_nn > 0:
it_cnt += 1
remain_vec = (nIS_vec == -1)
reverse_mapping = np.argwhere((nIS_vec == -1))
reverse_mapping = reverse_mapping[:, 0]
adj_nn = adj_0.copy()
adj_nn = adj_nn[remain_vec, :]
adj_nn = adj_nn[:, remain_vec]
wts_nn = wts.copy()
wts_nn = wts_nn[remain_vec, :]
if np.sum(wts_nn) <= 0:
break
# GCN
state = self.makestate(adj_nn, wts_nn)
act_vals, act = self.act(state, train)
if self.flags.predict == 'mwis':
gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
else:
gcn_wts = act_vals.flatten()
# # Rollout branches
ranks = np.argsort(-gcn_wts.flatten())
# ranks = np.argsort(-act_vals.flatten())
children = ranks[0:b]
scores = wts_nn[children]
if len(scores) > 1:
for i in range(len(children)):
child = children[i]
remain_rollout = np.ones((reduced_nn,), dtype=np.bool)
remain_rollout[child] = False
_, nb_v = np.nonzero(adj_nn[child])
remain_rollout[nb_v] = False
adj_ro = adj_nn[remain_rollout, :]
adj_ro = adj_ro[:, remain_rollout]
wts_ro = wts_nn[remain_rollout]
if np.sum(remain_rollout) > 1:
_, ss_eval = self.solve_mwis(adj_ro, wts_ro)
elif np.sum(remain_rollout) == 1:
ss_eval = wts_ro[0]
else:
ss_eval = 0.0
# _, ss_eval = greedy_search(adj_ro, wts_ro)
scores[i] += ss_eval
# 1-step CGS
i_best = np.random.choice(np.flatnonzero(scores == scores.max()))
# i_best = np.argmax(scores)
sol_part = children[i_best]
_, nb_v = np.nonzero(adj_nn[sol_part])
nIS_vec[reverse_mapping[sol_part]] = 1
nIS_vec[reverse_mapping[nb_v]] = 0
best_IS_util = np.dot(nIS_vec, wts)
reduced_nn = np.sum((nIS_vec == -1))
solu = np.argwhere((nIS_vec == 1))
mwis = set(solu.flatten())
return mwis, best_IS_util
N_bd = FLAGS.feature_size
# use gpu 0
os.environ['CUDA_VISIBLE_DEVICES'] = str(0)
# Initialize session
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
# Create model
# dqn_agent = DQNAgent(N_bd, 5000)