-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
375 lines (311 loc) · 15.6 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
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
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from algorithm import DAGD
from algorithmHBF import DAHBF
from Quadratic_inputs.inputs import inputsClass
from communicate import commClass
from communicateHBF import commClassHBF
#-----------------------------------------------------------------------------
# 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"
#plt.rcParams['pdf.fonttype'] = 42
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 DAGD or DAHBF but is used to plot distance from the
# solution, if desired.
import Quadratic_inputs.cvxpySol #inputs file for CVXPY
xActual = Quadratic_inputs.cvxpySol.findActual(10)
#-----------------------------------------------------------------------------
# Algorithm Parameters
#-----------------------------------------------------------------------------
n = 10 # dimension of decision vector, x
gamma = .3 # stepsize for GD
alpha = .3 # stepsize for HBF
beta = .075 # momentum parameter for HBF
InitCond = 10*np.ones(n)
# Create Inputs Class
inputs = inputsClass()
# Create Communication Class with Comm Rate of 0.50 (agents communicate ~50% of the time)
comm50 = commClass(.5)
commHBF50 = commClassHBF(.5)
#-----------------------------------------------------------------------------
# Scalar Blocks with Agent Updates Occuring Every Timestep,
# Non Ill-Conditioned Objective Function
#-----------------------------------------------------------------------------
## Scalar Blocks
print("Running with Scalar Blocks...")
# Create DAGD and DAHBF classes with inputs defined above
scalarBlocks = DAGD(gamma, n, inputs, comm50)
scalarBlocksHBF = DAHBF(alpha, beta, n, inputs, commHBF50)
# Optional: Set the "actual" decision variable values to compute error later
# If not set, error will not be calculated.
scalarBlocks.setActual(xActual)
scalarBlocksHBF.setActual(xActual,xActual)
# Optional: Set the initial decision variable values
# If not set, zero vectors will be used.
scalarBlocks.setInit(InitCond)
scalarBlocksHBF.setInit(InitCond,InitCond)
# 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 ** -8,10**6)
scalarBlocksHBF.stopIf(10 ** -8,10**6)
# Run DAGD and DAHBF for scalar blocks
scalarBlocks.run()
scalarBlocksHBF.run()
print("Number of iterations for GD: ")
print(scalarBlocks.numIter+1)
print(scalarBlocks.xError[-1])
print("Number of iterations for Double HBF: ")
print(scalarBlocksHBF.numIter+1)
print(scalarBlocksHBF.x1Error[-1])
#----------------------------------------
## Figure Plotting
plt.semilogy(np.arange(0,scalarBlocks.numIter+1), scalarBlocks.iterNorm[0:], color= dark_blue, label="GD")
plt.semilogy(np.arange(0,scalarBlocksHBF.numIter+1), scalarBlocksHBF.iterNorm[0:], color= red, label="D-HBF")
plt.ylabel("$|| z_1(k) - z_1(k-1)||$")
plt.xlabel("Time, k")
plt.ylim(10 ** -8, 10 ** 3)
plt.title("Convergence, GD vs. Double HBF")
plt.legend()
plt.savefig('Convergence.eps')
plt.show()
plt.semilogy(np.arange(0,scalarBlocks.numIter+1), scalarBlocks.xError[0:], color= dark_blue, label="Gradient Descent")
plt.semilogy(np.arange(0,scalarBlocksHBF.numIter+1), scalarBlocksHBF.x1Error[0:], color= red, label="Double HBF")
plt.ylabel('$|| z_1 - x^*||$')
plt.xlabel("Time, k")
plt.ylim(10 ** -8, 10 ** 2)
plt.title("Error, GD vs. Double HBF")
plt.legend()
plt.savefig('Error.eps')
plt.show()
#-----------------------------------------------------------------------------
# Scalar Blocks with 100% Agent Update Rate,
# Comm Rate 100 %
#-----------------------------------------------------------------------------
# Machine epsilon for python, used as the tolerance for the stopping parameters in stopIf, below.
MachineEps = 2.3*(10 ** -16)
# Used for the stopping parameters in stopIf, below, to stop when tolerance is reached.
BoolIter = 0
# Create Communication Class with Comm Rate of 1 (agents communicate ~100% of the time)
comm100 = commClass(1)
commHBF100 = commClassHBF(1)
## Scalar Blocks
print("Running with Scalar Blocks... 100% Update Rate, 100% Comm Rate")
# Create DAGD and DAHBF classes with inputs defined above
scalarBlocks100 = DAGD(gamma, n, inputs, comm100)
scalarBlocksHBF100 = DAHBF(alpha, beta, n, inputs, commHBF100)
# Optional: Set the "actual" decision variable values to compute error later
# If not set, error will not be calculated.
scalarBlocks100.setActual(xActual)
scalarBlocksHBF100.setActual(xActual,xActual)
# Optional: Set the initial decision variable values
# If not set, zero vectors will be used.
scalarBlocks100.setInit(InitCond)
scalarBlocksHBF100.setInit(InitCond,InitCond)
# 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.
scalarBlocks100.stopIf(MachineEps,10**6,BoolIter)
scalarBlocksHBF100.stopIf(MachineEps,10**6,BoolIter)
# Run DAGD and DAHBF for scalar blocks
scalarBlocks100.run()
scalarBlocksHBF100.run()
#-----------------------------------------------------------------------------
# Scalar Blocks with 75% Agent Update Rate,
# Comm Rate 100 %
#-----------------------------------------------------------------------------
## Scalar Blocks
print("Running with Scalar Blocks... 75% Update Rate, 100% Comm Rate")
# Create DAGD and DAHBF classes with inputs defined above
scalarBlocks75 = DAGD(gamma, n, inputs, comm100, .75)
scalarBlocksHBF75 = DAHBF(alpha, beta, n, inputs, commHBF100, .75)
# Optional: Set the "actual" decision variable values to compute error later
# If not set, error will not be calculated.
scalarBlocks75.setActual(xActual)
scalarBlocksHBF75.setActual(xActual,xActual)
# Optional: Set the initial decision variable values
# If not set, zero vectors will be used.
scalarBlocks75.setInit(InitCond)
scalarBlocksHBF75.setInit(InitCond,InitCond)
# 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.
scalarBlocks75.stopIf(MachineEps,10**6,BoolIter)
scalarBlocksHBF75.stopIf(MachineEps,10**6,BoolIter)
# Run DAGD and DAHBF for scalar blocks
scalarBlocks75.run()
scalarBlocksHBF75.run()
#-----------------------------------------------------------------------------
# Scalar Blocks with 65% Agent Update Rate,
# Comm Rate 100 %
#-----------------------------------------------------------------------------
## Scalar Blocks
print("Running with Scalar Blocks... 65% Update Rate, 100% Comm Rate")
# Create DAGD and DAHBF classes with inputs defined above
scalarBlocks65 = DAGD(gamma, n, inputs, comm100, .65)
scalarBlocksHBF65 = DAHBF(alpha, beta, n, inputs, commHBF100, .65)
# Optional: Set the "actual" decision variable values to compute error later
# If not set, error will not be calculated.
scalarBlocks65.setActual(xActual)
scalarBlocksHBF65.setActual(xActual,xActual)
# Optional: Set the initial decision variable values
# If not set, zero vectors will be used.
scalarBlocks65.setInit(InitCond)
scalarBlocksHBF65.setInit(InitCond,InitCond)
# 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.
scalarBlocks65.stopIf(MachineEps,10**6,BoolIter)
scalarBlocksHBF65.stopIf(MachineEps,10**6,BoolIter)
# Run DAGD and DAHBF for scalar blocks
scalarBlocks65.run()
scalarBlocksHBF65.run()
#-----------------------------------------------------------------------------
# Scalar Blocks with 50% Agent Update Rate,
# Comm Rate 100 %
#-----------------------------------------------------------------------------
## Scalar Blocks
print("Running with Scalar Blocks... 50% Update Rate, 100% Comm Rate")
# Create DAGD and DAHBF classes with inputs defined above
scalarBlocks50 = DAGD(gamma, n, inputs, comm100, .5)
scalarBlocksHBF50 = DAHBF(alpha, beta, n, inputs, commHBF100, .5)
# Optional: Set the "actual" decision variable values to compute error later
# If not set, error will not be calculated.
scalarBlocks50.setActual(xActual)
scalarBlocksHBF50.setActual(xActual,xActual)
# Optional: Set the initial decision variable values
# If not set, zero vectors will be used.
scalarBlocks50.setInit(InitCond)
scalarBlocksHBF50.setInit(InitCond,InitCond)
# 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.
scalarBlocks50.stopIf(MachineEps,10**6,BoolIter)
scalarBlocksHBF50.stopIf(MachineEps,10**6,BoolIter)
# Run DAGD and DAHBF for scalar blocks
scalarBlocks50.run()
scalarBlocksHBF50.run()
#-----------------------------------------------------------------------------
# Scalar Blocks with 25% Agent Update Rate,
# Comm Rate 100 %
#-----------------------------------------------------------------------------
## Scalar Blocks
print("Running with Scalar Blocks... 25% Update Rate, 100% Comm Rate")
# Create DAGD and DAHBF classes with inputs defined above
scalarBlocks25 = DAGD(gamma, n, inputs, comm100, .25)
scalarBlocksHBF25 = DAHBF(alpha, beta, n, inputs, commHBF100, .25)
# Optional: Set the "actual" decision variable values to compute error later
# If not set, error will not be calculated.
scalarBlocks25.setActual(xActual)
scalarBlocksHBF25.setActual(xActual,xActual)
# Optional: Set the initial decision variable values
# If not set, zero vectors will be used.
scalarBlocks25.setInit(InitCond)
scalarBlocksHBF25.setInit(InitCond,InitCond)
# 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.
scalarBlocks25.stopIf(MachineEps,10**6,BoolIter)
scalarBlocksHBF25.stopIf(MachineEps,10**6,BoolIter)
# Run DAGD and DAHBF for scalar blocks
scalarBlocks25.run()
scalarBlocksHBF25.run()
#----------------------------------------
## Figure Plotting
## GD Convergence Between Iterations
plt.semilogy(np.arange(0,scalarBlocks100.numIter+1), scalarBlocks100.iterNorm[0:], color= dark_blue, label="100% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocks75.numIter+1), scalarBlocks75.iterNorm[0:], color= red, linestyle= "dotted", label="75% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocks65.numIter+1), scalarBlocks65.iterNorm[0:], color= dark_green, linestyle= "dashdot", label="65% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocks50.numIter+1), scalarBlocks50.iterNorm[0:], color= dark_purple, linestyle= "dashed", label="50% Comp Rate")
plt.ylabel("$|| z_1(k) - z_1(k-1)||$")
plt.xlabel("Time, k")
plt.ylim(10 ** -8, 10 ** 3)
plt.xlim(-5,40)
plt.title("Convergence Between Iterations, GD")
plt.legend()
plt.savefig('ConvergenceGD.eps')
plt.show()
## HBF Convergence Between Iterations
plt.semilogy(np.arange(0,scalarBlocksHBF100.numIter+1), scalarBlocksHBF100.iterNorm[0:], color= dark_blue, label="100% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocksHBF75.numIter+1), scalarBlocksHBF75.iterNorm[0:], color= red, linestyle= "dotted", label="75% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocksHBF65.numIter+1), scalarBlocksHBF65.iterNorm[0:], color= dark_green, linestyle= "dashdot", label="65% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocksHBF50.numIter+1), scalarBlocksHBF50.iterNorm[0:], color= dark_purple, linestyle= "dashed", label="50% Comp Rate")
plt.ylabel("$|| z_1(k) - z_1(k-1)||$")
plt.xlabel("Time, k")
plt.ylim(10 ** -8, 10 ** 3)
plt.xlim(-5,40)
plt.title("Convergence Between Iterations, HBF")
plt.legend()
plt.savefig('ConvergenceHBF.eps')
plt.show()
## GD Error
plt.semilogy(np.arange(0,scalarBlocks100.numIter+1), scalarBlocks100.xError[0:], color= dark_blue, label="100% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocks75.numIter+1), scalarBlocks75.xError[0:], color= red, linestyle= "dotted", label="75% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocks65.numIter+1), scalarBlocks65.xError[0:], dark_green, linestyle= "dashdot", label="65% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocks50.numIter+1), scalarBlocks50.xError[0:], color= dark_purple, linestyle= "dashed", label="50% Comp Rate")
plt.ylabel('$|| z_1 - x^*||$')
plt.xlabel("Time, k")
plt.ylim(10 ** -8, 10 ** 2)
plt.xlim(-5,40)
plt.title("Error, GD")
plt.legend()
plt.savefig('ErrorGD.eps')
plt.show()
## HBF Error
plt.semilogy(np.arange(0,scalarBlocksHBF100.numIter+1), scalarBlocksHBF100.x1Error[0:], color= dark_blue, label="100% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocksHBF75.numIter+1), scalarBlocksHBF75.x1Error[0:], color= red, linestyle= "dotted", label="75% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocksHBF65.numIter+1), scalarBlocksHBF65.x1Error[0:], color= dark_green, linestyle= "dashdot", label="65% Comp Rate")
plt.semilogy(np.arange(0,scalarBlocksHBF50.numIter+1), scalarBlocksHBF50.x1Error[0:], color= dark_purple, linestyle= "dashed", label="50% Comp Rate")
plt.ylabel('$|| z_1 - x^*||$')
plt.xlabel("Time, k")
plt.ylim(10 ** -8, 10 ** 2)
plt.xlim(-5,40)
plt.title("Error, HBF")
plt.legend()
plt.savefig('ErrorHBF.eps')
plt.show()