Source code for PyTrans.UrbanNetworkAnalysis.TransportationNetworks

import networkx as nx
import math

[docs]class Node: """ Class for handling node object in Transportation Networks Parameters ---------- node_id: int identifier of a node """ def __init__(self,node_id = 0): self.node_id = node_id
[docs]class Network(): """ Class for handling Transportation Networks. This class contains methods to read various TNTP format files from the source and methods of network-wide operations Parameters ---------- link_file : string file path of network file, which containing various link information trip_file : string file path of trip table. An Origin label and then Origin node number, followed by Destination node numders and OD flow node_file : string file path of node file, which containing coordinates information of nodes SO: boolean True if objective is to find system optimal solution, False if objective is to find user equilibrium Attributes ---------- graph : networkx.DiGrapy graph of links with Link object and travel time under the current condition origins : list list of origin nodes od_vols : dictionary key: tuple(origin node, destination node), value: traffic flow """ link_fields = {"from":1, "to":2, "capacity":3, "length": 4, "t0": 5, \ "B": 6, "beta": 7, "V": 8} def __init__(self, link_file, trip_file, node_file=None, SO=False): self.link_file = link_file self.trip_file = trip_file self.node_file = node_file self.graph = None self.SO=SO self.build_datastructure()
[docs] def build_datastructure(self): """ Method for opening .tntp format network information files and preparing variables for the analysis """ links, nodes = self.open_link_file() self.open_trip_file() graph = nx.DiGraph() for l in links: graph.add_edge(l.from_node, l.to_node, object=l, time=l.get_time()) if self.node_file != None: self.open_node_file(graph) Visualization.reLocateLinks(graph) self.graph=graph
[docs] def open_node_file(self, graph): """ Method for opening node file, containing position information of nodes \n This method adds 'pos' key-value pair in graph variable """ f = open(self.node_file) n = 0 for i in f: row = i.split(" ") if n == 0: n += 1 else: try: if self.node_file=="berlin-center_node.tntp": ind, x, y = str(int(row[0])), float(row[1]), float(row[3]) else: ind, x, y = str(int(row[0])), float(row[1]), float(row[2]) graph.node[ind]["pos"]=(x, y) except: print(row) f.close()
[docs] def open_trip_file(self, demand_factor=1.0): """ Method for opening trip tables containing OD flows of each OD pair Parameter --------- demand_factor float demand factor """ f = open(self.trip_file) lines = f.readlines() f.close() self.od_vols = {} current_origin = None for line in lines: if current_origin == None and line.startswith("Origin"): origin = str(int(line.split("Origin")[1])) current_origin = origin elif current_origin != None and len(line) < 3: # print "blank",line, current_origin = None elif current_origin != None: to_process = line[0:-2] for el in to_process.split(";"): try: dest = str(int(el.split(":")[0])) demand = float(el.split(":")[1])*demand_factor self.od_vols[current_origin, dest] = demand except: continue origins = [str(i) for i, j in self.od_vols] self.origins = list(dict.fromkeys(origins).keys())
[docs] def all_or_nothing_assignment(self): """ Method for implementing all-or-nothing assignment based on the current graph. \n It updates link traffic flow """ for edge in self.graph.edges(data=True): edge[2]['object'].vol = 0 shortestpath_graph = {} for i in self.origins: shortestpath_graph[i] = nx.single_source_dijkstra(self.graph, i, weight="weight") for (i, j) in self.od_vols: odvol = self.od_vols[(i, j)] path = shortestpath_graph[str(i)][1][str(j)] for p in range(len(path) - 1): fnode, tnode = path[p], path[p + 1] self.graph[fnode][tnode]["object"].vol += odvol
[docs] def update_linkcost(self): """ Method for updating link travel time. """ for (u, v, d) in self.graph.edges(data=True): self.graph[u][v]["weight"] = d["object"].time
[docs]class Visualization(): """ Class for handling visualization effect """