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_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_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 __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_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_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_reload_individuals_in_memory_loss_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))"))

        # add population
        p = Population(3, 0, Config.get_instance(), mlc_repo)
        p._individuals = [2, 1, 2]

        # reload mlc_repository using another instance
        mlc_repo = self.__get_new_repo()
        self.assertEqual(mlc_repo.count_individual(), 0)
        self.assertEqual(mlc_repo.count_population(), 0)
    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 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_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 _update_individuals_per_generation_list(self):
        # Clean up ye olde list
        self._individuals_per_generation = []

        experiment_info = self._mlc_local.get_experiment_info(self._experiment_name)
        number_of_gens = experiment_info["generations"]
        indivs_per_gen = experiment_info["individuals_per_generation"]

        if number_of_gens == 0:
            # Disable Experiment tab buttons
            self._autogenerated_object.left_menu_frame.setDisabled(True)
            self._autogenerated_object.right_menu_frame.setDisabled(True)
            self._autogenerated_object.exp_gen_count_group.setDisabled(True)
            self._autogenerated_object.tabWidget.setTabEnabled(3, True)
            return
        else:
            self._autogenerated_object.left_menu_frame.setDisabled(False)
            self._autogenerated_object.right_menu_frame.setDisabled(False)
            self._autogenerated_object.exp_gen_count_group.setDisabled(False)
            self._autogenerated_object.tabWidget.setTabEnabled(3, False)

        # Complete the list
        individuals = self._mlc_local.get_individuals(self._experiment_name)
        for index in xrange(1, number_of_gens + 1):
            gens_list = []

            generation = self._mlc_local.get_generation(self._experiment_name, index)
            pop_individuals = generation.get_individuals()
            costs = generation.get_costs()
            gen_methods = generation.get_gen_methods()

            for pop_index in xrange(1, indivs_per_gen + 1):
                indiv_index = pop_individuals[pop_index - 1]
                indiv_cost = costs[pop_index - 1]
                indiv_value = individuals[indiv_index].get_value()
                indiv_appearences = individuals[indiv_index].get_appearances()

                indiv_gen_method = Population.gen_method_description(gen_methods[pop_index - 1])
                gens_list.append([pop_index, indiv_index, indiv_gen_method,
                                  indiv_appearences, indiv_cost, indiv_value])

            self._individuals_per_generation.append(gens_list)
    def test_cut_generations_more_than_once(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))"))
        mlc_repo.add_individual(Individual("(root (+ 6 6))"))
        mlc_repo.add_individual(Individual("(root (+ 7 7))"))
        mlc_repo.add_individual(Individual("(root (+ 8 8))"))

        # 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)

        p = Population(3, 0, Config.get_instance(), mlc_repo)
        p._individuals = [5, 5, 5]
        mlc_repo.add_population(p)

        p = Population(3, 0, Config.get_instance(), mlc_repo)
        p._individuals = [6, 6, 6]
        mlc_repo.add_population(p)

        p = Population(3, 0, Config.get_instance(), mlc_repo)
        p._individuals = [7, 7, 7]
        mlc_repo.add_population(p)

        p = Population(3, 0, Config.get_instance(), mlc_repo)
        p._individuals = [8, 8, 8]
        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, [7, 7, 7])

        individual = mlc_repo.get_individual(7)
        self.assertEqual(individual.get_value(), "(root (+ 7 7))")
    def test_remove_population_to_clear_generations(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(3)
        self.assertEqual(mlc_repo.count_population(), 0)

        # Insert populations again
        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(), 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_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])