def test_build_nodes(self): graph_obj = graph.Graph.build_from_parameters(n=5, l=1000, g=0.5, p=2) network = TransportNetwork(graph_obj) mode_manager = TransportModeManager() bus_obj = mode_manager.get_mode("bus") metro_obj = mode_manager.get_mode("metro") # train_obj = TransportMode("train", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) feeder_routes = network.get_feeder_routes(bus_obj) radial_routes = network.get_radial_routes(metro_obj) circular_routes = network.get_circular_routes(bus_obj) for route in feeder_routes: network.add_route(route) for route in radial_routes: network.add_route(route) for route in circular_routes: network.add_route(route) city_nodes = ExtendedGraph.build_city_nodes(graph_obj) tree_graph = ExtendedGraph.build_tree_graph(network.get_routes(), city_nodes) stop_nodes = ExtendedGraph.build_stop_nodes(tree_graph) route_nodes = ExtendedGraph.build_route_nodes(network.get_routes(), stop_nodes) self.assertEqual(len(city_nodes), 11) self.assertEqual(len(stop_nodes), 21) self.assertEqual(len(route_nodes), 60)
def setUp(self) -> None: graph_obj = Graph.build_from_parameters(n=2, l=10, g=0.5, p=2) demand_obj = Demand.build_from_parameters(graph_obj=graph_obj, y=100, a=0.5, alpha=1 / 3, beta=1 / 3) passenger_obj = Passenger.get_default_passenger() [bus_obj, metro_obj] = TransportMode.get_default_modes() self.network_obj = TransportNetwork(graph_obj=graph_obj) feeder_routes_metro = self.network_obj.get_feeder_routes(mode_obj=metro_obj) radial_routes_bus = self.network_obj.get_radial_routes(mode_obj=bus_obj) for route in feeder_routes_metro: self.network_obj.add_route(route_obj=route) for route in radial_routes_bus: self.network_obj.add_route(route_obj=route) self.extended_graph_obj = ExtendedGraph(graph_obj=graph_obj, routes=self.network_obj.get_routes(), TP=passenger_obj.pt, frequency_routes=None) hyperpath_obj = Hyperpath(extended_graph_obj=self.extended_graph_obj, passenger_obj=passenger_obj) self.hyperpaths, self.labels, self.successors, self.frequency, self.Vij = hyperpath_obj.get_all_hyperpaths( OD_matrix=demand_obj.get_matrix()) self.OD_assignment = Assignment.get_assignment(hyperpaths=self.hyperpaths, labels=self.labels, p=2, vp=passenger_obj.va, spa=passenger_obj.spa, spv=passenger_obj.spv)
def test_get_hyperpath_OD(self): """ to test get_hyperpath_OD method of class Hyperpath :return: """ graph_obj = Graph.build_from_parameters(n=2, l=10, g=0.5, p=2) [bus_obj, metro_obj] = TransportMode.get_default_modes() passenger_obj = Passenger.get_default_passenger() network_obj = TransportNetwork(graph_obj) radial = network_obj.get_radial_routes(bus_obj) diametral = network_obj.get_diametral_routes(bus_obj, jump=1) diametral_metro = network_obj.get_diametral_routes(metro_obj, jump=1) for route in radial: network_obj.add_route(route) for route in diametral: network_obj.add_route(route) for route in diametral_metro: network_obj.add_route(route) extended_graph_obj = ExtendedGraph(graph_obj, network_obj.get_routes(), 16) nodes = extended_graph_obj.get_extended_graph_nodes() P1 = None P2 = None stop_bus = None stop_metro = None for city_node in nodes: if city_node.graph_node.name == str("P_1"): P1 = city_node for stop_node in nodes[city_node]: if stop_node.mode.name == "bus": stop_bus = stop_node if stop_node.mode.name == "metro": stop_metro = stop_node if city_node.graph_node.name == str("P_2"): P2 = city_node hyper_path_obj = Hyperpath(extended_graph_obj, passenger_obj) hyperpaths_od, label, successors, frequencies = hyper_path_obj.get_hyperpath_OD( P1, P2) self.assertEqual(label[P1], float("inf")) self.assertEqual(label[P2], 0) self.assertEqual(round(label[stop_bus], 5), 1.30238) self.assertEqual(round(label[stop_metro], 5), 0.83571 + 0.05) self.assertEqual(len(hyperpaths_od), 2) self.assertEqual(len(hyperpaths_od[stop_bus]), 2) self.assertEqual(len(hyperpaths_od[stop_metro]), 1)
def test_get_extended_edges(self): graph_obj = graph.Graph.build_from_parameters(n=5, l=1000, g=0.5, p=2) network = TransportNetwork(graph_obj) passenger_obj = Passenger(4, 2, 2, 2, 2, 2, 2, 2, 2) mode_manager = TransportModeManager() bus_obj = mode_manager.get_mode("bus") metro_obj = mode_manager.get_mode("metro") # train_obj = TransportMode("train", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) feeder_routes = network.get_feeder_routes(bus_obj) radial_routes = network.get_radial_routes(metro_obj, express=True) circular_routes = network.get_circular_routes(bus_obj) for route in feeder_routes: network.add_route(route) for route in radial_routes: network.add_route(route) for route in circular_routes: network.add_route(route) extended_graph = ExtendedGraph(graph_obj, network.get_routes(), passenger_obj.spt) extended_graph_edges = extended_graph.get_extended_graph_edges() n_access = 0 n_boarding = 0 n_alighting = 0 n_route = 0 for edge in extended_graph_edges: if edge.type == ExtendedEdgesType.ACCESS: n_access = n_access + 1 if edge.type == ExtendedEdgesType.BOARDING: n_boarding = n_boarding + 1 if edge.type == ExtendedEdgesType.ALIGHTING: n_alighting = n_alighting + 1 if edge.type == ExtendedEdgesType.ROUTE: n_route = n_route + 1 self.assertEqual(n_access, 32) self.assertEqual(n_boarding, 30) self.assertEqual(n_alighting, 30) self.assertEqual(n_route, 30)
def test_build_hyperpath_graph(self): """ to test build_hyperpath_graph method of class Hyperpath :return: """ graph_obj = Graph.build_from_parameters(n=2, l=10, g=0.5, p=2) [bus_obj, _] = TransportMode.get_default_modes() passenger_obj = Passenger.get_default_passenger() network_obj = TransportNetwork(graph_obj) radial = network_obj.get_radial_routes(bus_obj) diametral = network_obj.get_diametral_routes(bus_obj, jump=1) for route in radial: network_obj.add_route(route) for route in diametral: network_obj.add_route(route) extended_graph_obj = ExtendedGraph(graph_obj, network_obj.get_routes(), 16) nodes = extended_graph_obj.get_extended_graph_nodes() P1 = None P2 = None stop = None for city_node in nodes: if city_node.graph_node.name == str("P_1"): P1 = city_node stop = None for stop_node in nodes[city_node]: stop = stop_node if city_node.graph_node.name == str("P_2"): P2 = city_node hyper_path_obj = Hyperpath(extended_graph_obj, passenger_obj) successors, label, frequencies = hyper_path_obj.build_hyperpath_graph( P1, P2) self.assertEqual(label[P1], float("inf")) self.assertEqual(label[P2], 0) self.assertEqual(label[stop], 1.55)
def test_resources_consumer(self): graph_obj = Graph.build_from_parameters(n=2, l=10, g=0.5, p=2) demand_obj = Demand.build_from_parameters(graph_obj=graph_obj, y=100, a=0.5, alpha=1 / 3, beta=1 / 3) passenger_obj = Passenger.get_default_passenger() [bus_obj, metro_obj] = TransportMode.get_default_modes() network_obj = TransportNetwork(graph_obj=graph_obj) feeder_routes_metro = network_obj.get_feeder_routes(mode_obj=metro_obj) radial_routes_bus = network_obj.get_radial_routes(mode_obj=bus_obj) for route in feeder_routes_metro: network_obj.add_route(route_obj=route) for route in radial_routes_bus: network_obj.add_route(route_obj=route) extended_graph_obj = ExtendedGraph(graph_obj=graph_obj, routes=network_obj.get_routes(), TP=passenger_obj.pt, frequency_routes=None) hyperpath_obj = Hyperpath(extended_graph_obj=extended_graph_obj, passenger_obj=passenger_obj) hyperpaths, labels, successors, frequency, Vij = hyperpath_obj.get_all_hyperpaths( OD_matrix=demand_obj.get_matrix()) OD_assignment = Assignment.get_assignment(hyperpaths=hyperpaths, labels=labels, p=2, vp=passenger_obj.va, spa=passenger_obj.spa, spv=passenger_obj.spv) f = defaultdict(float) z = defaultdict(lambda: defaultdict(lambda: defaultdict(float))) v = defaultdict(lambda: defaultdict(lambda: defaultdict(float))) for route in network_obj.get_routes(): f[route.id] = 28 CU_obj = UsersCost() ta, te, tv, t = CU_obj.resources_consumer(hyperpaths, Vij, OD_assignment, successors, extended_graph_obj, passenger_obj.va, f, z, v) self.assertEqual(round(ta, 4), 3.069) self.assertEqual(round(te, 4), 13.9597) self.assertEqual(round(tv, 4), 74.685) self.assertEqual(round(t, 4), 41.6667)
def test_build_edges(self): graph_obj = graph.Graph.build_from_parameters(n=5, l=1000, g=0.5, p=2) network = TransportNetwork(graph_obj) passenger_obj = Passenger(4, 2, 2, 2, 2, 2, 2, 2, 2) mode_manager = TransportModeManager() bus_obj = mode_manager.get_mode("bus") metro_obj = mode_manager.get_mode("metro") # train_obj = TransportMode("train", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) feeder_routes = network.get_feeder_routes(bus_obj) radial_routes = network.get_radial_routes(metro_obj) circular_routes = network.get_circular_routes(bus_obj) for route in feeder_routes: network.add_route(route) for route in radial_routes: network.add_route(route) for route in circular_routes: network.add_route(route) city_nodes = ExtendedGraph.build_city_nodes(graph_obj) tree_graph = ExtendedGraph.build_tree_graph(network.get_routes(), city_nodes) stop_nodes = ExtendedGraph.build_stop_nodes(tree_graph) route_nodes = ExtendedGraph.build_route_nodes(network.get_routes(), stop_nodes) extended_graph_nodes = ExtendedGraph.build_extended_graph_nodes(route_nodes) initial_frequency = defaultdict(float) for route in network.get_routes(): initial_frequency[route.id] = 28 access_edges = ExtendedGraph.build_access_edges(extended_graph_nodes) boarding_edges = ExtendedGraph.build_boarding_edges(extended_graph_nodes, initial_frequency) alighting_edges = ExtendedGraph.build_alighting_edges(extended_graph_nodes, passenger_obj.spt) route_edges = ExtendedGraph.build_route_edges(extended_graph_nodes) self.assertEqual(len(access_edges), 42) self.assertEqual(len(boarding_edges), 40) self.assertEqual(len(alighting_edges), 40) self.assertEqual(len(route_edges), 40)
def test_extended_graph_nodes(self): graph_obj = graph.Graph.build_from_parameters(n=5, l=1000, g=0.5, p=2) network = TransportNetwork(graph_obj) passenger_obj = Passenger(2, 2, 2, 2, 2, 2, 2, 2, 2) mode_manager = TransportModeManager() bus_obj = mode_manager.get_mode("bus") metro_obj = mode_manager.get_mode("metro") # train_obj = TransportMode("train", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) feeder_routes = network.get_feeder_routes(bus_obj) radial_routes = network.get_radial_routes(metro_obj) circular_routes = network.get_circular_routes(bus_obj) for route in feeder_routes: network.add_route(route) for route in radial_routes: network.add_route(route) for route in circular_routes: network.add_route(route) extended_graph = ExtendedGraph(graph_obj, network.get_routes(), passenger_obj.spt) extended_graph_nodes = extended_graph.get_extended_graph_nodes() n_city = 0 n_stop = 0 n_route = 0 for city_node in extended_graph_nodes: n_city = n_city + 1 for stop_node in extended_graph_nodes[city_node]: n_stop = n_stop + 1 for _ in extended_graph_nodes[city_node][stop_node]: n_route = n_route + 1 self.assertEqual(n_city, 11) self.assertEqual(n_stop, 21) self.assertEqual(n_route, 60)
def test_network_validator(self): """ to test network_validator method in class Hyperpath :return: """ graph_obj = Graph.build_from_parameters(n=2, l=10000, g=0.5, p=2000) demand_obj = Demand.build_from_parameters(graph_obj, y=1000, a=0.5, alpha=1 / 3, beta=1 / 3) OD_matrix = demand_obj.get_matrix() [bus_obj, _] = TransportMode.get_default_modes() passenger_obj = Passenger.get_default_passenger() network_obj = TransportNetwork(graph_obj) radial = network_obj.get_radial_routes(bus_obj) extended_graph_obj = ExtendedGraph(graph_obj, network_obj.get_routes(), 16) hyper_path_obj = Hyperpath(extended_graph_obj, passenger_obj) with self.assertRaises(TransportNetworkException): hyper_path_obj.network_validator(OD_matrix) for route in radial: network_obj.add_route(route) extended_graph_obj = ExtendedGraph(graph_obj, network_obj.get_routes(), 16) hyper_path_obj = Hyperpath(extended_graph_obj, passenger_obj) self.assertTrue(hyper_path_obj.network_validator(OD_matrix))
def __init__(self, graph_obj: Graph, demand_obj: Demand, passenger_obj: Passenger, network_obj: TransportNetwork, f: defaultdict_float = None): # definimos ciudad self.graph_obj = graph_obj _, _, _, self.p, _, _, _, _, _ = self.graph_obj.get_parameters() # definimos demanda self.demand_obj = demand_obj self.total_trips = demand_obj.get_total_trips() # definimos pasajeros self.passenger_obj = passenger_obj self.vp = self.passenger_obj.va self.pa = self.passenger_obj.pa self.pv = self.passenger_obj.pv self.TP = self.passenger_obj.pt # definimos red de transporte self.network_obj = network_obj # definimos frecuencia self.f, self.f_opt, self.lines_position = self.f0(f) self.extended_graph_obj = ExtendedGraph(self.graph_obj, self.network_obj.get_routes(), self.TP, self.f) self.hyperpath_obj = Hyperpath(self.extended_graph_obj, self.passenger_obj) # en este punto se debería levantar exception de que la red tiene mas de dos modos defnidos # o que existe un par OD con viaje y sin conexion self.hyperpaths, self.labels, self.successors, self.frequency, self.Vij = self.hyperpath_obj.get_all_hyperpaths( self.demand_obj.get_matrix()) self.assignment = Assignment.get_assignment(self.hyperpaths, self.labels, self.p, self.vp, self.pa, self.pv) self.len_constrains = len(self.get_constrains(self.f_opt)) self.len_var = len(self.f_opt) self.better_res = None # (fopt, success, status, message, constr_violation, vrc)
class test_graph(unittest.TestCase): def setUp(self) -> None: graph_obj = Graph.build_from_parameters(n=2, l=10, g=0.5, p=2) demand_obj = Demand.build_from_parameters(graph_obj=graph_obj, y=100, a=0.5, alpha=1 / 3, beta=1 / 3) passenger_obj = Passenger.get_default_passenger() [bus_obj, metro_obj] = TransportMode.get_default_modes() self.network_obj = TransportNetwork(graph_obj=graph_obj) feeder_routes_metro = self.network_obj.get_feeder_routes(mode_obj=metro_obj) radial_routes_bus = self.network_obj.get_radial_routes(mode_obj=bus_obj) for route in feeder_routes_metro: self.network_obj.add_route(route_obj=route) for route in radial_routes_bus: self.network_obj.add_route(route_obj=route) self.extended_graph_obj = ExtendedGraph(graph_obj=graph_obj, routes=self.network_obj.get_routes(), TP=passenger_obj.pt, frequency_routes=None) hyperpath_obj = Hyperpath(extended_graph_obj=self.extended_graph_obj, passenger_obj=passenger_obj) self.hyperpaths, self.labels, self.successors, self.frequency, self.Vij = hyperpath_obj.get_all_hyperpaths( OD_matrix=demand_obj.get_matrix()) self.OD_assignment = Assignment.get_assignment(hyperpaths=self.hyperpaths, labels=self.labels, p=2, vp=passenger_obj.va, spa=passenger_obj.spa, spv=passenger_obj.spv) def test_assignment(self): """ test assignment method of class Assignment :return: """ nodes = self.extended_graph_obj.get_extended_graph_nodes() P1 = None SC1 = None CBD = None stop_bus_p1 = None stop_metro_p1 = None for city_node in nodes: if city_node.graph_node.name == "P_1": P1 = city_node if city_node.graph_node.name == "SC_1": SC1 = city_node if city_node.graph_node.name == "CBD": CBD = city_node for stop_node in nodes[city_node]: if stop_node.mode.name == "bus": if city_node.graph_node.name == "P_1": stop_bus_p1 = stop_node if stop_node.mode.name == "metro": if city_node.graph_node.name == "P_1": stop_metro_p1 = stop_node self.assertEqual(round(self.OD_assignment[P1][CBD][stop_bus_p1], 2), 100) self.assertEqual(round(self.OD_assignment[P1][SC1][stop_bus_p1], 2), 84.88) self.assertEqual(round(self.OD_assignment[P1][SC1][stop_metro_p1], 2), 15.12) def test_get_alighting_and_boarding(self): """ to test get_alighting_and_boarding of class get_alighting_and_boarding :return: """ f = defaultdict(float) for route in self.network_obj.get_routes(): f[route.id] = 28 z, v, loaded_section_route = Assignment.get_alighting_and_boarding(Vij=self.Vij, hyperpaths=self.hyperpaths, successors=self.successors, assignment=self.OD_assignment, f=f) nodes = self.extended_graph_obj.get_extended_graph_nodes() stop_bus_p1 = None stop_bus_sc1 = None stop_bus_cbd = None stop_metro_p1 = None stop_metro_sc1 = None for city_node in nodes: for stop_node in nodes[city_node]: if stop_node.mode.name == "bus": if city_node.graph_node.name == "P_1": stop_bus_p1 = stop_node if city_node.graph_node.name == "SC_1": stop_bus_sc1 = stop_node if city_node.graph_node.name == "CBD": stop_bus_cbd = stop_node if stop_node.mode.name == "metro": if city_node.graph_node.name == "P_1": stop_metro_p1 = stop_node if city_node.graph_node.name == "SC_1": stop_metro_sc1 = stop_node self.assertEqual(round(z["R_bus_1"]["I"][stop_bus_p1], 2), 0.85) self.assertEqual(round(z["R_bus_1"]["I"][stop_bus_sc1], 2), 0.89) self.assertEqual(round(z["R_bus_1"]["R"][stop_bus_cbd], 2), 0.74) self.assertEqual(round(v["R_bus_1"]["I"][stop_bus_cbd], 2), 1.49) self.assertEqual(round(v["R_bus_1"]["I"][stop_bus_sc1], 2), 0.25) self.assertEqual(round(v["R_bus_1"]["R"][stop_bus_sc1], 2), 0.74) self.assertEqual(round(z["F_metro_1"]["I"][stop_metro_p1], 2), 0.04) self.assertEqual(round(v["F_metro_1"]["I"][stop_metro_sc1], 2), 0.04) self.assertEqual(round(z["F_metro_1"]["R"][stop_metro_sc1], 2), 0) def test_most_loaded_section(self): """ to test most_loaded_section class assignment :return: """ f = defaultdict(float) for route in self.network_obj.get_routes(): f[route.id] = 28 z, v, loaded_section_route = Assignment.get_alighting_and_boarding(Vij=self.Vij, hyperpaths=self.hyperpaths, successors=self.successors, assignment=self.OD_assignment, f=f) loaded_section = Assignment.most_loaded_section(loaded_section_route) self.assertEqual(len(loaded_section), 4) self.assertEqual(loaded_section['F_metro_1'], loaded_section['F_metro_2']) self.assertEqual(loaded_section['F_metro_1'], 0.044997165532879815) self.assertEqual(loaded_section['R_bus_1'], loaded_section['R_bus_2']) self.assertEqual(loaded_section['R_bus_1'], 1.4880952380952384)
def test_build_tree_graph(self): """ test methods get_tree_graph :return: """ graph_obj = graph.Graph.build_from_parameters(n=5, l=1000, g=0.5, p=2) network = TransportNetwork(graph_obj) mode_manager = TransportModeManager() bus_obj = mode_manager.get_mode("bus") metro_obj = mode_manager.get_mode("metro") # train_obj = TransportMode("train", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) feeder_routes = network.get_feeder_routes(bus_obj) radial_routes = network.get_radial_routes(metro_obj) circular_routes = network.get_circular_routes(bus_obj) for route in feeder_routes: network.add_route(route) for route in radial_routes: network.add_route(route) for route in circular_routes: network.add_route(route) city_nodes = ExtendedGraph.build_city_nodes(graph_obj) tree_graph = ExtendedGraph.build_tree_graph(network.get_routes(), city_nodes) one_stop = [] two_stop = [] three_stop = [] four_route_city = 0 six_route_city = 0 ten_route_city = 0 one_route_stop = 0 two_route_stop = 0 ten_route_stop = 0 for city_node in tree_graph: if len(tree_graph[city_node]) == 1: one_stop.append(city_node) if len(tree_graph[city_node]) == 2: two_stop.append(city_node) if len(tree_graph[city_node]) == 3: three_stop.append(city_node) n_routes_city = 0 for stop in tree_graph[city_node]: n_routes_stop = 0 for _ in tree_graph[city_node][stop]: n_routes_stop = n_routes_stop + 1 n_routes_city = n_routes_city + 1 if n_routes_stop == 1: one_route_stop = one_route_stop + 1 if n_routes_stop == 2: two_route_stop = two_route_stop + 1 if n_routes_stop == 10: ten_route_stop = ten_route_stop + 1 if n_routes_city == 4: four_route_city = four_route_city + 1 if n_routes_city == 6: six_route_city = six_route_city + 1 if n_routes_city == 10: ten_route_city = ten_route_city + 1 self.assertEqual(len(one_stop), 1) self.assertEqual(len(two_stop), 10) self.assertEqual(len(three_stop), 0) self.assertEqual(four_route_city, 5) self.assertEqual(six_route_city, 5) self.assertEqual(ten_route_city, 1) self.assertEqual(one_route_stop, 0) self.assertEqual(two_route_stop, 15) self.assertEqual(ten_route_stop, 1)
def resources_consumer( hyperpaths: dic_hyperpaths, Vij: dic_Vij, assignment: dic_assigment, successors: dic_successors, extended_graph: ExtendedGraph, vp: float, f: defaultdict_float, z: defaultdict2_float, v: defaultdict3_float) -> (float, float, float, int): """ to get resources consumer for all passenger in transport network access time, waiting time, time on board of vehicle, numbers of transfer :param hyperpaths: Dic[origin: CityNode][destination: CityNode][StopNode] = List[List[ExtendedNodes]] :param Vij: dic[origin: CityNode][destination: CityNode] = vij :param assignment: dic[origin: CityNode][destination: CityNode][Stop: StopNode] = %V_OD :param successors: dic[origin: CityNode][destination: CityNode][ExtendedNode] = List[ExtendedEdge] :param extended_graph: ExtendedGraph object :param vp: passenger velocity [km/hr] :param f: dict with frequency [veh/hr] for each route_id :param z: boarding, dic[route_id][direction][stop: StopNode] = pax [pax/veh] :param v: alighting, dic[route_id][direction][stop: StopNode] = pax [pax/veh] :return: (access time, waiting time, time on boad, numbers of transfers): (float, float, float, int) """ ta = 0 tv = 0 te = 0 t = 0 edges = extended_graph.get_extended_graph_edges() for origin in hyperpaths: for destination in hyperpaths[origin]: # viajes del par OD vod = Vij[origin][destination] for stop in hyperpaths[origin][destination]: # viajes de todas las rutas elementales que salen de esta parada vod_s = vod * assignment[origin][destination][stop] / 100 if vod_s == 0: continue paths = [] for suc in successors[origin][destination][stop]: nodej = suc.nodej paths.append((stop, nodej, vod_s)) # reportar ta inicial (lateral y tecnologico) ta += vod_s * (stop.mode.tat / 60 + assignment[origin][destination][stop] / 100 * origin.graph_node.width / (4 * vp * stop.mode.d)) while len(paths) != 0: nodei, nodej, pax = paths.pop(0) # evita continuar si llegaste a destino if isinstance( nodei, StopNode) and nodei.city_node == destination: continue dis_pax = pax # reportar te if isinstance(nodei, StopNode): if isinstance(nodej, RouteNode): f_acum = 0 for suc in successors[origin][destination][ nodei]: f_acum += f[suc.nodej.route.id] dis_pax = dis_pax * (f[nodej.route.id] / f_acum) te += dis_pax * nodei.mode.theta / ( f_acum / nodej.route.mode.d) # reportar tv if isinstance(nodei, RouteNode): if isinstance(nodej, RouteNode): for edge in edges: if edge.nodei == nodei and edge.nodej == nodej: tv += dis_pax * edge.t break # reportar transbordos y tv de bajada if isinstance(nodei, RouteNode): if isinstance(nodej, StopNode): # transbordos if nodej.city_node != destination: t += dis_pax # para tiempo de viaje adicional por esperar la bajada del vehiculo nodei_id = nodei.prev_route_node.stop_node.city_node.graph_node.id nodej_id = nodei.stop_node.city_node.graph_node.id stop_sequence_i = nodei.route.stops_sequence_i index = 0 index_i = 0 index_j = 0 for node_id in stop_sequence_i: if str(node_id) == str(nodei_id): index_i = index if str(node_id) == str(nodej_id): index_j = index index = index + 1 tb = nodei.route.mode.t / 3600 # verificamos si es del sentido de ida if index_i < index_j: pax_b = v[nodei.route.id]["I"][nodej] tv += (pax_b * 0.5 * tb) * dis_pax # sentido de vuelta else: pax_b = v[nodei.route.id]["R"][nodej] tv += (pax_b * 0.5 * tb) * dis_pax # reportar ta if isinstance(nodei, StopNode): if isinstance(nodej, CityNode): ta += nodei.mode.tat / 60 if isinstance(nodej, StopNode): if isinstance(nodei, CityNode): ta += nodej.mode.tat / 60 # agregamos elementos faltantes a las rutas # si nodo i, nodo j y nodo k (new suc ) son de rutas entonces for suc in successors[origin][destination][nodej]: # agrega elementos para continuar rutas elementales paths.append((nodej, suc.nodej, dis_pax)) # sumaremos tv producido por la espera de los que se bajan en paraderos cuando el ind sigue # en ruta # cbd #SC #p if isinstance(nodei, RouteNode) and isinstance( nodej, RouteNode) and isinstance( suc.nodej, RouteNode): bya = nodei.route.mode.bya tb = nodei.route.mode.t / 3600 # determinamos dirección nodei_id = nodei.stop_node.city_node.graph_node.id nodej_id = nodej.stop_node.city_node.graph_node.id stop_sequence_i = nodei.route.stops_sequence_i index = 0 index_i = 0 index_j = 0 for node_id in stop_sequence_i: if str(node_id) == str(nodei_id): index_i = index if str(node_id) == str(nodej_id): index_j = index index = index + 1 # verificamos si es del sentido de ida if index_i < index_j: # simultaneo if bya == 1: pasajeros = max( z[nodei.route.id]["I"][ nodej.stop_node], v[nodei.route.id]["I"][ nodej.stop_node]) tv += pasajeros * tb * dis_pax # secuencial if bya == 0: pasajeros = z[nodei.route.id]["I"][ nodej.stop_node] + v[ nodei.route.id]["I"][ nodej.stop_node] tv += pasajeros * tb * dis_pax # sentido de vuelta else: # simultaneo if bya == 1: pasajeros = max( z[nodei.route.id]["R"][ nodej.stop_node], v[nodei.route.id]["R"][ nodej.stop_node]) tv += pasajeros * tb * dis_pax # secuencial if bya == 0: pasajeros = z[nodei.route.id]["R"][ nodej.stop_node] + v[ nodei.route.id]["R"][ nodej.stop_node] tv += pasajeros * tb * dis_pax return ta, te, tv, t
def test_get_cycle_time(self): """ test get_cycle_time method of class operators_cost :return: """ graph_obj = Graph.build_from_parameters(n=2, l=10, g=0.5, p=2) demand_obj = Demand.build_from_parameters(graph_obj=graph_obj, y=100, a=0.5, alpha=1 / 3, beta=1 / 3) passenger_obj = Passenger.get_default_passenger() [bus_obj, metro_obj] = TransportMode.get_default_modes() network_obj = TransportNetwork(graph_obj=graph_obj) feeder_routes_metro = network_obj.get_feeder_routes(mode_obj=metro_obj) radial_routes_bus = network_obj.get_radial_routes(mode_obj=bus_obj) for route in feeder_routes_metro: network_obj.add_route(route_obj=route) for route in radial_routes_bus: network_obj.add_route(route_obj=route) extended_graph_obj = ExtendedGraph(graph_obj=graph_obj, routes=network_obj.get_routes(), TP=passenger_obj.pt, frequency_routes=None) hyperpath_obj = Hyperpath(extended_graph_obj=extended_graph_obj, passenger_obj=passenger_obj) hyperpaths, labels, successors, frequency, Vij = hyperpath_obj.get_all_hyperpaths( OD_matrix=demand_obj.get_matrix()) OD_assignment = Assignment.get_assignment(hyperpaths=hyperpaths, labels=labels, p=2, vp=passenger_obj.va, spa=passenger_obj.spa, spv=passenger_obj.spv) f = defaultdict(float) for route in network_obj.get_routes(): f[route.id] = 28 z, v, loaded_section_route = Assignment.get_alighting_and_boarding( Vij=Vij, hyperpaths=hyperpaths, successors=successors, assignment=OD_assignment, f=f) lines_travel_time = OperatorsCost.lines_travel_time( routes=network_obj.get_routes(), edge_distance=graph_obj.get_edges_distance()) line_cycle_time = OperatorsCost.get_cycle_time( z, v, network_obj.get_routes(), lines_travel_time) self.assertEqual(round(line_cycle_time["F_metro_1"], 7), 0.2500082) self.assertEqual(round(line_cycle_time["R_bus_1"], 7), 1.5032756) self.assertEqual(line_cycle_time["l1"], 0)
def test_get_all_hyperpaths(self): """ to test get_all_hyperpaths method of class Hyperpath :return: """ graph_obj = Graph.build_from_parameters(n=2, l=10, g=0.5, p=2) demand_obj = Demand.build_from_parameters(graph_obj, y=1000, a=0.5, alpha=1 / 3, beta=1 / 3) OD_matrix = demand_obj.get_matrix() [bus_obj, metro_obj] = TransportMode.get_default_modes() passenger_obj = Passenger.get_default_passenger() network_obj = TransportNetwork(graph_obj) radial = network_obj.get_radial_routes(bus_obj) diametral = network_obj.get_diametral_routes(bus_obj, jump=1) diametral_metro = network_obj.get_diametral_routes(metro_obj, jump=1) for route in radial: network_obj.add_route(route) for route in diametral: network_obj.add_route(route) for route in diametral_metro: network_obj.add_route(route) extended_graph_obj = ExtendedGraph(graph_obj, network_obj.get_routes(), 16) nodes = extended_graph_obj.get_extended_graph_nodes() P1 = None SC2 = None stop_bus = None stop_metro = None for city_node in nodes: if city_node.graph_node.name == str("P_1"): P1 = city_node for stop in nodes[city_node]: if stop.mode.name == "bus": stop_bus = stop if stop.mode.name == "metro": stop_metro = stop if city_node.graph_node.name == str("SC_2"): SC2 = city_node hyper_path_obj = Hyperpath(extended_graph_obj, passenger_obj) hyperpaths, labels, successors, frequencies, vij = hyper_path_obj.get_all_hyperpaths( OD_matrix) self.assertEqual(round(labels[P1][SC2][stop_bus], 5), 1.17738) self.assertEqual(round(labels[P1][SC2][stop_metro], 5), 0.71071 + 0.05) self.assertEqual(len(hyperpaths), 4) self.assertEqual(len(hyperpaths[P1]), 3) self.assertEqual(len(hyperpaths[SC2]), 2)