def test_skimming_on_assignment(self): matrix = AequilibraeMatrix() matrix.load(os.path.join(gettempdir(), self.mat_name)) matrix.computational_view(["cars"]) res = AssignmentResults() res.prepare(self.g, matrix) self.g.set_skimming([]) self.g.set_blocked_centroid_flows(True) assig = allOrNothing(matrix, self.g, res) assig.execute() if res.skims.distance.sum() > 0: self.fail( "skimming for nothing during assignment returned something different than zero" ) self.g.set_skimming("distance") res.prepare(self.g, matrix) assig = allOrNothing(matrix, self.g, res) assig.execute() if res.skims.distance.sum() != 2914644.0: self.fail("skimming during assignment returned the wrong value") matrix.close()
def new_record(self, name: str, file_name: str, matrix=AequilibraeMatrix()) -> MatrixRecord: """Creates a new record for a matrix in disk, but does not save it If the matrix file is not already on disk, it will fail Args: *name* (:obj:`str`): Name of the matrix *file_name* (:obj:`str`): Name of the file on disk Return: *matrix_record* (:obj:`MatrixRecord`): A matrix record that can be manipulated in memory before saving """ if name in self.__items: raise ValueError( f"There is already a matrix of name ({name}). It must be unique." ) for mat in self.__items.values(): if mat.file_name == file_name: raise ValueError( f"There is already a matrix record for file name ({file_name}). It must be unique." ) if matrix.cores > 0: if isfile(join(self.fldr, file_name)): raise FileExistsError( f"{file_name} already exists. Choose a different name or matrix format" ) mat_format = file_name.split(".")[-1].lower() if mat_format not in ["omx", "aem"]: raise ValueError( "Matrix needs to be either OMX or native AequilibraE") matrix.export(join(self.fldr, file_name)) cores = matrix.cores else: if not isfile(join(self.fldr, file_name)): raise FileExistsError( f"{file_name} does not exist. Cannot create this matrix record" ) mat = AequilibraeMatrix() mat.load(join(self.fldr, file_name)) cores = mat.cores mat.close() del mat tp = {key: None for key in self.__fields} tp["name"] = name tp["file_name"] = file_name tp["cores"] = cores mr = MatrixRecord(tp) mr.save() self.__items[name.lower()] = mr logger.warning("Matrix Record has been saved to the database") return mr
def test_execute(self): # Loads and prepares the graph car_loads = [] two_class_loads = [] for extension in ["omx", "aem"]: matrix = AequilibraeMatrix() if extension == 'omx': mat_name = os.path.join(gettempdir(), "my_matrix." + extension) else: mat_name = self.mat_name matrix.load(mat_name) matrix.computational_view(["cars"]) # Performs assignment res = AssignmentResults() res.prepare(self.g, matrix) assig = allOrNothing(matrix, self.g, res) assig.execute() car_loads.append(res.link_loads) res.save_to_disk( os.path.join(gettempdir(), "link_loads_{}.aed".format(extension))) res.save_to_disk( os.path.join(gettempdir(), "link_loads_{}.csv".format(extension))) matrix.computational_view() # Performs assignment res = AssignmentResults() res.prepare(self.g, matrix) assig = allOrNothing(matrix, self.g, res) assig.execute() two_class_loads.append(res.link_loads) res.save_to_disk( os.path.join(gettempdir(), "link_loads_2_classes_{}.aed".format(extension))) res.save_to_disk( os.path.join(gettempdir(), "link_loads_2_classes_{}.csv".format(extension))) matrix.close() load_diff = two_class_loads[0] - two_class_loads[1] if load_diff.max() > 0.0000000001 or load_diff.max() < -0.0000000001: self.fail( "Loads for two classes differ for OMX and AEM matrix types") load_diff = car_loads[0] - car_loads[1] if load_diff.max() > 0.0000000001 or load_diff.max() < -0.0000000001: self.fail( "Loads for a single class differ for OMX and AEM matrix types")
def test___init__(self): os.remove(name_test) if os.path.exists(name_test) else None args = {'file_name': name_test, 'zones': zones, 'matrix_names': ['mat', 'seed', 'dist'], 'index_names': ['my indices']} matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = np.arange(matrix.zones) + 100 matrix.mat[:, :] = np.random.rand(matrix.zones, matrix.zones)[:, :] matrix.mat[:, :] = matrix.mat[:, :] * (1000 / np.sum(matrix.mat[:, :])) matrix.setName('Test matrix - ' + str(random.randint(1, 10))) matrix.setDescription('Generated at ' + datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")) matrix.close(True) del (matrix)
def test_copy_from_omx(self): temp_file = AequilibraeMatrix().random_name() a = AequilibraeMatrix() a.create_from_omx(temp_file, omx_example) omxfile = omx.open_file(omx_example, "r") # Check if matrices values are compatible for m in ["m1", "m2", "m3"]: sm = a.matrix[m].sum() sm2 = np.array(omxfile[m]).sum() if sm != sm2: self.fail( "Matrix {} was copied with the wrong value".format(m)) if np.any(a.index[:] != np.array(list(omxfile.mapping("taz").keys()))): self.fail("Index was not created properly") a.close()
def test___init__(self): os.remove(name_test) if os.path.exists(name_test) else None args = { "file_name": name_test, "zones": zones, "matrix_names": ["mat", "seed", "dist"], "index_names": ["my indices"], } matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = np.arange(matrix.zones) + 100 matrix.mat[:, :] = np.random.rand(matrix.zones, matrix.zones)[:, :] matrix.mat[:, :] = matrix.mat[:, :] * (1000 / np.sum(matrix.mat[:, :])) matrix.setName("Test matrix - " + str(random.randint(1, 10))) matrix.setDescription( "Generated at " + datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")) matrix.close() del matrix
def setUp(self) -> None: self.mat_name = AequilibraeMatrix().random_name() self.g = Graph() self.g.load_from_disk(test_graph) self.g.set_graph(cost_field="distance") # Creates the matrix for assignment args = { "file_name": os.path.join(gettempdir(), self.mat_name), "zones": self.g.num_zones, "matrix_names": ["cars", "trucks"], "index_names": ["my indices"], } matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = self.g.centroids[:] matrix.cars.fill(1.1) matrix.trucks.fill(2.2) # Exports matrix to OMX in order to have two matrices to work with matrix.export(os.path.join(gettempdir(), "my_matrix.omx")) matrix.close()
class TestAequilibraeMatrix(TestCase): matrix = None def setUp(self) -> None: self.sf_skims = f"/Aequilibrae_matrix_{uuid.uuid4()}.omx" copyfile(siouxfalls_skims, self.sf_skims) temp_folder = gettempdir() self.name_test = temp_folder + f"/Aequilibrae_matrix_{uuid.uuid4()}.aem" self.copy_matrix_name = temp_folder + f"/Aequilibrae_matrix_{uuid.uuid4()}.aem" self.csv_export_name = temp_folder + f"/Aequilibrae_matrix_{uuid.uuid4()}.csv" self.omx_export_name = temp_folder + f"/Aequilibrae_matrix_{uuid.uuid4()}.omx" if self.matrix is not None: return args = { "file_name": self.name_test, "zones": zones, "matrix_names": ["mat", "seed", "dist"], "index_names": ["my indices"], } self.matrix = AequilibraeMatrix() self.matrix.create_empty(**args) self.matrix.index[:] = np.arange(self.matrix.zones) + 100 self.matrix.mat[:, :] = np.random.rand(self.matrix.zones, self.matrix.zones)[:, :] self.matrix.mat[:, :] = self.matrix.mat[:, :] * (1000 / np.sum(self.matrix.mat[:, :])) self.matrix.setName("Test matrix - " + str(random.randint(1, 10))) self.matrix.setDescription("Generated at " + datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")) self.new_matrix = self.matrix def tearDown(self) -> None: try: del self.matrix os.remove(self.name_test) if os.path.exists(self.name_test) else None os.remove(self.csv_export_name) if os.path.exists(self.csv_export_name) else None os.remove(self.copy_matrix_name) if os.path.exists(self.copy_matrix_name) else None os.remove(self.omx_export_name) if os.path.exists(self.omx_export_name) else None except Exception as e: print(f"Could not delete. {e.args}") def test_load(self): self.new_matrix = AequilibraeMatrix() # Cannot load OMX file with no indices with self.assertRaises(LookupError): self.new_matrix.load(no_index_omx) self.new_matrix = AequilibraeMatrix() self.new_matrix.load(self.name_test) del self.new_matrix def test_computational_view(self): self.new_matrix.computational_view(["mat", "seed"]) self.new_matrix.mat.fill(0) self.new_matrix.seed.fill(0) if self.new_matrix.matrix_view.shape[2] != 2: self.fail("Computational view returns the wrong number of matrices") self.new_matrix.computational_view(["mat"]) self.new_matrix.matrix_view[:, :] = np.arange(zones ** 2).reshape(zones, zones) if np.sum(self.new_matrix.mat) != np.sum(self.new_matrix.matrix_view): self.fail("Assigning to matrix view did not work") self.new_matrix.setName("Test matrix - " + str(random.randint(1, 10))) self.new_matrix.setDescription("Generated at " + datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")) del self.new_matrix def test_computational_view_with_omx(self): self.new_matrix = AequilibraeMatrix() self.new_matrix.load(omx_example) arrays = ["m1", "m2"] self.new_matrix.computational_view(arrays) total_mats = np.sum(self.new_matrix.matrix_view) self.new_matrix.computational_view([arrays[0]]) total_m1 = np.sum(self.new_matrix.matrix_view) self.new_matrix.close() omx_file = omx.open_file(omx_example, "r") m1 = np.array(omx_file["m1"]).sum() m2 = np.array(omx_file["m2"]).sum() self.assertEqual(m1 + m2, total_mats) self.assertEqual(m1, total_m1) omx_file.close() del omx_file def test_copy(self): # test in-memory matrix_procedures copy matrix_copy = self.new_matrix.copy(self.copy_matrix_name, cores=["mat"]) if not np.array_equal(matrix_copy.mat, self.new_matrix.mat): self.fail("Matrix copy was not perfect") matrix_copy.close() del matrix_copy def test_export_to_csv(self): self.new_matrix.export(self.csv_export_name) df = pd.read_csv(self.csv_export_name) df.fillna(0, inplace=True) self.assertEqual(df.shape[0], 2500, "Exported wrong size") self.assertEqual(df.shape[1], 5, "Exported wrong size") self.assertAlmostEqual(df.mat.sum(), np.nansum(self.new_matrix.matrices), 5, "Exported wrong matrix total") def test_export_to_omx(self): self.new_matrix.export(self.omx_export_name) omxfile = omx.open_file(self.omx_export_name, "r") # Check if matrices values are compatible for m in self.new_matrix.names: sm = np.nansum(self.new_matrix.matrix[m]) sm2 = np.nansum(np.array(omxfile[m])) self.assertEqual(sm, sm2, "Matrix {} was exported with the wrong value".format(m)) del omxfile def test_nan_to_num(self): m = self.new_matrix.mat.sum() - self.new_matrix.mat[1, 1] self.new_matrix.computational_view(["mat", "seed"]) self.new_matrix.nan_to_num() self.new_matrix.mat[1, 1] = np.nan self.new_matrix.computational_view(["mat"]) self.new_matrix.nan_to_num() if abs(m - self.new_matrix.mat.sum()) > 0.000000000001: self.fail("Total for mat matrix not maintained") del self.new_matrix def test_copy_from_omx(self): temp_file = AequilibraeMatrix().random_name() a = AequilibraeMatrix() a.create_from_omx(temp_file, omx_example) omxfile = omx.open_file(omx_example, "r") # Check if matrices values are compatible for m in ["m1", "m2", "m3"]: sm = a.matrix[m].sum() sm2 = np.array(omxfile[m]).sum() if sm != sm2: self.fail("Matrix {} was copied with the wrong value".format(m)) if np.any(a.index[:] != np.array(list(omxfile.mapping("taz").keys()))): self.fail("Index was not created properly") a.close() del a del omxfile def test_copy_from_omx_long_name(self): temp_file = AequilibraeMatrix().random_name() a = AequilibraeMatrix() with self.assertRaises(ValueError): a.create_from_omx(temp_file, omx_example, robust=False) del a def test_copy_omx_wrong_content(self): # Check if we get a result if we try to copy non-existing cores temp_file = AequilibraeMatrix().random_name() a = AequilibraeMatrix() with self.assertRaises(ValueError): a.create_from_omx(temp_file, omx_example, cores=["m1", "m2", "m3", "m4"]) with self.assertRaises(ValueError): a.create_from_omx(temp_file, omx_example, mappings=["wrong index"]) del a def test_get_matrix(self): a = AequilibraeMatrix() a.load(self.sf_skims) with self.assertRaises(AttributeError): a.get_matrix("does not exist") q = a.get_matrix("distance") self.assertEqual(q.shape[0], 24) a = AequilibraeMatrix() a.load(self.name_test) print(np.array_equal(a.get_matrix("seed"), a.matrix["seed"])) del a def test_save(self): a = AequilibraeMatrix() a.load(self.sf_skims) a.computational_view(["distance"]) new_mat = np.random.rand(a.zones, a.zones) a.matrix_view *= new_mat res = a.matrix_view.sum() a.save("new_name_for_matrix") self.assertEqual(res, a.matrix_view.sum(), "Saved wrong result") a.save(["new_name_for_matrix2"]) self.assertEqual(a.view_names[0], "new_name_for_matrix2", "Did not update computational view") self.assertEqual(len(a.view_names), 1, "computational view with the wrong number of matrices") a.computational_view(["distance", "new_name_for_matrix"]) with self.assertRaises(ValueError): a.save(["just_one_name"]) a.save(["one_name", "two_names"]) with self.assertRaises(ValueError): a.save("distance") b = AequilibraeMatrix() b.load(self.name_test) b.computational_view("seed") b.save() b.computational_view(["mat", "seed", "dist"]) b.save()
def produce_all_outputs(self): fn = os.path.join(self.output_path, "skims.aem") fn_omx = os.path.join(self.output_path, "skims.omx") if self.cb_choose_algorithm.currentText() == 'all-or-nothing': cls = [x for x in self.traffic_classes.values() if x is not None][0] cls.results.save_to_disk(os.path.join( self.output_path, f"link_flows_{cls.graph.mode}.csv"), output="loads") cls.results.save_to_disk(os.path.join( self.output_path, f"link_flows_{cls.graph.mode}.aed"), output="loads") if has_omx: cls.results.skims.export(fn_omx) else: cls.results.skims.export(fn) return table = self.skim_list_table skim_names = [] for i in range(table.rowCount()): mode = self.all_modes[table.item(i, 0).text()] field = table.item(i, 1).text() last_iter = table.cellWidget(i, 2).isChecked() blended = table.cellWidget(i, 3).isChecked() if last_iter: skim_names.append(f'{field}_{mode}_final') if blended: skim_names.append(f'{field}_{mode}_blended') for cls in self.assignment.classes: cls.results.save_to_disk(os.path.join( self.output_path, f"link_flows_{cls.graph.mode}.csv"), output="loads") cls.results.save_to_disk(os.path.join( self.output_path, f"link_flows_{cls.graph.mode}.aed"), output="loads") # cls.results.skims.export(os.path.join(self.output_path, f'blended_skims_{cls.graph.mode}.aed')) if skim_names: args = { 'file_name': fn, 'zones': self.project.network.count_centroids(), 'matrix_names': skim_names } skims = AequilibraeMatrix() skims.create_empty(**args) for i in range(table.rowCount()): mode_name = table.item(i, 0).text() mode = self.all_modes[mode_name] field = table.item(i, 1).text() last_iter = table.cellWidget(i, 2).isChecked() blended = table.cellWidget(i, 3).isChecked() cls = self.traffic_classes[mode_name] if last_iter: mat_name = f'{field}_{mode}_final' skims.matrix[ mat_name][:, :] = cls._aon_results.skims.matrix[ field][:, :] if blended: mat_name = f'{field}_{mode}_blended' skims.matrix[mat_name][:, :] = cls.results.skims.matrix[ field][:, :] skims.index[:] = cls.matrix.index[:] if has_omx: skims.export(fn_omx) skims.close() del skims os.unlink(fn)
class TestAequilibraeMatrix(TestCase): def test___init__(self): os.remove(name_test) if os.path.exists(name_test) else None args = {'file_name': name_test, 'zones': zones, 'matrix_names': ['mat', 'seed', 'dist'], 'index_names': ['my indices']} matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = np.arange(matrix.zones) + 100 matrix.mat[:, :] = np.random.rand(matrix.zones, matrix.zones)[:, :] matrix.mat[:, :] = matrix.mat[:, :] * (1000 / np.sum(matrix.mat[:, :])) matrix.setName('Test matrix - ' + str(random.randint(1, 10))) matrix.setDescription('Generated at ' + datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")) matrix.close(True) del (matrix) def test_load(self): # self.test___init__() self.new_matrix = AequilibraeMatrix() self.new_matrix.load(name_test) def test_computational_view(self): self.test_load() self.new_matrix.computational_view(['mat', 'seed']) self.new_matrix.mat.fill(0) self.new_matrix.seed.fill(0) if self.new_matrix.matrix_view.shape[2] != 2: self.fail('Computational view returns the wrong number of matrices') self.new_matrix.computational_view(['mat']) self.new_matrix.matrix_view[:, :] = np.arange(zones ** 2).reshape(zones, zones) if np.sum(self.new_matrix.mat) != np.sum(self.new_matrix.matrix_view): self.fail('Assigning to matrix view did not work') self.new_matrix.setName('Test matrix - ' + str(random.randint(1, 10))) self.new_matrix.setDescription('Generated at ' + datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")) self.new_matrix.close(True) def test_copy(self): self.test_load() # test in-memory matrix_procedures copy matrix_copy = self.new_matrix.copy(copy_matrix_name, cores=['mat']) if not np.array_equal(matrix_copy.mat, self.new_matrix.mat): self.fail('Matrix copy was not perfect') matrix_copy.close(True) self.new_matrix.close(True) def test_export(self): self.test_load() self.new_matrix.export(csv_export_name) self.new_matrix.close(True) def test_nan_to_num(self): self.test_load() s = self.new_matrix.seed.sum() - self.new_matrix.seed[1, 1] m = self.new_matrix.mat.sum() - self.new_matrix.mat[1, 1] self.new_matrix.seed[1,1] = np.nan self.new_matrix.computational_view(['mat', 'seed']) self.new_matrix.nan_to_num() self.new_matrix.mat[1,1] = np.nan self.new_matrix.computational_view(['mat']) self.new_matrix.nan_to_num() if s != self.new_matrix.seed.sum(): self.fail('Total for seed matrix not maintained') if m != self.new_matrix.mat.sum(): self.fail('Total for mat matrix not maintained')
class TestAequilibraeMatrix(TestCase): def test___init__(self): os.remove(name_test) if os.path.exists(name_test) else None args = { "file_name": name_test, "zones": zones, "matrix_names": ["mat", "seed", "dist"], "index_names": ["my indices"], } matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = np.arange(matrix.zones) + 100 matrix.mat[:, :] = np.random.rand(matrix.zones, matrix.zones)[:, :] matrix.mat[:, :] = matrix.mat[:, :] * (1000 / np.sum(matrix.mat[:, :])) matrix.setName("Test matrix - " + str(random.randint(1, 10))) matrix.setDescription( "Generated at " + datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")) matrix.close() del matrix def test_load(self): # self.test___init__() self.new_matrix = AequilibraeMatrix() # Cannot load OMX file with no indices with self.assertRaises(LookupError): self.new_matrix.load(no_index_omx) self.new_matrix = AequilibraeMatrix() self.new_matrix.load(name_test) def test_computational_view(self): self.test_load() self.new_matrix.computational_view(["mat", "seed"]) self.new_matrix.mat.fill(0) self.new_matrix.seed.fill(0) if self.new_matrix.matrix_view.shape[2] != 2: self.fail( "Computational view returns the wrong number of matrices") self.new_matrix.computational_view(["mat"]) self.new_matrix.matrix_view[:, :] = np.arange(zones**2).reshape( zones, zones) if np.sum(self.new_matrix.mat) != np.sum(self.new_matrix.matrix_view): self.fail("Assigning to matrix view did not work") self.new_matrix.setName("Test matrix - " + str(random.randint(1, 10))) self.new_matrix.setDescription( "Generated at " + datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")) self.new_matrix.close() def test_computational_view_with_omx(self): self.new_matrix = AequilibraeMatrix() self.new_matrix.load(omx_example) arrays = ["m1", "m2"] self.new_matrix.computational_view(arrays) total_mats = np.sum(self.new_matrix.matrix_view) self.new_matrix.computational_view([arrays[0]]) total_m1 = np.sum(self.new_matrix.matrix_view) self.new_matrix.close() omx_file = omx.open_file(omx_example, "r") m1 = np.array(omx_file["m1"]).sum() m2 = np.array(omx_file["m2"]).sum() self.assertEqual(m1 + m2, total_mats) self.assertEqual(m1, total_m1) omx_file.close() def test_copy(self): self.test_load() # test in-memory matrix_procedures copy matrix_copy = self.new_matrix.copy(copy_matrix_name, cores=["mat"]) if not np.array_equal(matrix_copy.mat, self.new_matrix.mat): self.fail("Matrix copy was not perfect") matrix_copy.close() self.new_matrix.close() def test_export_to_csv(self): self.test_load() self.new_matrix.export(csv_export_name) self.new_matrix.close() def test_export_to_omx(self): self.test_load() self.new_matrix.export(omx_export_name) omxfile = omx.open_file(omx_export_name, "r") # Check if matrices values are compatible for m in self.new_matrix.names: sm = np.nansum(self.new_matrix.matrix[m]) sm2 = np.nansum(np.array(omxfile[m])) self.assertEqual( sm, sm2, "Matrix {} was exported with the wrong value".format(m)) self.new_matrix.close() def test_nan_to_num(self): self.test_load() s = self.new_matrix.seed.sum() - self.new_matrix.seed[1, 1] m = self.new_matrix.mat.sum() - self.new_matrix.mat[1, 1] self.new_matrix.seed[1, 1] = np.nan self.new_matrix.computational_view(["mat", "seed"]) self.new_matrix.nan_to_num() self.new_matrix.mat[1, 1] = np.nan self.new_matrix.computational_view(["mat"]) self.new_matrix.nan_to_num() if s != self.new_matrix.seed.sum(): self.fail("Total for seed matrix not maintained") if m != self.new_matrix.mat.sum(): self.fail("Total for mat matrix not maintained") def test_copy_from_omx(self): temp_file = AequilibraeMatrix().random_name() a = AequilibraeMatrix() a.create_from_omx(temp_file, omx_example) omxfile = omx.open_file(omx_example, "r") # Check if matrices values are compatible for m in ["m1", "m2", "m3"]: sm = a.matrix[m].sum() sm2 = np.array(omxfile[m]).sum() if sm != sm2: self.fail( "Matrix {} was copied with the wrong value".format(m)) if np.any(a.index[:] != np.array(list(omxfile.mapping("taz").keys()))): self.fail("Index was not created properly") a.close() def test_copy_from_omx_long_name(self): temp_file = AequilibraeMatrix().random_name() a = AequilibraeMatrix() with self.assertRaises(ValueError): a.create_from_omx(temp_file, omx_example, robust=False) def test_copy_omx_wrong_content(self): # Check if we get a result if we try to copy non-existing cores temp_file = AequilibraeMatrix().random_name() a = AequilibraeMatrix() with self.assertRaises(ValueError): a.create_from_omx(temp_file, omx_example, cores=["m1", "m2", "m3", "m4"]) with self.assertRaises(ValueError): a.create_from_omx(temp_file, omx_example, mappings=["wrong index"])
class TestTrafficAssignment(TestCase): def setUp(self) -> None: self.matrix = AequilibraeMatrix() self.matrix.load(siouxfalls_demand) self.matrix.computational_view() self.project = Project() self.project.load(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) def tearDown(self) -> None: self.matrix.close() self.project.conn.close() def test_set_vdf(self): with self.assertRaises(ValueError): self.assignment.set_vdf('CQS') self.assignment.set_vdf('BPR') def test_set_classes(self): with self.assertRaises(ValueError): self.assignment.set_classes([1, 2]) # The traffic assignment class is unprotected. # Should we protect it? # self.assigclass = TrafficClass(self.car_graph, self.matrix) # self.assigclass.graph = 1 # with self.assertRaises(ValueError): # self.assignment.set_classes(self.assigclass) self.assignment.set_classes(self.assigclass) # self.fail() def test_algorithms_available(self): algs = self.assignment.algorithms_available() real = ['all-or-nothing', 'msa', 'frank-wolfe', 'bfw', 'cfw'] diff = [x for x in real if x not in algs] diff2 = [x for x in algs if x not in real] if len(diff) + len(diff2) > 0: self.fail('list of algorithms raised is wrong') def test_set_cores(self): with self.assertRaises(Exception): self.assignment.set_cores(3) self.assignment.set_classes(self.assigclass) with self.assertRaises(ValueError): self.assignment.set_cores('q') self.assignment.set_cores(3) def test_set_algorithm(self): with self.assertRaises(AttributeError): self.assignment.set_algorithm('not an algo') self.assignment.set_classes(self.assigclass) with self.assertRaises(Exception): self.assignment.set_algorithm('msa') self.assignment.set_vdf("BPR") self.assignment.set_vdf_parameters({"alpha": "b", "beta": "power"}) self.assignment.set_capacity_field("capacity") self.assignment.set_time_field("free_flow_time") self.assignment.max_iter = 10 self.assignment.set_algorithm('bfw') def test_set_vdf_parameters(self): with self.assertRaises(Exception): self.assignment.set_vdf_parameters({"alpha": "b", "beta": "power"}) self.assignment.set_vdf('bpr') self.assignment.set_classes(self.assigclass) self.assignment.set_vdf_parameters({"alpha": "b", "beta": "power"}) def test_set_time_field(self): N = random.randint(1, 50) val = ''.join( random.choices(string.ascii_uppercase + string.digits, k=N)) self.assignment.set_time_field(val) self.assertEqual(self.assignment.time_field, val) def test_set_capacity_field(self): N = random.randint(1, 50) val = ''.join( random.choices(string.ascii_uppercase + string.digits, k=N)) self.assignment.set_capacity_field(val) self.assertEqual(self.assignment.capacity_field, val) def test_execute(self): self.assignment.set_classes(self.assigclass) self.assignment.set_vdf("BPR") self.assignment.set_vdf_parameters({"alpha": 0.15, "beta": 4.0}) self.assignment.set_vdf_parameters({"alpha": "b", "beta": "power"}) self.assignment.set_capacity_field("capacity") self.assignment.set_time_field("free_flow_time") self.assignment.max_iter = 10 self.assignment.set_algorithm('msa') self.assignment.execute() msa10 = self.assignment.assignment.rgap self.assigclass.results.total_flows() correl = np.corrcoef(self.assigclass.results.total_link_loads, self.assigclass.graph.graph['volume'])[0, 1] self.assertLess(0.8, correl) self.assignment.max_iter = 30 self.assignment.set_algorithm('msa') self.assignment.execute() msa25 = self.assignment.assignment.rgap self.assigclass.results.total_flows() correl = np.corrcoef(self.assigclass.results.total_link_loads, self.assigclass.graph.graph['volume'])[0, 1] self.assertLess(0.95, correl) self.assignment.set_algorithm('frank-wolfe') self.assignment.execute() fw25 = self.assignment.assignment.rgap self.assigclass.results.total_flows() correl = np.corrcoef(self.assigclass.results.total_link_loads, self.assigclass.graph.graph['volume'])[0, 1] self.assertLess(0.97, correl) self.assignment.set_algorithm('cfw') self.assignment.execute() cfw25 = self.assignment.assignment.rgap self.assigclass.results.total_flows() correl = np.corrcoef(self.assigclass.results.total_link_loads, self.assigclass.graph.graph['volume'])[0, 1] self.assertLess(0.98, correl) self.assignment.set_algorithm('bfw') self.assignment.execute() bfw25 = self.assignment.assignment.rgap self.assigclass.results.total_flows() correl = np.corrcoef(self.assigclass.results.total_link_loads, self.assigclass.graph.graph['volume'])[0, 1] self.assertLess(0.99, correl) self.assertLess(msa25, msa10) self.assertLess(fw25, msa25) self.assertLess(cfw25, fw25) self.assertLess(bfw25, cfw25)