-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoute.py
100 lines (81 loc) · 2.96 KB
/
Route.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
from ProbGenerate import Problem, Demand
from helpers import succFun
import cvxpy as cp
class OptimalRouting:
def __init__(self, P):
self.graph = P.graph
self.capacities = P.capacities
self.bandwidths = P.bandwidths
self.weights = P.weights
self.demands = P.demands
self.catalog_size = max([d.item for d in self.demands]) + 1
def obj(self, X, R):
ecg = 0.0
for d in R:
item = self.demands[d].item
rate = self.demands[d].rate
paths = self.demands[d].routing_info['paths']
for path_id in R[d]:
path = paths[path_id]
prob = R[d][path_id]
x = self.demands[d].query_source
s = succFun(x, path)
prodsofar = (1 - prob) * (1 - X[x][item])
while s is not None:
ecg += rate * self.weights[(s, x)] * (1 - prodsofar)
x = s
s = succFun(x, path)
prodsofar *= (1 - X[x][item])
# return ecg / sumrate
return ecg
def OptimalRoute(self, X):
"""
Solve a linear programing, given cache X
"""
constr = []
R = {}
for d in range(len(self.demands)):
R[d] = {}
for p in self.demands[d].routing_info['paths']:
R[d][p] = cp.Variable()
constr.append(R[d][p] >= 0.)
constr.append(R[d][p] <= 1.)
out = {}
for d in range(len(self.demands)):
out[d] = 0
for p in self.demands[d].routing_info['paths']:
out[d] += (1 - R[d][p])
constr.append(out[d] >= 1.0)
flow = {}
for d in R:
item = self.demands[d].item
rate = self.demands[d].rate
paths = self.demands[d].routing_info['paths']
for path_id in R[d]:
path = paths[path_id]
prob = R[d][path_id]
x = self.demands[d].query_source
s = succFun(x, path)
prodsofar = (1 - prob) * (1 - X[x][item])
while s is not None:
# calculate flow over each edge
if (s, x) in flow:
flow[(s, x)] += prodsofar * rate
else:
flow[(s, x)] = prodsofar * rate
x = s
s = succFun(x, path)
prodsofar *= (1 - X[x][item])
for e in flow:
constr.append(flow[e] <= self.bandwidths[e])
obj = self.obj(X, R)
problem = cp.Problem(cp.Maximize(obj), constr)
problem.solve()
# print("status:", problem.status)
if problem.status == 'optimal':
for d in range(len(self.demands)):
for p in self.demands[d].routing_info['paths']:
R[d][p] = R[d][p].value
else:
R = None
return R