Traffic assignment algorithms can be broadly divided in link based and path based algorithms. Frank Wolfe, the most traditional algorithm is classified as link-based as its solution is the flow in each link; there is no information about the alternative routes for a given origin destination.
On the other hand, path-based algorithms provide the solution in path flows and the link flows are readily obtained by the path-link incidence matrix. ALthough the method varies, one advantage of path-based is a higher flexibility of the solution as it is possible to change the proportion of path flows for a given origin destination pair which does not happen in, for example, Frank-Wolfe algorithm.
Here the gradient projection algorithm is briefly presented.
*** import networkx as nx ***
*** import math
from scipy.misc import derivative ***
*** import visualize_graph as v ***
import networkx as nx
import math
#import random
import matplotlib.pyplot as plt
import os
import sys
import pytrans.UrbanNetworkAnalysis.visualize_graph as v
from scipy.misc import derivative
import pytrans.UrbanNetworkAnalysis.TransportationNetworks as loadProblem
*GradientProjection* (network, ods, use_simpy,SO , tol=1e-4 )
$min Z = \sum_{a} \int_{0}^{x_a} t_a(\omega)d\omega$
$\sum_{k} f_k^{rs}= q^{rs}$
$f_k^{rs} \geq 0$
$x_{a}= \sum_{\forall (r,s)} \sum_{k \in K_{rs}} f_{k}^{rs}\delta_{k}^{rs}(a) $
$\delta_{k}^{rs}(a)=1$ if link $a$ is in the route $k$ of pair $(r,s)$
$K_{rs}$ is the set of paths on pair (r,s) so $k \in K_{rs}$
The first is apply the all or nothing assignment based on $t_a=t_a(0)$ ($f_{rs}^1=q^{rs}$), add the path to the path-set.
Update $t^n=t_a(x_a^n)$ based on path-link incidence matrix and path flows.
Find the shortest paths from each origin $r$ to each destination $s$ based on $t_a(x_a^n)$; if not on the path set, add path to the path-set.
$f_k^{n+1,rs}=max \{0,f_k^{n,rs}-\frac{\alpha^n}{s_k^{n,rs}}(d_k^{n,rs}-\bar{d}_{rs}^\bar{k} \}$
where $\bar{d}_{rs}$ is the derivative of the shortest path (travel time) on iteration $k$.
$s_k^n = \sum_{a} \frac{d}{dt}t_a^n$ where a denotes links that are on either k or $\bar{k}_{rs}$ but not on both.
$f_{\bar{k}}^{n+1,rs}=q_{rs}-\sum_{k \neq \bar{k}}f_k^{n+1,rs}$
class GradientProjection(object):
def __init__(self, network, ods, use_simpy, SO, tol=1e-4):
self.network = network
self.ods = ods
self.step = 0.6
self.total_trips = 0
self.tol = tol
self.use_simpy = use_simpy
self.SO = SO
self.edges = self.getedges()
def getedges(self):
edges = []
for f in self.network.nodes():
for t in self.network[f]:
edges.append((f, t))
return edges
def solve(self):
solution = {}
by_id_links = {}
for (node_from, node_to) in self.edges:
l = self.network.get_edge_data(node_from, node_to)['object']
l.flow = 0
by_id_links[l.link_id] = l
od_pairs = []
for origin in self.ods:
paths = nx.shortest_path(self.network, origin, weight='time')
for destination in self.ods[origin]:
od_pairs.append((origin, destination))
node_list = paths[destination]
link_list = []
for ind in range(len(node_list) - 1):
node_from = node_list[ind]
node_to = node_list[ind + 1]
edge_data = self.network.get_edge_data(node_from, node_to)
edge_data['object'].flow += self.ods[origin][destination]
# by_id_links[edge_data['object'].link_id].flow += self.ods[origin][destination]
if self.SO == True:
edge_data['time'] = edge_data['object'].getmarginalcost_l(edge_data['object'].flow)
if edge_data['object'].getmarginalcost_l(edge_data['object'].flow) < 0:
raise ValueError
else:
edge_data['time'] = edge_data['object'].get_time()
link_list.append(edge_data['object'].link_id)
self.total_trips += self.ods[origin][destination]
solution[(origin, destination)] = {tuple(link_list): self.ods[origin][destination]}
for link_id in by_id_links:
edge_data = self.network.get_edge_data(by_id_links[link_id].from_node, by_id_links[link_id].to_node)
if self.SO == True:
edge_data['time'] = edge_data['object'].getmarginalcost_l(edge_data['object'].flow)
# if edge_data['object'].getmarginalcost_l(edge_data['object'].flow)<0:
# raise ValueError
else:
edge_data['time'] = edge_data['object'].get_time()
objetive_function_history = []
for i in range(300):
# random.shuffle(od_pairs)
for (origin, destination) in od_pairs:
# for origin in self.ods:
# for destination in self.ods[origin]:
paths = nx.shortest_path(self.network, origin, weight='time')
node_list = paths[destination]
link_sequence = []
for ind in range(len(node_list) - 1):
node_from = node_list[ind]
node_to = node_list[ind + 1]
edge_data = self.network.get_edge_data(node_from, node_to)
link = edge_data['object']
link_sequence.append(link.link_id)
link_sequence = tuple(link_sequence)
# costs, previous = dijkstra_helper.get_shortest_paths(origin)
# link_sequence = dijkstra_helper.get_link_sequence(origin, destination, previous)
# link_sequence = tuple(link_sequence)
if not link_sequence in solution[(origin, destination)]:
solution[(origin, destination)][link_sequence] = 0.0
derivatives = {}
s_paths = {}
for path in solution[(origin, destination)]:
e_derivative = 0.0
s = 0.0
for link_id in path:
link = by_id_links[link_id]
if self.use_simpy == False:
e_derivative += link.get_time()
else:
if self.SO == True:
e_derivative += link.getmarginalcost_l(link.flow)
else:
e_derivative += link.actualcost
if link_id not in link_sequence:
if self.use_simpy == True:
if self.SO == True:
diff = derivative(link.getmarginalcost_l, link.flow)
if diff < 0 or diff > 10000:
print("%.6f" % diff)
diff = 0.0001
s += diff
else:
s += derivative(link.getbprcost, link.flow)
a = math.pow(link.flow,
link.beta - 1) * link.beta * link.alpha * link.length / (
link.free_speed * math.pow(link.capacity, link.beta))
b = derivative(link.getbprcost, link.flow)
if a - b > 0.0001:
print("%.10f" % (a - b))
else:
s += math.pow(link.flow, link.beta - 1) * link.beta * link.alpha * link.length / (
link.free_speed * math.pow(link.capacity, link.beta)) # 1/31/2018 DS
derivatives[path] = e_derivative
if path != link_sequence:
for link_id in link_sequence:
if link_id not in path:
if self.use_simpy == True:
if self.SO == True:
s += derivative(link.getmarginalcost_l, link.flow)
else:
s += derivative(link.getbprcost, link.flow)
a = math.pow(link.flow,
link.beta - 1) * link.beta * link.alpha * link.length / (
link.free_speed * math.pow(link.capacity, link.beta))
b = derivative(link.getbprcost, link.flow)
if a - b > 0.0001:
print("%.10f" % (a - b))
else:
s += math.pow(link.flow, link.beta - 1) * link.beta * link.alpha * link.length / (
link.free_speed * math.pow(link.capacity, link.beta))
s_paths[path] = s
# s_paths[path] = 1.0
total_flow = 0.0
for path in s_paths:
try:
previous_flow = solution[(origin, destination)][path]
except:
previous_flow = 0.0
if derivatives[path] < derivatives[link_sequence]:
print('-- path', path)
print('--shortest', link_sequence)
print('--od', origin, destination)
print(derivatives[path], derivatives[link_sequence])
raise Exception
flow = solution[(origin, destination)][path] - self.step * (
derivatives[path] - derivatives[link_sequence]) / s_paths[path]
flow = max(0.0, flow)
total_flow += flow
if flow > 0:
solution[(origin, destination)][path] = flow
else:
del solution[origin, destination][path]
for link_id in path:
by_id_links[link_id].flow += (flow - previous_flow)
# if by_id_links[link_id].flow<0:
# raise ValueError
edge_data = self.network.get_edge_data(by_id_links[link_id].from_node,
by_id_links[link_id].to_node)
# if by_id_links[link_id].flow != link.flow:
# link.flow =by_id_links[link_id].flow
if self.SO == True:
edge_data['time'] = by_id_links[link_id].getmarginalcost_l(by_id_links[link_id].flow)
else:
edge_data['time'] = by_id_links[link_id].get_time()
try:
previous_flow = solution[(origin, destination)][link_sequence]
except:
previous_flow = 0.0
solution[(origin, destination)][link_sequence] = self.ods[origin][destination] - total_flow
if self.ods[origin][destination] - total_flow < 0:
raise ValueError
for link_id in link_sequence:
by_id_links[link_id].flow += (solution[(origin, destination)][link_sequence] - previous_flow)
# if by_id_links[link_id].flow - total_flow < 0:
# raise ValueError
edge_data = self.network.get_edge_data(by_id_links[link_id].from_node, by_id_links[link_id].to_node)
# if by_id_links[link_id].flow != link.flow:
# link.flow = by_id_links[link_id].flow
if self.SO == True:
edge_data['time'] = max(0, by_id_links[link_id].getmarginalcost_l(by_id_links[link_id].flow))
else:
edge_data['time'] = max(0, by_id_links[link_id].get_time())
objetive_function_history.append(self.convergence_test(solution, by_id_links))
if objetive_function_history[-1] < self.tol:
break
return solution, by_id_links, objetive_function_history
def get_bpr_objetive_function(self, solution):
s = 0.0
for edge in self.edges:
s += self.network.get_edge_data(edge[0], edge[1])['object'].get_bpr_objective_function()
return s
def get_total_traveltime(self, solution):
s = 0.0
for edge in self.edges:
s += self.network.get_edge_data(edge[0], edge[1])['object'].get_total_travel_time_function()
return s
def get_objetive_function_SO(self, solution):
s = 0.0
for edge in self.edges:
# print ("obj SO", edge[0], edge[1], self.network.get_edge_data(edge[0], edge[1])['object'].flow, self.network.get_edge_data(edge[0], edge[1])['object'].get_objective_function(), self.network.get_edge_data(edge[0], edge[1])['object'].get_objective_function_SO())
s += self.network.get_edge_data(edge[0], edge[1])['object'].get_objective_function_SO()
return s
def convergence_test(self, solution, by_link_id):
total_gap = 0
for origin in self.ods:
for destination in self.ods[origin]:
times = {}
min_t = 10E5
index_min = None
for path in solution[origin, destination]:
t = 0
for link_id in path:
link = by_link_id[link_id]
t += link.get_time()
times[path] = t
if t < min_t:
min_t = t
index_min = path
if len(solution[origin, destination]) <= 1:
continue
for path in solution[origin, destination]:
total_gap += solution[origin, destination][path] * (times[path] - min_t)
print("UE Objective:", self.get_bpr_objetive_function(solution), "SO Objective:",self.get_total_traveltime(solution))
return total_gap / (self.total_trips)
SiouxFalls Network for UE and SO will be examined to test the GP algorithm.
You can see the detailed information about Sioux-Falls Network in the Data Folder.
datafolder="./Data/TransportationNetworks/SiouxFalls/"
link_file = datafolder+"SiouxFalls_net.tntp"
trip_file = datafolder+"SiouxFalls_trips.tntp"
node_file =datafolder+"SiouxFalls_node.tntp"
visualization = False
bch = loadProblem.Network(link_file, trip_file, node_file)
# link_fields = {"from": 0, "to": 1, "capacity": 2, "length": 3, "t0": 4, "B": 5, "beta": 6, "V": 7}
# bch.link_fields = link_fields
bch.SO=True
use_simpy = False
if bch.SO==True:
use_simpy=True
# bch.open_link_file()
bch.open_trip_file()
graph, ods = bch.graph, bch.get_od_dic()
gp = GradientProjection(graph, ods, use_simpy, bch.SO, 1e-3)
solution, by_id_links, objetive_function_history_UE = gp.solve()
e_function_history_UE = gp.solve()
UE Objective: 4619072.39123 SO Objective: 8445685.04787 UE Objective: 4459175.3064 SO Objective: 7801589.10128 UE Objective: 4400679.27003 SO Objective: 7555324.3493 UE Objective: 4367173.18756 SO Objective: 7420676.41633 UE Objective: 4342987.37886 SO Objective: 7352874.3098 UE Objective: 4324246.11353 SO Objective: 7296696.3132 UE Objective: 4310880.31653 SO Objective: 7249129.54315 UE Objective: 4303720.65468 SO Objective: 7224676.33951 UE Objective: 4299868.96731 SO Objective: 7216660.75994 UE Objective: 4297805.72979 SO Objective: 7211529.72166 UE Objective: 4297401.31795 SO Objective: 7208066.18817 UE Objective: 4296981.27813 SO Objective: 7205125.86412 UE Objective: 4296635.27963 SO Objective: 7202464.30667 UE Objective: 4296125.60546 SO Objective: 7200618.32359 UE Objective: 4295668.66157 SO Objective: 7198927.07087 UE Objective: 4295329.95504 SO Objective: 7197301.73279 UE Objective: 4295125.11181 SO Objective: 7195746.57059 UE Objective: 4295053.43834 SO Objective: 7195237.37697 UE Objective: 4294754.86939 SO Objective: 7195102.04363 UE Objective: 4294503.70869 SO Objective: 7194983.90907 UE Objective: 4294450.90194 SO Objective: 7194876.87711 UE Objective: 4294484.20244 SO Objective: 7194792.53636 UE Objective: 4294529.72927 SO Objective: 7194724.20165 UE Objective: 4294582.38167 SO Objective: 7194666.46309 UE Objective: 4294635.00114 SO Objective: 7194616.35979 UE Objective: 4294683.87723 SO Objective: 7194572.13411 UE Objective: 4294712.69545 SO Objective: 7194532.9485 UE Objective: 4294740.40799 SO Objective: 7194498.2739 UE Objective: 4294766.08418 SO Objective: 7194467.02174 UE Objective: 4294797.86899 SO Objective: 7194438.48942 UE Objective: 4294831.71078 SO Objective: 7194412.06281 UE Objective: 4294875.22041 SO Objective: 7194389.96586 UE Objective: 4294931.82771 SO Objective: 7194372.33071 UE Objective: 4294981.83267 SO Objective: 7194357.30086 UE Objective: 4295019.05915 SO Objective: 7194344.9811 UE Objective: 4295058.28802 SO Objective: 7194334.35301 UE Objective: 4295096.6254 SO Objective: 7194325.17272 UE Objective: 4295135.922 SO Objective: 7194317.68672 UE Objective: 4295177.29855 SO Objective: 7194311.15641 UE Objective: 4295208.07032 SO Objective: 7194305.27664 UE Objective: 4295235.34553 SO Objective: 7194300.08274 UE Objective: 4295266.30108 SO Objective: 7194295.44661 UE Objective: 4295296.8227 SO Objective: 7194291.25001 UE Objective: 4295327.26889 SO Objective: 7194287.42542 UE Objective: 4295355.45175 SO Objective: 7194283.93569 UE Objective: 4295383.45781 SO Objective: 7194280.73673 UE Objective: 4295409.99673 SO Objective: 7194277.81983 UE Objective: 4295435.17042 SO Objective: 7194275.15583 UE Objective: 4295459.19463 SO Objective: 7194272.72031 UE Objective: 4295482.12864 SO Objective: 7194270.49322 UE Objective: 4295504.02555 SO Objective: 7194268.45655 UE Objective: 4295524.63182 SO Objective: 7194266.59101 UE Objective: 4295544.58884 SO Objective: 7194264.885 UE Objective: 4295563.77078 SO Objective: 7194263.32821 UE Objective: 4295582.04172 SO Objective: 7194261.90555 UE Objective: 4295599.54857 SO Objective: 7194260.60391 UE Objective: 4295616.29956 SO Objective: 7194259.41311 UE Objective: 4295632.10733 SO Objective: 7194258.31977 UE Objective: 4295644.40378 SO Objective: 7194257.47614 UE Objective: 4295649.30168 SO Objective: 7194257.14745 UE Objective: 4295653.66181 SO Objective: 7194256.91126 UE Objective: 4295656.58493 SO Objective: 7194256.73233 UE Objective: 4295659.00233 SO Objective: 7194256.59301 UE Objective: 4295660.81054 SO Objective: 7194256.48348 UE Objective: 4295662.02139 SO Objective: 7194256.39717 UE Objective: 4295662.91412 SO Objective: 7194256.32877 UE Objective: 4295663.67492 SO Objective: 7194256.27437 UE Objective: 4295664.23093 SO Objective: 7194256.23088 UE Objective: 4295664.90661 SO Objective: 7194256.19602 UE Objective: 4295665.29613 SO Objective: 7194256.16814 UE Objective: 4295665.7469 SO Objective: 7194256.14568 UE Objective: 4295666.13559 SO Objective: 7194256.12763 UE Objective: 4295666.47692 SO Objective: 7194256.11312 UE Objective: 4295666.78113 SO Objective: 7194256.10144 UE Objective: 4295667.05435 SO Objective: 7194256.09204 UE Objective: 4295667.30095 SO Objective: 7194256.08446 UE Objective: 4295667.52544 SO Objective: 7194256.07835 UE Objective: 4295667.73795 SO Objective: 7194256.07344 UE Objective: 4295667.9631 SO Objective: 7194256.06949 UE Objective: 4295668.12297 SO Objective: 7194256.06631 UE Objective: 4295668.2726 SO Objective: 7194256.06374 UE Objective: 4295668.41024 SO Objective: 7194256.06166 UE Objective: 4295668.53533 SO Objective: 7194256.05998 UE Objective: 4295668.64981 SO Objective: 7194256.05862 UE Objective: 4295668.75388 SO Objective: 7194256.05752 UE Objective: 4295668.84873 SO Objective: 7194256.05664 UE Objective: 4295668.93517 SO Objective: 7194256.05592 UE Objective: 4295669.01392 SO Objective: 7194256.05534 UE Objective: 4295669.08566 SO Objective: 7194256.05487 UE Objective: 4295669.151 SO Objective: 7194256.05449 UE Objective: 4295669.21049 SO Objective: 7194256.05419 UE Objective: 4295669.26515 SO Objective: 7194256.05394 UE Objective: 4295669.31517 SO Objective: 7194256.05374 UE Objective: 4295669.36076 SO Objective: 7194256.05358 UE Objective: 4295669.40224 SO Objective: 7194256.05345 UE Objective: 4295669.43933 SO Objective: 7194256.05334 UE Objective: 4295669.47394 SO Objective: 7194256.05325 UE Objective: 4295669.50454 SO Objective: 7194256.05318 UE Objective: 4295669.53233 SO Objective: 7194256.05313 UE Objective: 4295669.55784 SO Objective: 7194256.05308 UE Objective: 4295669.58086 SO Objective: 7194256.05305 UE Objective: 4295669.60164 SO Objective: 7194256.05302 UE Objective: 4295669.62041 SO Objective: 7194256.05299 UE Objective: 4295669.63759 SO Objective: 7194256.05297 UE Objective: 4295669.65299 SO Objective: 7194256.05296 UE Objective: 4295669.66685 SO Objective: 7194256.05295 UE Objective: 4295669.67928 SO Objective: 7194256.05294 UE Objective: 4295669.69047 SO Objective: 7194256.05293 UE Objective: 4295669.70054 SO Objective: 7194256.05292 UE Objective: 4295669.70961 SO Objective: 7194256.05292 UE Objective: 4295669.71779 SO Objective: 7194256.05291 UE Objective: 4295669.72515 SO Objective: 7194256.05291 UE Objective: 4295669.73178 SO Objective: 7194256.0529 UE Objective: 4295669.73779 SO Objective: 7194256.0529 UE Objective: 4295669.74318 SO Objective: 7194256.0529 UE Objective: 4295669.74802 SO Objective: 7194256.0529 UE Objective: 4295669.75238 SO Objective: 7194256.0529 UE Objective: 4295669.7563 SO Objective: 7194256.0529 UE Objective: 4295669.75982 SO Objective: 7194256.0529 UE Objective: 4295669.76299 SO Objective: 7194256.0529 UE Objective: 4295669.76585 SO Objective: 7194256.0529 UE Objective: 4295669.76842 SO Objective: 7194256.05289 UE Objective: 4295669.77073 SO Objective: 7194256.05289 UE Objective: 4295669.77281 SO Objective: 7194256.05289 UE Objective: 4295669.77468 SO Objective: 7194256.05289 UE Objective: 4295669.77637 SO Objective: 7194256.05289 UE Objective: 4295669.77788 SO Objective: 7194256.05289 UE Objective: 4295669.77924 SO Objective: 7194256.05289 UE Objective: 4295669.78047 SO Objective: 7194256.05289 UE Objective: 4295669.78157 SO Objective: 7194256.05289 UE Objective: 4295669.78256 SO Objective: 7194256.05289 UE Objective: 4295669.78345 SO Objective: 7194256.05289 UE Objective: 4295669.78425 SO Objective: 7194256.05289 UE Objective: 4295669.78497 SO Objective: 7194256.05289 UE Objective: 4295669.78561 SO Objective: 7194256.05289 UE Objective: 4295669.7862 SO Objective: 7194256.05289 UE Objective: 4295669.78672 SO Objective: 7194256.05289 UE Objective: 4295669.78719 SO Objective: 7194256.05289 UE Objective: 4295669.78761 SO Objective: 7194256.05289 UE Objective: 4295669.78799 SO Objective: 7194256.05289 UE Objective: 4295669.78833 SO Objective: 7194256.05289 UE Objective: 4295669.78864 SO Objective: 7194256.05289 UE Objective: 4295669.78891 SO Objective: 7194256.05289 UE Objective: 4295669.78916 SO Objective: 7194256.05289 UE Objective: 4295669.78938 SO Objective: 7194256.05289 UE Objective: 4295669.78958 SO Objective: 7194256.05289 UE Objective: 4295669.78976 SO Objective: 7194256.05289 UE Objective: 4295669.78993 SO Objective: 7194256.05289 UE Objective: 4295669.79007 SO Objective: 7194256.05289 UE Objective: 4295669.7902 SO Objective: 7194256.05289 UE Objective: 4295669.79032 SO Objective: 7194256.05289 UE Objective: 4295669.79042 SO Objective: 7194256.05289 UE Objective: 4295669.79052 SO Objective: 7194256.05289 UE Objective: 4295669.7906 SO Objective: 7194256.05289 UE Objective: 4295669.79068 SO Objective: 7194256.05289 UE Objective: 4295669.79075 SO Objective: 7194256.05289 UE Objective: 4295669.79081 SO Objective: 7194256.05289 UE Objective: 4295669.79087 SO Objective: 7194256.05289 UE Objective: 4295669.79092 SO Objective: 7194256.05289 UE Objective: 4295669.79096 SO Objective: 7194256.05289 UE Objective: 4295669.791 SO Objective: 7194256.05289 UE Objective: 4295669.79104 SO Objective: 7194256.05289 UE Objective: 4295669.79107 SO Objective: 7194256.05289 UE Objective: 4295669.7911 SO Objective: 7194256.05289 UE Objective: 4295669.79113 SO Objective: 7194256.05289 UE Objective: 4295669.79115 SO Objective: 7194256.05289 UE Objective: 4295669.79117 SO Objective: 7194256.05289 UE Objective: 4295669.79119 SO Objective: 7194256.05289 UE Objective: 4295669.79121 SO Objective: 7194256.05289 UE Objective: 4295669.79122 SO Objective: 7194256.05289 UE Objective: 4295669.79124 SO Objective: 7194256.05289 UE Objective: 4295669.79125 SO Objective: 7194256.05289 UE Objective: 4295669.79126 SO Objective: 7194256.05289 UE Objective: 4295669.79127 SO Objective: 7194256.05289 UE Objective: 4295669.79128 SO Objective: 7194256.05289 UE Objective: 4295669.79129 SO Objective: 7194256.05289 UE Objective: 4295669.7913 SO Objective: 7194256.05289 UE Objective: 4295669.7913 SO Objective: 7194256.05289 UE Objective: 4295669.79131 SO Objective: 7194256.05289 UE Objective: 4295669.79131 SO Objective: 7194256.05289 UE Objective: 4295669.79132 SO Objective: 7194256.05289 UE Objective: 4295669.79132 SO Objective: 7194256.05289 UE Objective: 4295669.79133 SO Objective: 7194256.05289 UE Objective: 4295669.79133 SO Objective: 7194256.05289 UE Objective: 4295669.79133 SO Objective: 7194256.05289 UE Objective: 4295669.79134 SO Objective: 7194256.05289 UE Objective: 4295669.79134 SO Objective: 7194256.05289 UE Objective: 4295669.79134 SO Objective: 7194256.05289 UE Objective: 4295669.79134 SO Objective: 7194256.05289 UE Objective: 4295669.79135 SO Objective: 7194256.05289 UE Objective: 4295669.79135 SO Objective: 7194256.05289 UE Objective: 4295669.79135 SO Objective: 7194256.05289 UE Objective: 4295669.79135 SO Objective: 7194256.05289 UE Objective: 4295669.79135 SO Objective: 7194256.05289 UE Objective: 4295669.79135 SO Objective: 7194256.05289 UE Objective: 4295669.79135 SO Objective: 7194256.05289 UE Objective: 4295669.79135 SO Objective: 7194256.05289 UE Objective: 4295669.79135 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289 UE Objective: 4295669.79136 SO Objective: 7194256.05289
bch.a()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-31-6bb30c1dff9a> in <module>() ----> 1 bch.a() AttributeError: 'Network' object has no attribute 'a'
def get_OD_path_flow(odpair, solution, by_id_links, verbose=False):
e_ODpair = odpair
pathset=[]
for path in solution[e_ODpair]:
t = 0
path_link=""
for link_id in path:
t += by_id_links[link_id].get_time()
path_link += str(link_id+1)+","
pathset.append({"path":path, "volume":solution[e_ODpair][path], "Traveltime":t})
if verbose==True:
print("-------------------------------------------------------------------------------------------------------")
print('vol:%.2f, time:%.2f'%(solution[e_ODpair][path], t), 'path:', path_link)
return pathset
odpair=('13','17')
paths=get_OD_path_flow(odpair, solution_UE,by_id_links_UE, verbose=True)
------------------------------------------------------------------------------------------------------- ('vol:285.74, time:45.30', 'path:', '38,36,32,30,') ------------------------------------------------------------------------------------------------------- ('vol:214.26, time:45.30', 'path:', '38,35,6,9,13,25,30,')
paths=get_OD_path_flow(odpair, solution_SO,by_id_links_SO, verbose=True)
------------------------------------------------------------------------------------------------------- ('vol:92.69, time:49.02', 'path:', '38,35,6,10,32,30,') ------------------------------------------------------------------------------------------------------- ('vol:294.60, time:42.62', 'path:', '38,36,32,30,') ------------------------------------------------------------------------------------------------------- ('vol:112.72, time:48.22', 'path:', '38,35,6,9,13,25,30,')
def print_link_stats(graph):
for f in graph:
for t in graph[f]:
link_time = graph[f][t]["object"].get_time()
link_flow = graph[f][t]["object"].flow
link_VHT = link_time*link_flow
link_VMT = link_flow*graph[f][t]["object"].link_length
print(f,t, link_flow, link_time, link_VHT,link_VMT)
print_link_stats(graph_SO)
('24', '13', 10545.026861262624, 15.041859590450455, 158616.81342464086, 42180.107445050497) ('24', '21', 9783.789379374688, 10.238614869180703, 100172.45141659792, 29351.368138124064) ('24', '23', 7686.022223304908, 3.5739215628753085, 27469.240556608231, 15372.044446609816) ('20', '19', 8712.5602396527593, 9.520122106382463, 82944.637340707122, 34850.240958611037) ('20', '18', 20708.164029769388, 4.367787005972321, 90448.849766770159, 82832.656119077554) ('20', '21', 6359.0562752535443, 8.245128540671908, 52431.236386831799, 38154.337651521266) ('20', '22', 7117.0188495436869, 7.8991514803738205, 56218.409981221397, 35585.094247718436) ('21', '24', 9837.4949940832248, 10.398866455504534, 102298.79670016581, 29512.484982249676) ('21', '20', 6314.8670844542321, 8.183370273818218, 51676.895582035882, 37889.202506725393) ('21', '22', 7937.6923550527117, 3.591920676651756, 28511.561295014406, 15875.384710105423) ('22', '20', 7083.5754736551462, 7.845041107168697, 55570.940776556599, 35417.877368275731) ('22', '15', 16155.135710924278, 6.6100923541485175, 106786.93904301224, 48465.407132772831) ('22', '21', 7947.2087789618899, 3.5995685706664657, 28606.522945275839, 15894.41755792378) ('22', '23', 9299.860145014829, 11.180867331332639, 103980.50248135872, 37199.440580059316) ('23', '24', 7738.7618070472545, 3.6175676783189914, 27995.494583383617, 15477.523614094509) ('23', '14', 7601.9891929025262, 7.406506039529969, 56304.178869674113, 30407.956771610105) ('23', '22', 9259.3666543971412, 11.056613966837899, 102377.24267508055, 37037.466617588565) ('1', '3', 11239.633518586108, 4.031918215238256, 45317.283116189778, 44958.534074344432) ('1', '2', 7620.0340027819766, 6.0067430694531305, 45771.586435207835, 45720.20401669186) ('3', '1', 11220.034002781995, 4.031696162750931, 45235.768034951136, 44880.136011127979) ('3', '12', 14313.666645365784, 4.083952491667294, 58456.334561236625, 57254.666581463134) ('3', '4', 17501.113594633131, 4.656690561850684, 81497.2704980048, 70004.454378532522) ('2', '1', 7639.6335185861599, 6.006812713062812, 45889.847742584134, 45837.801111516957) ('2', '6', 6620.0340027819911, 7.383486311102036, 48878.930438570853, 33100.170013909956) ('5', '9', 16895.59948650705, 11.111610764443398, 187737.32512599608, 84477.997432535252) ('5', '4', 18800.267220682512, 2.374780808412545, 44646.51378870428, 37600.534441365024) ('5', '6', 6994.4978430016308, 6.395853395528997, 44735.782779182227, 27977.991372006523) ('4', '11', 6080.4941056634616, 8.118786443196502, 49366.23311299675, 36482.964633980766) ('4', '3', 17474.03312677945, 4.652635447525547, 81300.305936889752, 69896.1325071178) ('4', '5', 18732.187587226195, 2.3693815910979534, 44383.700430167337, 37464.375174452391) ('7', '8', 13272.892448464896, 6.693254728896493, 88838.850146822209, 39818.677345394688) ('7', '18', 16324.353953043425, 2.071014005825788, 33807.965672810504, 32648.707906086849) ('6', '8', 12556.622103501186, 14.951766909154433, 187743.68685788615, 25113.244207002372) ('6', '2', 6639.6335185861526, 7.411838459981333, 49211.891073238032, 33198.16759293076) ('6', '5', 7014.3381068128429, 6.423153137604844, 45054.167818996131, 28057.352427251371) ('9', '8', 7535.6402681996105, 17.436009497763592, 131391.49528805818, 75356.4026819961) ('9', '5', 16943.838856152131, 11.181708374766943, 189461.06483853783, 84719.194280760654) ('9', '10', 21764.782595976627, 5.692765780064686, 123901.80957292319, 65294.347787929881) ('8', '9', 7566.0336837205323, 17.556703542737658, 132834.61037944871, 75660.336837205323) ('8', '16', 7967.9084186574128, 9.663486177268936, 76997.772865440696, 39839.542093287062) ('8', '7', 13224.353953043494, 6.639525950519692, 87803.441250089949, 39673.061859130481) ('8', '6', 12596.061883116679, 15.11525882314972, 190392.73551571925, 25192.123766233359) ('11', '10', 17387.458199769841, 11.854971588743439, 206127.8229587356, 86937.290998849203) ('11', '12', 7324.915017446092, 10.462128367274328, 76634.201191896485, 43949.490104676552) ('11', '4', 6085.3340043534936, 8.125540474520216, 49446.62775334849, 36512.004026120965) ('11', '14', 9443.0096923153887, 12.436399278035697, 117437.03891999519, 37772.038769261555) ('10', '9', 21882.6285501005, 5.751561344195886, 125859.2804781553, 65647.885650301498) ('10', '11', 17467.774426754793, 11.9825099489702, 209307.78085495654, 87338.87213377397) ('10', '15', 23361.195074341023, 14.041628159519227, 328029.2145958888, 140167.17044604613) ('10', '17', 8320.0053091195678, 17.24808308001834, 143504.14279788797, 66560.042472956542) ('10', '16', 10744.945889711464, 18.39596183474059, 197663.61450348486, 42979.783558845855) ('13', '24', 10538.581662811854, 15.014888864943648, 158235.63246125303, 42154.326651247415) ('13', '12', 15645.026861262666, 3.0599108184519217, 47872.386947748542, 46935.080583787996) ('12', '11', 7323.8792638476007, 10.459605096858532, 76604.884876916869, 43943.275583085604) ('12', '3', 14321.147597414978, 4.084128138373987, 58489.401876409524, 57284.590389659912) ('12', '13', 15538.581662811772, 3.058296904968539, 47521.596206978138, 46615.744988435312) ('15', '19', 18556.611552179071, 4.1857542964837195, 77673.416532712974, 55669.834656537212) ('15', '10', 23419.983731680713, 14.122881547564251, 330757.65608840849, 140519.90239008429) ('15', '14', 8954.969694878615, 11.977281098984694, 107256.18926845037, 44774.848474393075) ('15', '22', 16171.702249562659, 6.624923252035338, 107136.28625811984, 48515.106748687977) ('14', '11', 9468.5691176190085, 12.52811010868741, 118623.27647724812, 37874.276470476034) ('14', '15', 8917.1641764502601, 11.860200183288697, 105759.35219995078, 44585.820882251297) ('14', '23', 7614.2352860271485, 7.428509385879231, 56562.418288545501, 30456.941144108594) ('17', '19', 8257.2120269327534, 4.575392997106167, 37779.99008364894, 16514.424053865507) ('17', '10', 8337.9044331627265, 17.327923186272802, 144478.56755232718, 66703.235465301812) ('17', '16', 10582.007505392818, 7.028247351840191, 74372.966226930104, 21164.015010785635) ('16', '8', 7989.2031183722765, 9.713540143800946, 77603.445207288809, 39946.015591861382) ('16', '18', 18460.626311243559, 3.3484231315046347, 61813.988162631009, 55381.878933730681) ('16', '17', 10598.862396700541, 7.060359694272462, 74831.78087080452, 21197.724793401081) ('16', '10', 10766.420289437643, 18.51139197010206, 199301.42609263989, 43065.681157750572) ('19', '20', 8698.3552925109525, 9.484210051991731, 82497.028701027855, 34793.42117004381) ('19', '15', 18569.772266585453, 4.1891217179370495, 77791.036299098225, 55709.316799756358) ('19', '17', 8258.2562596681964, 4.576696013115036, 37795.528498905725, 16516.512519336393) ('18', '20', 20700.001543599072, 4.367207469931913, 90401.201368808004, 82800.006174396287) ('18', '7', 16372.89244846498, 2.0718623865881174, 33922.380023627215, 32745.784896929959) ('18', '16', 18520.250301992444, 3.3529463236200496, 62097.405162588679, 55560.750905977329)
if visualization ==True:
#v.NeworkX2Pyplot(graph)
#v.NetworkX2_Nodes_Shapefile(graph)
v.NetworkX2_Links_Shapefile(graph, filename="SO_1")
print (gp.get_objetive_function(solution_SO),gp.get_objetive_function_SO(solution_SO))
print('--')
(7194256.0528930053, 21687187.678703856) --
12/9/2017 Gradient Projection for UE was coded by Felipe
2/13/2018 Added a code for Visualization and graph export to ArcGIS format
5/3/2018 Minor code revision by Daisik
5/3/2018 Added System Optimum function