Exemple #1
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 #2
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)
    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_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 #5
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)
    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_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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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