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 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_get_individual_data(self): mlc_repo = self.__get_new_repo() # add individuals mlc_repo.add_individual(Individual("(root (+ 1 1))")) mlc_repo.add_individual(Individual("(root (+ 2 2))")) mlc_repo.add_individual(Individual("(root (+ 3 3))")) mlc_repo.add_individual(Individual("(root (+ 4 4))")) # first population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [1, 2, 1] p._costs = [4, 5, 6] p._ev_time = [5, 6, 7] mlc_repo.add_population(p) # second population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [3, 4, 2] p._costs = [7, 4, 9] p._ev_time = [8, 5, 10] mlc_repo.add_population(p) # check idividuals data loaded from the mlc_repo self.assertEqual(mlc_repo.count_population(), 2) # Individual 1 have two appearances in the first generation data = mlc_repo.get_individual_data(1) self.assertEqual(data.get_value(), "(root (+ 1 1))") self.assertEqual(data.get_appearances(), 2) self.assertEqual(data.get_cost_history(), {1: [(4.0, 5), (6.0, 7)]}) # Individual 2 have two appearances data = mlc_repo.get_individual_data(2) self.assertEqual(data.get_value(), "(root (+ 2 2))") self.assertEqual(data.get_appearances(), 2) self.assertEqual(data.get_cost_history(), { 1: [(5.0, 6)], 2: [(9.0, 10)] }) # Individual 3 have one appearances data = mlc_repo.get_individual_data(3) self.assertEqual(data.get_value(), "(root (+ 3 3))") self.assertEqual(data.get_appearances(), 1) self.assertEqual(data.get_cost_history(), {2: [(7.0, 8)]}) # Individual 4 have one appearances data = mlc_repo.get_individual_data(4) self.assertEqual(data.get_value(), "(root (+ 4 4))") self.assertEqual(data.get_appearances(), 1) self.assertEqual(data.get_cost_history(), {2: [(4.0, 5)]}) # get individual data from invalid individual try: data = mlc_repo.get_individual_data(100) self.assertTrue(False) except KeyError, ex: self.assertTrue(True)
def test_update_individual_cost_in_generation(self): 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 first population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [1, 2, 1] p._costs = [4, 5, 6] p._ev_time = [5, 6, 7] mlc_repo.add_population(p) # add second population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [2, 1, 2] p._costs = [8, 9, 10] p._ev_time = [11, 12, 13] mlc_repo.add_population(p) # update cost for individual 1 mlc_repo.update_individual_cost(1, 45, 46, generation=1) # check cost update in the first population p = mlc_repo.get_population(1) self.assertEqual(p._individuals, [1, 2, 1]) self.assertEqual(p._costs, [45, 5, 45]) self.assertEqual(p._ev_time, [46, 6, 46]) # check cost update in the second population p = mlc_repo.get_population(2) self.assertEqual(p._individuals, [2, 1, 2]) self.assertEqual(p._costs, [8, 9, 10]) self.assertEqual(p._ev_time, [11, 12, 13])
def test_remove_population_to_from_bad_values(self): mlc_repo = self.__get_new_repo() # add individuals mlc_repo.add_individual(Individual("(root (+ 1 1))")) mlc_repo.add_individual(Individual("(root (+ 2 2))")) mlc_repo.add_individual(Individual("(root (+ 3 3))")) mlc_repo.add_individual(Individual("(root (+ 4 4))")) mlc_repo.add_individual(Individual("(root (+ 5 5))")) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [1, 1, 1] mlc_repo.add_population(p) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [2, 2, 2] mlc_repo.add_population(p) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [3, 3, 3] mlc_repo.add_population(p) self.assertEqual(mlc_repo.count_population(), 3) # Remove all generations (1 to 3) mlc_repo.remove_population_to(10) self.assertEqual(mlc_repo.count_population(), 0)
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_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 reload_configuration(self): """ Load again the experiment configuration, reading the config file from disk """ self._configuration = ConfigParser.ConfigParser() self._configuration.read(self._config_file) Config.get_instance().read(self._config_file) return Config.to_dictionary(self._configuration)
def test_remove_population_to(self): mlc_repo = self.__get_new_repo() # add individuals mlc_repo.add_individual(Individual("(root (+ 1 1))")) mlc_repo.add_individual(Individual("(root (+ 2 2))")) mlc_repo.add_individual(Individual("(root (+ 3 3))")) mlc_repo.add_individual(Individual("(root (+ 4 4))")) mlc_repo.add_individual(Individual("(root (+ 5 5))")) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [1, 1, 1] mlc_repo.add_population(p) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [2, 2, 2] mlc_repo.add_population(p) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [3, 3, 3] mlc_repo.add_population(p) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [4, 4, 4] mlc_repo.add_population(p) self.assertEqual(mlc_repo.count_population(), 4) # remove generations 1 to 2 mlc_repo.remove_population_to(2) self.assertEqual(mlc_repo.count_population(), 2) p = mlc_repo.get_population(1) self.assertEqual(p._individuals, [3, 3, 3]) p = mlc_repo.get_population(2) self.assertEqual(p._individuals, [4, 4, 4]) # New generation must be number 3 p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [5, 5, 5] mlc_repo.add_population(p) self.assertEqual(mlc_repo.count_population(), 3) p = mlc_repo.get_population(1) self.assertEqual(p._individuals, [3, 3, 3]) p = mlc_repo.get_population(2) self.assertEqual(p._individuals, [4, 4, 4]) p = mlc_repo.get_population(3) self.assertEqual(p._individuals, [5, 5, 5])
def test_cut_generation(self): """ Cut a generation using remove_population_from/remove_population_last :return: """ mlc_repo = self.__get_new_repo() # add individuals mlc_repo.add_individual(Individual("(root (+ 1 1))")) mlc_repo.add_individual(Individual("(root (+ 2 2))")) mlc_repo.add_individual(Individual("(root (+ 3 3))")) mlc_repo.add_individual(Individual("(root (+ 4 4))")) mlc_repo.add_individual(Individual("(root (+ 5 5))")) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [1, 1, 1] mlc_repo.add_population(p) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [2, 2, 2] mlc_repo.add_population(p) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [3, 3, 3] mlc_repo.add_population(p) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [4, 4, 4] mlc_repo.add_population(p) # add population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [5, 5, 5] mlc_repo.add_population(p) self.assertEqual(mlc_repo.count_population(), 5) # Cut population 4 mlc_repo.remove_population_from(4 + 1) mlc_repo.remove_population_to(4 - 1) # remove unused individuals mlc_repo.remove_unused_individuals() self.assertEqual(mlc_repo.count_population(), 1) self.assertEqual(mlc_repo.count_individual(), 1) p = mlc_repo.get_population(1) self.assertEqual(p._individuals, [4, 4, 4]) individual = mlc_repo.get_individual(4) self.assertEqual(individual.get_value(), "(root (+ 4 4))")
def test_crossover_same_level_4(self): individual_1 = Individual("(root S0)", Config.get_instance()) individual_2 = Individual("(root S0)", Config.get_instance()) try: new_ind_1, new_ind_2, _ = individual_1.crossover(individual_2) self.assertFalse(True, "crossover with individual type 4 should fail") except OperationOverIndividualFail, ex: self.assertTrue(True)
def test_compare_random_individuals(self): individual_1 = Individual.generate(individual_type=3, config=Config.get_instance()) RandomManager.clear_random_values() RandomManager.load_random_values(self._random_file) individual_2 = Individual.generate(individual_type=3, config=Config.get_instance()) self.assertTrue(individual_1.compare(individual_2))
def create_empty_population_for(generation): from MLC.Population.Population import Population cascade = Config.get_instance().get_list('OPTIMIZATION', 'cascade') size = Config.get_instance().get_list('POPULATION', 'size') population_size = cascade[1] if (generation > 1 and len(size) > 1) else size[0] population_subgenerations = 1 if cascade[1] == 0 else cascade[1] return Population(population_size, population_subgenerations, Config.get_instance(), MLCRepository.get_instance())
def test_remove_from_population(self): mlc_repo = self.__get_new_repo() # add individuals mlc_repo.add_individual(Individual("(root (+ 1 1))")) mlc_repo.add_individual(Individual("(root (+ 2 2))")) mlc_repo.add_individual(Individual("(root (+ 3 3))")) mlc_repo.add_individual(Individual("(root (+ 4 4))")) # add first population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [1, 2, 1] mlc_repo.add_population(p) # add second population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [1, 2, 3] mlc_repo.add_population(p) # add third population p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [1, 2, 4] mlc_repo.add_population(p) # remove last population mlc_repo.remove_population_from(2) # last generation must be removed self.assertEqual(mlc_repo.count_population(), 1) # first generation exists p = mlc_repo.get_population(1) self.assertEqual(p._individuals, [1, 2, 1]) # all individuals exists and the third individual do not appear in any generation self.assertEqual(mlc_repo.count_individual(), 4) # remove unused individuals deleted = mlc_repo.remove_unused_individuals() self.assertEqual(deleted, 2) self.assertEqual(mlc_repo.count_individual(), 2) individual = mlc_repo.get_individual(1) self.assertEqual(individual.get_value(), "(root (+ 1 1))") individual = mlc_repo.get_individual(2) self.assertEqual(individual.get_value(), "(root (+ 2 2))") try: individual = mlc_repo.get_individual(3) self.assertTrue(False) except KeyError: self.assertTrue(True)
def get_simulation(self): if Experiment.__last_simulation is None or Experiment.__last_simulation != self._simulation: MLCRepository._instance = None Config._instance = None Config.get_instance().read(self._config_file) set_logger(Config.get_instance().get('LOGGING', 'logmode')) if Experiment.__last_simulation: Experiment.__last_simulation.close() self._simulation = Simulation(self._name) Experiment.__last_simulation = self._simulation return self._simulation
def test_crossover_same_level_0(self): individual_1 = Individual("(root (cos 5.046))", Config.get_instance()) individual_2 = Individual("(root (cos 5.046))", Config.get_instance()) new_ind_1, new_ind_2, _ = individual_1.crossover(individual_2) self._assert_individual(new_ind_1, complexity=4, value="(root (cos 5.046))", formal="cos(5.046)") self._assert_individual(new_ind_2, complexity=4, value="(root (cos 5.046))", formal="cos(5.046)")
def individual_data(indiv): SAMPLES = 201 x = np.linspace(-10.0, 10.0, num=SAMPLES) y = np.tanh(x**3 - x**2 - 1) config = Config.get_instance() artificial_noise = config.getint('EVALUATOR', 'artificialnoise') y_with_noise = y + [random.random() / 2 - 0.25 for _ in xrange(SAMPLES)] + artificial_noise * 500 if isinstance(indiv.get_formal(), str): formal = indiv.get_formal().replace('S0', 'x') else: # toy problem support for multiple controls formal = indiv.get_formal()[0].replace('S0', 'x') # Calculate J like the sum of the square difference of the # functions in every point lg.logger_.debug('[POP][TOY_PROBLEM] Individual Formal: ' + formal) b = indiv.get_tree().calculate_expression([x]) # If the expression doesn't have the term 'x', # the eval returns a value (float) instead of an array. # In that case transform it to an array if type(b) == float: b = np.repeat(b, SAMPLES) return x, y, y_with_noise, b
def test_add_population(self): mlc_repo = self.__get_new_repo() mlc_repo.add_individual(Individual("(root (+ 1 1))")) mlc_repo.add_individual(Individual("(root (+ 2 2))")) mlc_repo.add_individual(Individual("(root (+ 3 3))")) self.assertEqual(mlc_repo.count_individual(), 3) p = Population(3, 0, Config.get_instance(), mlc_repo) p._individuals = [1, 2, 3] p._costs = [4, 5, 6] p._ev_time = [7, 8, 9] p._gen_method = [10, 11, 12] # add population to the mlc_repository self.assertEqual(mlc_repo.count_population(), 0) mlc_repo.add_population(p) self.assertEqual(mlc_repo.count_population(), 1) # obtain population p_from_repo = mlc_repo.get_population(1) # check population content self.assertEqual(p_from_repo._individuals, p._individuals) self.assertEqual(p_from_repo._costs, p._costs) self.assertEqual(p_from_repo._ev_time, p._ev_time) self.assertEqual(p_from_repo._gen_method, p._gen_method)
def setUpClass(cls): Config._instance = Config.from_dictionary({ "BEHAVIOUR": { "save": "false" }, "POPULATION": { "size": "3", "range": 0, "precision": 0 }, "OPTIMIZATION": { "probrep": 0, "probmut": 0, "probcro": 0, "cascade": "1,1", "simplify": "false" } }) set_working_directory(MLCRepositoryTest.WORKSPACE_DIR) if not os.path.exists(MLCRepositoryTest.WORKSPACE_DIR): os.makedirs(MLCRepositoryTest.WORKSPACE_DIR) os.makedirs( os.path.join(MLCRepositoryTest.WORKSPACE_DIR, MLCRepositoryTest.EXPERIMENT_NAME))
def _fill_creation(self, individuals, index, type): while index < len(individuals): indiv = Individual.generate(individual_type=type, config=Config.get_instance()) response = MLCRepository.get_instance().add_individual(indiv) if not response[1]: # The individual didn't exist indiv_number = individuals[index] lg.logger_.info('[FILL_CREATION] Generating individual N#' + str(indiv_number)) lg.logger_.debug('[FILL_CREATION] Individual N#' + str(indiv_number) + ' - Value: ' + indiv.get_value()) # Call the preevaluation function if it exists and if it is configured if self._config.getboolean('EVALUATOR', 'preevaluation'): callback = PreevaluationManager.get_callback().preev if callback is not None: if not callback(indiv): lg.logger_.info( '[FILL_CREATION] Preevaluation failed' '. Individual value: ' + indiv.get_value()) continue self._individuals.append((index, response[0])) index += 1 else: lg.logger_.debug('[FILL_CREATION] Replica created.') return index
def __init__(self, simulation, callbacks={}, gen_creator=None): self._config = Config.get_instance() # Reload the Operations supported Operations.get_instance(reload_operations=True) self._simulation = simulation self._mlc_repository = MLCRepository.get_instance() # Set logger mode of the App set_logger(self._config.get('LOGGING', 'logmode')) self._simulation = simulation self._project_validations() # callbacks configuration self.__callbacks_manager = MLCCallbacksManager() # bad values and duplicates self.__badvalues_elim = self._config.get('EVALUATOR', 'badvalues_elim') # Gen creator if gen_creator is None: gen_method = self._config.get('GP', 'generation_method') self._gen_creator = CreationFactory.make(gen_method) else: self._gen_creator = gen_creator # Gen evaluator ev_method = self._config.get('EVALUATOR', 'evaluation_method') self._evaluator = EvaluatorFactory.make(ev_method, self.__callbacks_manager) self._look_for_duplicates = self._config.getboolean( 'OPTIMIZATION', 'lookforduplicates') # callbacks for the MLC application if MLC_CALLBACKS.ON_START in callbacks: self.__callbacks_manager.subscribe( MLC_CALLBACKS.ON_START, callbacks[MLC_CALLBACKS.ON_START]) if MLC_CALLBACKS.ON_EVALUATE in callbacks: self.__callbacks_manager.subscribe( MLC_CALLBACKS.ON_EVALUATE, callbacks[MLC_CALLBACKS.ON_EVALUATE]) if MLC_CALLBACKS.ON_NEW_GENERATION in callbacks: self.__callbacks_manager.subscribe( MLC_CALLBACKS.ON_NEW_GENERATION, callbacks[MLC_CALLBACKS.ON_NEW_GENERATION]) if MLC_CALLBACKS.ON_FINISH in callbacks: self.__callbacks_manager.subscribe( MLC_CALLBACKS.ON_FINISH, callbacks[MLC_CALLBACKS.ON_FINISH]) # add callback to show best individual self.__callbacks_manager.subscribe(MLC_CALLBACKS.ON_NEW_GENERATION, self.show_best) self.__display_best = True
def test_generate_individuals_types(self): individual = Individual.generate(individual_type=0, config=Config.get_instance()) self._assert_individual( individual, complexity=120, value= "(root (sin (+ (/ (cos -3.0973) (exp (log (* (* -1.3423 (tanh (log -3.5094))) (+ (/ (/ (* -9.1213 (cos (exp 3.6199))) (cos (* S0 (cos (- 5.0161 (sin 4.2656)))))) S0) (- (cos (* (+ (sin -9.8591) (exp S0)) -9.4159)) (log (* (- (tanh -8.5969) S0) (/ (exp (/ 8.2118 S0)) (* (* S0 (* 1.6755 -0.0699)) (log (exp -3.2288)))))))))))) S0)))", formal= "sin(((my_div(cos((-3.0973)),exp(my_log((((-1.3423) .* tanh(my_log((-3.5094)))) .* ((my_div((my_div(((-9.1213) .* cos(exp(3.6199))),cos((S0 .* cos((5.0161 - sin(4.2656))))))),S0)) + (cos(((sin((-9.8591)) + exp(S0)) .* (-9.4159))) - my_log(((tanh((-8.5969)) - S0) .* (my_div(exp((my_div(8.2118,S0))),((S0 .* (1.6755 .* (-0.0699))) .* my_log(exp((-3.2288))))))))))))))) + S0))" ) individual = Individual.generate(individual_type=1, config=Config.get_instance()) self._assert_individual( individual, complexity=24, value= "(root (- (sin (* (log -3.7260) (+ -5.0573 -6.2191))) (* 7.3027 (/ (cos S0) (* 4.7410 6.7097)))))", formal= "(sin((my_log((-3.7260)) .* ((-5.0573) + (-6.2191)))) - (7.3027 .* (my_div(cos(S0),(4.7410 .* 6.7097)))))" ) individual = Individual.generate(individual_type=2, config=Config.get_instance()) self._assert_individual( individual, complexity=15, value="(root (tanh (cos (+ (+ 5.4434 -3.1258) (+ S0 5.1136)))))", formal="tanh(cos(((5.4434 + (-3.1258)) + (S0 + 5.1136))))") individual = Individual.generate(individual_type=3, config=Config.get_instance()) self._assert_individual( individual, complexity=18, value="(root (log (sin (+ (log -6.2620) (* 8.3709 -6.7676)))))", formal="my_log(sin((my_log((-6.2620)) + (8.3709 .* (-6.7676)))))") individual = Individual.generate(individual_type=4, config=Config.get_instance()) self._assert_individual(individual, complexity=1, value="(root -0.6212)", formal="(-0.6212)")
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 _update_scatter_chart(self): indivs_per_gen = Config.get_instance().getint("POPULATION", "size") costs = None if self._current_gen != 0: generation = self._mlc_local.get_generation(self._experiment_name, self._current_gen) costs = generation.get_costs() self._chart_conf.update_chart(indivs_per_gen, costs)
def _draw_individual_tree(self, pop_index, indiv_index, simplify, evaluate, value): fig = plt.figure() # Put figure window on top of all other windows fig.canvas.manager.window.setWindowModality(Qt.ApplicationModal) fig.canvas.manager.window.setWindowTitle( "Individual Tree Representation") # Simplify the tree if neccesary tree = LispTreeExpr(value) if simplify == True: tree.simplify_tree() graph = tree.construct_graph() # Evaluate the Individual if necessary cost = None if evaluate == True: cost = test_individual_value(parent=self, experiment_name=self._experiment_name, log_prefix=self._log_prefix, indiv_value=value, config=Config.get_instance()) # Remove image axes ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') # Networkx use the node id as label. We have to relabel every node labels = {} for node_id in graph: labels[node_id] = graph.node[node_id]['value'] # Use Pygraph to visualize the graph as a hierarchical tree pos = nx.nx_pydot.graphviz_layout(graph, prog='dot') # Draw nodes and edges in separated nodes to be able to change # the perimter color of the nodes nodes = nx.draw_networkx_nodes(graph, pos, node_size=1000, node_color='#D3D3D3') nodes.set_edgecolor('k') nx.draw_networkx_edges(graph, pos, arrows=False) nx.draw_networkx_labels(graph, pos, labels, font_size=12) # Set the figure Title plt.rc('font', family='serif') title = ( u"Generation N°{0} - Individual N°{1} - Simplified: {2}".format( self._current_gen, indiv_index, simplify)) if evaluate == True: title += " - Cost: {0}".format(cost) plt.suptitle(title, fontsize=12) plt.show() self.close()
def __init__(self): self._config = Config.get_instance() operations_config_file = os.path.join(mlc_paths.get_config_path(), 'operations.yaml') available_operations = yaml.load(open(operations_config_file)) opsetrange = self._config.get_list('POPULATION', 'opsetrange') self._ops = {} for operation_id in opsetrange: self._ops[operation_id] = available_operations[operation_id]
def setUp(self): test_file = """[SECTION_NUMBERS] int_field=1 float_field=0.004 [SECTION_BOOLEANS] bool_field_true=true bool_field_false=false [SECTION_LISTS] list_int_field = 1,5,10,15 list_float_field = 1.01,5.05 list_string_field = 1,2,3 list_range = 1:10 list_float_range = 1:10""" with open(ConfigTest.TEST_FILENAME, 'w+') as f: f.write(test_file) self._config = Config().get_instance() self._config.read(ConfigTest.TEST_FILENAME)
def setUpClass(cls): config_file = os.path.join(MLCIntegrationTest.TEST_DIRECTORY, './configuration.ini') ev_script = os.path.join(MLCIntegrationTest.TEST_DIRECTORY, './default_evaluation_script.py') preev_script = os.path.join(MLCIntegrationTest.TEST_DIRECTORY, './default_preevaluation_script.py') # Load the config config = Config.get_instance() config.read(config_file) # Load randoms from file random_file = 'matlab_randoms.txt' RandomManager.clear_random_values() RandomManager.load_random_values(random_file) # Create the workspace directory try: os.makedirs(MLCIntegrationTest.WORKSPACE_DIR) except OSError: shutil.rmtree(MLCIntegrationTest.WORKSPACE_DIR) os.makedirs(MLCIntegrationTest.WORKSPACE_DIR) # Create a new experiment, which will be used to cls._mlc = MLCLocal(working_dir=MLCIntegrationTest.WORKSPACE_DIR) cls._mlc.new_experiment( experiment_name=MLCIntegrationTest.EXPERIMENT_NAME, experiment_configuration=config_file, evaluation_script=ev_script, preevaluation_script=preev_script) cls._mlc.open_experiment( experiment_name=MLCIntegrationTest.EXPERIMENT_NAME) for generation_params in MLCIntegrationTest.GENERATIONS: if isinstance(generation_params, int): cls._mlc.go(experiment_name=MLCIntegrationTest.EXPERIMENT_NAME, to_generation=generation_params) elif isinstance(generation_params, list): cls._mlc.go(experiment_name=MLCIntegrationTest.EXPERIMENT_NAME, from_generation=generation_params[0], to_generation=generation_params[1]) else: raise Exception( "Integration test, bad value for generations param") # List with individuals data cls._indivs = [] cls._populate_indiv_dict() cls._pops = [] cls._populate_pop_dict()
def test_add_population_with_invalid_individual(self): mlc_repo = self.__get_new_repo() # add population with invalid individual p = Population(1, 0, Config.get_instance(), mlc_repo) p._individuals = [100] try: mlc_repo.add_population(p) self.assertTrue(False) except KeyError: self.assertTrue(True)
def test_random_generate(self): individual = Individual.generate(individual_type=3, config=Config.get_instance()) self.assertEquals( individual.get_value(), "(root (sin (/ (+ (exp -2.6118) (cos S0)) (/ (log 5.9383) (log -4.5037)))))" ) self.assertEquals( individual.get_formal(), "sin((my_div((exp((-2.6118)) + cos(S0)),(my_div(my_log(5.9383),my_log((-4.5037)))))))" ) self.assertEquals(individual.get_complexity(), 28)