-
Notifications
You must be signed in to change notification settings - Fork 0
/
frank_wolfe.py
executable file
·190 lines (168 loc) · 6.48 KB
/
frank_wolfe.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
__author__ = "Jerome Thai"
__email__ = "[email protected]"
import numpy as np
from All_Or_Nothing import all_or_nothing
def potential(graph ,f):
# this routine is useful for doing a line search
# computes the potential at flow assignment f
links = int(np.max(graph[:,0])+1)
g = np.copy(graph.dot(np.diag([1.,1.,1.,1.,1/2.,1/3.,1/4.,1/5.])))
x = np.power(f.reshape((links,1)), np.array([1,2,3,4,5]))
return np.sum(np.einsum('ij,ij->i', x, g[:,3:]))
def line_search(f, res=20):
# on a grid of 2^res points bw 0 and 1, find global minimum
# of continuous convex function
d = 1./(2**res-1)
l, r = 0, 2**res-1
while r-l > 1:
if f(l*d) <= f(l*d+d): return l*d
if f(r*d-d) >= f(r*d): return r*d
# otherwise f(l) > f(l+d) and f(r-d) < f(r)
m1, m2 = (l+r)/2, 1+(l+r)/2
if f(m1*d) < f(m2*d): r = m1
if f(m1*d) > f(m2*d): l = m2
if f(m1*d) == f(m2*d): return m1*d
return l*d
def search_direction(f, graph, g, demand):
# computes the Frank-Wolfe step
# g is just a canvas containing the link information and to be updated with
# the most recent edge costs
# print 'f shape', f.shape
x = np.power(f.reshape((f.shape[0],1)), np.array([0,1,2,3,4]))
# print 'x shape', x.shape
# print 'graph shape', graph.shape
grad = np.einsum('ij,ij->i', x, graph[:,3:])
# print 'grad shape', grad.shape
g[:,3] = grad
return all_or_nothing(g, demand), grad
def total_free_flow_cost(g, demand):
# computes the total cost under free flow travel times
L = all_or_nothing(g, demand)
return g[:,3].dot(L)
def solver(graph, demand, max_iter=100, eps=1e-8, q=None, display=1, past=None,\
stop=1e-8):
# Prepares arrays for assignment
# we change stop to 10^-2 to mimic Frank_wolfe_2
stop = 1e-2
links = int(np.max(graph[:,0])+1)
print 'links', str(links)
f = np.zeros(links,dtype="float64") # initial flow assignment is null
g = np.copy(graph[:,:4])
K = total_free_flow_cost(g, demand)
if K < eps:
K = np.sum(demand[:,2])
elif display >= 1:
print 'average free-flow travel time', K / np.sum(demand[:,2])
for i in range(max_iter):
if display >= 1:
if i <= 1:
print 'iteration: {}'.format(i+1)
else:
print 'iteration: {}, error: {}'.format(i+1, error)
# construct weighted graph with latest flow assignment
#import pdb; pdb.set_trace()
L, grad = search_direction(f, graph, g, demand)
if i >= 1:
# w = f - L
# norm_w = np.linalg.norm(w,1)
# if norm_w < eps: return f
error = grad.dot(f - L) / K
if error < stop: return f
f = f + 2.*(L-f)/(i+2.)
return f
def solver_2(graph, demand, max_iter=100, eps=1e-8, q=10, display=0, past=None,\
stop=1e-8):
# version with line search
# Prepares arrays for assignment
links = int(np.max(graph[:,0])+1)
f = np.zeros(links,dtype="float64") # initial flow assignment is null
g = np.copy(graph[:,:4])
ls = max_iter/q # frequency of line search
K = total_free_flow_cost(g, demand)
if K < eps:
K = np.sum(demand[:,2])
elif display >= 1:
print 'average free-flow travel time', K / np.sum(demand[:,2])
for i in range(max_iter):
if display >= 1:
if i <= 1:
print 'iteration: {}'.format(i+1)
else:
print 'iteration: {}, error: {}'.format(i+1, error)
# construct weighted graph with latest flow assignment
L, grad = search_direction(f, graph, g, demand)
if i >= 1:
# w = f - L
# norm_w = np.linalg.norm(w,1)
# if norm_w < eps: return f
error = grad.dot(f - L) / K
if error < stop: return f
# s = line_search(lambda a: potential(graph, (1.-a)*f+a*L)) if i>max_iter-q \
# else 2./(i+2.)
s = line_search(lambda a: potential(graph, (1.-a)*f+a*L)) if i%ls==(ls-1) \
else 2./(i+2.)
if s < eps: return f
f = (1.-s) * f + s * L
return f
def solver_3(graph, demand, past=10, max_iter=100, eps=1e-8, q=50, display=1,\
stop=1e-8):
# modified Frank-Wolfe from Masao Fukushima
links = int(np.max(graph[:,0])+1)
f = np.zeros(links,dtype="float64") # initial flow assignment is null
fs = np.zeros((links,past),dtype="float64")
g = np.copy(graph[:,:4])
K = total_free_flow_cost(g, demand)
if K < eps:
K = np.sum(demand[:,2])
elif display >= 1:
print 'average free-flow travel time', K / np.sum(demand[:,2])
for i in range(max_iter):
if display >= 1:
if i <= 1:
print 'iteration: {}'.format(i+1)
else:
print 'iteration: {}, error: {}'.format(i+1, error)
# construct weighted graph with latest flow assignment
L, grad = search_direction(f, graph, g, demand)
fs[:,i%past] = L
w = L - f
if i >= 1:
error = -grad.dot(w) / K
# if error < stop and error > 0.0:
if error < stop:
if display >= 1: print 'stop with error: {}'.format(error)
return f
if i > q:
# step 3 of Fukushima
v = np.sum(fs,1) / min(past,i+1) - f
norm_v = np.linalg.norm(v,1)
if norm_v < eps:
if display >= 1: print 'stop with norm_v: {}'.format(norm_v)
return f
norm_w = np.linalg.norm(w,1)
if norm_w < eps:
if display >= 1: print 'stop with norm_w: {}'.format(norm_w)
return f
# step 4 of Fukushima
gamma_1 = grad.dot(v) / norm_v
gamma_2 = grad.dot(w) / norm_w
if gamma_2 > -eps:
if display >= 1: print 'stop with gamma_2: {}'.format(gamma_2)
return f
d = v if gamma_1 < gamma_2 else w
# step 5 of Fukushima
s = line_search(lambda a: potential(graph, f+a*d))
if s < eps:
if display >= 1: print 'stop with step_size: {}'.format(s)
return f
f = f + s*d
else:
f = f + 2. * w/(i+2.)
return f
def main():
graph = np.loadtxt('data/braess_net.csv', delimiter=',', skiprows=1)
demand = np.loadtxt('data/braess_od.csv', delimiter=',', skiprows=1)
f = solver(graph, demand)
print f
if __name__ == '__main__':
main()