def test_parameter_controls_mutate_hoist(self): # save and restore original configuration with saved(Config.get_instance()): Config.get_instance().set("POPULATION", "controls", "5") individual = Individual( '(root (/ (exp (/ 8.2118 S0)) (* (* S0 (* 1.6755 -0.0699)) (log (exp -3.2288)))) (* (+ (sin -9.8591) (exp S0)) -9.4159) 0.0290 (* (log (* (+ -5.0573 -6.2191) S0)) (/ (cos (log S0)) (cos (tanh 2.2886)))) (log -8.6795))' ) new_ind = individual.mutate(Individual.MutationType.HOIST) self.assertEqual( new_ind.get_value(), "(root (log (exp -3.2288)) (* (+ (sin -9.8591) (exp S0)) -9.4159) 0.0290 (* (log (* (+ -5.0573 -6.2191) S0)) (/ (cos (log S0)) (cos (tanh 2.2886)))) (log -8.6795))" ) new_ind = individual.mutate(Individual.MutationType.HOIST) self.assertEqual( new_ind.get_value(), "(root (/ (exp (/ 8.2118 S0)) (* (* S0 (* 1.6755 -0.0699)) (log (exp -3.2288)))) (exp S0) 0.0290 (* (log (* (+ -5.0573 -6.2191) S0)) (/ (cos (log S0)) (cos (tanh 2.2886)))) (log -8.6795))" ) new_ind = individual.mutate(Individual.MutationType.HOIST) self.assertEqual( new_ind.get_value(), "(root (* S0 (* 1.6755 -0.0699)) (* (+ (sin -9.8591) (exp S0)) -9.4159) 0.0290 (* (log (* (+ -5.0573 -6.2191) S0)) (/ (cos (log S0)) (cos (tanh 2.2886)))) (log -8.6795))" )
def test_set_custom_gen_creator(self): with saved(Config.get_instance()) as config: Config.get_instance().set("POPULATION", "size", "5") Config.get_instance().set("BEHAVIOUR", "save", "false") from MLC.Population.Creation.BaseCreation import BaseCreation from MLC.individual.Individual import Individual class TestCreator(BaseCreation): def __init__(self): BaseCreation.__init__(self) def create(self, gen_size): MLCRepository.get_instance().add_individual( Individual("(root 1)")) MLCRepository.get_instance().add_individual( Individual("(root 2)")) MLCRepository.get_instance().add_individual( Individual("(root 3)")) MLCRepository.get_instance().add_individual( Individual("(root 4)")) MLCRepository.get_instance().add_individual( Individual("(root 5)")) def individuals(self): return [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)] simulation = Simulation("") mlc = Application(simulation, gen_creator=TestCreator()) mlc.go(to_generation=1) # Assert first Population was filled using the TestCreator population = MLCRepository.get_instance().get_population(1) self.assertEqual(population.get_individuals(), [1, 2, 3, 4, 5])
def test_list_of_callbacks(self): with saved(Config.get_instance()) as config: Config.get_instance().set("POPULATION", "size", "10") Config.get_instance().set("BEHAVIOUR", "save", "false") ApplicationTest.on_start = 0 ApplicationTest.on_start_counter_2 = 0 def test_on_start_callback(): ApplicationTest.on_start += 1 def test_on_start_callback_increment_2(): ApplicationTest.on_start_counter_2 += 1 start_callbacks = [ test_on_start_callback, test_on_start_callback_increment_2 ] ApplicationTest.mlc_local.new_experiment( ApplicationTest.experiment_name, ApplicationTest.test_conf_path) ApplicationTest.mlc_local.open_experiment( ApplicationTest.experiment_name) ApplicationTest.mlc_local.go( experiment_name=ApplicationTest.experiment_name, to_generation=2, from_generation=0, callbacks={MLC_CALLBACKS.ON_START: start_callbacks}) ApplicationTest.mlc_local.close_experiment( ApplicationTest.experiment_name) ApplicationTest.mlc_local.delete_experiment( ApplicationTest.experiment_name) self.assertEqual(ApplicationTest.on_start, 1) self.assertEqual(ApplicationTest.on_start_counter_2, 1)
def test_reload_individuals_from_file(self): with saved(Config.get_instance()) as config: config.set("BEHAVIOUR", "save", "true") config.set("BEHAVIOUR", "savedir", "test.db") config.set("POPULATION", "sensor_spec", "false") config.set("POPULATION", "sensors", "0") config.set("OPTIMIZATION", "simplify", "false") mlc_repo = self.__get_new_repo() # add individuals mlc_repo.add_individual(Individual("(root (+ 1 1))")) mlc_repo.add_individual(Individual("(root (+ 2 2))")) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [2, 1, 2] mlc_repo.add_population(p) # check status self.assertEqual(mlc_repo.count_individual(), 2) self.assertEqual(mlc_repo.count_population(), 1) # reload mlc_repository using another instance mlc_repo = self.__get_new_repo() self.assertEqual(mlc_repo.count_individual(), 2) self.assertEqual(mlc_repo.count_population(), 1)
def __fill_and_assert(self, fill_creator, expected_pop_indexes, expected_individuals): with saved(Config.get_instance()) as config: Config.get_instance().set("POPULATION", "size", "5") Config.get_instance().set("BEHAVIOUR", "save", "false") from MLC.Log.log import set_logger set_logger('testing') population = Population(5, 0, Config.get_instance(), MLCRepository.make("")) population.fill(fill_creator) MLCRepository.get_instance().add_population(population) # Assert that one Population was added self.assertEqual(MLCRepository.get_instance().count_population(), 1) # Assert that the individuals are in the expected position inside the Population for position, i in enumerate(expected_pop_indexes): expected_individual = expected_individuals[i] inserted_individual_id = population.get_individuals()[position] inserted_individual = MLCRepository.get_instance( ).get_individual(inserted_individual_id) self.assertEqual(expected_individual.get_value(), inserted_individual.get_value())
def test_sensor_list(self): # save and restore original configuration with saved(Config.get_instance()): Config.get_instance().set("POPULATION", "sensor_list", "6,15,2,8,4,10") Config.get_instance().set("POPULATION", "sensors", "6") Config.get_instance().set("POPULATION", "sensor_spec", "true") Config.get_instance().set("POPULATION", "sensor_prob", "1.0") # test generate and mutate using sensor list individual = Individual.generate(individual_type=3, config=Config.get_instance()) self.assertEqual( individual.get_value(), '(root (sin (/ (+ (exp S6) (cos S10)) (/ (log S10) (log S4)))))' ) individual = Individual.generate(individual_type=3, config=Config.get_instance()) self.assertEqual( individual.get_value(), '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))' ) new_ind = self._individual_l2.mutate( Individual.MutationType.REMOVE_SUBTREE_AND_REPLACE) self.assertEqual( individual.get_value(), '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))' ) new_ind = self._individual_l2.mutate( Individual.MutationType.REMOVE_SUBTREE_AND_REPLACE) self.assertEqual( individual.get_value(), '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))' ) new_ind = self._individual_l2.mutate( Individual.MutationType.SHRINK) self.assertEqual( individual.get_value(), '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))' ) new_ind = self._individual_l2.mutate( Individual.MutationType.SHRINK) self.assertEqual( individual.get_value(), '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))' )
def test_callback_on_evaluate(self): with saved(Config.get_instance()) as config: Config.get_instance().set("POPULATION", "size", "10") Config.get_instance().set("BEHAVIOUR", "save", "false") def test_on_start_callback(): ApplicationTest.on_start += 1 def test_on_evaluate_callback(individual_id, cost): ApplicationTest.on_evaluate += 1 def test_on_new_generation_callback(generation_number): ApplicationTest.on_new_generation.append(generation_number) def test_on_finish_callback(): ApplicationTest.on_finish += 1 callbacks_dict = { MLC_CALLBACKS.ON_START: test_on_start_callback, MLC_CALLBACKS.ON_EVALUATE: test_on_evaluate_callback, MLC_CALLBACKS.ON_NEW_GENERATION: test_on_new_generation_callback, MLC_CALLBACKS.ON_FINISH: test_on_finish_callback } ApplicationTest.mlc_local.new_experiment( ApplicationTest.experiment_name, ApplicationTest.test_conf_path) ApplicationTest.mlc_local.open_experiment( ApplicationTest.experiment_name) ApplicationTest.mlc_local.go( experiment_name=ApplicationTest.experiment_name, to_generation=2, from_generation=0, callbacks=callbacks_dict) ApplicationTest.mlc_local.close_experiment( ApplicationTest.experiment_name) ApplicationTest.mlc_local.delete_experiment( ApplicationTest.experiment_name) self.assertEqual(ApplicationTest.on_start, 1) self.assertEqual(ApplicationTest.on_evaluate, 2 * 10) self.assertEqual(ApplicationTest.on_new_generation, range(1, 2 + 1)) self.assertEqual(ApplicationTest.on_finish, 1)
def test_parameter_controls_generate(self): # save and restore original configuration with saved(Config.get_instance()): # random generation Config.get_instance().set("POPULATION", "controls", "3") individual = Individual.generate(individual_type=2, config=Config.get_instance()) self.assertEqual( individual.get_value(), '(root (/ -3.0632 (cos -3.0973)) (exp (log (* (* S0 -0.8182) (sin -6.5057)))) (/ (/ -1.4169 (/ (* S0 S0) (cos -7.5988))) (log (cos (* S0 5.7489)))))' ) formal_exp = individual.get_formal() self.assertIsInstance(formal_exp, list) self.assertEqual(len(formal_exp), 3) self.assertEqual(formal_exp[0], '(my_div((-3.0632),cos((-3.0973))))') self.assertEqual( formal_exp[1], 'exp(my_log(((S0 .* (-0.8182)) .* sin((-6.5057)))))') self.assertEqual( formal_exp[2], '(my_div((my_div((-1.4169),(my_div((S0 .* S0),cos((-7.5988)))))),my_log(cos((S0 .* 5.7489)))))' ) self.assertEqual(individual.get_complexity(), 46) # generate individual with one control Config.get_instance().set("POPULATION", "controls", "1") individual = Individual("(root (cos (- 5.0161 (sin (log S0)))))", Config.get_instance()) self.assertIsInstance(individual.get_formal(), str) self.assertEqual(individual.get_formal(), 'cos((5.0161 - sin(my_log(S0))))') self.assertEqual(individual.get_complexity(), 14) # generate individual with 3 controls Config.get_instance().set("POPULATION", "controls", "3") individual = Individual( '(root (exp 2.1314) (* (+ (sin -9.8591) (exp S0)) -9.4159) (exp (/ (/ 8.0187 -8.5969) S0)))', Config.get_instance()) formal_exp = individual.get_formal() self.assertIsInstance(formal_exp, list) self.assertEqual(len(formal_exp), 3) self.assertEqual(formal_exp[0], 'exp(2.1314)') self.assertEqual(formal_exp[1], '((sin((-9.8591)) + exp(S0)) .* (-9.4159))') self.assertEqual(formal_exp[2], 'exp((my_div((my_div(8.0187,(-8.5969))),S0)))') self.assertEqual(individual.get_complexity(), 29) # generate individual with 5 controls Config.get_instance().set("POPULATION", "controls", "5") individual = Individual( '(root (/ (exp (/ 8.2118 S0)) (* (* S0 (* 1.6755 -0.0699)) (log (exp -3.2288)))) S0 0.0290 (* (log (* (+ -5.0573 -6.2191) S0)) (/ (cos (log S0)) (cos (tanh 2.2886)))) (log -8.6795))', Config.get_instance()) formal_exp = individual.get_formal() self.assertIsInstance(formal_exp, list) self.assertEqual(len(formal_exp), 5) self.assertEqual( formal_exp[0], '(my_div(exp((my_div(8.2118,S0))),((S0 .* (1.6755 .* (-0.0699))) .* my_log(exp((-3.2288))))))' ) self.assertEqual(formal_exp[1], 'S0') self.assertEqual(formal_exp[2], '0.0290') self.assertEqual( formal_exp[3], '(my_log((((-5.0573) + (-6.2191)) .* S0)) .* (my_div(cos(my_log(S0)),cos(tanh(2.2886)))))' ) self.assertEqual(formal_exp[4], 'my_log((-8.6795))') self.assertEqual(individual.get_complexity(), 64) # generate individual with multiple simplifications Config.get_instance().set("POPULATION", "controls", "3") Config.get_instance().set("OPTIMIZATION", "simplify", "true") individual = Individual( '(root (exp 2.1314) (* (+ (sin -9.8591) (exp S0)) -9.4159) (exp (/ (/ 8.0187 -8.5969) S0)))', Config.get_instance()) formal_exp = individual.get_formal() self.assertIsInstance(formal_exp, list) self.assertEqual(len(formal_exp), 3) self.assertEqual(formal_exp[0], 'exp(2.1314)') self.assertEqual(formal_exp[1], '((sin((-9.8591)) + exp(S0)) .* (-9.4159))') self.assertEqual(formal_exp[2], 'exp((my_div((my_div(8.0187,(-8.5969))),S0)))')