def test_simulation_add_algorithm_valid(self): dataset = Dataset('name', 30, 5, 5, 5, []) simulation = Simulation(dataset=dataset, n_fes=30, np=5, save_to_dir='/notNeeded') simulation.add_algorithm('GreyWolfOptimizer') simulation.add_algorithm('GeneticAlgorithm') added_algorithms = simulation.algorithms() self.assertEqual('GreyWolfOptimizer', added_algorithms[0]) self.assertEqual('GeneticAlgorithm', added_algorithms[1])
def test_simulation_add_algorithm_none(self): try: dataset = Dataset('name', 30, 5, 5, 5, []) simulation = Simulation(dataset=dataset, n_fes=30, np=5, save_to_dir='/notNeeded') simulation.add_algorithm(None) self.fail('None value should not be accepted or ignored') except InvalidAlgorithmName: pass
def test_simulation_should_not_start_with_no_save_options(self): try: dataset = Dataset('name', 30, 5, 5, 5, []) simulation = Simulation(dataset=dataset, n_fes=30, np=5, save_to_dir='../results') simulation.add_algorithm('GreyWolfOptimizer') simulation.run(sort_by_best=SortAttribute.fitness) self.fail("Simulation started with no algorithms") except InvalidSimulationInitialState: pass
def test_simulation_add_save_opt_valid_config_no_args(self): try: dataset = Dataset('name', 30, 5, 5, 5, []) simulation = Simulation(dataset=dataset, n_fes=30, np=5, save_to_dir='/notNeeded') simulation.add_save_option( OutputOptionConfig(class_name='ConsoleOutputOption', included_kwargs=[])) except InvalidSaveOptionName: self.fail('Valid save option kwargs was not accepted.')
def test_simulation_add_save_opt_none_name_string(self): try: dataset = Dataset('name', 30, 5, 5, 5, []) simulation = Simulation(dataset=dataset, n_fes=30, np=5, save_to_dir='/notNeeded') simulation.add_save_option( OutputOptionConfig(class_name=None, included_kwargs=['dir_path', 'dataset'])) self.fail('None value save option should raise error') except InvalidSaveOptionName: pass
def test_simulation_add_save_opt_invalid_kwargs(self): try: dataset = Dataset('name', 30, 5, 5, 5, []) simulation = Simulation(dataset=dataset, n_fes=30, np=5, save_to_dir='/notNeeded') simulation.add_save_option( OutputOptionConfig(class_name='GraphOutputOption', included_kwargs=[])) self.fail('Invalid save option kwargs should raise error') except InvalidSaveOptionName: pass
def test_simulation_should_not_start_with_no_algorithms(self): try: dataset = Dataset('name', 30, 5, 5, 5, []) simulation = Simulation(dataset=dataset, n_fes=30, np=5, save_to_dir='../results') simulation.add_save_option( OutputOptionConfig(class_name='ConsoleOutputOption', included_kwargs=[])) simulation.run(sort_by_best=SortAttribute.fitness) self.fail("Simulation started with no algorithms") except InvalidSimulationInitialState: pass
def make(self, title: str, pack_c: int, stat_n: int, cargo_dim: int) -> Dataset: """Generates new dataset. Throws ValueError if any. Args: title: Data set name, without file extension. pack_c: Total number of packages. stat_n: Total number of stations. cargo_dim: Cargo stowage space width and height. Returns: Generated dataset. """ self._validate(title, pack_c, stat_n, cargo_dim) package_collection = [] in_index_start = 1 dist_in = [0 for _ in range(stat_n)] dist_out = [0 for _ in range(stat_n)] for i in range(1, pack_c + 1, 1): if in_index_start == stat_n: in_index_start = 1 station_in = in_index_start out_next_station = random.randint(0, 100) > 40 station_out = station_in + 1 if out_next_station else random.randint( station_in + 1, stat_n) dist_in[station_in - 1] += 1 dist_out[station_out - 1] += 1 package_collection.append( Package(id_num=i, station_in=station_in, station_out=station_out, weight=random.randint(1, 99))) in_index_start += 1 # Performs final check, so indexes won't go out of bounds for columns in cargo space. self._final_check(package_collection, stat_n, cargo_dim * cargo_dim) return Dataset(title=title, total_packages=pack_c, total_stations=stat_n, width=cargo_dim, height=cargo_dim, packages=package_collection)
def test_simulation_should_not_start_with_invalid_dataset_invalid_package_count( self): try: dataset = Dataset( 'name', 30, 5, 5, 5, [Package(id_num=1, station_in=1, station_out=3, weight=30)]) simulation = Simulation(dataset=dataset, n_fes=30, np=5, save_to_dir='../results') simulation.add_algorithm('GreyWolfOptimizer') simulation.add_save_option( OutputOptionConfig(class_name='ConsoleOutputOption', included_kwargs=[])) simulation.run(sort_by_best=SortAttribute.fitness) self.fail("Simulation started with no algorithms") except InvalidSimulationInitialState: pass