-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfig6.py
80 lines (68 loc) · 2.25 KB
/
fig6.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
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 3 16:09:20 2018
@author: Meng Ma ([email protected])
This is for simulation 1, which will produce two figure:
1. accuracy vs iteration number;
2. accuracy vs communication cost;
"""
#%%
from Simulator import Simulator
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
#%% simulation setting
# 1. graph
n_nodes = 50 # number of nodes
d = 3 # dimension of variable at each node
# 2. function: objective value
v = np.random.rand(n_nodes, d)
# 3. simulation setting
graphs = [nx.path_graph(n_nodes),
nx.cycle_graph(n_nodes),
nx.star_graph(n_nodes-1)]
graph_name = ['Line', 'Cycle', 'Star']
line_style = ['--rd', '-rd',
'--bs', '-bs',
'--go', '-go']
# penalty parameters are hand-tuned, it would be better if there is some
# automatic way of finding the best parameters
best_penalty = [{'D-CADMM': 8.25, 'H-CADMM': 6.68},# Line
{'D-CADMM': 4.03, 'H-CADMM': 3.3},# Cycle
{'D-CADMM': 1, 'H-CADMM': 1}]# Star
all_mode = ['D-CADMM', 'H-CADMM']
max_iter = 500
epsilon = 1e-8
setting = {'penalty': -1,
'max_iter': max_iter,
'objective': v,
'initial': np.zeros((n_nodes, d)),
'epsilon': epsilon}
#%% simulation
sim_data = []
for G, name, rho in zip(graphs, graph_name, best_penalty):
sim = Simulator(G, simulation_setting=setting)
for mode in all_mode:
data = {}
sim.mode = mode
sim.setting['penalty'] = rho[mode]
opt_gap, primal_residual, dual_residual, _ = sim.run_least_squares()
data['legend'] = name + ' ' + sim.mode
data['opt_gap'] = opt_gap
sim_data.append(data)
#%% plot
n_markers = 30
marker_at = setting['max_iter'] // n_markers
fig = plt.figure(1, figsize=(8, 6))
for data, style in zip(sim_data, line_style):
plt.semilogy(data['opt_gap'], style, label=data['legend'],
markevery=marker_at)
plt.ylabel('Accuracy')
plt.xlabel('Iterations/Communication cost')
plt.ylim(ymin=epsilon)
plt.legend()
fig.tight_layout()
# uncomment the lines below to save figure to tikz file
#from matplotlib2tikz import save as tikz_save
#tikz_save('in_net_fixed.tex', figureheight='4cm', figurewidth='6cm')
plt.show()