forked from kathendrickson/DACOA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_network.py
executable file
·355 lines (281 loc) · 13.6 KB
/
main_network.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 13 13:38:32 2020
@author: kat.hendrickson
"""
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from algorithm import DACOA
from Network_inputs.inputs import NetworkInputs
from Network_inputs.communicate import commClass
#-----------------------------------------------------------------------------
# Plot Formatting
#-----------------------------------------------------------------------------
#import matplotlib #uncomment to reset plot styles
#matplotlib.rc_file_defaults() #uncomment to reset plot styles
plt.set_loglevel("error")
plt.rcParams["font.family"] = "Times New Roman"
sns.set_context("notebook", rc={"font.size":16,
"axes.titlesize":18,
"axes.labelsize":16,
"figure.figsize": 6.4,
"savefig.dpi":600,
"savefig.format": "eps",
"savefig.bbox": "tight",
"savefig.pad_inches": 0.1
})
sns.despine()
light_blue = "#a6cee3"
dark_blue = "#1f78b4"
light_green = "#b2df8a"
dark_green = "#33a02c"
pink = "#fb9a99"
red = "#e31a1c"
light_orange = "#fdbf6f"
dark_orange = "#ff7f00"
light_purple = "#cab2d6"
dark_purple = "#6a3d9a"
#-----------------------------------------------------------------------------
# Use CVXPY to Find "Actual" Solution
#-----------------------------------------------------------------------------
# This is not necessary to run DACOA but is used to plot distance from the
# solution, if desired.
import Network_inputs.cvxpySol_network #inputs file for CVXPY
[xActual, muActual] = Network_inputs.cvxpySol_network.findActual(.1)
#-----------------------------------------------------------------------------
# Algorithm Parameters
#-----------------------------------------------------------------------------
n = 15 # dimension of primal vector, x
m = 66 # dimension of dual vector, mu
## Use problem parameters to define stepsizes and regularization parameter.
gamma = .01 # Primal stepsize, bounded by equation (4) in [1]
delta = .1 # Dual regularization parameter, chosen to reduce reg. error
rho = delta/(delta ** 2 + 1) # Dual stepsize, bounded in [1].
print("Primal stepsize, gamma:", gamma)
print("Dual regularization parameter, delta:", delta)
print("Dual stepsize, rho:", rho)
#-----------------------------------------------------------------------------
# Scalars vs Block Runs with Primal Updates Occuring Every K
#-----------------------------------------------------------------------------
## Scalar Blocks
print("Running with Scalar Blocks...")
# Create Inputs Class With Beta = .1 (See [1] or readme for more details.)
inputs = NetworkInputs(.1)
regError = np.sqrt((delta/.1))*np.sqrt(inputs.B/np.sqrt(15))
print("Regularization error is bounded above by:", regError)
# Create Np by Nd matrix where each entry i,j is 1 if dual agent j needs
# updates from prial agent i and is 0 otherwise. We can use the input A matrix
# to do so for scalar blocks.
scalarDualNeighbors = np.transpose(inputs.A)
# Create Communication Class with Comm Rate of 0.50 (agents communicate ~50% of the time)
comm50scalar = commClass(.5, scalarDualNeighbors)
# Create DACOA class with inputs defined above
scalarBlocks = DACOA(delta, gamma, rho, n, m, inputs, comm50scalar)
# Optional: Set the "actual" primal and dual values to compute error later
# If not set, error will not be calculated.
scalarBlocks.setActual(xActual,muActual)
# Optional: Set the initial primal and dual values
# If not set, zero vectors will be used.
scalarBlocks.setInit(0*np.ones(n), np.zeros(m))
# Optional: Set stopping parameters, stopIf(tol, maxIter, maxIterBool=1), where
# tol = tolerance for distance between iterations,
# maxIter = max number of iterations to run
# maxIterBool = whether to stop at the maxIter (1) or continue
# running until tol is reached (0).
# If not set, tol = 10**-8, maxIter = 10 ** 5, maxIterBool=1.
scalarBlocks.stopIf(10 ** -6,10**5)
# Run DACOA for scalar blocks
scalarBlocks.run()
#----------------------------------------
## Separable Blocks
print("Running with Separable Blocks...")
# Create non-scalar block arrays : xBlocks is an array containing the first index for each primal block.
#Similarly, muBlocks is an array containing the beginning indices for all dual agents.
xBlocks = np.array([0,5,10]) #Primal agent 1 is from 0 to 4, agent 2 is from 5 to 9, and agent 3 is from 10 to the end.
muBlocks = np.array([0,17,40])
# Create dualNeighbors matrix for new block setup. (See above for more discussion.)
blockDualNeighbors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
# Create comm class
comm50blocks = commClass(.5, blockDualNeighbors)
# Create DACOA class with inputs defined above.
vecBlocks = DACOA(delta, gamma, rho, n, m, inputs, comm50blocks)
# Set optional inputs
vecBlocks.setActual(xActual,muActual)
vecBlocks.setInit(0*np.ones(n), np.zeros(m))
vecBlocks.stopIf(10 ** -6,10**5)
vecBlocks.setBlocks(xBlocks,muBlocks)
# Run DACOA for non-scalar blocks
vecBlocks.run()
#----------------------------------------
## Figure Plotting
plt.semilogy(np.arange(1,scalarBlocks.numIter+1), scalarBlocks.iterNorm[1:], color= dark_blue, label="Scalar Blocks")
plt.semilogy(np.arange(1,vecBlocks.numIter+1), vecBlocks.iterNorm[1:], color= red, linestyle= "dotted", label="Non-Scalar Blocks")
plt.ylabel("$|| x(k) - x(k-1)||$")
plt.xlabel("Time, k")
plt.title("Convergence for Scalar and Non-Scalar Blocks")
plt.legend()
#plt.savefig('blocks.eps')
plt.show()
plt.semilogy(np.arange(1,scalarBlocks.numIter+1), scalarBlocks.xError[1:], color= dark_blue, label="Scalar Blocks")
plt.semilogy(np.arange(1,vecBlocks.numIter+1), vecBlocks.xError[1:], color= red, linestyle= "dotted", label="Non-Scalar Blocks")
plt.ylabel('$|| x - \hat{x}||$')
plt.xlabel("Time, k")
plt.title("Error for Scalar and Non-Scalar Blocks")
plt.legend()
#plt.savefig('figure.eps',bbox_inches = "tight",dpi=300)
plt.show()
#-----------------------------------------------------------------------------
# Scalars vs Block Runs with Primal Updates Occuring 50% of the Time
#-----------------------------------------------------------------------------
## Scalar Blocks
print("Running with Scalar Blocks...")
# Create Inputs Class With Beta = .1 (See [1] or readme for more details.)
inputs = NetworkInputs(.1)
regError = np.sqrt((delta/.1))*np.sqrt(inputs.B/np.sqrt(15))
print("Regularization error is bounded above by:", regError)
# Create Np by Nd matrix where each entry i,j is 1 if dual agent j needs
# updates from prial agent i and is 0 otherwise. We can use the input A matrix
# to do so for scalar blocks.
scalarDualNeighbors = np.transpose(inputs.A)
# Create Communication Class with Comm Rate of 0.50 (agents communicate ~50% of the time)
comm50scalar = commClass(.5, scalarDualNeighbors)
# Create DACOA class with inputs defined above
scalarBlocks = DACOA(delta, gamma, rho, n, m, inputs, comm50scalar, 0.5)
# Optional: Set the "actual" primal and dual values to compute error later
# If not set, error will not be calculated.
scalarBlocks.setActual(xActual,muActual)
# Optional: Set the initial primal and dual values
# If not set, zero vectors will be used.
scalarBlocks.setInit(0*np.ones(n), np.zeros(m))
# Optional: Set stopping parameters, stopIf(tol, maxIter, maxIterBool=1), where
# tol = tolerance for distance between iterations,
# maxIter = max number of iterations to run
# maxIterBool = whether to stop at the maxIter (1) or continue
# running until tol is reached (0).
# If not set, tol = 10**-8, maxIter = 10 ** 5, maxIterBool=1.
scalarBlocks.stopIf(-1,3000) #!!tolerance negative since primal updates do not occur every iteration
# Run DACOA for scalar blocks
scalarBlocks.run()
#----------------------------------------
## Separable Blocks
print("Running with Separable Blocks...")
# Create non-scalar block arrays : xBlocks is an array containing the first index for each primal block.
#Similarly, muBlocks is an array containing the beginning indices for all dual agents.
xBlocks = np.array([0,5,10]) #Primal agent 1 is from 0 to 4, agent 2 is from 5 to 9, and agent 3 is from 10 to the end.
muBlocks = np.array([0,17,40])
# Create dualNeighbors matrix for new block setup. (See above for more discussion.)
blockDualNeighbors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
# Create comm class
comm50blocks = commClass(.5, blockDualNeighbors)
# Create DACOA class with inputs defined above.
vecBlocks = DACOA(delta, gamma, rho, n, m, inputs, comm50blocks, 0.50)
# Set optional inputs
vecBlocks.setActual(xActual,muActual)
vecBlocks.setInit(0*np.ones(n), np.zeros(m))
vecBlocks.stopIf(-1,3000)
vecBlocks.setBlocks(xBlocks,muBlocks)
# Run DACOA for non-scalar blocks
vecBlocks.run()
#----------------------------------------
## Figure Plotting
plt.semilogy(np.arange(1,scalarBlocks.numIter+1), scalarBlocks.xError[1:], color= dark_blue, label="Scalar Blocks")
plt.semilogy(np.arange(1,vecBlocks.numIter+1), vecBlocks.xError[1:], color= red, linestyle= "dotted", label="Non-Scalar Blocks")
plt.ylabel('$|| x - \hat{x}||$')
plt.xlabel("Time, k")
plt.title("Error for Scalar and Non-Scalar Blocks")
plt.legend()
plt.savefig('blocks.eps')
plt.show()
#-----------------------------------------------------------------------------
# Varying Communication Rate
#-----------------------------------------------------------------------------
# Using the non-scalar block setup from above:
xBlocks = np.array([0,5,10])
muBlocks = np.array([0,17,40])
blockDualNeighbors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
# Create communication classes for different comm rates:
comm25 = commClass(.25, blockDualNeighbors)
comm50 = commClass(.50, blockDualNeighbors)
comm75 = commClass(.75, blockDualNeighbors)
comm100 = commClass(1, blockDualNeighbors)
# Run for 25% Comms
opt25 = DACOA(delta, gamma, rho, n, m, inputs, comm25)
opt25.setInit(0*np.ones(n), np.zeros(m))
opt25.setActual(xActual,muActual)
opt25.stopIf(10 ** -6,10**5)
opt25.setBlocks(xBlocks,muBlocks)
opt25.run()
# Run for 50% Comms
opt50 = DACOA(delta, gamma, rho, n, m, inputs, comm50)
opt50.setInit(0*np.ones(n), np.zeros(m))
opt50.setActual(xActual,muActual)
opt50.stopIf(10 ** -6,10**5)
opt50.setBlocks(xBlocks,muBlocks)
opt50.run()
# Run for 75% Comms
opt75 = DACOA(delta, gamma, rho, n, m, inputs, comm75)
opt75.setInit(0*np.ones(n), np.zeros(m))
opt75.setActual(xActual,muActual)
opt75.stopIf(10 ** -6,10**5)
opt75.setBlocks(xBlocks,muBlocks)
opt75.run()
# Run for 100% Comms
opt100 = DACOA(delta, gamma, rho, n, m, inputs, comm100)
opt100.setInit(0*np.ones(n), np.zeros(m))
opt100.setActual(xActual,muActual)
opt100.stopIf(10 ** -6,10**5)
opt100.setBlocks(xBlocks,muBlocks)
opt100.run()
#Plotting:
plt.semilogy(np.arange(1,opt25.numIter+1), opt25.iterNorm[1:], color= dark_blue, label="25% Comm. Rate")
plt.semilogy(np.arange(1,opt50.numIter+1), opt50.iterNorm[1:], color= red, linestyle = "dotted", label="50% Comm. Rate")
plt.semilogy(np.arange(1,opt75.numIter+1), opt75.iterNorm[1:], color= dark_green, linestyle = "dashed", label="75% Comm. Rate")
plt.semilogy(np.arange(1,opt100.numIter+1), opt100.iterNorm[1:], color= dark_orange, linestyle = "dashdot", label="100% Comm. Rate")
plt.ylabel("$|| x(k) - x(k-1)||$")
plt.xlabel("Time, k")
plt.title("Communication Rate and Convergence")
plt.legend()
plt.savefig('comm.eps')
plt.show()
plt.semilogy(np.arange(1,opt25.numIter+1), opt25.xError[1:], color= dark_blue, label="25% Comm. Rate")
plt.semilogy(np.arange(1,opt50.numIter+1), opt50.xError[1:], color= red, linestyle = "dotted", label="50% Comm. Rate")
plt.semilogy(np.arange(1,opt75.numIter+1), opt75.xError[1:], color= dark_green, linestyle = "dashed", label="75% Comm. Rate")
plt.semilogy(np.arange(1,opt100.numIter+1), opt100.xError[1:], color= dark_orange, linestyle = "dashdot", label="100% Comm. Rate")
plt.ylabel('$|| x - \hat{x}||$')
plt.xlabel("Time, k")
plt.title("Communication Rate and Error")
plt.legend()
#plt.savefig('figure.eps',bbox_inches = "tight",dpi=300)
plt.show()
#-----------------------------------------------------------------------------
# Varying the Amount of Diagonal Dominance
#-----------------------------------------------------------------------------
inputsBeta10 = NetworkInputs(.10)
inputsBeta25 = NetworkInputs(.25)
inputsBeta75 = NetworkInputs(.75)
beta10 = DACOA(delta, gamma, rho, n, m, inputsBeta10, comm75)
beta10.setInit(0*np.ones(n), np.zeros(m))
beta10.stopIf(10 ** -6,10**5)
beta10.setBlocks(xBlocks,muBlocks)
beta10.run()
beta25 = DACOA(delta, gamma, rho, n, m, inputsBeta25, comm75)
beta25.setInit(0*np.ones(n), np.zeros(m))
beta25.stopIf(10 ** -6,10**5)
beta25.setBlocks(xBlocks,muBlocks)
beta25.run()
beta75 = DACOA(delta, gamma, rho, n, m, inputsBeta75, comm75)
beta75.setInit(0*np.ones(n), np.zeros(m))
beta75.stopIf(10 ** -6,10**5)
beta75.setBlocks(xBlocks,muBlocks)
beta75.run()
plt.semilogy(np.arange(1,beta10.numIter+1), beta10.iterNorm[1:], color= dark_blue, label="Beta = 0.10")
plt.semilogy(np.arange(1,beta25.numIter+1), beta25.iterNorm[1:], color= red, linestyle= "dotted", label="Beta = 0.25")
plt.semilogy(np.arange(1,beta75.numIter+1), beta75.iterNorm[1:], color= dark_green, linestyle= "dashed", label="Beta = 0.75")
plt.ylabel("$|| x(k) - x(k-1)||$")
plt.xlabel("Time, k")
plt.title("Diagonal Dominance and Convergence")
plt.legend()
plt.savefig('beta.eps')
plt.show()