-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.py
More file actions
executable file
·923 lines (793 loc) · 38.8 KB
/
assignment.py
File metadata and controls
executable file
·923 lines (793 loc) · 38.8 KB
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
import math
import time
import heapq
import networkx as nx
import numpy as np
from scipy.optimize import fsolve, minimize
import warnings
from network_import import *
from utils import PathUtils
warnings.filterwarnings('ignore', 'The iteration is not making good progress')
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 BPRcostFunctionDerivative(optimal: bool,
fft: float,
alpha: float,
flow: float,
capacity: float,
beta: float,
length: float,
maxSpeed: float
) -> float:
return fft * alpha * beta * math.pow((flow * 1.0 / capacity), beta - 1) / capacity
def BPRcostFunctionIntegral(optimal: bool,
fft: float,
alpha: float,
flow: float,
capacity: float,
beta: float,
length: float,
maxSpeed: float
) -> float:
return fft * (flow + alpha * flow * math.pow((flow * 1.0 / capacity), beta) / (1 + beta))
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 sum(alpha):
# alpha = max(0, min(1, alpha))
sum_integral = 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 = BPRcostFunctionIntegral(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_integral = sum_integral + tmpCost
return sum_integral
sol = minimize(sum, np.array([0.5]), tol=1e-10)
return max(0, min(1, sol.x[0]))
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"]
if init_node == term_node or demand == 0: continue
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 calculate_conjugate_beta(network: FlowTransportNetwork, d_FW, d_bar, optimal: bool = False):
beta_numerator = np.sum([d_bar[l] * d_FW[l] * BPRcostFunctionDerivative(
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) for l in network.linkSet])
beta_denominator = np.sum([d_bar[l] * (d_FW[l] - d_bar[l]) * BPRcostFunctionDerivative(
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) for l in network.linkSet])
if beta_denominator == 0:
beta = 0
else:
beta = beta_numerator / beta_denominator
if beta > 1 - 1e-9:
beta = 1 - 1e-9
elif beta < 0:
beta = 0
return beta
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,
output_file: str = None):
"""
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
gaps = []
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 == "CFW":
d_FW = {l: x_bar[l] - network.linkSet[l].flow for l in network.linkSet}
if iteration_number == 1:
d_CFW = d_FW
else:
d_bar = {l: (1 - alpha) * d_CFW[l] for l in network.linkSet}
beta = calculate_conjugate_beta(network, d_FW, d_bar, systemOptimal)
d_CFW = {l: d_FW[l] + beta * (d_bar[l] - d_FW[l]) for l in network.linkSet}
x_bar = {l: d_CFW[l] + network.linkSet[l].flow for l in network.linkSet}
if algorithm == "MSA" or iteration_number == 1:
alpha = (2 / (iteration_number + 1))
elif algorithm in ["FW", "CFW"]:
# 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, FW or CFW')
# 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
runTime = time.time() - assignmentStartTime
gaps.append([runTime, gap])
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))
verbose = False
break
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))
verbose = False
break
if verbose:
print("Assignment converged in ", iteration_number, "iterations")
print("Assignment took", round(time.time() - assignmentStartTime, 5), "seconds")
print("Current gap:", round(gap, 5))
if output_file is not None:
np.savetxt(f"./iteration_gaps/{output_file}", np.array(gaps), fmt="%.9f")
return TSTT
class Route:
def __init__(self):
self.r = -1
self.s = -1
self.cost = np.inf
self.links = []
self.flow = 0
def cal_shortest_routes(network: FlowTransportNetwork):
"""
This method calculates shortest route for all OD pairs.
"""
shortest_routes = {}
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
route = Route()
route.r = r
route.s = s
route.cost = network.nodeSet[s].label
if r != s: route.links = tracePreds(s, network)
shortest_routes[(r, s)] = route
return shortest_routes
def update_network_flow(network: FlowTransportNetwork, routes: dict):
network.reset_flow()
for OD, route_list in routes.items():
for route in route_list:
for link in route.links:
network.linkSet[link].flow += route.flow
def update_routes_cost(network: FlowTransportNetwork, routes: dict):
for OD, route_list in routes.items():
for route in route_list:
route.cost = 0
for link in route.links:
route.cost += network.linkSet[link].cost
def calculate_second_derivative(network: FlowTransportNetwork, route: Route, shortest_route: Route):
h = 0
route_set = set(route.links)
shortest_route_set = set(shortest_route.links)
link_set = route_set ^ shortest_route_set
for l in link_set:
link = network.linkSet[l]
h += BPRcostFunctionDerivative(
False,
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)
return h
def obtain_step_direction(routes, shortest_routes, network: FlowTransportNetwork):
step_direction = {(OD, route_idx): 0 for OD, OD_routes in routes for route_idx in range(len(OD_routes))}
for OD, OD_routes in routes.items():
shortest_route = shortest_routes[OD]
route_idx = 0
for route in OD_routes:
if len(set(route.links) ^ set(shortest_route.links)) == 0:
shortest_route = route
step_direction[(OD, route_idx)] = 0
else:
h = calculate_second_derivative(network, route, shortest_route)
if h == 0:
step_direction[(OD, route_idx)] = np.inf
else:
step_direction[(OD, route_idx)] = (route.cost - shortest_route.cost) / h
route_idx += 1
return step_direction
def exact_line_search_for_path_based_gp(routes, shortest_routes, network: FlowTransportNetwork):
"""
This uses unconstrained optimization to calculate the optimal step size required
for GP Algorithm
"""
step_direction = obtain_step_direction(routes, shortest_routes, network)
def sum(alpha):
sum_integral = 0 # this line is the derivative of the objective function.
tmp_link_flow = {l: 0 for l in network.linkSet}
for OD, OD_routes in routes.items():
shortest_route = shortest_routes[OD]
flow_sum = 0
route_idx = 0
for route in OD_routes:
tmp_route_flow = max(0, route.flow - alpha * step_direction[(OD, route_idx)])
flow_sum += tmp_route_flow
for link in route.links:
tmp_link_flow[link] += tmp_route_flow
route_idx += 1
tmp_shortest_route_flow = network.tripSet[shortest_route.r, shortest_route.s].demand - flow_sum
for link in shortest_route.links:
tmp_link_flow[link] += tmp_shortest_route_flow
for l, tmpFlow in tmp_link_flow.items():
tmpCost = BPRcostFunctionIntegral(False,
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_integral = sum_integral + tmpCost
return sum_integral
sol = minimize(sum, x0=np.array([0.1]), tol=1e-3)
alpha = max(0, min(1, sol.x[0]))
if alpha == 0: alpha = 1e-2
return alpha, step_direction
def path_based_gp_method(network: FlowTransportNetwork,
algorithm: str = "GP",
systemOptimal: bool = False,
costFunction=BPRcostFunction,
accuracy: float = 0.001,
maxIter: int = 1000,
maxTime: int = 60,
verbose: bool = True,
output_file: str = None,
step_size: float = 0.05):
"""
Ref: A Faster Path-Based Algorithm for Traffic Assignment
"""
network.reset_flow()
iteration_number = 1
gap = np.inf
gaps = []
TSTT = np.inf
assignmentStartTime = time.time()
routes = {(r, s): [] for r in network.originZones for s in network.zoneSet[r].destList }
# Check if desired accuracy is reached
while gap > accuracy:
shortest_routes = cal_shortest_routes(network)
if iteration_number == 1:
for OD, route in shortest_routes.items():
route.flow = network.tripSet[route.r, route.s].demand
routes[OD].append(route)
else:
if algorithm == "GP-E":
alpha, step_direction = exact_line_search_for_path_based_gp(routes, shortest_routes, network)
else:
alpha = step_size
step_direction = obtain_step_direction(routes, shortest_routes, network)
for OD, OD_routes in routes.items():
shortest_route = shortest_routes[OD]
is_append = True
flow_sum = 0
route_idx = 0
for route in OD_routes:
if step_direction[(OD, route_idx)] == 0:
shortest_route = route
is_append = False
else:
route.flow = max(0, route.flow - alpha * step_direction[(OD, route_idx)])
flow_sum += route.flow
route_idx += 1
shortest_route.flow = network.tripSet[shortest_route.r, shortest_route.s].demand - flow_sum
if is_append: OD_routes.append(shortest_route)
# Compute the new travel time
update_network_flow(network, routes)
updateTravelTime(network=network, optimal=systemOptimal, costFunction=costFunction)
update_routes_cost(network, routes)
# 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
runTime = time.time() - assignmentStartTime
gaps.append([runTime, gap])
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))
verbose = False
break
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))
verbose = False
break
if verbose:
print("Assignment converged in ", iteration_number, "iterations")
print("Assignment took", round(time.time() - assignmentStartTime, 5), "seconds")
print("Current gap:", round(gap, 5))
if output_file is not None:
np.savetxt(f"./iteration_gaps/{output_file}", np.array(gaps), fmt="%.9f")
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,
step_size = 0.05,
) -> 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: Total system travel time
"""
network = load_network(net_file=net_file, demand_file=demand_file, verbose=verbose, force_net_reprocess=force_net_reprocess)
iteration_gaps_file = '_'.join(net_file.split("/")[-1].split("_")[:-1] + [algorithm] + ["gaps.csv"])
if verbose:
print("Computing assignment...")
if algorithm in ["FW", "MSA", "CFW"]:
TSTT = assignment_loop(network=network, algorithm=algorithm, systemOptimal=systemOptimal, costFunction=costFunction,
accuracy=accuracy, maxIter=maxIter, maxTime=maxTime, verbose=verbose, output_file=iteration_gaps_file)
elif algorithm in ["GP", "GP-E"]:
TSTT = path_based_gp_method(network=network, algorithm=algorithm, systemOptimal=systemOptimal, costFunction=costFunction,
accuracy=accuracy, maxIter=maxIter, maxTime=maxTime, verbose=verbose, output_file=iteration_gaps_file,
step_size=step_size)
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_equilibrium_FW = computeAssingment(net_file=net_file,
algorithm="FW",
costFunction=BPRcostFunction,
systemOptimal=False,
verbose=True,
accuracy=0.000000001,
maxIter=20000,
maxTime=100)
total_system_travel_time_equilibrium_GP = computeAssingment(net_file=net_file,
algorithm="GP",
costFunction=BPRcostFunction,
systemOptimal=False,
verbose=True,
accuracy=0.000000001,
maxIter=20000,
maxTime=100,
step_size=0.05)
total_system_travel_time_equilibrium_GP_E = computeAssingment(net_file=net_file,
algorithm="GP-E",
costFunction=BPRcostFunction,
systemOptimal=False,
verbose=True,
accuracy=0.000000001,
maxIter=20000,
maxTime=100)
total_system_travel_time_equilibrium_CFW = computeAssingment(net_file=net_file,
algorithm="CFW",
costFunction=BPRcostFunction,
systemOptimal=False,
verbose=True,
accuracy=0.000000001,
maxIter=20000,
maxTime=100)
total_system_travel_time_equilibrium_MSA = computeAssingment(net_file=net_file,
algorithm="MSA",
costFunction=BPRcostFunction,
systemOptimal=False,
verbose=True,
accuracy=0.000000001,
maxIter=20000,
maxTime=100)
print("CFW - FW = ", total_system_travel_time_equilibrium_CFW - total_system_travel_time_equilibrium_FW)
print("MSA - FW = ", total_system_travel_time_equilibrium_MSA - total_system_travel_time_equilibrium_FW)
print("GP - FW = ", total_system_travel_time_equilibrium_GP - total_system_travel_time_equilibrium_FW)
print("GP-E - FW = ", total_system_travel_time_equilibrium_GP_E - total_system_travel_time_equilibrium_FW)