def test_get_mode_network_distance(self):

        graph_obj = Graph.build_from_parameters(2, 10, 1, 2)
        network_obj = TransportNetwork(graph_obj)
        [bus_obj, metro_obj] = TransportMode.get_default_modes()

        CI_obj = InfrastructureCost()

        circular = network_obj.get_circular_routes(mode_obj=metro_obj)
        radial = network_obj.get_radial_routes(mode_obj=bus_obj)

        for route in circular:
            network_obj.add_route(route)

        for route in radial:
            network_obj.add_route(route)

        f = defaultdict(float)
        for route in network_obj.get_routes():
            f[route.id] = 28

        mode_distance = CI_obj.get_mode_network_distance(graph_obj,
                                                         network_obj,
                                                         f=f)

        self.assertEqual(mode_distance[bus_obj], 160)
        self.assertEqual(mode_distance[metro_obj], 20)
Пример #2
0
    def test_network_to_file(self):
        """
        to test method to write a file with network information
        :return:
        """

        graph_obj = graph.Graph.build_from_parameters(5, 1000, 0.5, 2)
        mode_manager = TransportModeManager()
        bus_obj = mode_manager.get_mode("bus")
        metro = mode_manager.get_mode("metro")
        network = TransportNetwork(graph_obj)
        route1 = Route("r1", bus_obj, "1,2,0,4,3", "3,4,0,2,1", "1,0,3", "3,0,1")
        route2 = Route("r2", metro, "1,2,0,4,3", "3,4,0,2,1", "1,0,3", "3,0,1")
        network.add_route(route1)
        network.add_route(route2)
        # write file
        network.routes_to_file(os.path.join(self.data_path, 'write_test.csv'))
        file_obj = Path(os.path.join(self.data_path, 'write_test.csv'))
        # test
        self.assertTrue(file_obj.is_file())
        # to compare file with a test file
        self.assertTrue(
            filecmp.cmp(os.path.join(self.data_path, 'write1_test.csv'),
                        os.path.join(self.data_path, "write_test.csv")))
        # remove file
        os.remove(os.path.join(self.data_path, 'write_test.csv'))
Пример #3
0
    def test_get_operators_cost(self):
        """
        to test operators cost method
        :return:
        """

        graph_obj = Graph.build_from_parameters(n=2, l=10, g=0.5, p=2)
        [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)

        for route in radial_routes_bus:
            network_obj.add_route(route)

        f = defaultdict(float)
        k = defaultdict(float)

        line_cycle_time = defaultdict(float)
        for route in network_obj.get_routes():
            f[route.id] = 28
            k[route.id] = 100
            line_cycle_time[route.id] = 10

        cost = OperatorsCost.get_operators_cost(network_obj.get_routes(),
                                                line_cycle_time, f, k)

        self.assertEqual(cost, 75331.2)
Пример #4
0
    def test_lines_travel_time(self):
        """
        test lines_travel_time method of class operators_cost
        :return:
        """
        graph_obj = Graph.build_from_parameters(n=2, l=10, g=0.5, p=2)
        [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)

        lines_travel_time = OperatorsCost.lines_travel_time(
            routes=network_obj.get_routes(),
            edge_distance=graph_obj.get_edges_distance())

        self.assertEqual(lines_travel_time["R_bus_1"], 1.5)
        self.assertEqual(lines_travel_time["F_metro_1"], 0.25)
Пример #5
0
    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)
Пример #6
0
    def test_add_route_transport_network(self):
        """
        to test add_route method of transport_network class
        :return:
        """
        graph_obj = graph.Graph.build_from_parameters(5, 1000, 0.5, 2)
        mode_manager = TransportModeManager()
        bus_obj = mode_manager.get_mode("bus")
        network = TransportNetwork(graph_obj)
        route = Route("r1", bus_obj, "1,2,0,4,3", "3,4,0,2,1", "1,0,3", "3,0,1")
        network.add_route(route)

        self.assertTrue(isinstance(network.get_route("r1"), Route))
        self.assertEqual(len(network.get_routes()), 1)

        with self.assertRaises(exceptions.RouteIsNotvalidException):
            network.add_route("route")

        with self.assertRaises(exceptions.NodeSequencesIsNotValidException):
            route = Route("r2", bus_obj, "1,3,0,4,3", "3,4,0,2,1", "1,0,3",
                          "3,0,1")
            network.add_route(route)

        with self.assertRaises(exceptions.RouteIdDuplicatedException):
            route = Route("r1", bus_obj, "1,2,0,4,3", "3,4,0,2,1", "1,0,3",
                          "3,0,1")
            network.add_route(route)
Пример #7
0
    def test_remove_route_transport_network(self):
        """
        to test delete_route method of class transport_network class
        :return:
        """

        graph_obj = graph.Graph.build_from_parameters(5, 1000, 0.5, 2)
        mode_manager = TransportModeManager()
        bus_obj = mode_manager.get_mode("bus")
        network = TransportNetwork(graph_obj)
        route = Route("r1", bus_obj, "1,2,0,4,3", "3,4,0,2,1", "1,0,3", "3,0,1")
        network.add_route(route)
        network.remove_route("r1")
        self.assertEqual(len(network.get_routes()), 0)

        with self.assertRaises(exceptions.RouteIdNotFoundException):
            network.remove_route("r1")
Пример #8
0
    def test_most_loaded_section_constrains(self):

        graph_obj = Graph.build_from_parameters(2, 10, 1, 2)

        network_obj = TransportNetwork(graph_obj)
        [bus_obj, metro_obj] = TransportMode.get_default_modes()
        radial_bus = network_obj.get_radial_routes(bus_obj)
        feeder_bus = network_obj.get_feeder_routes(bus_obj)
        radial_metro = network_obj.get_radial_routes(metro_obj, short=True)

        for route in radial_bus:
            network_obj.add_route(route)

        for route in feeder_bus:
            network_obj.add_route(route)

        for route in radial_metro:
            network_obj.add_route(route)

        most_loaded_section = defaultdict(float)

        for route in network_obj.get_routes():
            most_loaded_section[route.id] = 10

        ineq_k = Constrains.most_loaded_section_constrains(
            network_obj.get_routes(), most_loaded_section)

        self.assertEqual(len(ineq_k), 6)

        self.assertEqual(ineq_k, [-150, -150, -150, -150, -1430, -1430])
Пример #9
0
    def test_fmax_constrains(self):

        graph_obj = Graph.build_from_parameters(2, 10, 1, 2)

        network_obj = TransportNetwork(graph_obj)
        [bus_obj, metro_obj] = TransportMode.get_default_modes()
        radial_bus = network_obj.get_radial_routes(bus_obj)
        feeder_bus = network_obj.get_feeder_routes(bus_obj)
        radial_metro = network_obj.get_radial_routes(metro_obj, short=True)

        for route in radial_bus:
            network_obj.add_route(route)

        for route in feeder_bus:
            network_obj.add_route(route)

        for route in radial_metro:
            network_obj.add_route(route)

        f = defaultdict(float)
        for route in network_obj.get_routes():
            f[route.id] = 28

        ineq_f = Constrains.fmax_constrains(graph_obj,
                                            network_obj.get_routes(),
                                            network_obj.get_modes(), f)

        self.assertEqual(ineq_f, [
            -136.0, -40.0, -136.0, -40.0, -143.0, -12.0, -143.0, -12.0, -150.0,
            -40.0, -150.0, -40.0, -136.0, -40.0, -136.0, -40.0, -143.0, -12.0,
            -143.0, -12.0
        ])
Пример #10
0
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)
Пример #11
0
# 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,
                                         network_obj,
                                         max_number_of_iteration=2)

# to get a data structure with overall results
overall_result = opt_obj.get_overall_results()
# to get a data structure with network results
network_result = opt_obj.get_network_results()

# to get a string with overall results and print in console
Пример #12
0
    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)