-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
222 lines (157 loc) · 6.83 KB
/
main.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
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import scipy.io
import pdb
from scipy import sparse
import pickle
from systems import *
from networks import *
from reinforce_policy import ReinforcePolicy
from reinforce_policy import *
import graphtools as gt
#######################
## UTILITY FUNCTIONS ##
#######################
def moving_average(data, window=10):
cumsum = np.cumsum(data)
moving_sum = cumsum[window:] - cumsum[:-window]
moving_avg = moving_sum / window
moving_avg = np.concatenate((np.zeros(window), moving_avg))
moving_avg[:window] = cumsum[:window] / window
return moving_avg
def sample_graph(self,batch_size,mu,A,state_dim):
samples = np.random.exponential(mu, size=(batch_size, state_dim, state_dim))
samples = (samples + np.transpose(samples,(0,2,1)))/2
PP = samples[None,:,:] * A
return PP[0]
def sample_one_graph(self,mu,A,state_dim):
samples = np.random.exponential(mu, size=(state_dim, state_dim))
samples = (samples + np.transpose(samples))/2
return samples * A
def run_sys(sys, policy, num_iter, batch_size=64,save_file="test8.mat"):
history_dict = {'lambd': [],
's': [],
'f0': [],
'f1': [],
'A': [],
'p': [],
'S': [],
'x': []}
history_dict['A'].append(sys.A)
for k in range(num_iter):
if k%1000 == 0:
print("Iteration " + str(k))
# sample state and actions
states = sys.sample(batch_size)
graph_state = sys.sample_graph(batch_size)
actions = policy.get_action(states, graph_state)
# compute reward and constraint violation
capacity = sys.compute_capacity(states,actions,graph_state)
f0 = sys.reward(states,actions,graph_state, capacity=capacity)
f1 = sys.constraint(states,actions,graph_state, capacity=capacity)
# save training data from iteration k
if k%1000 == 0:
history_dict['S'].append(graph_state)
history_dict['p'].append(actions)
history_dict['x'].append(states)
if k%100 == 0:
history_dict['lambd'].append(policy.lambd)
history_dict['s'].append(policy.slack)
history_dict['f0'].append(np.mean(f0))
history_dict['f1'].append(np.mean(f1,axis=0))
# performing learning step
policy.learn(states, actions, f0, f1, graph_state)
#save_policy(policy,save_file)
return history_dict
#############################################
############ Save NN architecture ###############
##############################################
def save_policy(policy, filename):
data_save = {}
index = 0
tvars = tf.trainable_variables()
num_layers = int(len(tvars)/2)
variable_names = []
for i in np.arange(num_layers):
variable_names.append("weight"+str(i))
variable_names.append("bias"+str(i))
variable_names.append("weight"+str(num_layers))
tvars_vals = policy.sess.run(tvars)
for var, val in zip(tvars, tvars_vals):
data_save[variable_names[index]] = val
index +=1
scipy.io.savemat(filename,data_save)
#############################################
############ Save simulation data ###############
##############################################
def save_data(data, filename):
scipy.io.savemat(filename, data)
####################
## TEST FUNCTIONS ##
####################
def interference_test():
mu = 2 # parameter for exponential distribution of wireless channel distribution
num_channels = 10 # number of wireless channels (action_dim and state_dim)
pmax = num_channels # maximum power allocation
batch_size = 100
mu2 = 4
pl = 2.2
A = build_adhoc_network(num_channels,pl)
sys = SumCapacity_Interference(num_channels, A, pmax=pmax,mu=mu, sigma=1)
distribution = BernoulliDistribution(sys.action_dim, upper_bound=4.0)
lambda_lr = .01
theta_lr = 3e-3
reinforce_policy = ReinforcePolicy(sys,
model_builder=regnn_model,
distribution=distribution, lambda_lr = lambda_lr, theta_lr = theta_lr, batch_size = batch_size)
history_dict = run_sys(sys, reinforce_policy, 40000, batch_size=batch_size, save_file = "gnn5_" + str(num_channels) + ".mat")
save_data(history_dict, "test3.mat")
def cellular_test():
mu = 1 # parameter for exponential distribution of wireless channel distribution
n = 5
k = 10
num_channels = int(n*k)
pmax = num_channels # maximum power allocation
batch_size = 64
pl = 2.2
A, assign = build_cellular_network(n,k,pl)
sys = SumCapacity_Interference(num_channels, A, pmax=pmax,mu=mu, sigma=1, cell = True, assign = assign)
distribution = BernoulliDistribution(sys.action_dim, upper_bound=4.0)
lambda_lr = 0.001
theta_lr = 5e-3
reinforce_policy = ReinforcePolicy(sys, model_builder=regnn_model, distribution=distribution, lambda_lr = lambda_lr, theta_lr = theta_lr, batch_size = batch_size)
history_dict = run_sys(sys, reinforce_policy, 40000, batch_size=batch_size, save_file = "cell_network.mat")
save_data(history_dict, "wireless_net_interference_data_gnn_50_34.mat")
def aggregation_test():
mu = 2 # parameter for exponential distribution of wireless channel distribution
num_channels = 10 # number of wireless channels (action_dim and state_dim)
pmax = num_channels # maximum power allocation
batch_size = 100
mu2 = 4
GSO = 'Adjacency'
pl = 2.2
A = build_adhoc_network(num_channels,pl)
sys = SumCapacity_Interference(num_channels, A, pmax=pmax,mu=mu, sigma=1)
# distribution = TruncatedGaussianDistribution(sys.action_dim,
# lower_bound=np.ones(sys.action_dim)*0.0,
# upper_bound=np.ones(sys.action_dim)*8.0)
distribution = BernoulliDistribution(sys.action_dim, upper_bound=4.0)
lambda_lr = .001
theta_lr = 5e-4
reinforce_policy = ReinforcePolicy(sys,
model_builder=gnn_model,
distribution=distribution, lambda_lr = lambda_lr, theta_lr = theta_lr, batch_size=batch_size, archit = "aggregation")
history_dict = run_sys(sys, reinforce_policy, 20000, batch_size=batch_size, save_file = "agnn_" + str(num_channels) + ".mat", subsample = subsample)
save_data(history_dict, "wireless_net_distributed_data_gnn_" + str(num_channels) +".mat")
if __name__ == '__main__':
import argparse
import sys
rn = np.random.randint(2**20)
rn1 = np.random.randint(2**20)
tf.set_random_seed(rn)
np.random.seed(rn1)
interference_test()
# fairness_test()
#cellular_test()
#