-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
71 lines (52 loc) · 1.37 KB
/
helpers.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
def reverse_graph(graph):
rev_graph = [[]] * len(graph)
for l in graph:
for edge in l:
u, v, _, _ = edge
rev_graph[v] = rev_graph[v] + [((v,u))]
return rev_graph
def one_index(graph, paths=[]):
index = {}
i = 1
for node in graph:
for edge in node:
u, v, a, b = edge
index[(u,v)] = i
i +=1
for path in paths:
index[tuple(path)] = i
i+=1
return index
def weight_fxn(graph, index):
coeff = [0] * (len(index)+1)
const = [0] * (len(index)+1)
for node in graph:
for edge in node:
u, v, a, b = edge
ind = index[(u,v)]
coeff[ind] = a
const[ind] = b
return coeff, const
def remove_flows(graph):
n = len(graph)
new_graph = [[]] * n
for i in range(n):
new_graph[i] = list(map(lambda (u,v,a,b,f): (u,v,a,b), graph[i]))
return new_graph
def path_cost(graph, path):
cost = 0
for i in range(len(path)-1):
u,v = path[i], path[i+1]
for edge in graph[u]:
u,w,a,b,f = edge
if w==v:
cost+=a*(f+1)+b
break
return cost
def curr_total_cost(graph, res):
cost2 = 0
for node in graph:
for edge in node:
u,w,a,b,f = edge
cost2+=f * (a * f + b)
return cost2/res