-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathassignment.py
executable file
·596 lines (489 loc) · 23.6 KB
/
assignment.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import heapq
import math
import time
import networkx as nx
import scipy
from network_import import *
from utils import PathUtils
class FlowTransportNetwork:
def __init__(self):
self.linkSet = {}
self.nodeSet = {}
self.tripSet = {}
self.zoneSet = {}
self.originZones = {}
self.networkx_graph = None
def to_networkx(self):
if self.networkx_graph is None:
self.networkx_graph = nx.DiGraph([(int(begin),int(end)) for (begin,end) in self.linkSet.keys()])
return self.networkx_graph
def reset_flow(self):
for link in self.linkSet.values():
link.reset_flow()
def reset(self):
for link in self.linkSet.values():
link.reset()
class Zone:
def __init__(self, zoneId: str):
self.zoneId = zoneId
self.lat = 0
self.lon = 0
self.destList = [] # list of zone ids (strs)
class Node:
"""
This class has attributes associated with any node
"""
def __init__(self, nodeId: str):
self.Id = nodeId
self.lat = 0
self.lon = 0
self.outLinks = [] # list of node ids (strs)
self.inLinks = [] # list of node ids (strs)
# For Dijkstra
self.label = np.inf
self.pred = None
class Link:
"""
This class has attributes associated with any link
"""
def __init__(self,
init_node: str,
term_node: str,
capacity: float,
length: float,
fft: float,
b: float,
power: float,
speed_limit: float,
toll: float,
linkType
):
self.init_node = init_node
self.term_node = term_node
self.max_capacity = float(capacity) # veh per hour
self.length = float(length) # Length
self.fft = float(fft) # Free flow travel time (min)
self.beta = float(power)
self.alpha = float(b)
self.speedLimit = float(speed_limit)
self.toll = float(toll)
self.linkType = linkType
self.curr_capacity_percentage = 1
self.capacity = self.max_capacity
self.flow = 0.0
self.cost = self.fft
# Method not used for assignment
def modify_capacity(self, delta_percentage: float):
assert -1 <= delta_percentage <= 1
self.curr_capacity_percentage += delta_percentage
self.curr_capacity_percentage = max(0, min(1, self.curr_capacity_percentage))
self.capacity = self.max_capacity * self.curr_capacity_percentage
def reset(self):
self.curr_capacity_percentage = 1
self.capacity = self.max_capacity
self.reset_flow()
def reset_flow(self):
self.flow = 0.0
self.cost = self.fft
class Demand:
def __init__(self,
init_node: str,
term_node: str,
demand: float
):
self.fromZone = init_node
self.toNode = term_node
self.demand = float(demand)
def DijkstraHeap(origin, network: FlowTransportNetwork):
"""
Calcualtes shortest path from an origin to all other destinations.
The labels and preds are stored in node instances.
"""
for n in network.nodeSet:
network.nodeSet[n].label = np.inf
network.nodeSet[n].pred = None
network.nodeSet[origin].label = 0.0
network.nodeSet[origin].pred = None
SE = [(0, origin)]
while SE:
currentNode = heapq.heappop(SE)[1]
currentLabel = network.nodeSet[currentNode].label
for toNode in network.nodeSet[currentNode].outLinks:
link = (currentNode, toNode)
newNode = toNode
newPred = currentNode
existingLabel = network.nodeSet[newNode].label
newLabel = currentLabel + network.linkSet[link].cost
if newLabel < existingLabel:
heapq.heappush(SE, (newLabel, newNode))
network.nodeSet[newNode].label = newLabel
network.nodeSet[newNode].pred = newPred
def BPRcostFunction(optimal: bool,
fft: float,
alpha: float,
flow: float,
capacity: float,
beta: float,
length: float,
maxSpeed: float
) -> float:
if capacity < 1e-3:
return np.finfo(np.float32).max
if optimal:
return fft * (1 + (alpha * math.pow((flow * 1.0 / capacity), beta)) * (beta + 1))
return fft * (1 + alpha * math.pow((flow * 1.0 / capacity), beta))
def constantCostFunction(optimal: bool,
fft: float,
alpha: float,
flow: float,
capacity: float,
beta: float,
length: float,
maxSpeed: float
) -> float:
if optimal:
return fft + flow
return fft
def greenshieldsCostFunction(optimal: bool,
fft: float,
alpha: float,
flow: float,
capacity: float,
beta: float,
length: float,
maxSpeed: float
) -> float:
if capacity < 1e-3:
return np.finfo(np.float32).max
if optimal:
return (length * (capacity ** 2)) / (maxSpeed * (capacity - flow) ** 2)
return length / (maxSpeed * (1 - (flow / capacity)))
def updateTravelTime(network: FlowTransportNetwork, optimal: bool = False, costFunction=BPRcostFunction):
"""
This method updates the travel time on the links with the current flow
"""
for l in network.linkSet:
network.linkSet[l].cost = costFunction(optimal,
network.linkSet[l].fft,
network.linkSet[l].alpha,
network.linkSet[l].flow,
network.linkSet[l].capacity,
network.linkSet[l].beta,
network.linkSet[l].length,
network.linkSet[l].speedLimit
)
def findAlpha(x_bar, network: FlowTransportNetwork, optimal: bool = False, costFunction=BPRcostFunction):
"""
This uses unconstrained optimization to calculate the optimal step size required
for Frank-Wolfe Algorithm
"""
def df(alpha):
assert 0 <= alpha <= 1
sum_derivative = 0 # this line is the derivative of the objective function.
for l in network.linkSet:
tmpFlow = alpha * x_bar[l] + (1 - alpha) * network.linkSet[l].flow
tmpCost = costFunction(optimal,
network.linkSet[l].fft,
network.linkSet[l].alpha,
tmpFlow,
network.linkSet[l].capacity,
network.linkSet[l].beta,
network.linkSet[l].length,
network.linkSet[l].speedLimit
)
sum_derivative = sum_derivative + (x_bar[l] - network.linkSet[l].flow) * tmpCost
return sum_derivative
sol = scipy.optimize.root_scalar(df, x0=np.array([0.5]), bracket=(0, 1))
assert 0 <= sol.root <= 1
return sol.root
def tracePreds(dest, network: FlowTransportNetwork):
"""
This method traverses predecessor nodes in order to create a shortest path
"""
prevNode = network.nodeSet[dest].pred
spLinks = []
while prevNode is not None:
spLinks.append((prevNode, dest))
dest = prevNode
prevNode = network.nodeSet[dest].pred
return spLinks
def loadAON(network: FlowTransportNetwork, computeXbar: bool = True):
"""
This method produces auxiliary flows for all or nothing loading.
"""
x_bar = {l: 0.0 for l in network.linkSet}
SPTT = 0.0
for r in network.originZones:
DijkstraHeap(r, network=network)
for s in network.zoneSet[r].destList:
dem = network.tripSet[r, s].demand
if dem <= 0:
continue
SPTT = SPTT + network.nodeSet[s].label * dem
if computeXbar and r != s:
for spLink in tracePreds(s, network):
x_bar[spLink] = x_bar[spLink] + dem
return SPTT, x_bar
def readDemand(demand_df: pd.DataFrame, network: FlowTransportNetwork):
for index, row in demand_df.iterrows():
init_node = str(int(row["init_node"]))
term_node = str(int(row["term_node"]))
demand = row["demand"]
network.tripSet[init_node, term_node] = Demand(init_node, term_node, demand)
if init_node not in network.zoneSet:
network.zoneSet[init_node] = Zone(init_node)
if term_node not in network.zoneSet:
network.zoneSet[term_node] = Zone(term_node)
if term_node not in network.zoneSet[init_node].destList:
network.zoneSet[init_node].destList.append(term_node)
print(len(network.tripSet), "OD pairs")
print(len(network.zoneSet), "OD zones")
def readNetwork(network_df: pd.DataFrame, network: FlowTransportNetwork):
for index, row in network_df.iterrows():
init_node = str(int(row["init_node"]))
term_node = str(int(row["term_node"]))
capacity = row["capacity"]
length = row["length"]
free_flow_time = row["free_flow_time"]
b = row["b"]
power = row["power"]
speed = row["speed"]
toll = row["toll"]
link_type = row["link_type"]
network.linkSet[init_node, term_node] = Link(init_node=init_node,
term_node=term_node,
capacity=capacity,
length=length,
fft=free_flow_time,
b=b,
power=power,
speed_limit=speed,
toll=toll,
linkType=link_type
)
if init_node not in network.nodeSet:
network.nodeSet[init_node] = Node(init_node)
if term_node not in network.nodeSet:
network.nodeSet[term_node] = Node(term_node)
if term_node not in network.nodeSet[init_node].outLinks:
network.nodeSet[init_node].outLinks.append(term_node)
if init_node not in network.nodeSet[term_node].inLinks:
network.nodeSet[term_node].inLinks.append(init_node)
print(len(network.nodeSet), "nodes")
print(len(network.linkSet), "links")
def get_TSTT(network: FlowTransportNetwork, costFunction=BPRcostFunction, use_max_capacity: bool = True):
TSTT = round(sum([network.linkSet[a].flow * costFunction(optimal=False,
fft=network.linkSet[
a].fft,
alpha=network.linkSet[
a].alpha,
flow=network.linkSet[
a].flow,
capacity=network.linkSet[
a].max_capacity if use_max_capacity else network.linkSet[
a].capacity,
beta=network.linkSet[
a].beta,
length=network.linkSet[
a].length,
maxSpeed=network.linkSet[
a].speedLimit
) for a in
network.linkSet]), 9)
return TSTT
def assignment_loop(network: FlowTransportNetwork,
algorithm: str = "FW",
systemOptimal: bool = False,
costFunction=BPRcostFunction,
accuracy: float = 0.001,
maxIter: int = 1000,
maxTime: int = 60,
verbose: bool = True):
"""
For explaination of the algorithm see Chapter 7 of:
https://sboyles.github.io/blubook.html
PDF:
https://sboyles.github.io/teaching/ce392c/book.pdf
"""
network.reset_flow()
iteration_number = 1
gap = np.inf
TSTT = np.inf
assignmentStartTime = time.time()
# Check if desired accuracy is reached
while gap > accuracy:
# Get x_bar throug all-or-nothing assignment
_, x_bar = loadAON(network=network)
if algorithm == "MSA" or iteration_number == 1:
alpha = (1 / iteration_number)
elif algorithm == "FW":
# If using Frank-Wolfe determine the step size alpha by solving a nonlinear equation
alpha = findAlpha(x_bar,
network=network,
optimal=systemOptimal,
costFunction=costFunction)
else:
print("Terminating the program.....")
print("The solution algorithm ", algorithm, " does not exist!")
raise TypeError('Algorithm must be MSA or FW')
# Apply flow improvement
for l in network.linkSet:
network.linkSet[l].flow = alpha * x_bar[l] + (1 - alpha) * network.linkSet[l].flow
# Compute the new travel time
updateTravelTime(network=network,
optimal=systemOptimal,
costFunction=costFunction)
# Compute the relative gap
SPTT, _ = loadAON(network=network, computeXbar=False)
SPTT = round(SPTT, 9)
TSTT = round(sum([network.linkSet[a].flow * network.linkSet[a].cost for a in
network.linkSet]), 9)
# print(TSTT, SPTT, "TSTT, SPTT, Max capacity", max([l.capacity for l in network.linkSet.values()]))
gap = (TSTT / SPTT) - 1
if gap < 0:
print("Error, gap is less than 0, this should not happen")
print("TSTT", "SPTT", TSTT, SPTT)
# Uncomment for debug
# print("Capacities:", [l.capacity for l in network.linkSet.values()])
# print("Flows:", [l.flow for l in network.linkSet.values()])
# Compute the real total travel time (which in the case of system optimal rounting is different from the TSTT above)
TSTT = get_TSTT(network=network, costFunction=costFunction)
iteration_number += 1
if iteration_number > maxIter:
if verbose:
print(
"The assignment did not converge to the desired gap and the max number of iterations has been reached")
print("Assignment took", round(time.time() - assignmentStartTime, 5), "seconds")
print("Current gap:", round(gap, 5))
return TSTT
if time.time() - assignmentStartTime > maxTime:
if verbose:
print("The assignment did not converge to the desired gap and the max time limit has been reached")
print("Assignment did ", iteration_number, "iterations")
print("Current gap:", round(gap, 5))
return TSTT
if verbose:
print("Assignment converged in ", iteration_number, "iterations")
print("Assignment took", round(time.time() - assignmentStartTime, 5), "seconds")
print("Current gap:", round(gap, 5))
return TSTT
def writeResults(network: FlowTransportNetwork, output_file: str, costFunction=BPRcostFunction,
systemOptimal: bool = False, verbose: bool = True):
outFile = open(output_file, "w")
TSTT = get_TSTT(network=network, costFunction=costFunction)
if verbose:
print("\nTotal system travel time:", f'{TSTT} secs')
tmpOut = "Total Travel Time:\t" + str(TSTT)
outFile.write(tmpOut + "\n")
tmpOut = "Cost function used:\t" + BPRcostFunction.__name__
outFile.write(tmpOut + "\n")
tmpOut = ["User equilibrium (UE) or system optimal (SO):\t"] + ["SO" if systemOptimal else "UE"]
outFile.write("".join(tmpOut) + "\n\n")
tmpOut = "init_node\tterm_node\tflow\ttravelTime"
outFile.write(tmpOut + "\n")
for i in network.linkSet:
tmpOut = str(network.linkSet[i].init_node) + "\t" + str(
network.linkSet[i].term_node) + "\t" + str(
network.linkSet[i].flow) + "\t" + str(costFunction(False,
network.linkSet[i].fft,
network.linkSet[i].alpha,
network.linkSet[i].flow,
network.linkSet[i].max_capacity,
network.linkSet[i].beta,
network.linkSet[i].length,
network.linkSet[i].speedLimit
))
outFile.write(tmpOut + "\n")
outFile.close()
def load_network(net_file: str,
demand_file: str = None,
force_net_reprocess: bool = False,
verbose: bool = True
) -> FlowTransportNetwork:
readStart = time.time()
if demand_file is None:
demand_file = '_'.join(net_file.split("_")[:-1] + ["trips.tntp"])
net_name = net_file.split("/")[-1].split("_")[0]
if verbose:
print(f"Loading network {net_name}...")
net_df, demand_df = import_network(
net_file,
demand_file,
force_reprocess=force_net_reprocess
)
network = FlowTransportNetwork()
readDemand(demand_df, network=network)
readNetwork(net_df, network=network)
network.originZones = set([k[0] for k in network.tripSet])
if verbose:
print("Network", net_name, "loaded")
print("Reading the network data took", round(time.time() - readStart, 2), "secs\n")
return network
def computeAssingment(net_file: str,
demand_file: str = None,
algorithm: str = "FW", # FW or MSA
costFunction=BPRcostFunction,
systemOptimal: bool = False,
accuracy: float = 0.0001,
maxIter: int = 1000,
maxTime: int = 60,
results_file: str = None,
force_net_reprocess: bool = False,
verbose: bool = True
) -> float:
"""
This is the main function to compute the user equilibrium UE (default) or system optimal (SO) traffic assignment
All the networks present on https://github.com/bstabler/TransportationNetworks following the tntp format can be loaded
:param net_file: Name of the network (net) file following the tntp format (see https://github.com/bstabler/TransportationNetworks)
:param demand_file: Name of the demand (trips) file following the tntp format (see https://github.com/bstabler/TransportationNetworks), leave None to use dafault demand file
:param algorithm:
- "FW": Frank-Wolfe algorithm (see https://en.wikipedia.org/wiki/Frank%E2%80%93Wolfe_algorithm)
- "MSA": Method of successive averages
For more information on how the algorithms work see https://sboyles.github.io/teaching/ce392c/book.pdf
:param costFunction: Which cost function to use to compute travel time on edges, currently available functions are:
- BPRcostFunction (see https://rdrr.io/rforge/travelr/man/bpr.function.html)
- greenshieldsCostFunction (see Greenshields, B. D., et al. "A study of traffic capacity." Highway research board proceedings. Vol. 1935. National Research Council (USA), Highway Research Board, 1935.)
- constantCostFunction
:param systemOptimal: Wheather to compute the system optimal flows instead of the user equilibrium
:param accuracy: Desired assignment precision gap
:param maxIter: Maximum nuber of algorithm iterations
:param maxTime: Maximum seconds allowed for the assignment
:param results_file: Name of the desired file to write the results,
by default the result file is saved with the same name as the input network with the suffix "_flow.tntp" in the same folder
:param force_net_reprocess: True if the network files should be reprocessed from the tntp sources
:param verbose: print useful info in standard output
:return: Totoal system travel time
"""
network = load_network(net_file=net_file, demand_file=demand_file, verbose=verbose, force_net_reprocess=force_net_reprocess)
if verbose:
print("Computing assignment...")
TSTT = assignment_loop(network=network, algorithm=algorithm, systemOptimal=systemOptimal, costFunction=costFunction,
accuracy=accuracy, maxIter=maxIter, maxTime=maxTime, verbose=verbose)
if results_file is None:
results_file = '_'.join(net_file.split("_")[:-1] + ["flow.tntp"])
writeResults(network=network,
output_file=results_file,
costFunction=costFunction,
systemOptimal=systemOptimal,
verbose=verbose)
return TSTT
if __name__ == '__main__':
# This is an example usage for calculating System Optimal and User Equilibrium with Frank-Wolfe
net_file = str(PathUtils.sioux_falls_net_file)
total_system_travel_time_optimal = computeAssingment(net_file=net_file,
algorithm="FW",
costFunction=BPRcostFunction,
systemOptimal=True,
verbose=True,
accuracy=0.00001,
maxIter=1000,
maxTime=6000000)
total_system_travel_time_equilibrium = computeAssingment(net_file=net_file,
algorithm="FW",
costFunction=BPRcostFunction,
systemOptimal=False,
verbose=True,
accuracy=0.001,
maxIter=1000,
maxTime=6000000)
print("UE - SO = ", total_system_travel_time_equilibrium - total_system_travel_time_optimal)