-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_sens.py
132 lines (110 loc) · 5.82 KB
/
plot_sens.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
import matplotlib.pyplot as plt
import logging, argparse
import pickle
import numpy as np
bandwidth_coefficients = [1, 1.5, 2, 2.5, 3]
algorithm = ['PrimalDual', 'Random1', 'Random2', 'Greedy1', 'Greedy2', 'Alternating']
Dirs = {1: ["OUTPUT1/", "Random1/CacheRoute/", "Random1/RouteCache/", "Greedy1/CacheRoute/", "Greedy1/RouteCache/", "Heuristic1/"],
2: ["OUTPUT2/", "Random2/CacheRoute/", "Random2/RouteCache/", "Greedy2/CacheRoute/", "Greedy2/RouteCache/", "Heuristic2/"],
3: ["OUTPUT3/", "Random3/CacheRoute/", "Random3/RouteCache/", "Greedy3/CacheRoute/", "Greedy3/RouteCache/", "Heuristic3/"]}
colors = ['r', 'sandybrown', 'gold', 'darkseagreen', 'c', 'dodgerblue', 'm']
line_styles = ['s-', '*-', 'd--', '^-', 'v-', '.:']
def readresult(fname):
with open(fname, 'rb') as f:
result = pickle.load(f)
return result
def plotSensitivity(x, type, graph):
fig, ax = plt.subplots()
fig.set_size_inches(4, 4)
for i in range(len(algorithm)):
alg = algorithm[i]
for j in range(len(x[alg])):
if x[alg][j]:
break
else:
j = len(x[alg])
ax.plot(bandwidth_coefficients[j:], x[alg][j:], line_styles[i], markersize=10, color=colors[i], label=alg, linewidth=3)
ax.tick_params(labelsize=10)
ax.set_ylabel('Cache Gain $F$', fontsize=15)
ax.set_xlabel('Looseness $\kappa$', fontsize=15)
lgd = fig.legend(fontsize=13, loc='upper left', bbox_to_anchor=(0.95, 0.9))
plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
plt.tight_layout()
plt.show()
fig.savefig('Figure/sens%d/%s.pdf' % (type, graph), bbox_extra_artists=(lgd,), bbox_inches='tight')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Plot bar',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--graph_type', default="erdos_renyi", type=str, help='Graph type',
choices=['erdos_renyi', 'balanced_tree', 'hypercube', "cicular_ladder", "cycle", "grid_2d",
'lollipop', 'expander', 'star', 'barabasi_albert', 'watts_strogatz',
'regular', 'powerlaw_tree', 'small_world', 'geant', 'abilene', 'dtelekom',
'servicenetwork', 'example1', 'example2', 'abilene1', 'abilene2', 'real1', 'real2'])
parser.add_argument('--catalog_size', default=1000, type=int, help='Catalog size')
parser.add_argument('--graph_size', default=100, type=int, help='Network size')
parser.add_argument('--query_nodes', default=10, type=int, help='Number of nodes generating queries')
parser.add_argument('--demand_size', default=5000, type=int, help='Demand size')
parser.add_argument('--max_capacity', default=20, type=int, help='Maximum capacity per cache')
parser.add_argument('--bandwidth_type', default=1, type=int,
help='Type of generating bandwidth: 1. no cache, 2. uniform cache, 3. random integer cache')
parser.add_argument('--debug_level', default='INFO', type=str, help='Debug Level',
choices=['INFO', 'DEBUG', 'WARNING', 'ERROR'])
parser.add_argument('--stepsize', default=1000, type=int, help='Stepsize')
args = parser.parse_args()
args.debug_level = eval("logging." + args.debug_level)
logging.basicConfig(level=args.debug_level)
obj = {}
for alg in algorithm:
obj[alg] = []
Dir = Dirs[args.bandwidth_type]
for bandwidth_coefficient in bandwidth_coefficients:
fname2 = "%s_%ditems_%dnodes_%dquerynodes_%ddemands_%dcapcity_%fbandwidth" % (
args.graph_type, args.catalog_size, args.graph_size, args.query_nodes, args.demand_size, args.max_capacity,
bandwidth_coefficient)
result = 0
vio = 1000000
for stepsize in [100, 500, 1000, 5000, 10000]:
fname1 = "%s_%ditems_%dnodes_%dquerynodes_%ddemands_%dcapcity_%fbandwidth_%dstepsize" % (
args.graph_type, args.catalog_size, args.graph_size, args.query_nodes, args.demand_size, args.max_capacity,
bandwidth_coefficient, stepsize)
fname = Dir[0] + fname1
results = readresult(fname)
'''calculate violation'''
SumFlows = []
NumNonzeroFlows = []
iterations, durations, Xs, Rs, overflows, Duals, lagrangians, objs = zip(*results)
for overflow in overflows:
ActiveFlow = []
Flow = []
for e in overflow:
if overflow[e] > 0: # violated flow
ActiveFlow.append(overflow[e])
if overflow[e] > -1: # non zero flow
Flow.append(overflow[e])
if ActiveFlow:
SumFlows.append(sum(ActiveFlow))
else:
SumFlows.append(0)
if Flow:
NumNonzeroFlows.append(len(Flow))
else:
NumNonzeroFlows.append(0)
vios = np.array(SumFlows) / np.array(NumNonzeroFlows)
vio_min = min(vios)
'''obj'''
for i in range(len(objs)):
if vios[i] == vio_min:
if (objs[i] >= 0.999 * result and vio_min < vio) or (objs[i] > result and vio_min <= 1.001 * vio):
result = objs[i]
vio = vio_min
obj[algorithm[0]].append(result)
for i in range(1, len(algorithm)-1):
fname = Dir[i] + fname2
result = readresult(fname)
result = result[-1]
obj[algorithm[i]].append(result)
fname = Dir[-1] + fname2
result = readresult(fname)
result = result[-1][-1]
obj[algorithm[-1]].append(result)
plotSensitivity(obj, args.bandwidth_type, args.graph_type)