Ejemplo n.º 1
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)
Ejemplo n.º 2
0
    def test_raises_routes_exceptions(self):
        """
        to check route class exceptions
        :return:
        """

        mode_manager = TransportModeManager()
        bus_obj = mode_manager.get_mode("bus")

        with self.assertRaises(exceptions.RouteIdIsNotValidException):
            Route(None, bus_obj, "1,2,0,4,3", "3,4,0,2,1", "1,0,3",
                  "3,0,1")

        with self.assertRaises(exceptions.ModeIsNotValidException):
            Route("r1", "train", "1,2,0,4,3", "3,4,0,2,1",
                  "1,0,3", "3,0,1")

        with self.assertRaises(exceptions.StopsSequencesException):
            Route("r1", bus_obj, "1,2,0,4,3", "3,4,0,2,1", "1,5,3",
                  "3,0,1")

        with self.assertRaises(exceptions.FirstStopIsNotValidException):
            Route("r1", bus_obj, "1,2,0,4,3", "3,4,0,2,1", "2,0,3",
                  "3,0,1")

        with self.assertRaises(exceptions.LastStopIsNotValidException):
            Route("r1", bus_obj, "1,2,0,4,3", "3,4,0,2,1", "1,0,2",
                  "3,0,1")

        with self.assertRaises(exceptions.NotCycleException):
            Route("r1", bus_obj, "1,2,0,4,3", "4,0,2,1", "1,0,3",
                  "4,0,1")
Ejemplo n.º 3
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'))
Ejemplo n.º 4
0
    def add_transport_mode(self, mode: TransportMode):
        """
        to add a transport mode in the network
        :param mode: TransportMode
        :return:
        """

        if not isinstance(mode, TransportMode):
            raise ModeIsNotValidException("Mode is not valid")

        if mode not in self.__modes:

            mode_manager = TransportModeManager(add_default_mode=False)

            list_mode = [mode]

            for modes in self.__modes:
                if modes not in list_mode:
                    list_mode.append(modes)

            for modes in list_mode:
                mode_manager.add_mode(modes)

            if mode_manager.is_valid_to_assignment_step():
                self.__modes.append(mode)
            else:
                raise TransportModeException(
                    "only 2 transport mode can be defined and at least one should have d parameter = 1"
                )
Ejemplo n.º 5
0
    def test_add_mode(self):
        """
        to test add_mode method
        :return:
        """
        m = TransportModeManager()

        name = "train"
        bya = 1
        co = 1
        c1 = 1
        c2 = 1
        v = 20
        t = 0.2
        fmax = 100
        kmax = 50
        theta = 0.5
        tat = 1
        d = 1
        fini = 28

        mode_obj = TransportMode(name, bya, co, c1, c2, v, t, fmax, kmax, theta, tat, d, fini)

        m.add_mode(mode_obj)

        self.assertEqual(len(m.get_modes()), 3)

        with self.assertRaises(exceptions.AddModeExceptions):
            mode_obj = TransportMode("bus", bya, co, c1, c2, v, t, fmax, kmax, theta, tat, d, fini)
            m.add_mode(mode_obj)

        with self.assertRaises(exceptions.ModeIsNotValidException):
            m.add_mode("mode_obj")
Ejemplo n.º 6
0
    def test_routes(self):
        """
        test Route class
        :return:
        """

        mode_manager = TransportModeManager()
        bus_obj = mode_manager.get_mode("bus")

        r = Route("r1", bus_obj, "1,2,0,4,3", "3,4,0,2,1", "1,0,3", "3,0,1")

        self.assertTrue(isinstance(r, Route))
Ejemplo n.º 7
0
    def network_validator(self, OD_matrix: defaultdict2_float) -> bool:
        """
        to check if Transport network is well defined for all pairs OD with trips. This must has at least a route for
        each OD pair with trips. Also this must has until 2 TransportMode and at least one has parameter d=1.
        :param OD_matrix: OD matrix get from Demand object
        :return: True if all OD pairs with trips have at least one path between origin and destination. False if not.
        """
        nodes = self.extended_graph_obj.get_extended_graph_nodes()
        # to check a path between all OD pair with trips
        for origin_id in OD_matrix:
            for destination_id in OD_matrix[origin_id]:
                vij = OD_matrix[origin_id][destination_id]
                if vij != 0:
                    origin = None
                    destination = None
                    for city_node in nodes:
                        if str(origin_id) == str(city_node.graph_node.id):
                            origin = city_node
                        if str(destination_id) == str(city_node.graph_node.id):
                            destination = city_node

                    _, label, _ = self.build_hyperpath_graph(
                        origin, destination)

                    # if there is a stop with a label != infinity, you can get from the origin to the destination
                    conection = False
                    for stop in nodes[origin]:
                        if label[stop] != float('inf'):
                            conection = True
                            break
                    if conection is False:
                        raise TransportNetworkException(
                            "par OD {}-{} without connection".format(
                                origin_id, destination_id))
        # to check network must has until 2 TransportMode
        list_mode = []
        for city_node in nodes:
            for stop in nodes[city_node]:
                if stop.mode not in list_mode:
                    list_mode.append(stop.mode)

        mode_manager = TransportModeManager(add_default_mode=False)

        for mode in list_mode:
            mode_manager.add_mode(mode)

        return mode_manager.is_valid_to_assignment_step()
Ejemplo n.º 8
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")
Ejemplo n.º 9
0
    def test_is_valid_to_assignment_step(self):
        """
        test is_valid method
        :return:
        """

        m = TransportModeManager()

        self.assertTrue(m.is_valid_to_assignment_step())

        m.remove_mode("bus")

        self.assertTrue(m.is_valid_to_assignment_step())

        m.remove_mode("metro")

        with self.assertRaises(exceptions.TransportNetworkException):
            m.is_valid_to_assignment_step()
Ejemplo n.º 10
0
    def test_get_circular_routes(self):
        """
        to test add circular routes
        :return:
        """
        g = graph.Graph.build_from_parameters(5, 1000, 0.5, 2)
        mode_manager = TransportModeManager()
        bus_obj = mode_manager.get_mode("bus")
        network = TransportNetwork(g)

        routes = network.get_circular_routes(bus_obj)

        self.assertEqual(len(routes), 2)
        self.assertEqual(routes[0].nodes_sequence_i, [2, 4, 6, 8, 10, 2])
        self.assertEqual(routes[0].stops_sequence_i, [2, 4, 6, 8, 10, 2])
        self.assertEqual(routes[0].nodes_sequence_r, [])
        self.assertEqual(routes[0].stops_sequence_r, [])

        self.assertEqual(routes[1].nodes_sequence_i, [])
        self.assertEqual(routes[1].stops_sequence_i, [])
        self.assertEqual(routes[1].nodes_sequence_r, [10, 8, 6, 4, 2, 10])
        self.assertEqual(routes[1].stops_sequence_r, [10, 8, 6, 4, 2, 10])

        with self.assertRaises(exceptions.ModeIsNotValidException):
            g = graph.Graph.build_from_parameters(5, 1000, 0.5, 2)
            network = TransportNetwork(g)
            network.get_circular_routes("bus_obj")

        with self.assertRaises(exceptions.CircularRouteIsNotValidException):
            g = graph.Graph.build_from_parameters(1, 1000, 0.5, 2)
            mode_manager = TransportModeManager()
            bus_obj = mode_manager.get_mode("bus")
            network = TransportNetwork(g)
            network.get_circular_routes(bus_obj)
Ejemplo n.º 11
0
    def test_delete_mode(self):
        """
        to test delete_mode method
        :return:
        """

        m = TransportModeManager()

        m.remove_mode("bus")
        self.assertEqual(len(m.get_modes()), 1)

        with self.assertRaises(exceptions.ModeDoesNotExistExceptions):
            m.remove_mode("train")
Ejemplo n.º 12
0
    def test_get_feeder_routes(self):
        """
        to test add feeder routes
        :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)

        routes = network.get_feeder_routes(bus_obj)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [1, 2])
        self.assertEqual(routes[0].nodes_sequence_r, [2, 1])
        self.assertEqual(routes[0].stops_sequence_i, [1, 2])
        self.assertEqual(routes[0].stops_sequence_r, [2, 1])
        self.assertEqual(routes[1].nodes_sequence_i, [3, 4])
        self.assertEqual(routes[1].nodes_sequence_r, [4, 3])
        self.assertEqual(routes[1].stops_sequence_i, [3, 4])
        self.assertEqual(routes[1].stops_sequence_r, [4, 3])

        with self.assertRaises(exceptions.ModeIsNotValidException):
            network.get_feeder_routes("bus_obj")
Ejemplo n.º 13
0
    def test_get(self):
        """
        to test get methods
        :return:
        """
        m = TransportModeManager()

        self.assertEqual(len(m.get_modes()), 2)
        self.assertEqual(len(m.get_modes_names()), 2)
        self.assertTrue(
            isinstance(m.get_mode("bus"), TransportMode))

        with self.assertRaises(exceptions.ModeNotFoundExceptions):
            m.get_mode("train")
Ejemplo n.º 14
0
    def test_get_tangencial_routes(self):
        """
        to test add tangencial routes
        :return:
        """
        g = graph.Graph.build_from_parameters(5, 1000, 0.5, 2)
        m = TransportModeManager()
        bus = m.get_mode("bus")
        t = TransportNetwork(g)

        routes = t.get_tangencial_routes(mode_obj=bus, jump=2, short=False, express=False)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [1, 2, 4, 6, 5])
        self.assertEqual(routes[0].nodes_sequence_r, [5, 6, 4, 2, 1])
        self.assertEqual(routes[0].stops_sequence_i, [1, 2, 4, 6, 5])
        self.assertEqual(routes[0].stops_sequence_r, [5, 6, 4, 2, 1])
        self.assertEqual(routes[len(routes) - 1].nodes_sequence_i, [9, 10, 2, 4, 3])
        self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [3, 4, 2, 10, 9])
        self.assertEqual(routes[len(routes) - 1].stops_sequence_i, [9, 10, 2, 4, 3])
        self.assertEqual(routes[len(routes) - 1].stops_sequence_r, [3, 4, 2, 10, 9])

        routes = t.get_tangencial_routes(mode_obj=bus, jump=2, short=True, express=False)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [2, 4, 6])
        self.assertEqual(routes[0].nodes_sequence_r, [6, 4, 2])
        self.assertEqual(routes[0].stops_sequence_i, [2, 4, 6])
        self.assertEqual(routes[0].stops_sequence_r, [6, 4, 2])
        self.assertEqual(routes[len(routes) - 1].nodes_sequence_i, [10, 2, 4])
        self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [4, 2, 10])
        self.assertEqual(routes[len(routes) - 1].stops_sequence_i, [10, 2, 4])
        self.assertEqual(routes[len(routes) - 1].stops_sequence_r, [4, 2, 10])

        routes = t.get_tangencial_routes(mode_obj=bus, jump=2, short=False, express=True)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [1, 2, 4, 6, 5])
        self.assertEqual(routes[0].nodes_sequence_r, [5, 6, 4, 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, 2, 4, 3])
        self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [3, 4, 2, 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 = t.get_tangencial_routes(mode_obj=bus, jump=2, short=True, express=True)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [2, 4, 6])
        self.assertEqual(routes[0].nodes_sequence_r, [6, 4, 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, 2, 4])
        self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [4, 2, 10])
        self.assertEqual(routes[len(routes) - 1].stops_sequence_i, [10, 4])
        self.assertEqual(routes[len(routes) - 1].stops_sequence_r, [4, 10])

        routes = t.get_tangencial_routes(mode_obj=bus, jump=1, short=True, express=True)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [2, 4])
        self.assertEqual(routes[0].nodes_sequence_r, [4, 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, 2])
        self.assertEqual(routes[len(routes) - 1].nodes_sequence_r, [2, 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):
            t.get_tangencial_routes(mode_obj=bus, jump=6, short=True, express=True)

        with self.assertRaises(exceptions.ModeIsNotValidException):
            t.get_tangencial_routes(mode_obj="bus", jump=6, short=True, express=True)
Ejemplo n.º 15
0
    def test_get_radial_routes(self):
        """
        to test add radial routes
        :return:
        """
        g = graph.Graph.build_from_parameters(5, 1000, 0.5, 2)
        mode_manager = TransportModeManager()
        bus_obj = mode_manager.get_mode("bus")
        metro_obj = mode_manager.get_mode("metro")
        network = TransportNetwork(g)

        routes = network.get_radial_routes(mode_obj=bus_obj, short=False, express=False)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [1, 2, 0])
        self.assertEqual(routes[0].nodes_sequence_r, [0, 2, 1])
        self.assertEqual(routes[0].stops_sequence_i, [1, 2, 0])
        self.assertEqual(routes[0].stops_sequence_r, [0, 2, 1])
        self.assertEqual(routes[1].nodes_sequence_i, [3, 4, 0])
        self.assertEqual(routes[1].nodes_sequence_r, [0, 4, 3])
        self.assertEqual(routes[1].stops_sequence_i, [3, 4, 0])
        self.assertEqual(routes[1].stops_sequence_r, [0, 4, 3])

        routes = network.get_radial_routes(mode_obj=bus_obj, short=True, express=False)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [2, 0])
        self.assertEqual(routes[0].nodes_sequence_r, [0, 2])
        self.assertEqual(routes[0].stops_sequence_i, [2, 0])
        self.assertEqual(routes[0].stops_sequence_r, [0, 2])
        self.assertEqual(routes[1].nodes_sequence_i, [4, 0])
        self.assertEqual(routes[1].nodes_sequence_r, [0, 4])
        self.assertEqual(routes[1].stops_sequence_i, [4, 0])
        self.assertEqual(routes[1].stops_sequence_r, [0, 4])

        routes = network.get_radial_routes(mode_obj=bus_obj, short=False, express=True)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [1, 2, 0])
        self.assertEqual(routes[0].nodes_sequence_r, [0, 2, 1])
        self.assertEqual(routes[0].stops_sequence_i, [1, 0])
        self.assertEqual(routes[0].stops_sequence_r, [0, 1])
        self.assertEqual(routes[1].nodes_sequence_i, [3, 4, 0])
        self.assertEqual(routes[1].nodes_sequence_r, [0, 4, 3])
        self.assertEqual(routes[1].stops_sequence_i, [3, 0])
        self.assertEqual(routes[1].stops_sequence_r, [0, 3])

        routes = network.get_radial_routes(mode_obj=metro_obj, short=True, express=True)

        self.assertEqual(len(routes), 5)
        self.assertEqual(routes[0].nodes_sequence_i, [2, 0])
        self.assertEqual(routes[0].nodes_sequence_r, [0, 2])
        self.assertEqual(routes[0].stops_sequence_i, [2, 0])
        self.assertEqual(routes[0].stops_sequence_r, [0, 2])
        self.assertEqual(routes[1].nodes_sequence_i, [4, 0])
        self.assertEqual(routes[1].nodes_sequence_r, [0, 4])
        self.assertEqual(routes[1].stops_sequence_i, [4, 0])
        self.assertEqual(routes[1].stops_sequence_r, [0, 4])

        with self.assertRaises(exceptions.ModeIsNotValidException):
            network.get_radial_routes("bus_obj")