-
Notifications
You must be signed in to change notification settings - Fork 0
/
frank_wolfe_heterogeneous.py
executable file
·382 lines (328 loc) · 14.6 KB
/
frank_wolfe_heterogeneous.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
__author__ = "Jerome Thai"
__email__ = "jerome.thai@berkeley.edu"
import numpy as np
from AoN_igraph import all_or_nothing
from frank_wolfe_2 import total_free_flow_cost, search_direction, line_search, solver_3
from process_data import construct_igraph, construct_od, extract_features
from utils import multiply_cognitive_cost, heterogeneous_demand
#Profiling the code
import timeit
from collections import defaultdict
def search_direction_multi(f, graphs, gs, ods, L, grad):
# extension of search_direction routine in frank_wolfe_2.py
# for the heterogeneous game
#start timer
start_time1 = timeit.default_timer()
path_flows = defaultdict(np.float64)
links = graphs[0].shape[0]
types = len(graphs)
for j, (graph, g, od) in enumerate(zip(graphs, gs, ods)):
#start timer
start_time2 = timeit.default_timer()
l, gr, pf = search_direction(np.sum(np.reshape(f,(types,links)).T,1), \
graph, g, od)
#end of timer
#elapsed2 = timeit.default_timer() - start_time2;
#print ("step0 too %s seconds" % elapsed1)
L[(j*links) : ((j+1)*links)] = l
grad[(j*links) : ((j+1)*links)] = gr
for k in pf:
path_flows[k + (j,)] = pf[k]
#end of timer
#elapsed1 = timeit.default_timer() - start_time1;
#print ("Search_direction for all pairs: %s seconds" % elapsed1)
return L, grad, path_flows
def merit(f, graphs, gs, ods, L, grad):
# this computes the merit function associated to the VI problem
# max_y <F(x), x-y>
L, grad, path_flows = search_direction_multi(f, graphs, gs, ods, L, grad)
return grad.dot(f - L)
def fw_heterogeneous_1(graphs, demands, r, max_iter=100, eps=1e-8, q=None, \
display=0, past=None, stop=1e-8):
'''
Frank-Wolfe algorithm on the heterogeneous game
given a list of graphs in the format
g = [[link_id from to a0 a1 a2 a3 a4]]
and demand in the format
d = [[o d flow]]
r = % app users
'''
#start timer
#start_time1 = timeit.default_timer()
# construct graph and demand objects suiteable for AoN_igraph
gs = [construct_igraph(graph) for graph in graphs]
ods = [construct_od(demand) for demand in demands]
# construct empty vector to be filled in with values
links = graphs[0].shape[0]
types = len(graphs)
f = np.zeros(links*types,dtype="float64") # initial flow assignment is null
L = np.zeros(links*types,dtype="float64")
grad = np.zeros(links*types,dtype="float64")
h = defaultdict(np.float64) # initial path flow assignment is null
# compute re-normalization constant
K = sum([total_free_flow_cost(g, od) for g,od in zip(gs, ods)])
if K < eps:
K = sum([np.sum(demand[:,2]) for demand in demands])
elif display >= 1:
print 'average free-flow travel time', \
K / sum([np.sum(demand[:,2]) for demand in demands])
#end of timer
#elapsed1 = timeit.default_timer() - start_time1;
#print ("step0 too %s seconds" % elapsed1)
# compute iterations
start_time = timeit.default_timer()
for i in range(max_iter):
#start timer
#start_time3 = timeit.default_timer()
if display >= 1:
if i <= 1:
print 'iteration: {}'.format(i+1)
else:
print 'iteration: {}, error: {}'.format(i+1, error)
#start timer
#start_time2 = timeit.default_timer()
# construct weighted graph with latest flow assignment
L, grad, path_flows = search_direction_multi(f, graphs, gs, ods, L, grad)
#end of timer
#elapsed2 = timeit.default_timer() - start_time2;
#print ("search_direction took %s seconds" % elapsed2)
if i >= 1:
error = grad.dot(f - L) / K
if error < stop:
return np.reshape(f,(types,links)).T, h, np.dot(grad[:links], np.sum(np.reshape(f,(types,links)).T,1) - np.sum(np.reshape(L,(types,links)).T,1) * r)
f = f + 2.*(L-f)/(i+2.)
# print type(h), type(path_flows)
for k in set(h.keys()).union(set(path_flows.keys())):
h[k] = h[k] + 2.*(path_flows[k]-h[k])/(i+2.)
print 'iteration', i
print 'time(sec):', timeit.default_timer() - start_time;
print 'num path flows:', len(h)
f_h = np.zeros(graph.shape[0],dtype='float64') # initial flow assignment is null
for k in h:
flow = h[k]
for link in k[2]:
f_h[link] += flow
print "path vs link flow diff:", np.sum(np.abs(f_h - np.sum(np.reshape(f,(types,links)).T,1))), f.shape
#end of timer
#elapsed3 = timeit.default_timer() - start_time3;
#print ("The whole iteration took %s seconds" % elapsed3)
# find how many paths each od pair really has
od_paths = defaultdict(int)
most_paths = 0
for k in h.keys():
od_paths[(k[:2])] += 1
most_paths = max(most_paths, od_paths[(k[:2])])
path_counts = [0 for i in range(most_paths + 1)]
for k in od_paths.keys():
path_counts[od_paths[k]] += 1
for i in range(len(path_counts)):
print i, path_counts[i]
L, grad, path_flows = search_direction_multi(f, graphs, gs, ods, L, grad)
return np.reshape(f,(types,links)).T, h, np.dot(grad[:links], np.sum(np.reshape(f,(types,links)).T,1) - np.sum(np.reshape(L,(types,links)).T,1) * r)
def fw_heterogeneous_2(graphs, demands, past=10, max_iter=100, eps=1e-8, q=50, \
display=0, stop=1e-8):
'''
Frank-Wolfe algorithm on the heterogeneous game
given a list of graphs in the format
g = [[link_id from to a0 a1 a2 a3 a4]]
and demand in the format
d = [[o d flow]]
'''
assert past <= q, "'q' must be bigger or equal to 'past'"
# construct graph and demand objects suiteable for AoN_igraph
gs = [construct_igraph(graph) for graph in graphs]
ods = [construct_od(demand) for demand in demands]
# construct empty vector to be filled in with values
links = graphs[0].shape[0]
types = len(graphs)
f = np.zeros(links*types,dtype="float64") # initial flow assignment is null
fs = np.zeros((links*types,past),dtype="float64")
L = np.zeros(links*types,dtype="float64")
grad = np.zeros(links*types,dtype="float64")
L2 = np.zeros(links*types,dtype="float64")
grad2 = np.zeros(links*types,dtype="float64")
h = defaultdict(np.float64) # initial path flow assignment is null
hs = defaultdict(lambda : [0. for _ in range(past)]) # initial path flow assignment is null
# compute re-normalization constant
K = sum([total_free_flow_cost(g, od) for g,od in zip(gs, ods)])
if K < eps:
K = sum([np.sum(demand[:,2]) for demand in demands])
elif display >= 1:
print 'average free-flow travel time', \
K / sum([np.sum(demand[:,2]) for demand in demands])
# compute iterations
start_time = timeit.default_timer()
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
#print 'f', f
#print 'reshape', np.reshape(f,(links,types))
total_f = np.sum(np.reshape(f,(types,links)).T,1)
#print 'total flow', total_f
L, grad, path_flows = search_direction_multi(f, graphs, gs, ods, L, grad)
#print 'L', L
#print 'grad', grad
fs[:,i%past] = L
for k in set(h.keys()).union(set(path_flows.keys())):
hs[k][i%past] = path_flows[k]
w = L - f
w_h = defaultdict(np.float64)
for k in set(h.keys()).union(set(path_flows.keys())):
w_h[k] = path_flows[k] - h[k]
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 np.reshape(f,(types,links)).T
if i > q:
# step 3 of Fukushima
v = np.sum(fs,axis=1) / min(past,i+1) - f
v_h = np.defaultdict(np.float64)
for k in set(hs.keys()).union(set(path_flows.keys())):
v_h[k] = sum(hs[k]) / min(past,i+1) - h[k]
norm_v = np.linalg.norm(v,1)
if norm_v < eps:
if display >= 1: print 'stop with norm_v: {}'.format(norm_v)
return np.reshape(f,(types,links)).T
norm_w = np.linalg.norm(w,1)
if norm_w < eps:
if display >= 1: print 'stop with norm_w: {}'.format(norm_w)
return np.reshape(f,(types,links)).T
# 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 np.reshape(f,(types,links)).T
d = v if gamma_1 < gamma_2 else w
d_h = v_h if gamma_1 < gamma_2 else w_h
# step 5 of Fukushima
s = line_search(lambda a: merit(f+a*d, graphs, gs, ods, L2, grad2))
# print 'step', s
if s < eps:
if display >= 1: print 'stop with step_size: {}'.format(s)
return np.reshape(f,(types,links)).T
f = f + s*d
for k in set(hs.keys()).union(set(path_flows.keys())):
h[k] = h[k] + s*d_h[k]
else:
f = f + 2. * w/(i+2.)
for k in set(h.keys()).union(set(path_flows.keys())):
h[k] = h[k] + 2.*(w_h[k])/(i+2.)
print 'iteration', i
print 'time(sec):', timeit.default_timer() - start_time;
print 'num path flows:', len(h)
f_h = np.zeros(graph.shape[0],dtype='float64') # initial flow assignment is null
for k in h:
flow = h[k]
for link in k[2]:
f_h[link] += flow
print "path vs link flow diff:", np.sum(np.abs(f_h - np.sum(np.reshape(f,(types,links)).T,1))), f.shape
# find how many paths each od pair really has
od_paths = defaultdict(int)
most_paths = 0
for k in h.keys():
od_paths[(k[:2])] += 1
most_paths = max(most_paths, od_paths[(k[:2])])
path_counts = [0 for i in range(most_paths + 1)]
for k in od_paths.keys():
path_counts[od_paths[k]] += 1
for i in range(len(path_counts)):
print i, path_counts[i]
return np.reshape(f,(types,links)).T
def parametric_study_2(alphas, g, d, node, geometry, thres, cog_cost, output, \
stop=1e-2):
g_nr, small_capacity = multiply_cognitive_cost(g, geometry, thres, cog_cost)
if (type(alphas) is float) or (type(alphas) is int):
alphas = [alphas]
for alpha in alphas:
#special case where in fact homogeneous game
if alpha == 0.0:
print 'non-routed = 1.0, routed = 0.0'
f_nr = solver_3(g_nr, d, max_iter=1000, display=1, stop=stop)
fs=np.zeros((f_nr.shape[0],2))
fs[:,0]=f_nr
elif alpha == 1.0:
print 'non-routed = 0.0, routed = 1.0'
f_r = solver_3(g, d, max_iter=1000, display=1, stop=stop)
fs=np.zeros((f_r.shape[0],2))
fs[:,1]=f_r
#run solver
else:
print 'non-routed = {}, routed = {}'.format(1-alpha, alpha)
d_nr, d_r = heterogeneous_demand(d, alpha)
fs = fw_heterogeneous_2([g_nr,g], [d_nr,d_r], max_iter=1000, \
display=1, stop=stop)
np.savetxt(output.format(int(alpha*100)), fs, \
delimiter=',', header='f_nr,f_r')
def parametric_study_3(alphas, beta, g, d, node, geometry, thres, cog_cost, output, \
stop=1e-2):
g_nr, small_capacity = multiply_cognitive_cost(g, geometry, thres, cog_cost)
if (type(alphas) is float) or (type(alphas) is int):
alphas = [alphas]
for alpha in alphas:
#special case where in fact homogeneous game
if alpha == 0.0:
print 'non-routed = 1.0, routed = 0.0'
f_nr = solver_3(g_nr, d, max_iter=1000, display=1, stop=stop)
fs=np.zeros((f_nr.shape[0],2))
fs[:,0]=f_nr
elif alpha == 1.0:
print 'non-routed = 0.0, routed = 1.0'
f_r = solver_3(g, d, max_iter=1000, display=1, stop=stop)
fs=np.zeros((f_r.shape[0],2))
fs[:,1]=f_r
#run solver
else:
print 'non-routed = {}, routed = {}'.format(1-alpha, alpha)
d_nr, d_r = heterogeneous_demand(d, alpha)
fs = fw_heterogeneous_1([g_nr,g], [d_nr,d_r], max_iter=1000, \
display=1, stop=stop)
np.savetxt(output.format(int(alpha*100),int(beta*100)), fs, \
delimiter=',', header='f_nr,f_r')
import json
import cPickle as pickle
def main():
for alpha in [.75]:
# for alpha in np.linspace(0, .49, 50):
# for alpha in np.linspace(.5, .99, 50):
print "ALPHA:", alpha
start_time2 = timeit.default_timer()
graph = np.loadtxt('data/LA_net.csv', delimiter=',', skiprows=1)
demand = np.loadtxt('data/LA_od_2.csv', delimiter=',', skiprows=1)
graph[10787,-1] = graph[10787,-1] / (1.5**4)
graph[3348,-1] = graph[3348,-1] / (1.2**4)
node = np.loadtxt('data/LA_node.csv', delimiter=',')
features = extract_features('data/LA_net.txt')
# graph = np.loadtxt('data/Chicago_net.csv', delimiter=',', skiprows=1)
# demand = np.loadtxt('data/Chicago_od.csv', delimiter=',', skiprows=1)
# node = np.loadtxt('data/Chicago_node.csv', delimiter=',', skiprows=1)
# features = extract_features('data/ChicagoSketch_net.txt')
# features = table in the format [[capacity, length, FreeFlowTime]]
# alpha = .2 # also known as r
thres = 1000.
cog_cost = 3000.
demand[:,2] = 0.5*demand[:,2] / 4000
g_nr, small_capacity = multiply_cognitive_cost(graph, features, thres, cog_cost)
d_nr, d_r = heterogeneous_demand(demand, alpha)
fs, hs, n_d = fw_heterogeneous_1([graph, g_nr], [d_r,d_nr], alpha, max_iter=30, display=1)
print n_d
output = {
'f': fs,
'h': hs,
'n_d': n_d
}
with open('graph_stuff/LA_net_od_2_alpha_{}.txt'.format(alpha), 'w') as outfile:
# with open('graph_stuff/Chicago_net_od_2_alpha_{}.txt'.format(alpha), 'w') as outfile:
outfile.write(pickle.dumps(output))
#end of timer
elapsed2 = timeit.default_timer() - start_time2;
print ("Execution took %s seconds" % elapsed2)
# visualize_LA()
if __name__ == '__main__':
main()