forked from FFrankyy/FINDER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFINDER.pyx
904 lines (793 loc) · 38.3 KB
/
FINDER.pyx
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 19 00:33:33 2017
@author: fanchangjun
"""
from __future__ import print_function, division
import tensorflow as tf
import numpy as np
import networkx as nx
import random
import time
import pickle as cp
import sys
from tqdm import tqdm
import PrepareBatchGraph
import graph
import nstep_replay_mem
import nstep_replay_mem_prioritized
import mvc_env
import utils
import scipy.linalg as linalg
import os
# Hyper Parameters:
cdef double GAMMA = 1 # decay rate of past observations
cdef int UPDATE_TIME = 1000
cdef int EMBEDDING_SIZE = 64
cdef int MAX_ITERATION = 500000
cdef double LEARNING_RATE = 0.0001 #dai
cdef int MEMORY_SIZE = 500000
cdef double Alpha = 0.001 ## weight of reconstruction loss
########################### hyperparameters for priority(start)#########################################
cdef double epsilon = 0.0000001 # small amount to avoid zero priority
cdef double alpha = 0.6 # [0~1] convert the importance of TD error to priority
cdef double beta = 0.4 # importance-sampling, from initial value increasing to 1
cdef double beta_increment_per_sampling = 0.001
cdef double TD_err_upper = 1. # clipped abs error
########################## hyperparameters for priority(end)#########################################
cdef int N_STEP = 5
cdef int NUM_MIN = 30
cdef int NUM_MAX = 50
cdef int REG_HIDDEN = 32
cdef int BATCH_SIZE = 64
cdef double initialization_stddev = 0.01 # 权重初始化的方差
cdef int n_valid = 200
cdef int aux_dim = 4
cdef int num_env = 1
cdef double inf = 2147483647/2
######################### embedding method ##########################################################
cdef int max_bp_iter = 3
cdef int aggregatorID = 0 #0:sum; 1:mean; 2:GCN
cdef int embeddingMethod = 1 #0:structure2vec; 1:graphsage
class FINDER:
def __init__(self):
# init some parameters
self.embedding_size = EMBEDDING_SIZE
self.learning_rate = LEARNING_RATE
self.g_type = 'barabasi_albert' #erdos_renyi, powerlaw, small-world, barabasi_albert
self.TrainSet = graph.py_GSet()
self.TestSet = graph.py_GSet()
self.inputs = dict()
self.reg_hidden = REG_HIDDEN
self.utils = utils.py_Utils()
############----------------------------- variants of DQN(start) ------------------- ###################################
self.IsHuberloss = False
self.IsDoubleDQN = False
self.IsPrioritizedSampling = False
self.IsMultiStepDQN = True ##(if IsNStepDQN=False, N_STEP==1)
############----------------------------- variants of DQN(end) ------------------- ###################################
#Simulator
self.ngraph_train = 0
self.ngraph_test = 0
self.env_list=[]
self.g_list=[]
self.pred=[]
if self.IsPrioritizedSampling:
self.nStepReplayMem = nstep_replay_mem_prioritized.py_Memory(epsilon,alpha,beta,beta_increment_per_sampling,TD_err_upper,MEMORY_SIZE)
else:
self.nStepReplayMem = nstep_replay_mem.py_NStepReplayMem(MEMORY_SIZE)
for i in range(num_env):
self.env_list.append(mvc_env.py_MvcEnv(NUM_MAX))
self.g_list.append(graph.py_Graph())
self.test_env = mvc_env.py_MvcEnv(NUM_MAX)
# [batch_size, node_cnt]
self.action_select = tf.sparse_placeholder(tf.float32, name="action_select")
# [node_cnt, batch_size]
self.rep_global = tf.sparse_placeholder(tf.float32, name="rep_global")
# [node_cnt, node_cnt]
self.n2nsum_param = tf.sparse_placeholder(tf.float32, name="n2nsum_param")
# [node_cnt, node_cnt]
self.laplacian_param = tf.sparse_placeholder(tf.float32, name="laplacian_param")
# [batch_size, node_cnt]
self.subgsum_param = tf.sparse_placeholder(tf.float32, name="subgsum_param")
# [batch_size,1]
self.target = tf.placeholder(tf.float32, [BATCH_SIZE,1], name="target")
# [batch_size, aux_dim]
self.aux_input = tf.placeholder(tf.float32, name="aux_input")
#[batch_size, 1]
if self.IsPrioritizedSampling:
self.ISWeights = tf.placeholder(tf.float32, [BATCH_SIZE, 1], name='IS_weights')
# init Q network
self.loss,self.trainStep,self.q_pred, self.q_on_all,self.Q_param_list = self.BuildNet() #[loss,trainStep,q_pred, q_on_all, ...]
#init Target Q Network
self.lossT,self.trainStepT,self.q_predT, self.q_on_allT,self.Q_param_listT = self.BuildNet()
#takesnapsnot
self.copyTargetQNetworkOperation = [a.assign(b) for a,b in zip(self.Q_param_listT,self.Q_param_list)]
self.UpdateTargetQNetwork = tf.group(*self.copyTargetQNetworkOperation)
# saving and loading networks
self.saver = tf.train.Saver(max_to_keep=None)
#self.session = tf.InteractiveSession()
config = tf.ConfigProto(device_count={"CPU": 8}, # limit to num_cpu_core CPU usage
inter_op_parallelism_threads=100,
intra_op_parallelism_threads=100,
log_device_placement=False)
config.gpu_options.allow_growth = True
self.session = tf.Session(config = config)
# self.session = tf_debug.LocalCLIDebugWrapperSession(self.session)
self.session.run(tf.global_variables_initializer())
#################################################New code for FINDER#####################################
def BuildNet(self):
# [2, embed_dim]
w_n2l = tf.Variable(tf.truncated_normal([2, self.embedding_size], stddev=initialization_stddev), tf.float32)
# [embed_dim, embed_dim]
p_node_conv = tf.Variable(tf.truncated_normal([self.embedding_size, self.embedding_size], stddev=initialization_stddev), tf.float32)
if embeddingMethod == 1: #'graphsage'
# [embed_dim, embed_dim]
p_node_conv2 = tf.Variable(tf.truncated_normal([self.embedding_size, self.embedding_size], stddev=initialization_stddev), tf.float32)
# [2*embed_dim, embed_dim]
p_node_conv3 = tf.Variable(tf.truncated_normal([2*self.embedding_size, self.embedding_size], stddev=initialization_stddev), tf.float32)
#[reg_hidden+aux_dim, 1]
if self.reg_hidden > 0:
#[2*embed_dim, reg_hidden]
# h1_weight = tf.Variable(tf.truncated_normal([2 * self.embedding_size, self.reg_hidden], stddev=initialization_stddev), tf.float32)
# [embed_dim, reg_hidden]
h1_weight = tf.Variable(tf.truncated_normal([self.embedding_size, self.reg_hidden], stddev=initialization_stddev), tf.float32)
#[reg_hidden1, reg_hidden2]
# h2_weight = tf.Variable(tf.truncated_normal([self.reg_hidden1, self.reg_hidden2], stddev=initialization_stddev), tf.float32)
#[reg_hidden+aux_dim, 1]
h2_weight = tf.Variable(tf.truncated_normal([self.reg_hidden + aux_dim, 1], stddev=initialization_stddev), tf.float32)
#[reg_hidden2 + aux_dim, 1]
last_w = h2_weight
else:
#[2*embed_dim, reg_hidden]
h1_weight = tf.Variable(tf.truncated_normal([2 * self.embedding_size, self.reg_hidden], stddev=initialization_stddev), tf.float32)
# [embed_dim, reg_hidden]
# h1_weight = tf.Variable(tf.truncated_normal([self.embedding_size, self.reg_hidden], stddev=initialization_stddev), tf.float32)
#[2*embed_dim, reg_hidden]
last_w = h1_weight
## [embed_dim, 1]
cross_product = tf.Variable(tf.truncated_normal([self.embedding_size, 1], stddev=initialization_stddev), tf.float32)
#[node_cnt, 2]
nodes_size = tf.shape(self.n2nsum_param)[0]
node_input = tf.ones((nodes_size,2))
y_nodes_size = tf.shape(self.subgsum_param)[0]
# [batch_size, 2]
y_node_input = tf.ones((y_nodes_size,2))
#[node_cnt, 2] * [2, embed_dim] = [node_cnt, embed_dim]
input_message = tf.matmul(tf.cast(node_input,tf.float32), w_n2l)
#[node_cnt, embed_dim] # no sparse
input_potential_layer = tf.nn.relu(input_message)
# # no sparse
# [batch_size, embed_dim]
y_input_message = tf.matmul(tf.cast(y_node_input,tf.float32), w_n2l)
#[batch_size, embed_dim] # no sparse
y_input_potential_layer = tf.nn.relu(y_input_message)
#input_potential_layer = input_message
cdef int lv = 0
#[node_cnt, embed_dim], no sparse
cur_message_layer = input_potential_layer
cur_message_layer = tf.nn.l2_normalize(cur_message_layer, axis=1)
#[batch_size, embed_dim], no sparse
y_cur_message_layer = y_input_potential_layer
# [batch_size, embed_dim]
y_cur_message_layer = tf.nn.l2_normalize(y_cur_message_layer, axis=1)
while lv < max_bp_iter:
lv = lv + 1
#[node_cnt, node_cnt] * [node_cnt, embed_dim] = [node_cnt, embed_dim], dense
n2npool = tf.sparse_tensor_dense_matmul(tf.cast(self.n2nsum_param,tf.float32), cur_message_layer)
#[node_cnt, embed_dim] * [embed_dim, embed_dim] = [node_cnt, embed_dim], dense
node_linear = tf.matmul(n2npool, p_node_conv)
# [batch_size, node_cnt] * [node_cnt, embed_dim] = [batch_size, embed_dim]
y_n2npool = tf.sparse_tensor_dense_matmul(tf.cast(self.subgsum_param,tf.float32), cur_message_layer)
#[batch_size, embed_dim] * [embed_dim, embed_dim] = [batch_size, embed_dim], dense
y_node_linear = tf.matmul(y_n2npool, p_node_conv)
if embeddingMethod == 0: # 'structure2vec'
#[node_cnt, embed_dim] + [node_cnt, embed_dim] = [node_cnt, embed_dim], return tensed matrix
merged_linear = tf.add(node_linear,input_message)
#[node_cnt, embed_dim]
cur_message_layer = tf.nn.relu(merged_linear)
#[batch_size, embed_dim] + [batch_size, embed_dim] = [batch_size, embed_dim], return tensed matrix
y_merged_linear = tf.add(y_node_linear, y_input_message)
#[batch_size, embed_dim]
y_cur_message_layer = tf.nn.relu(y_merged_linear)
else: # 'graphsage'
#[node_cnt, embed_dim] * [embed_dim, embed_dim] = [node_cnt, embed_dim], dense
cur_message_layer_linear = tf.matmul(tf.cast(cur_message_layer, tf.float32), p_node_conv2)
#[[node_cnt, embed_dim] [node_cnt, embed_dim]] = [node_cnt, 2*embed_dim], return tensed matrix
merged_linear = tf.concat([node_linear, cur_message_layer_linear], 1)
#[node_cnt, 2*embed_dim]*[2*embed_dim, embed_dim] = [node_cnt, embed_dim]
cur_message_layer = tf.nn.relu(tf.matmul(merged_linear, p_node_conv3))
#[batch_size, embed_dim] * [embed_dim, embed_dim] = [batch_size, embed_dim], dense
y_cur_message_layer_linear = tf.matmul(tf.cast(y_cur_message_layer, tf.float32), p_node_conv2)
#[[batch_size, embed_dim] [batch_size, embed_dim]] = [batch_size, 2*embed_dim], return tensed matrix
y_merged_linear = tf.concat([y_node_linear, y_cur_message_layer_linear], 1)
#[batch_size, 2*embed_dim]*[2*embed_dim, embed_dim] = [batch_size, embed_dim]
y_cur_message_layer = tf.nn.relu(tf.matmul(y_merged_linear, p_node_conv3))
cur_message_layer = tf.nn.l2_normalize(cur_message_layer, axis=1)
y_cur_message_layer = tf.nn.l2_normalize(y_cur_message_layer, axis=1)
# self.node_embedding = cur_message_layer
#[batch_size, node_cnt] * [node_cnt, embed_dim] = [batch_size, embed_dim], dense
# y_potential = tf.sparse_tensor_dense_matmul(tf.cast(self.subgsum_param,tf.float32), cur_message_layer)
y_potential = y_cur_message_layer
#[batch_size, node_cnt] * [node_cnt, embed_dim] = [batch_size, embed_dim]
action_embed = tf.sparse_tensor_dense_matmul(tf.cast(self.action_select, tf.float32), cur_message_layer)
# embed_s_a = tf.concat([action_embed,y_potential],1)
# # [batch_size, embed_dim, embed_dim]
temp = tf.matmul(tf.expand_dims(action_embed, axis=2),tf.expand_dims(y_potential, axis=1))
# # [batch_size, embed_dim]
Shape = tf.shape(action_embed)
# # [batch_size, embed_dim], first transform
embed_s_a = tf.reshape(tf.matmul(temp, tf.reshape(tf.tile(cross_product,[Shape[0],1]),[Shape[0],Shape[1],1])),Shape)
#[batch_size, embed_dim]
last_output = embed_s_a
if self.reg_hidden > 0:
#[batch_size, 2*embed_dim] * [2*embed_dim, reg_hidden] = [batch_size, reg_hidden], dense
hidden = tf.matmul(embed_s_a, h1_weight)
#[batch_size, reg_hidden]
last_output = tf.nn.relu(hidden)
# if reg_hidden == 0: ,[[batch_size, 2*embed_dim], [batch_size, aux_dim]] = [batch_size, 2*embed_dim+aux_dim]
# if reg_hidden > 0: ,[[batch_size, reg_hidden], [batch_size, aux_dim]] = [batch_size, reg_hidden+aux_dim]
last_output = tf.concat([last_output, self.aux_input], 1)
#if reg_hidden == 0: ,[batch_size, 2*embed_dim+aux_dim] * [2*embed_dim+aux_dim, 1] = [batch_size, 1]
#if reg_hidden > 0: ,[batch_size, reg_hidden+aux_dim] * [reg_hidden+aux_dim, 1] = [batch_size, 1]
q_pred = tf.matmul(last_output, last_w)
## first order reconstruction loss
loss_recons = 2 * tf.trace(tf.matmul(tf.transpose(cur_message_layer), tf.sparse_tensor_dense_matmul(tf.cast(self.laplacian_param,tf.float32), cur_message_layer)))
edge_num = tf.sparse_reduce_sum(tf.cast(self.n2nsum_param, tf.float32))
loss_recons = tf.divide(loss_recons, edge_num)
if self.IsPrioritizedSampling:
self.TD_errors = tf.reduce_sum(tf.abs(self.target - q_pred), axis=1) # for updating Sumtree
if self.IsHuberloss:
loss_rl = tf.losses.huber_loss(self.ISWeights * self.target, self.ISWeights * q_pred)
else:
loss_rl = tf.reduce_mean(self.ISWeights * tf.squared_difference(self.target, q_pred))
else:
if self.IsHuberloss:
loss_rl = tf.losses.huber_loss(self.target, q_pred)
else:
loss_rl = tf.losses.mean_squared_error(self.target, q_pred)
loss = loss_rl + Alpha * loss_recons
trainStep = tf.train.AdamOptimizer(self.learning_rate).minimize(loss)
#[node_cnt, batch_size] * [batch_size, embed_dim] = [node_cnt, embed_dim]
rep_y = tf.sparse_tensor_dense_matmul(tf.cast(self.rep_global, tf.float32), y_potential)
# embed_s_a_all = tf.concat([cur_message_layer,rep_y],1)
# # # [node_cnt, embed_dim, embed_dim]
temp1 = tf.matmul(tf.expand_dims(cur_message_layer, axis=2),tf.expand_dims(rep_y, axis=1))
# # [node_cnt embed_dim]
Shape1 = tf.shape(cur_message_layer)
# # [batch_size, embed_dim], first transform
embed_s_a_all = tf.reshape(tf.matmul(temp1, tf.reshape(tf.tile(cross_product,[Shape1[0],1]),[Shape1[0],Shape1[1],1])),Shape1)
#[node_cnt, 2 * embed_dim]
last_output = embed_s_a_all
if self.reg_hidden > 0:
#[node_cnt, 2 * embed_dim] * [2 * embed_dim, reg_hidden] = [node_cnt, reg_hidden1]
hidden = tf.matmul(embed_s_a_all, h1_weight)
#Relu, [node_cnt, reg_hidden1]
last_output = tf.nn.relu(hidden)
#[node_cnt, reg_hidden1] * [reg_hidden1, reg_hidden2] = [node_cnt, reg_hidden2]
#[node_cnt, batch_size] * [batch_size, aux_dim] = [node_cnt, aux_dim]
rep_aux = tf.sparse_tensor_dense_matmul(tf.cast(self.rep_global, tf.float32), self.aux_input)
#if reg_hidden == 0: , [[node_cnt, 2 * embed_dim], [node_cnt, aux_dim]] = [node_cnt, 2*embed_dim + aux_dim]
#if reg_hidden > 0: , [[node_cnt, reg_hidden], [node_cnt, aux_dim]] = [node_cnt, reg_hidden + aux_dim]
last_output = tf.concat([last_output,rep_aux],1)
#if reg_hidden == 0: , [node_cnt, 2 * embed_dim + aux_dim] * [2 * embed_dim + aux_dim, 1] = [node_cnt,1]
#f reg_hidden > 0: , [node_cnt, reg_hidden + aux_dim] * [reg_hidden + aux_dim, 1] = [node_cnt,1]
q_on_all = tf.matmul(last_output, last_w)
return loss, trainStep, q_pred, q_on_all, tf.trainable_variables()
def gen_graph(self, num_min, num_max):
cdef int max_n = num_max
cdef int min_n = num_min
cdef int cur_n = np.random.randint(max_n - min_n + 1) + min_n
if self.g_type == 'erdos_renyi':
g = nx.erdos_renyi_graph(n=cur_n, p=0.15)
elif self.g_type == 'powerlaw':
g = nx.powerlaw_cluster_graph(n=cur_n, m=4, p=0.05)
elif self.g_type == 'small-world':
g = nx.connected_watts_strogatz_graph(n=cur_n, k=8, p=0.1)
elif self.g_type == 'barabasi_albert':
g = nx.barabasi_albert_graph(n=cur_n, m=4)
return g
def gen_new_graphs(self, num_min, num_max):
print('\ngenerating new training graphs...')
sys.stdout.flush()
self.ClearTrainGraphs()
cdef int i
for i in tqdm(range(1000)):
g = self.gen_graph(num_min, num_max)
self.InsertGraph(g, is_test=False)
def ClearTrainGraphs(self):
self.ngraph_train = 0
self.TrainSet.Clear()
def ClearTestGraphs(self):
self.ngraph_test = 0
self.TestSet.Clear()
def InsertGraph(self,g,is_test):
cdef int t
if is_test:
t = self.ngraph_test
self.ngraph_test += 1
self.TestSet.InsertGraph(t, self.GenNetwork(g))
else:
t = self.ngraph_train
self.ngraph_train += 1
self.TrainSet.InsertGraph(t, self.GenNetwork(g))
def PrepareValidData(self):
print('\ngenerating validation graphs...')
sys.stdout.flush()
cdef double result_degree = 0.0
cdef double result_betweeness = 0.0
for i in tqdm(range(n_valid)):
g = self.gen_graph(NUM_MIN, NUM_MAX)
g_degree = g.copy()
g_betweenness = g.copy()
val_degree, sol = self.HXA(g_degree, 'HDA')
result_degree += val_degree
val_betweenness, sol = self.HXA(g_betweenness, 'HBA')
result_betweeness += val_betweenness
self.InsertGraph(g, is_test=True)
print ('Validation of HDA: %.6f'%(result_degree / n_valid))
print ('Validation of HBA: %.6f'%(result_betweeness / n_valid))
def Run_simulator(self, int num_seq, double eps, TrainSet, int n_step):
cdef int num_env = len(self.env_list)
cdef int n = 0
cdef int i
while n < num_seq:
for i in range(num_env):
if self.env_list[i].graph.num_nodes == 0 or self.env_list[i].isTerminal():
if self.env_list[i].graph.num_nodes > 0 and self.env_list[i].isTerminal():
n = n + 1
self.nStepReplayMem.Add(self.env_list[i], n_step)
#print ('add experience transition!')
g_sample= TrainSet.Sample()
self.env_list[i].s0(g_sample)
self.g_list[i] = self.env_list[i].graph
if n >= num_seq:
break
Random = False
if random.uniform(0,1) >= eps:
pred = self.PredictWithCurrentQNet(self.g_list, [env.action_list for env in self.env_list])
else:
Random = True
for i in range(num_env):
if (Random):
a_t = self.env_list[i].randomAction()
else:
a_t = self.argMax(pred[i])
self.env_list[i].step(a_t)
#pass
def PlayGame(self,int n_traj, double eps):
self.Run_simulator(n_traj, eps, self.TrainSet, N_STEP)
def SetupTrain(self, idxes, g_list, covered, actions, target):
self.m_y = target
self.inputs['target'] = self.m_y
prepareBatchGraph = PrepareBatchGraph.py_PrepareBatchGraph(aggregatorID)
prepareBatchGraph.SetupTrain(idxes, g_list, covered, actions)
self.inputs['action_select'] = prepareBatchGraph.act_select
self.inputs['rep_global'] = prepareBatchGraph.rep_global
self.inputs['n2nsum_param'] = prepareBatchGraph.n2nsum_param
self.inputs['laplacian_param'] = prepareBatchGraph.laplacian_param
self.inputs['subgsum_param'] = prepareBatchGraph.subgsum_param
self.inputs['aux_input'] = prepareBatchGraph.aux_feat
def SetupPredAll(self, idxes, g_list, covered):
prepareBatchGraph = PrepareBatchGraph.py_PrepareBatchGraph(aggregatorID)
prepareBatchGraph.SetupPredAll(idxes, g_list, covered)
self.inputs['rep_global'] = prepareBatchGraph.rep_global
self.inputs['n2nsum_param'] = prepareBatchGraph.n2nsum_param
# self.inputs['laplacian_param'] = prepareBatchGraph.laplacian_param
self.inputs['subgsum_param'] = prepareBatchGraph.subgsum_param
self.inputs['aux_input'] = prepareBatchGraph.aux_feat
return prepareBatchGraph.idx_map_list
def Predict(self,g_list,covered,isSnapSnot):
cdef int n_graphs = len(g_list)
cdef int i, j, k, bsize
for i in range(0, n_graphs, BATCH_SIZE):
bsize = BATCH_SIZE
if (i + BATCH_SIZE) > n_graphs:
bsize = n_graphs - i
batch_idxes = np.zeros(bsize)
for j in range(i, i + bsize):
batch_idxes[j - i] = j
batch_idxes = np.int32(batch_idxes)
idx_map_list = self.SetupPredAll(batch_idxes, g_list, covered)
my_dict = {}
my_dict[self.rep_global] = self.inputs['rep_global']
my_dict[self.n2nsum_param] = self.inputs['n2nsum_param']
my_dict[self.subgsum_param] = self.inputs['subgsum_param']
my_dict[self.aux_input] = np.array(self.inputs['aux_input'])
if isSnapSnot:
result = self.session.run([self.q_on_allT], feed_dict = my_dict)
else:
result = self.session.run([self.q_on_all], feed_dict = my_dict)
raw_output = result[0]
pos = 0
pred = []
for j in range(i, i + bsize):
idx_map = idx_map_list[j-i]
cur_pred = np.zeros(len(idx_map))
for k in range(len(idx_map)):
if idx_map[k] < 0:
cur_pred[k] = -inf
else:
cur_pred[k] = raw_output[pos]
pos += 1
for k in covered[j]:
cur_pred[k] = -inf
pred.append(cur_pred)
assert (pos == len(raw_output))
return pred
def PredictWithCurrentQNet(self,g_list,covered):
result = self.Predict(g_list,covered,False)
return result
def PredictWithSnapshot(self,g_list,covered):
result = self.Predict(g_list,covered,True)
return result
#pass
def TakeSnapShot(self):
self.session.run(self.UpdateTargetQNetwork)
def Fit(self):
sample = self.nStepReplayMem.Sampling(BATCH_SIZE)
ness = False
cdef int i
for i in range(BATCH_SIZE):
if (not sample.list_term[i]):
ness = True
break
if ness:
if self.IsDoubleDQN:
double_list_pred = self.PredictWithCurrentQNet(sample.g_list, sample.list_s_primes)
double_list_predT = self.PredictWithSnapshot(sample.g_list, sample.list_s_primes)
list_pred = [a[self.argMax(b)] for a, b in zip(double_list_predT, double_list_pred)]
else:
list_pred = self.PredictWithSnapshot(sample.g_list, sample.list_s_primes)
list_target = np.zeros([BATCH_SIZE, 1])
for i in range(BATCH_SIZE):
q_rhs = 0
if (not sample.list_term[i]):
if self.IsDoubleDQN:
q_rhs=GAMMA * list_pred[i]
else:
q_rhs=GAMMA * self.Max(list_pred[i])
q_rhs += sample.list_rt[i]
list_target[i] = q_rhs
# list_target.append(q_rhs)
if self.IsPrioritizedSampling:
return self.fit_with_prioritized(sample.b_idx,sample.ISWeights,sample.g_list, sample.list_st, sample.list_at,list_target)
else:
return self.fit(sample.g_list, sample.list_st, sample.list_at,list_target)
def fit_with_prioritized(self,tree_idx,ISWeights,g_list,covered,actions,list_target):
cdef double loss = 0.0
cdef int n_graphs = len(g_list)
cdef int i, j, bsize
for i in range(0,n_graphs,BATCH_SIZE):
bsize = BATCH_SIZE
if (i + BATCH_SIZE) > n_graphs:
bsize = n_graphs - i
batch_idxes = np.zeros(bsize)
# batch_idxes = []
for j in range(i, i + bsize):
batch_idxes[j-i] = j
# batch_idxes.append(j)
batch_idxes = np.int32(batch_idxes)
self.SetupTrain(batch_idxes, g_list, covered, actions,list_target)
my_dict = {}
my_dict[self.action_select] = self.inputs['action_select']
my_dict[self.rep_global] = self.inputs['rep_global']
my_dict[self.n2nsum_param] = self.inputs['n2nsum_param']
my_dict[self.laplacian_param] = self.inputs['laplacian_param']
my_dict[self.subgsum_param] = self.inputs['subgsum_param']
my_dict[self.aux_input] = np.array(self.inputs['aux_input'])
my_dict[self.ISWeights] = np.mat(ISWeights).T
my_dict[self.target] = self.inputs['target']
result = self.session.run([self.trainStep,self.TD_errors,self.loss],feed_dict=my_dict)
self.nStepReplayMem.batch_update(tree_idx, result[1])
loss += result[2]*bsize
return loss / len(g_list)
def fit(self,g_list,covered,actions,list_target):
cdef double loss = 0.0
cdef int n_graphs = len(g_list)
cdef int i, j, bsize
for i in range(0,n_graphs,BATCH_SIZE):
bsize = BATCH_SIZE
if (i + BATCH_SIZE) > n_graphs:
bsize = n_graphs - i
batch_idxes = np.zeros(bsize)
# batch_idxes = []
for j in range(i, i + bsize):
batch_idxes[j-i] = j
# batch_idxes.append(j)
batch_idxes = np.int32(batch_idxes)
self.SetupTrain(batch_idxes, g_list, covered, actions,list_target)
my_dict = {}
my_dict[self.action_select] = self.inputs['action_select']
my_dict[self.rep_global] = self.inputs['rep_global']
my_dict[self.n2nsum_param] = self.inputs['n2nsum_param']
my_dict[self.laplacian_param] = self.inputs['laplacian_param']
my_dict[self.subgsum_param] = self.inputs['subgsum_param']
my_dict[self.aux_input] = np.array(self.inputs['aux_input'])
my_dict[self.target] = self.inputs['target']
result = self.session.run([self.loss,self.trainStep],feed_dict=my_dict)
loss += result[0]*bsize
return loss / len(g_list)
def Train(self):
self.PrepareValidData()
self.gen_new_graphs(NUM_MIN, NUM_MAX)
cdef int i, iter, idx
for i in range(10):
self.PlayGame(100, 1)
self.TakeSnapShot()
cdef double eps_start = 1.0
cdef double eps_end = 0.05
cdef double eps_step = 10000.0
cdef int loss = 0
cdef double frac, start, end
#save_dir = './models/%s'%self.g_type
save_dir = './models/Model_powerlaw'
if not os.path.exists(save_dir):
os.mkdir(save_dir)
VCFile = '%s/ModelVC_%d_%d.csv'%(save_dir, NUM_MIN, NUM_MAX)
f_out = open(VCFile, 'w')
for iter in range(MAX_ITERATION):
start = time.clock()
###########-----------------------normal training data setup(start) -----------------##############################
if iter and iter % 5000 == 0:
self.gen_new_graphs(NUM_MIN, NUM_MAX)
eps = eps_end + max(0., (eps_start - eps_end) * (eps_step - iter) / eps_step)
if iter % 10 == 0:
self.PlayGame(10, eps)
if iter % 300 == 0:
if(iter == 0):
N_start = start
else:
N_start = N_end
frac = 0.0
test_start = time.time()
for idx in range(n_valid):
frac += self.Test(idx)
test_end = time.time()
f_out.write('%.16f\n'%(frac/n_valid)) #write vc into the file
f_out.flush()
print('iter %d, eps %.4f, average size of vc:%.6f'%(iter, eps, frac/n_valid))
print ('testing 200 graphs time: %.2fs'%(test_end-test_start))
N_end = time.clock()
print ('300 iterations total time: %.2fs\n'%(N_end-N_start))
sys.stdout.flush()
model_path = '%s/nrange_%d_%d_iter_%d.ckpt' % (save_dir, NUM_MIN, NUM_MAX, iter)
self.SaveModel(model_path)
if iter % UPDATE_TIME == 0:
self.TakeSnapShot()
self.Fit()
f_out.close()
def findModel(self):
VCFile = './models/%s/ModelVC_%d_%d.csv'%(self.g_type, NUM_MIN, NUM_MAX)
vc_list = []
for line in open(VCFile):
vc_list.append(float(line))
start_loc = 33
min_vc = start_loc + np.argmin(vc_list[start_loc:])
best_model_iter = 300 * min_vc
best_model = './models/%s/nrange_%d_%d_iter_%d.ckpt' % (self.g_type, NUM_MIN, NUM_MAX, best_model_iter)
return best_model
def Evaluate1(self, g, save_dir, model_file=None):
if model_file == None: #if user do not specify the model_file
model_file = self.findModel()
print ('The best model is :%s'%(model_file))
sys.stdout.flush()
self.LoadModel(model_file)
cdef double frac = 0.0
cdef double frac_time = 0.0
result_file = '%s/test.csv' % (save_dir)
with open(result_file, 'w') as f_out:
print ('testing')
sys.stdout.flush()
self.InsertGraph(g, is_test=True)
t1 = time.time()
val, sol = self.GetSol(0)
t2 = time.time()
for i in range(len(sol)):
f_out.write(' %d\n' % sol[i])
frac += val
frac_time += (t2 - t1)
print ('average size of vc: ', frac)
print('average time: ', frac_time)
def Evaluate(self, data_test, model_file=None):
if model_file == None: #if user do not specify the model_file
model_file = self.findModel()
print ('The best model is :%s'%(model_file))
sys.stdout.flush()
self.LoadModel(model_file)
cdef int n_test = 100
cdef int i
result_list_score = []
result_list_time = []
sys.stdout.flush()
for i in tqdm(range(n_test)):
g_path = '%s/'%data_test + 'g_%d'%i
g = nx.read_gml(g_path)
self.InsertGraph(g, is_test=True)
t1 = time.time()
val, sol = self.GetSol(i)
t2 = time.time()
result_list_score.append(val)
result_list_time.append(t2-t1)
self.ClearTestGraphs()
score_mean = np.mean(result_list_score)
score_std = np.std(result_list_score)
time_mean = np.mean(result_list_time)
time_std = np.std(result_list_time)
return score_mean, score_std, time_mean, time_std
def EvaluateRealData(self, model_file, data_test, save_dir, stepRatio=0.0025): #测试真实数据
cdef double solution_time = 0.0
test_name = data_test.split('/')[-1]
save_dir_local = save_dir+'/StepRatio_%.4f'%stepRatio
if not os.path.exists(save_dir_local):#make dir
os.mkdir(save_dir_local)
result_file = '%s/%s' %(save_dir_local, test_name)
g = nx.read_edgelist(data_test)
with open(result_file, 'w') as f_out:
print ('testing')
sys.stdout.flush()
print ('number of nodes:%d'%(nx.number_of_nodes(g)))
print ('number of edges:%d'%(nx.number_of_edges(g)))
if stepRatio > 0:
step = np.max([int(stepRatio*nx.number_of_nodes(g)),1]) #step size
else:
step = 1
self.InsertGraph(g, is_test=True)
t1 = time.time()
solution = self.GetSolution(0,step)
t2 = time.time()
solution_time = (t2 - t1)
for i in range(len(solution)):
f_out.write('%d\n' % solution[i])
self.ClearTestGraphs()
return solution, solution_time
def GetSolution(self, int gid, int step=1):
g_list = []
self.test_env.s0(self.TestSet.Get(gid))
g_list.append(self.test_env.graph)
sol = []
start = time.time()
cdef int iter = 0
cdef int new_action
sum_sort_time = 0
while (not self.test_env.isTerminal()):
print ('Iteration:%d'%iter)
iter += 1
list_pred = self.PredictWithCurrentQNet(g_list, [self.test_env.action_list])
start_time = time.time()
batchSol = np.argsort(-list_pred[0])[:step]
end_time = time.time()
sum_sort_time += (end_time-start_time)
for new_action in batchSol:
if not self.test_env.isTerminal():
self.test_env.stepWithoutReward(new_action)
sol.append(new_action)
else:
continue
return sol
def EvaluateSol(self, data_test, sol_file, strategyID, reInsertStep):
sys.stdout.flush()
g = nx.read_edgelist(data_test)
g_inner = self.GenNetwork(g)
print ('number of nodes:%d'%nx.number_of_nodes(g))
print ('number of edges:%d'%nx.number_of_edges(g))
nodes = list(range(nx.number_of_nodes(g)))
sol = []
for line in open(sol_file):
sol.append(int(line))
print ('number of sol nodes:%d'%len(sol))
sol_left = list(set(nodes)^set(sol))
if strategyID > 0:
start = time.time()
if reInsertStep > 0 and reInsertStep < 1:
step = np.max([int(reInsertStep*nx.number_of_nodes(g)),1]) #step size
else:
step = reInsertStep
sol_reinsert = self.utils.reInsert(g_inner, sol, sol_left, strategyID, step)
end = time.time()
print ('reInsert time:%.6f'%(end-start))
else:
sol_reinsert = sol
solution = sol_reinsert + sol_left
print ('number of solution nodes:%d'%len(solution))
Robustness = self.utils.getRobustness(g_inner, solution)
MaxCCList = self.utils.MaxWccSzList
return Robustness, MaxCCList
def Test(self,int gid):
g_list = []
self.test_env.s0(self.TestSet.Get(gid))
g_list.append(self.test_env.graph)
cdef double cost = 0.0
cdef int i
sol = []
while (not self.test_env.isTerminal()):
# cost += 1
list_pred = self.PredictWithCurrentQNet(g_list, [self.test_env.action_list])
# new_action = self.argMax(list_pred[0])
new_action = self.argMax(list_pred[0])
self.test_env.stepWithoutReward(new_action)
sol.append(new_action)
nodes = list(range(g_list[0].num_nodes))
solution = sol + list(set(nodes)^set(sol))
Robustness = self.utils.getRobustness(g_list[0], solution)
return Robustness
def GetSol(self, int gid, int step=1):
g_list = []
self.test_env.s0(self.TestSet.Get(gid))
g_list.append(self.test_env.graph)
cdef double cost = 0.0
sol = []
cdef int new_action
while (not self.test_env.isTerminal()):
list_pred = self.PredictWithCurrentQNet(g_list, [self.test_env.action_list])
batchSol = np.argsort(-list_pred[0])[:step]
for new_action in batchSol:
if not self.test_env.isTerminal():
self.test_env.stepWithoutReward(new_action)
sol.append(new_action)
else:
break
nodes = list(range(g_list[0].num_nodes))
solution = sol + list(set(nodes)^set(sol))
Robustness = self.utils.getRobustness(g_list[0], solution)
return Robustness, sol
def SaveModel(self,model_path):
self.saver.save(self.session, model_path)
print('model has been saved success!')
def LoadModel(self,model_path):
self.saver.restore(self.session, model_path)
print('restore model from file successfully')
def GenNetwork(self, g): #networkx2four
edges = g.edges()
if len(edges) > 0:
a, b = zip(*edges)
A = np.array(a)
B = np.array(b)
else:
A = np.array([0])
B = np.array([0])
return graph.py_Graph(len(g.nodes()), len(edges), A, B)
def argMax(self, scores):
cdef int n = len(scores)
cdef int pos = -1
cdef double best = -10000000
cdef int i
for i in range(n):
if pos == -1 or scores[i] > best:
pos = i
best = scores[i]
return pos
def Max(self, scores):
cdef int n = len(scores)
cdef int pos = -1
cdef double best = -10000000
cdef int i
for i in range(n):
if pos == -1 or scores[i] > best:
pos = i
best = scores[i]
return best
def HXA(self, g, method):
# 'HDA', 'HBA', 'HPRA', ''
sol = []
G = g.copy()
while (nx.number_of_edges(G)>0):
if method == 'HDA':
dc = nx.degree_centrality(G)
elif method == 'HBA':
dc = nx.betweenness_centrality(G)
elif method == 'HCA':
dc = nx.closeness_centrality(G)
elif method == 'HPRA':
dc = nx.pagerank(G)
keys = list(dc.keys())
values = list(dc.values())
maxTag = np.argmax(values)
node = keys[maxTag]
sol.append(int(node))
G.remove_node(node)
solution = sol + list(set(g.nodes())^set(sol))
solutions = [int(i) for i in solution]
Robustness = self.utils.getRobustness(self.GenNetwork(g), solutions)
return Robustness, sol