Example #1
0
 def setUp(self) -> None:
     self.project = create_example(
         join(gettempdir(), "test_set_pce_" + uuid4().hex))
     self.project.network.build_graphs()
     car_graph = self.project.network.graphs["c"]  # type: Graph
     car_graph.set_graph("distance")
     car_graph.set_blocked_centroid_flows(False)
     matrix = self.project.matrices.get_matrix("demand_omx")
     matrix.computational_view()
     self.tc = TrafficClass(name="car", graph=car_graph, matrix=matrix)
Example #2
0
    def test_set_pce(self):
        mat_name = AequilibraeMatrix().random_name()
        g = Graph()
        g.load_from_disk(test_graph)
        g.set_graph(cost_field="distance")

        # Creates the matrix for assignment
        args = {
            "file_name": os.path.join(gettempdir(), mat_name),
            "zones": g.num_zones,
            "matrix_names": ["cars", "trucks"],
            "index_names": ["my indices"],
        }

        matrix = AequilibraeMatrix()
        matrix.create_empty(**args)

        matrix.index[:] = g.centroids[:]
        matrix.cars.fill(1.1)
        matrix.trucks.fill(2.2)
        matrix.computational_view()

        tc = TrafficClass(graph=g, matrix=matrix)

        self.assertIsInstance(tc.results, AssignmentResults, 'Results have the wrong type')
        self.assertIsInstance(tc._aon_results, AssignmentResults, 'Results have the wrong type')

        with self.assertRaises(ValueError):
            tc.set_pce('not a number')
        tc.set_pce(1)
        tc.set_pce(3.9)
Example #3
0
    def setUp(self) -> None:
        self.matrix = AequilibraeMatrix()
        self.matrix.load(siouxfalls_demand)
        self.matrix.computational_view()

        self.project = Project(siouxfalls_project)
        self.project.network.build_graphs()
        self.car_graph = self.project.network.graphs['c']  # type: Graph
        self.car_graph.set_graph('free_flow_time')
        self.car_graph.set_blocked_centroid_flows(False)

        self.assignment = TrafficAssignment()
        self.assigclass = TrafficClass(self.car_graph, self.matrix)
Example #4
0
    def test_set_pce(self):
        project = create_example(
            join(gettempdir(), "test_set_pce_" + uuid4().hex))
        project.network.build_graphs()
        car_graph = project.network.graphs["c"]  # type: Graph
        car_graph.set_graph("distance")
        car_graph.set_blocked_centroid_flows(False)

        matrix = project.matrices.get_matrix("demand_omx")
        matrix.computational_view()

        tc = TrafficClass(graph=car_graph, matrix=matrix)

        self.assertIsInstance(tc.results, AssignmentResults,
                              "Results have the wrong type")
        self.assertIsInstance(tc._aon_results, AssignmentResults,
                              "Results have the wrong type")

        with self.assertRaises(ValueError):
            tc.set_pce("not a number")
        tc.set_pce(1)
        tc.set_pce(3.9)
        project.close()
Example #5
0
    def assign_matrix(self, matrix: AequilibraeMatrix, result_name: str):
        conn = database_connection()

        sql = f"select link_id, direction, a_node, b_node, distance, 1 capacity from {DELAUNAY_TABLE}"

        df = pd.read_sql(sql, conn)
        centroids = np.array(np.unique(np.hstack((df.a_node.values, df.b_node.values))), int)

        g = Graph()
        g.mode = 'delaunay'
        g.network = df
        g.prepare_graph(centroids)
        g.set_blocked_centroid_flows(True)

        tc = TrafficClass('delaunay', g, matrix)
        ta = TrafficAssignment()
        ta.set_classes([tc])
        ta.set_time_field('distance')
        ta.set_capacity_field('capacity')
        ta.set_vdf('BPR')
        ta.set_vdf_parameters({"alpha": 0, "beta": 1.0})
        ta.set_algorithm('all-or-nothing')
        ta.execute()

        report = {"setup": str(ta.info())}
        data = [result_name, "Delaunay assignment", self.procedure_id, str(report), ta.procedure_date, '']
        conn.execute("""Insert into results(table_name, procedure, procedure_id, procedure_report, timestamp,
                                            description) Values(?,?,?,?,?,?)""", data)
        conn.commit()
        conn.close()

        cols = []
        for x in matrix.view_names:
            cols.extend([f'{x}_ab', f'{x}_ba', f'{x}_tot'])
        df = ta.results()[cols]
        conn = sqlite3.connect(join(environ[ENVIRON_VAR], "results_database.sqlite"))
        df.to_sql(result_name, conn)
        conn.close()
# so let's inspect what we have in the project
proj_matrices = project.matrices
proj_matrices.list()

# %%

# Let's get it in this better way
demand = proj_matrices.get_matrix("demand_omx")
demand.computational_view(["matrix"])

# %%

assig = TrafficAssignment()

# Creates the assignment class
assigclass = TrafficClass(graph, demand)

# The first thing to do is to add at list of traffic classes to be assigned
assig.add_class(assigclass)

# We set these parameters only after adding one class to the assignment
assig.set_vdf(
    "BPR"
)  # This is not case-sensitive # Then we set the volume delay function

assig.set_vdf_parameters({"alpha": "b", "beta": "power"})  # And its parameters

assig.set_capacity_field(
    "capacity"
)  # The capacity and free flow travel times as they exist in the graph
assig.set_time_field("free_flow_time")
Example #7
0
class TestTrafficClass(TestCase):
    def setUp(self) -> None:
        self.project = create_example(
            join(gettempdir(), "test_set_pce_" + uuid4().hex))
        self.project.network.build_graphs()
        car_graph = self.project.network.graphs["c"]  # type: Graph
        car_graph.set_graph("distance")
        car_graph.set_blocked_centroid_flows(False)
        matrix = self.project.matrices.get_matrix("demand_omx")
        matrix.computational_view()
        self.tc = TrafficClass(name="car", graph=car_graph, matrix=matrix)

    def tearDown(self) -> None:
        self.project.close()

    def test_result_type(self):
        self.assertIsInstance(self.tc.results, AssignmentResults,
                              "Results have the wrong type")
        self.assertIsInstance(self.tc._aon_results, AssignmentResults,
                              "Results have the wrong type")

    def test_set_pce(self):
        with self.assertRaises(ValueError):
            self.tc.set_pce("not a number")
        self.tc.set_pce(1)
        self.tc.set_pce(3.9)

    def test_set_vot(self):
        self.assertEqual(self.tc.vot, 1.0)
        self.tc.set_vot(4.5)
        self.assertEqual(self.tc.vot, 4.5)

    def test_set_fixed_cost(self):
        self.assertEqual(self.tc.fc_multiplier, 1.0)

        with self.assertRaises(ValueError):
            self.tc.set_fixed_cost("Field_Does_Not_Exist", 2.5)
        self.assertEqual(self.tc.fc_multiplier, 1.0)

        self.tc.set_fixed_cost("distance", 3.0)
        self.assertEqual(self.tc.fc_multiplier, 3.0)
        self.assertEqual(self.tc.fixed_cost_field, "distance")
Example #8
0
# so let's inspect what we have in the project
proj_matrices = project.matrices
proj_matrices.list()

# %%

# Let's get it in this better way
demand = proj_matrices.get_matrix("demand_omx")
demand.computational_view(["matrix"])

# %%

assig = TrafficAssignment()

# Creates the assignment class
assigclass = TrafficClass(name='car', graph=graph, matrix=demand)

# The first thing to do is to add at list of traffic classes to be assigned
assig.add_class(assigclass)

# We set these parameters only after adding one class to the assignment
assig.set_vdf("BPR")  # This is not case-sensitive # Then we set the volume delay function

assig.set_vdf_parameters({"alpha": "b", "beta": "power"})  # And its parameters

assig.set_capacity_field("capacity")  # The capacity and free flow travel times as they exist in the graph
assig.set_time_field("free_flow_time")

# And the algorithm we want to use to assign
assig.set_algorithm("bfw")