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_should_not_start_with_invalid_np(self): try: dataset = Dataset('name', 30, 5, 5, 5, []) simulation = Simulation(dataset=dataset, n_fes=30, np=0, 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
def test_simulation_should_not_start_with_invalid_dir_path(self): try: dataset = Dataset('name', 5, 3, 3, 3, [ Package(id_num=1, station_in=1, station_out=2, weight=30), Package(id_num=2, station_in=2, station_out=3, weight=30), Package(id_num=3, station_in=1, station_out=3, weight=30), Package(id_num=4, station_in=2, station_out=3, weight=30), Package(id_num=5, station_in=1, station_out=2, weight=30) ]) simulation = Simulation(dataset=dataset, n_fes=30, np=5, save_to_dir='../invalidDir') 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
try: config_data = json.load(open(configFile, 'r')) dataset = CSVDatasetReader().read(config_data['dataset']) result_dir_path = os.path.join( config_data['saveToDir'], datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')) os.mkdir(path=result_dir_path) simulation = Simulation(dataset=dataset, n_fes=config_data['n_fes'], np=config_data['np'], save_to_dir=result_dir_path) for algorithm in config_data['algorithms']: simulation.add_algorithm(algorithm) save_opt_configs = [ OutputOptionConfig(class_name=config['class'], included_kwargs=config['included_kwargs']) for config in config_data['outputOptions'] ] for output_option in save_opt_configs: simulation.add_save_option(output_option) simulation.run( sort_by_best=SortAttribute[config_data['sortByBest'].lower()]) except Exception as e: print('Execution stopped with error: {0}'.format(str(e)))