-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmwcds_gcn_test.py
executable file
·187 lines (163 loc) · 6.55 KB
/
mwcds_gcn_test.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
# 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__)) )
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
from scipy.stats.stats import pearsonr, linregress
from scipy.spatial import distance_matrix
import tensorflow as tf
import networkx as nx
from collections import deque
import warnings
warnings.filterwarnings('ignore')
from gcn.utils import *
# Settings (FLAGS)
from runtime_config import flags, FLAGS
from heuristics_mwcds import *
flags.DEFINE_string('gtype', 'ba', 'test graph type: er, grp, ws, ba')
flags.DEFINE_string('test_datapath', './data/ER_Graph_Uniform_NP20_test', 'test dataset')
flags.DEFINE_integer('ntrain', 1, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('nvalid', 100, 'Number of outputs.')
from mwcds_gcn_call_twin import DPGAgent, heuristic_func
# Get a list of available GPUs
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
# Set the number of GPUs to use
num_gpus = len(gpus)
# Set up a MirroredStrategy to use all available GPUs
if num_gpus > 1:
strategy = tf.distribute.MirroredStrategy(devices=["/gpu:%d" % i for i in range(num_gpus)])
else:
strategy = tf.distribute.get_strategy() # default strategy
# Define and compile your model within the strategy scope
with strategy.scope():
dqn_agent = DPGAgent(FLAGS, 5000)
# test data path
data_path = FLAGS.datapath
test_datapath = FLAGS.test_datapath
# Some preprocessing
noout = min(FLAGS.diver_num, FLAGS.diver_out) # number of outputs
time_limit = FLAGS.timeout # time limit for searching
backoff_thresh = 1 - FLAGS.backoff_prob
num_supports = 1 + FLAGS.max_degree
nsr = np.power(10.0, -FLAGS.snr_db/20.0)
from directory import create_result_folder, find_model_folder
model_origin = find_model_folder(FLAGS, 'dpg_policy')
critic_origin = find_model_folder(FLAGS, 'critic')
def random_graph(size, k=20, p=0.25, gtype='grp', gseed=None):
if gtype == 'grp':
graph = nx.gaussian_random_partition_graph(size, k, min(7, k), p, max(0.1, p/3.0), seed=gseed)
elif gtype == 'ws':
graph = nx.connected_watts_strogatz_graph(size, k, p, tries=1000, seed=gseed)
elif gtype == 'er':
graph = nx.generators.random_graphs.fast_gnp_random_graph(size, float(k) / float(size))
elif gtype == 'ba':
graph = nx.generators.random_graphs.barabasi_albert_graph(size, int(np.round(k * p)))
else:
raise ValueError('Unsupported graph type')
wts = 10.0*np.random.uniform(0.01, 1.01, (size,))
for u in graph:
graph.nodes[u]['weight'] = wts[u]
adj = nx.adjacency_matrix(graph, nodelist=list(range(size)), weight=None)
return graph, adj, wts
try:
dqn_agent.load(model_origin)
except:
print("Unable to load {}".format(model_origin))
# baseline = mwcds_vvv
baseline = greedy_mwcds2
best_IS_vec = []
loss_vec = []
results = pd.DataFrame([], columns=["type", "size", "k", "p", "mwds", "mwcds", "gcn", "greedy", "ratio",
"t0", "t1", "t2", "t3"])
csvname = "./output/{}_{}_test_foo.csv".format(model_origin.split('/')[-1], test_datapath.split('/')[-1])
epislon_reset = [5, 10, 15, 20]
epislon_val = 1.0
eval_size = FLAGS.nvalid
n_samples = FLAGS.ntrain
best_ratio = 3.0
last_ap = 1.0
batch_size = 100
tr_best = 0
for id in range(500):
losses = []
losses_crt = []
cnt = 0
f_ct = 0
size = np.random.choice([100, 150, 200, 250, 300, 350])
# size = np.random.choice([100, 150, 200, 250, 300, 350, 400, 450, 500])
k = np.random.randint(10, 30)
p = np.random.uniform(0.15, 0.35)
# seed = id+epoch*100
graph, adj, wts = random_graph(size=size, k=k, p=p, gtype=FLAGS.gtype, gseed=id+2000)
if not nx.is_connected(graph):
print("unconnected")
continue
adj_0 = adj.copy()
nn = adj_0.shape[0]
newtime0 = time.time()
mwcds_c, mwds_c, total_wt_c = baseline(adj, wts)
runtime0 = time.time() - newtime0
newtime1 = time.time()
mwcds_0, mwds_0, total_wt_0 = heuristic_func(adj, wts)
runtime1 = time.time() - newtime1
newtime2 = time.time()
state, zs_t = dqn_agent.foo_train(adj_0, wts, train=True)
runtime2 = time.time() - newtime2
zs_np = zs_t.numpy()
if dqn_agent.flags.diver_num == 2:
gcn_wts = zs_np[:, 0].flatten() * wts.flatten() + zs_np[:, 1].flatten()
else:
gcn_wts = np.multiply(zs_np.flatten(), wts.flatten())
top_wts = np.clip(gcn_wts, a_min=0.0, a_max=None)
mwcds_i, mwds_i, _ = heuristic_func(adj_0, top_wts)
runtime3 = time.time() - newtime2
total_wt_i = np.sum(wts[list(mwcds_i)])
ind_vec, apu_avg = dqn_agent.predict_train(adj_0, zs_t, state, n_samples=n_samples)
p_ratio = total_wt_i/total_wt_0
print("ID: {}".format(id),
"gtype: GRP, size: {}, k: {}, p: {:.3f}".format(size, k, p),
"Model: Actor",
"mwds_i: {}".format(len(mwds_i)),
"mwcds_i: {}".format(len(mwcds_i)),
"mwds_0: {:.4f}".format(len(mwds_0)),
"mwcds_0: {:.4f}".format(len(mwcds_0)),
"gcn: {:.4f}".format(total_wt_i),
"greedy: {:.4f}".format(total_wt_0),
"ratio: {:.3f}".format(total_wt_i / total_wt_0),
"gcn ar: {:.3f}".format(total_wt_i / total_wt_c),
"grd ar: {:.3f}".format(total_wt_0 / total_wt_c),
"runtime: {:.2f},{:.2f},{:.2f},{:.2f}".format(runtime0, runtime1, runtime2, runtime3),
)
results = results.append({"type": "GRP", "size": size, "k": k, "p": p,
"mwds_i": len(mwds_i),
"mwcds_i": len(mwcds_i),
"mwds_0": len(mwds_0),
"mwcds_0": len(mwcds_0),
"gcn": total_wt_i,
"greedy": total_wt_0,
"vvv": total_wt_c,
"ar_gb": total_wt_i/total_wt_c,
"ar_db": total_wt_0/total_wt_c,
"ratio": total_wt_i/total_wt_0,
"t0": runtime0,
"t1": runtime1,
"t2": runtime2,
"t3": runtime3},
ignore_index=True)
results.to_csv(csvname)