-
Notifications
You must be signed in to change notification settings - Fork 17
/
frank_wolfe_2.py
221 lines (197 loc) · 7.6 KB
/
frank_wolfe_2.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import numpy as np
from process_data import construct_igraph, construct_od
from AoN_igraph import all_or_nothing
__author__ = "Jerome Thai"
__email__ = "[email protected]"
'''
This module is frank-wolfe algorithm using an all-or-nothing assignment
based on igraph package
'''
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 = 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 total_free_flow_cost(g, od):
return np.array(g.es["weight"]).dot(all_or_nothing(g, od))
def search_direction(f, graph, g, od):
# computes the Frank-Wolfe step
# g is just a canvas containing the link information and to be updated with
# the most recent edge costs
x = np.power(f.reshape((f.shape[0], 1)), np.array([0, 1, 2, 3, 4]))
grad = np.einsum('ij,ij->i', x, graph[:, 3:])
g.es["weight"] = grad.tolist()
return all_or_nothing(g, od), grad
def solver(graph, demand, g=None, od=None, max_iter=100, eps=1e-8, q=None,
display=0, past=None, stop=1e-8):
if g is None:
g = construct_igraph(graph)
if od is None:
od = construct_od(demand)
# initial flow assignment is null
error = 'N/A'
f = np.zeros(graph.shape[0], dtype="float64")
K = total_free_flow_cost(g, od)
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:
print 'iteration: {}, error: {}'.format(i + 1, error)
# construct weighted graph with latest flow assignment
L, grad = search_direction(f, graph, g, od)
if i >= 1:
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, g=None, od=None, max_iter=100, eps=1e-8, q=10,
display=0, past=None, stop=1e-8):
if g is None:
g = construct_igraph(graph)
if od is None:
od = construct_od(demand)
# initial flow assignment is null
error = 'N/A'
f = np.zeros(graph.shape[0], dtype="float64")
ls = max_iter / q # frequency of line search
K = total_free_flow_cost(g, od)
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:
print 'iteration: {}, error: {}'.format(i + 1, error)
# construct weighted graph with latest flow assignment
L, grad = search_direction(f, graph, g, od)
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
if i % ls == (ls - 1):
s = line_search(lambda a: potential(graph, (1. - a) * f + a * L))
else:
s = 2. / (i + 2.)
if s < eps:
return f
f = (1. - s) * f + s * L
return f
def solver_3(graph, demand, g=None, od=None, past=10, max_iter=100, eps=1e-8,
q=50, display=0, stop=1e-8):
'''
this is an adaptation of Fukushima's algorithm
graph: numpy array of the format [[link_id from to a0 a1 a2 a3 a4]]
demand: mumpy arrau of the format [[o d flow]]
g: igraph object constructed from graph
od: od in the format {from: ([to], [rate])}
past: search direction is the mean over the last 'past' directions
max_iter: maximum number of iterations
eps: used as stopping criteria if some quantities are too close to 0
q: first 'q' iterations uses open loop step sizes 2/(i+2)
display: controls the display of information in the terminal
stop: stops the algorithm if the error is less than 'stop'
'''
assert past <= q, "'q' must be bigger or equal to 'past'"
if g is None:
g = construct_igraph(graph)
if od is None:
od = construct_od(demand)
# initial flow assignment is null
error = 'N/A'
f = np.zeros(graph.shape[0], dtype="float64")
fs = np.zeros((graph.shape[0], past), dtype="float64")
K = total_free_flow_cost(g, od)
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:
print 'iteration: {}, error: {}'.format(i + 1, error)
# construct weighted graph with latest flow assignment
L, grad = search_direction(f, graph, g, od)
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, axis=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 single_class_parametric_study(factors, output, net, demand,
max_iter=100, display=1):
'''
parametric study where the equilibrium flow is computed under different
demand levels alpha*demand for alpha in factors
'''
g = construct_igraph(net)
d = np.copy(demand)
fs = np.zeros((net.shape[0], len(factors)))
header = ','.join(['X{}'.format(i) for i in range(len(factors))])
for i, alpha in enumerate(factors):
if display >= 1:
print 'computing equilibrium {}/{}'.format(i + 1, len(factors))
d[:, 2] = alpha * demand[:, 2]
f = solver_3(net, d, g=g, past=20, q=50, stop=1e-3, display=display,
max_iter=max_iter)
fs[:, i] = f
np.savetxt(output, fs, delimiter=',', header=header, comments='')