Exemple #1
0
    def get_demand_matrix_header(self, obj):
        content = []
        try:
            graph_obj = Graph.build_from_parameters(obj.n, obj.l, obj.g, obj.p,
                                                    obj.etha, obj.etha_zone,
                                                    obj.angles, obj.gi, obj.hi)
        except (SIDERMITException, TypeError):
            graph_obj = Graph.build_from_content(obj.graph,
                                                 GraphContentFormat.PAJEK)

        if graph_obj is not None:
            for node_obj in graph_obj.get_nodes():
                content.append(node_obj.name)

        return content
Exemple #2
0
    def network_data_from_pajek_file(self, request):
        try:
            graph_content = request.query_params.get('graph')

            graph_obj = Graph.build_from_content(graph_content,
                                                 GraphContentFormat.PAJEK)
            network = get_network_descriptor(graph_obj)
            n, l, g, p, etha, etha_zone, angles, gi, hi = graph_obj.get_parameters(
            )
            angles = ','.join(map(lambda x: str(round(x, 2)), angles))
            gi = ','.join(map(lambda x: str(round(x, 2)), gi))
            hi = ','.join(map(lambda x: str(round(x, 2)), hi))
        except (ValueError, SIDERMITException) as e:
            raise ParseError(e)

        return Response(
            {
                'network': network,
                'n': n,
                'l': l,
                'p': p,
                'g': g,
                'etha': etha,
                'etha_zone': etha_zone,
                'angles': angles,
                'gi': gi,
                'hi': hi
            }, status.HTTP_200_OK)
    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
        ])
    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)
    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])
Exemple #6
0
    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)
Exemple #7
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)
Exemple #8
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)
Exemple #9
0
    def get_network_descriptor(self, obj):
        """
        :return: list of nodes and edges based on parameters or graph variable
        """
        content = dict(nodes=[], edges=[])
        try:
            graph_obj = Graph.build_from_parameters(obj.n, obj.l, obj.g, obj.p,
                                                    obj.etha, obj.etha_zone,
                                                    obj.angles, obj.gi, obj.hi)
        except (SIDERMITException, TypeError):
            graph_obj = Graph.build_from_content(obj.graph,
                                                 GraphContentFormat.PAJEK)

        if graph_obj is not None:
            content = get_network_descriptor(graph_obj)

        return content
Exemple #10
0
    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)
Exemple #11
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)
Exemple #12
0
 def build_city_nodes(graph_obj: Graph) -> List[CityNode]:
     """
     to build CityNodes in the extended graph
     :param graph_obj: Graph object
     :return: List[CityNode]
     """
     city_nodes = []
     for node in graph_obj.get_nodes():
         city_node = CityNode(len(city_nodes), node)
         city_nodes.append(city_node)
     return city_nodes
Exemple #13
0
    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)
Exemple #14
0
    def fmax_constrains(graph_obj: Graph, routes: List[Route], list_mode: List[TransportMode], f: defaultdict_float) -> \
            List[float]:
        """
        to get constrains about fmax in each edge in the network with respect to capacity in stop of the each mode
        :param graph_obj: Graph object
        :param routes: list of Route object
        :param list_mode: list TransportMode object
        :param f: dict with frequency for each route_id
        :return: list with constrains
        """

        ineq_constrain = []

        edges = graph_obj.get_edges()

        for edge in edges:
            nodei = edge.node1.id
            nodej = edge.node2.id
            for mode in list_mode:
                fmax = mode.fmax / mode.d
                sum_f = 0
                for route in routes:
                    if route.mode == mode:
                        if f[route.id] != 0:
                            node_sequence_i = route.nodes_sequence_i
                            node_sequence_r = route.nodes_sequence_r

                            for i in range(len(node_sequence_i) - 1):
                                j = i + 1
                                if str(nodei) == str(
                                        node_sequence_i[i]) and str(
                                            nodej) == str(node_sequence_i[j]):
                                    sum_f += f[route.id] / mode.d

                            for i in range(len(node_sequence_r) - 1):
                                j = i + 1
                                if str(nodei) == str(
                                        node_sequence_r[i]) and str(
                                            nodej) == str(node_sequence_r[j]):
                                    sum_f += f[route.id] / mode.d
                ineq_constrain.append(sum_f - fmax)

        return ineq_constrain
Exemple #15
0
    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))
Exemple #16
0
    def build_graph_file_from_parameters(self, request):
        try:
            n = int(request.query_params.get('n'))
            l = float(request.query_params.get('l'))
            p = float(request.query_params.get('p'))
            g = float(request.query_params.get('g'))

            etha = request.query_params.get('etha')
            etha_zone = request.query_params.get('etha_zone')
            angles = request.query_params.get('angles')
            gi = request.query_params.get('gi')
            hi = request.query_params.get('hi')
            if etha is not None:
                etha = float(etha)
            if etha_zone is not None:
                etha_zone = int(etha_zone)
            if angles is not None:
                angles = [float(value) for value in angles.split(',')]
            if gi is not None:
                gi = [float(value) for value in gi.split(',')]
            if hi is not None:
                hi = [float(value) for value in hi.split(',')]

            graph_obj = Graph.build_from_parameters(n, l, g, p, etha,
                                                    etha_zone, angles, gi, hi)
            content = graph_obj.export_graph(GraphContentFormat.PAJEK)
            network = get_network_descriptor(graph_obj)
        except (ValueError, SIDERMITException) as e:
            raise ParseError(e)
        except TypeError:
            raise ParseError('Parameter can not be empty')

        return Response({
            'pajek': content,
            'network': network
        }, status.HTTP_200_OK)
Exemple #17
0
    def build_matrix_data(self, request, public_id=None):
        try:
            y = int(request.query_params.get('y'))
            a = float(request.query_params.get('a'))
            alpha = float(request.query_params.get('alpha'))
            beta = float(request.query_params.get('beta'))

            city_obj = self.get_object()
            graph_obj = Graph.build_from_content(city_obj.graph,
                                                 GraphContentFormat.PAJEK)

            demand_obj = Demand.build_from_parameters(graph_obj, y, a, alpha,
                                                      beta)
            demand_matrix = demand_obj.get_matrix()
            # pass matrix dict to list of list
            demand_matrix_data = []
            size = len(demand_matrix.keys())
            for i in range(size):
                row = []
                for j in range(size):
                    row.append(round(demand_matrix[i][j], 2))
                demand_matrix_data.append(row)

            demand_matrix_header = [
                node_obj.name for node_obj in graph_obj.get_nodes()
            ]
        except (ValueError, SIDERMITException) as e:
            raise ParseError(e)
        except TypeError:
            raise ParseError('Parameter can not be empty')

        return Response(
            {
                'demand_matrix': demand_matrix_data,
                'demand_matrix_header': demand_matrix_header
            }, status.HTTP_200_OK)
Exemple #18
0
    def build_matrix_from_file(self, request, public_id=None):
        try:
            content = request.data.get('content', '')
            matrix = []
            rows = content.split('\n')[1:]
            for row in rows:
                matrix.append([float(value) for value in row.split(',')[1:]])

            city_obj = self.get_object()
            graph_obj = Graph.build_from_content(city_obj.graph,
                                                 GraphContentFormat.PAJEK)

            demand_obj = Demand.build_from_content(graph_obj, matrix)
            demand_matrix = demand_obj.get_matrix()
            # pass matrix dict to list of list
            demand_matrix_data = []
            size = len(demand_matrix.keys())
            for i in range(size):
                row = []
                for j in range(size):
                    row.append(round(demand_matrix[i][j], 2))
                demand_matrix_data.append(row)

            demand_matrix_header = [
                node_obj.name for node_obj in graph_obj.get_nodes()
            ]
        except SIDERMITException as e:
            raise ParseError(e)
        except TypeError:
            raise ParseError('file format wrong')

        return Response(
            {
                'demand_matrix': demand_matrix_data,
                'demand_matrix_header': demand_matrix_header
            }, status.HTTP_200_OK)
    def get_mode_network_distance(graph_obj: Graph,
                                  network_obj: TransportNetwork,
                                  f: defaultdict_float) -> defaultdict_float:
        """
        to get total distance builded in each transport mode
        :param network_obj: TransportNetwork object
        :param graph_obj: Graph object
        :param f: dict with frequency for each route_id
        :return: ddict with total distance for each mode in transport network
        """
        edges = graph_obj.get_edges()
        edges_distance = graph_obj.get_edges_distance()

        routes = network_obj.get_routes()
        list_modes = network_obj.get_modes()

        mode_distance = defaultdict(float)

        for mode in list_modes:
            edge_list = []
            for edge in edges:
                d_e = edges_distance[edge.node1.id][edge.node2.id]
                for route in routes:
                    if route.mode == mode:
                        if f[route.id] != 0:
                            node_sequence_i = route.nodes_sequence_i
                            node_sequence_r = route.nodes_sequence_r

                            ver_sum = False
                            for i in range(len(node_sequence_i) - 1):
                                j = i + 1
                                if node_sequence_i[
                                        i] == edge.node1.id and node_sequence_i[
                                            j] == edge.node2.id and (
                                                edge.node1.id, edge.node2.id
                                            ) not in edge_list:
                                    mode_distance[mode] += d_e * mode.d
                                    edge_list.append(
                                        (edge.node1.id, edge.node2.id))
                                    edge_list.append(
                                        (edge.node2.id, edge.node1.id))
                                    ver_sum = True
                                    break
                            if ver_sum is True:
                                break
                            for i in range(len(node_sequence_r) - 1):
                                j = i + 1
                                if node_sequence_r[
                                        i] == edge.node1.id and node_sequence_r[
                                            j] == edge.node2.id and (
                                                edge.node1.id, edge.node2.id
                                            ) not in edge_list:
                                    mode_distance[mode] += d_e * mode.d
                                    edge_list.append(
                                        (edge.node1.id, edge.node2.id))
                                    edge_list.append(
                                        (edge.node2.id, edge.node1.id))
                                    ver_sum = True
                                    break
                            if ver_sum is True:
                                break

        return mode_distance
Exemple #20
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)
Exemple #21
0
 def get_sidermit_graph(self):
     return Graph.build_from_content(self.graph, GraphContentFormat.PAJEK)
Exemple #22
0
#                                                                          tv / 100000 * 60, t, CU / 100000))
#
# edge_distance = Graph.get_edges_distance(graph_obj)
# tiempo_viaje = CO_obj.lines_travel_time(network_obj.get_routes(), edge_distance)
# tiempo_ciclo = CO_obj.get_cycle_time(z, v, network_obj.get_routes(), tiempo_viaje)
#
# print(tiempo_viaje)
# print(tiempo_ciclo)
# print(CO_obj.get_operators_cost(network_obj.get_routes(), tiempo_ciclo, f, k))

from sidermit.city import Graph, Demand
from sidermit.optimization.optimizer import Optimizer
from sidermit.publictransportsystem import TransportMode, TransportNetwork, Passenger

n, l, g, p = 4, 10, 0.85, 2
graph_obj = Graph.build_from_parameters(n, l, g, p)
graph_obj.graph_to_pajek_file("graph.pajek")
graph_obj = Graph.build_from_file("graph.pajek")
print(graph_obj.get_parameters())

demand_obj = Demand.build_from_parameters(graph_obj, 300000, 0.78, 0.25, 0.22)
network_obj = TransportNetwork(graph_obj)

va = 4
pv = 2.74
pw = 2 * 2.74
pa = 3 * 2.74
pt = 16
spv = 2.74
spw = 2 * 2.74
spa = 3 * 2.74
Exemple #23
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)
Exemple #24
0
    def validate(self, validated_data):

        if validated_data.get('step') == self.STEP_1:
            key_exists = []
            keys = ['n', 'l', 'g', 'p']
            for key in keys:
                key_exists.append(validated_data.get(key) is None)

            etha = validated_data.get('etha')
            etha_zone = validated_data.get('etha_zone')
            angles = None
            gi = None
            hi = None

            if validated_data.get('angles') is not None:
                angles = [
                    float(v.strip())
                    for v in validated_data.get('angles').split(',')
                ]
            if validated_data.get('gi') is not None:
                gi = [
                    float(v.strip())
                    for v in validated_data.get('gi').split(',')
                ]
            if validated_data.get('hi') is not None:
                hi = [
                    float(v.strip())
                    for v in validated_data.get('hi').split(',')
                ]

            try:
                if not all(key_exists):
                    # if all keys are not none, there are not parameters but graph has to exist
                    Graph.build_from_parameters(validated_data.get('n'),
                                                validated_data.get('l'),
                                                validated_data.get('g'),
                                                validated_data.get('p'), etha,
                                                etha_zone, angles, gi, hi)

                Graph.build_from_content(validated_data['graph'],
                                         GraphContentFormat.PAJEK)
            except SIDERMITException as e:
                raise serializers.ValidationError(e)

        elif validated_data.get('step') == self.STEP_2:
            key_exists = []
            keys = ['y', 'a', 'alpha', 'beta']
            for key in keys:
                key_exists.append(validated_data.get(key) is None)

            try:
                public_id = self.context['view'].kwargs['public_id']
                graph_obj = Graph.build_from_content(
                    City.objects.only('graph').get(public_id=public_id).graph,
                    GraphContentFormat.PAJEK)
                if not all(key_exists):
                    # if all keys are not none, check values
                    Demand.build_from_parameters(graph_obj,
                                                 validated_data.get('y'),
                                                 validated_data.get('a'),
                                                 validated_data.get('alpha'),
                                                 validated_data.get('beta'))

                if validated_data['demand_matrix'] is None:
                    raise serializers.ValidationError(
                        'You have to create a demand matrix before to continue'
                    )
                Demand.build_from_content(graph_obj,
                                          validated_data['demand_matrix'])
            except SIDERMITException as e:
                raise serializers.ValidationError(e)

        return validated_data