# tc2_bus = network_obj.get_tangencial_routes(bus, 2, True) # dc3_bus = network_obj.get_diametral_routes(bus, 3, True) # dc4_bus = network_obj.get_diametral_routes(bus, 4, True) # rc_bus = network_obj.get_radial_routes(bus, True) # r_bus = network_obj.get_radial_routes(bus) # f_bus = network_obj.get_feeder_routes(bus) # t1_bus = network_obj.get_tangencial_routes(bus, 1) # t2_bus = network_obj.get_tangencial_routes(bus, 2) # d3_bus = network_obj.get_diametral_routes(bus, 3) # d4_bus = network_obj.get_diametral_routes(bus, 4) # c_bus = network_obj.get_circular_routes(bus) # # routes = dc4_metro + tc1_bus + tc2_bus + dc3_bus + dc4_bus + rc_bus + r_bus + f_bus + t1_bus + t2_bus + d3_bus + d4_bus + c_bus c = network_obj.get_circular_routes(bus) d2_bus = network_obj.get_diametral_routes(bus, 2) d1_bus = network_obj.get_diametral_routes(bus, 1) t1_bus = network_obj.get_tangencial_routes(bus, 1) ds2_metro = network_obj.get_diametral_routes(metro, 2, True) r_bus = network_obj.get_radial_routes(bus) routes = r_bus + t1_bus + ds2_metro + d2_bus + d1_bus + c for route in routes: network_obj.add_route(route) # static method to run a optimization, return optimizer object # max_number_of_iteration set in 5 opt_obj = Optimizer.network_optimization(graph_obj, demand_obj, passenger_obj,
def test_get_diametral_routes(self): """ to test add diametral routes :return: """ g = graph.Graph.build_from_parameters(5, 1000, 0.5, 2) m = TransportModeManager() bus_obj = m.get_mode("bus") network = TransportNetwork(g) routes = network.get_diametral_routes(mode_obj=bus_obj, jump=2, short=False, express=False) self.assertEqual(len(routes), 5) self.assertEqual(routes[0].nodes_sequence_i, [1, 2, 0, 6, 5]) self.assertEqual(routes[0].nodes_sequence_r, [5, 6, 0, 2, 1]) self.assertEqual(routes[0].stops_sequence_i, [1, 2, 0, 6, 5]) self.assertEqual(routes[0].stops_sequence_r, [5, 6, 0, 2, 1]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_i, [9, 10, 0, 4, 3]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [3, 4, 0, 10, 9]) self.assertEqual(routes[len(routes) - 1].stops_sequence_i, [9, 10, 0, 4, 3]) self.assertEqual(routes[len(routes) - 1].stops_sequence_r, [3, 4, 0, 10, 9]) routes = network.get_diametral_routes(mode_obj=bus_obj, jump=2, short=True, express=False) self.assertEqual(len(routes), 5) self.assertEqual(routes[0].nodes_sequence_i, [2, 0, 6]) self.assertEqual(routes[0].nodes_sequence_r, [6, 0, 2]) self.assertEqual(routes[0].stops_sequence_i, [2, 0, 6]) self.assertEqual(routes[0].stops_sequence_r, [6, 0, 2]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_i, [10, 0, 4]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [4, 0, 10]) self.assertEqual(routes[len(routes) - 1].stops_sequence_i, [10, 0, 4]) self.assertEqual(routes[len(routes) - 1].stops_sequence_r, [4, 0, 10]) routes = network.get_diametral_routes(mode_obj=bus_obj, jump=2, short=False, express=True) self.assertEqual(len(routes), 5) self.assertEqual(routes[0].nodes_sequence_i, [1, 2, 0, 6, 5]) self.assertEqual(routes[0].nodes_sequence_r, [5, 6, 0, 2, 1]) self.assertEqual(routes[0].stops_sequence_i, [1, 5]) self.assertEqual(routes[0].stops_sequence_r, [5, 1]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_i, [9, 10, 0, 4, 3]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [3, 4, 0, 10, 9]) self.assertEqual(routes[len(routes) - 1].stops_sequence_i, [9, 3]) self.assertEqual(routes[len(routes) - 1].stops_sequence_r, [3, 9]) routes = network.get_diametral_routes(mode_obj=bus_obj, jump=2, short=True, express=True) self.assertEqual(len(routes), 5) self.assertEqual(routes[0].nodes_sequence_i, [2, 0, 6]) self.assertEqual(routes[0].nodes_sequence_r, [6, 0, 2]) self.assertEqual(routes[0].stops_sequence_i, [2, 6]) self.assertEqual(routes[0].stops_sequence_r, [6, 2]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_i, [10, 0, 4]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [4, 0, 10]) self.assertEqual(routes[len(routes) - 1].stops_sequence_i, [10, 4]) self.assertEqual(routes[len(routes) - 1].stops_sequence_r, [4, 10]) routes = network.get_diametral_routes(mode_obj=bus_obj, jump=1, short=True, express=True) self.assertEqual(len(routes), 5) self.assertEqual(routes[0].nodes_sequence_i, [2, 0, 4]) self.assertEqual(routes[0].nodes_sequence_r, [4, 0, 2]) self.assertEqual(routes[0].stops_sequence_i, [2, 4]) self.assertEqual(routes[0].stops_sequence_r, [4, 2]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_i, [10, 0, 2]) self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [2, 0, 10]) self.assertEqual(routes[len(routes) - 1].stops_sequence_i, [10, 2]) self.assertEqual(routes[len(routes) - 1].stops_sequence_r, [2, 10]) with self.assertRaises(exceptions.JumpIsNotValidException): network.get_diametral_routes(mode_obj=bus_obj, jump=6, short=True, express=True) with self.assertRaises(exceptions.ModeIsNotValidException): network.get_diametral_routes(mode_obj="bus_obj", jump=6, short=True, express=True)
def create_default_routes(self, request): """ create defaults routes """ default_routes = request.data.get('default_routes', []) scene_public_id = request.data.get('scene_public_id') scene_obj = Scene.objects.select_related('city').get( public_id=scene_public_id) graph_obj = scene_obj.city.get_sidermit_graph() network_obj = SidermitTransportNetwork(graph_obj) transport_mode_dict = dict() try: route_tuple = [] all_routes = [] for default_route in default_routes: transport_mode_public_id = default_route['transportMode'] if transport_mode_public_id not in transport_mode_dict: transport_mode_obj = TransportMode.objects.get( public_id=transport_mode_public_id) transport_mode_dict[ transport_mode_public_id] = transport_mode_obj.get_sidermit_transport_mode( ) if default_route['type'] == 'Feeder': route_tuple.append((network_obj.get_feeder_routes( transport_mode_dict[transport_mode_public_id]), transport_mode_public_id)) elif default_route['type'] == 'Circular': route_tuple.append((network_obj.get_circular_routes( transport_mode_dict[transport_mode_public_id]), transport_mode_public_id)) elif default_route['type'] == 'Radial': route_tuple.append((network_obj.get_radial_routes( transport_mode_dict[transport_mode_public_id], short=default_route['extension'], express=default_route['odExclusive']), transport_mode_public_id)) elif default_route['type'] == 'Diametral': route_tuple.append((network_obj.get_diametral_routes( transport_mode_dict[transport_mode_public_id], jump=default_route['zoneJumps'], short=default_route['extension'], express=default_route['odExclusive']), transport_mode_public_id)) elif default_route['type'] == 'Tangential': route_tuple.append((network_obj.get_tangencial_routes( transport_mode_dict[transport_mode_public_id], jump=default_route['zoneJumps'], short=default_route['extension'], express=default_route['odExclusive']), transport_mode_public_id)) else: raise ParseError('type "{0}" is not valid.'.format( default_route['type'])) for (routes, transport_mode_public_id) in route_tuple: for route in routes: all_routes.append( dict(name=route.id, transport_mode_public_id=transport_mode_public_id, nodes_sequence_i=','.join( str(x) for x in route.nodes_sequence_i), nodes_sequence_r=','.join( str(x) for x in route.nodes_sequence_r), stops_sequence_i=','.join( str(x) for x in route.stops_sequence_i), stops_sequence_r=','.join( str(x) for x in route.stops_sequence_r), type=3 if route._type.value == 3 else 1)) except SIDERMITException as e: raise ParseError(e) except TransportMode.DoesNotExist: raise ParseError('Transport mode does not exist') return Response(all_routes, status.HTTP_200_OK)