コード例 #1
0
ファイル: main.py プロジェクト: uofuonye/EvolvingImagesPy
def main():
    try:
        config = Config(elitism=True, eliteCount=2, mutationProbability=0.1)
        masterImage = cv2.imread("images/facebook.jpg")
        masterImage = cv2.resize(masterImage, (10, 10))
        cv2.imwrite('images/output/master.jpg', masterImage)
        population = Population(populationSize=50,
                                masterImage=masterImage,
                                config=config,
                                populate=True)
        originalFitness = population.fitestCitizen.fitness
        generation = 0
        Max_Generation = 100000000
        while generation < Max_Generation:
            population = population.Evolve(generation)
            fitestCitizen = population.fitestCitizen
            fitnessPercentage = 100 * (originalFitness -
                                       fitestCitizen.fitness) / originalFitness
            print("Fitness = " + str(fitestCitizen.fitness) +
                  " : FitnessPercentage = " + str(fitnessPercentage) + "%")
            if generation % 1000 == 0:
                cv2.imwrite(
                    'images/output/generation' + str(generation) + ".jpg",
                    fitestCitizen.image)
            generation += 1
    except IOError:
        pass
コード例 #2
0
    def test_fit(self):
        file_path = "../Dataset/00-91-Drugs-All-In-One-File.csv"
        loaded_data = FileManager.load_file(file_path)

        data_manager = DataManager(normalizer=None)
        data_manager.set_data(loaded_data)
        data_manager.split_data_into_train_valid_test_sets(test_split=0.15,
                                                           train_split=0.70)

        model = svm.SVR()

        velocity = Velocity()
        velocity_matrix = velocity.create_first_velocity()

        # define the first population
        # validation of a row generating random row for
        population = Population(velocity_matrix=velocity_matrix)
        population.create_first_population()

        debpso = DEBPSO(population.population_matrix[1])
        debpso.fit(data_manager.inputs[SplitTypes.Train],
                   data_manager.targets[SplitTypes.Train])
        print("Population 1 row sum ", population.population_matrix[1].sum())
        print("Selected feature descriptors",
              debpso.sel_descriptors_for_curr_population)
コード例 #3
0
    def run(self, iterations):
        """Runs the Differential Evolution algorithm.

        Args:
            iterations (int): Number of iterations to be made by the algorithm.
        """
        # print(f'Before:\n {self.population}\n')
        # self.best()
        # print(f'Best Genome before: {self.best_genome.array}, fitness={self.best_genome.fitness} ')

        mutator = Rand1MutationOperator(self.population, self.bounds, 0.2)
        mixer = ExponentialCrossoverOperator(self.minfun)
        replacer = ElitistReplacementOperator()

        for _ in range(iterations):
            candidate_population = Population(None, None, 0)
            for target in self.population.collection:
                # List with genomes who will be the donors
                mutant = mutator.apply(target)
                # Genome modified by replacing a few random positions
                candidate_genome = mixer.apply(target, mutant)

                candidate_population.add(candidate_genome)

            # Targets are replaced by candidates from the population if candidate has less fitness than target
            self.population = replacer.apply(self.population,
                                             candidate_population)
コード例 #4
0
    def test_transform(self):
        read_data = ReadData()
        loaded_data = read_data.read_data_and_set_variable_settings("../Dataset/00-91-Drugs-All-In-One-File.csv", "../Dataset/VariableSetting.csv")


        data_manager = DataManager(normalizer=None)
        data_manager.set_data(loaded_data)
        data_manager.split_data_into_train_valid_test_sets()

        model = svm.SVR()

        velocity = Velocity()
        velocity_matrix = velocity.create_first_velocity()

        # define the first population
        # validation of a row generating random row for
        population = Population(velocity_matrix=velocity_matrix)
        population.create_first_population()

        debpso = DEBPSO(population.population_matrix[0])
        debpso.fit(data_manager.inputs[SplitTypes.Train], data_manager.targets[SplitTypes.Train])
        data_manager.transformed_input[SplitTypes.Train] = debpso.transform(data_manager.inputs[SplitTypes.Train])
        print("Population 0 row sum ", population.population_matrix[0].sum())
        print("Selected feature descriptors",debpso.sel_descriptors_for_curr_population)
        print("Transformed array", data_manager.transformed_input[SplitTypes.Train])
コード例 #5
0
    def test_transform(self):
        read_data = ReadData()
        loaded_data = read_data.read_data_and_set_variable_settings(
            "../Dataset/00-91-Drugs-All-In-One-File.csv",
            "../Dataset/VariableSetting.csv")

        data_manager = DataManager(normalizer=None)
        data_manager.set_data(loaded_data)
        data_manager.split_data_into_train_valid_test_sets()

        model = svm.SVR()

        velocity = Velocity()
        velocity_matrix = velocity.create_first_velocity()

        # define the first population
        # validation of a row generating random row for
        population = Population(velocity_matrix=velocity_matrix)
        population.create_first_population()

        debpso = DEBPSO(population.population_matrix[0])
        debpso.fit(data_manager.inputs[SplitTypes.Train],
                   data_manager.targets[SplitTypes.Train])
        data_manager.transformed_input[SplitTypes.Train] = debpso.transform(
            data_manager.inputs[SplitTypes.Train])
        print("Population 0 row sum ", population.population_matrix[0].sum())
        print("Selected feature descriptors",
              debpso.sel_descriptors_for_curr_population)
        print("Transformed array",
              data_manager.transformed_input[SplitTypes.Train])
コード例 #6
0
    def test_fit(self):
        #file_path = "../Dataset/00-91-Drugs-All-In-One-File.csv"
        #loaded_data = FileManager.load_file(file_path)

        read_data = ReadData()
        loaded_data = read_data.read_data_and_set_variable_settings(
            "../Dataset/00-91-Drugs-All-In-One-File.csv",
            "../Dataset/VariableSetting.csv")

        data_manager = DataManager(normalizer=None)
        data_manager.set_data(loaded_data)
        data_manager.split_data_into_train_valid_test_sets()

        model = svm.SVR()

        velocity = Velocity()
        velocity_matrix = velocity.create_first_velocity()

        # define the first population
        # validation of a row generating random row for
        population = Population(velocity_matrix=velocity_matrix)
        population.create_first_population()

        debpso = DEBPSO(population.population_matrix[1])
        debpso.fit(data_manager.inputs[SplitTypes.Train],
                   data_manager.targets[SplitTypes.Train])
コード例 #7
0
    def test_fit(self):
        #file_path = "../Dataset/00-91-Drugs-All-In-One-File.csv"
        #loaded_data = FileManager.load_file(file_path)

        read_data = ReadData()
        loaded_data = read_data.read_data_and_set_variable_settings("../Dataset/00-91-Drugs-All-In-One-File.csv", "../Dataset/VariableSetting.csv")

        data_manager = DataManager(normalizer=None)
        data_manager.set_data(loaded_data)
        data_manager.split_data_into_train_valid_test_sets()

        model = svm.SVR()

        velocity = Velocity()
        velocity_matrix = velocity.create_first_velocity()

        # define the first population
        # validation of a row generating random row for
        population = Population(velocity_matrix=velocity_matrix)
        population.create_first_population()



        debpso = DEBPSO(population.population_matrix[1])
        debpso.fit(data_manager.inputs[SplitTypes.Train], data_manager.targets[SplitTypes.Train])
コード例 #8
0
    def generate_population_matrix(self, current_alpha):
        self.old_population_matrix = np.copy(self.population_matrix)

        for row_index in range(0, self.selective_section):
            for col_index in range(0, VariableSetting.No_of_Descriptors):
                if current_alpha < self.velocity_matrix[row_index][
                        col_index] and self.velocity_matrix[row_index][
                            col_index] <= (0.5 * (1 + current_alpha)):
                    self.population_matrix[row_index][
                        col_index] = self.local_best_matrix[row_index][
                            col_index]
                elif ((0.5 * (1 + current_alpha)) <
                      self.velocity_matrix[row_index][col_index]) and (
                          self.velocity_matrix[row_index][col_index] <=
                          (1 - VariableSetting.Beta)):
                    self.population_matrix[row_index][
                        col_index] = self.global_best_row[col_index]
                elif ((1 - VariableSetting.Beta) <
                      self.velocity_matrix[row_index][col_index]) and (
                          self.velocity_matrix[row_index][col_index] <= 1):
                    self.population_matrix[row_index][
                        col_index] = 1 - self.population_matrix[row_index][
                            col_index]

        for row_index in range(self.selective_section,
                               VariableSetting.Population_Size):
            velocity_object = Velocity()
            random_velocity_row = velocity_object.get_valid_row()
            self.population_matrix[
                row_index] = Population.create_valid_random_population_row(
                    random_velocity_row)
コード例 #9
0
    def apply(self, current_population, candidate_population):
        """
        Each genome of 'current_population' is compared with the genome which is in the same position than
        'candidate_population' and the one with the best fitness of these two genomes is added to
        'result_population'.

        Args:
            current_population (Population): Object which contains a list of genomes.
            candidate_population (list): List which contains a list of genomes.

        Returns:
            result_population (Population) : Resulting population with the best fitness in each position.
        """
        result_population = Population(None, None, 0)
        for target, candidate in zip(current_population.collection, candidate_population.collection):
            result_population.add(candidate if candidate.fitness < target.fitness else target)

        return result_population
コード例 #10
0
 def fill_values_for_charts(self, individuals, generation_number):
     evaluated_values = Population.evaluate_individuals(individuals, self.fitness_function)[:, 1]
     best_val = np.min(evaluated_values)
     if generation_number > 0 and self.elite_strategy_num > 0:
         if best_val < self.searching_value(self.bests_values):
             self.best_individual_generation = generation_number
     self.bests_values.append(best_val)
     self.mean_values.append(np.mean(evaluated_values))
     self.sd_values.append(np.std(evaluated_values))
コード例 #11
0
    def run(self):
        time_start = time.time()
        next_generation_individuals = np.array([False])
        for generation in range(self.epochs_num):
            new_population = Population(self.chromosome_type, self.population_size, self.chromosomes_number,
                                        self.range_start, self.range_end, self.accuracy, self.fitness_function,
                                        self.searching_value, self.crossover_type, self.crossover_prob,
                                        self.elite_strategy_num,
                                        next_generation_individuals)
            self.population_set.add(new_population)
            if self.elite_strategy_num > 0:
                self.best_individuals = self.elite_strategy(new_population.best_individuals)
            new_population.select_individuals(self.selection_type, self.selection_args)
            new_individuals = new_population.crossover_selected_individuals()
            if self.mutation_prob > 0.0:
                Population.mutate_individuals(new_individuals, self.mutation_type, self.mutation_prob)
            if self.inversion_prob > 0.0:
                Population.inverse_individuals(new_individuals, self.inversion_prob)
            if self.elite_strategy_num > 0:
                next_generation_individuals = np.append(new_individuals, self.best_individuals)
            else:
                next_generation_individuals = new_individuals
            self.fill_values_for_charts(next_generation_individuals, generation)

        best = Population.get_n_best_individuals(1, self.searching_value, next_generation_individuals,
                                                 self.fitness_function)
        self.best_individual = best[0]
        time_end = time.time()
        self.time = time_end - time_start

        print("Final best:")
        print(best[0].get_decimal_value_of_chromosomes())
        print("value:")
        print(best[0].evaluate(self.fitness_function))
        print("evolution time: ")
        print(self.time)
        print("BESTS")
        print(self.bests_values)
        print("MEAN")
        print(self.mean_values)
        print("STD")
        print(self.sd_values)
コード例 #12
0
    def test_fit(self):
        file_path = "../Dataset/00-91-Drugs-All-In-One-File.csv"
        loaded_data = FileManager.load_file(file_path)

        data_manager = DataManager(normalizer=None)
        data_manager.set_data(loaded_data)
        data_manager.split_data_into_train_valid_test_sets(test_split=0.15, train_split=0.70)

        model = svm.SVR()

        velocity = Velocity()
        velocity_matrix = velocity.create_first_velocity()

        # define the first population
        # validation of a row generating random row for
        population = Population(velocity_matrix=velocity_matrix)
        population.create_first_population()

        debpso = DEBPSO(population.population_matrix[1])
        debpso.fit(data_manager.inputs[SplitTypes.Train], data_manager.targets[SplitTypes.Train])
        print("Population 1 row sum ", population.population_matrix[1].sum())
        print("Selected feature descriptors",debpso.sel_descriptors_for_curr_population)
コード例 #13
0
    def _init_population(self, population_size, pixels_to_change_count):

        population = Population()
        width = self.img.shape[0] - 1
        height = self.img.shape[1] - 1
        all_indices = []
        for i in range(0, width):
            for j in range(0, height):
                all_indices.append((i, j))
        random.shuffle(all_indices)
        population.phenotype = all_indices[0:pixels_to_change_count]

        for i in range(population_size):
            fake_img_candidate = FakeImgCandidate(np.copy(self.img))
            for x, y in population.phenotype:
                current_pixel_value = fake_img_candidate.get_pixel_value(x, y)
                new_pixel_value = self._generate_new_pixel_value(current_pixel_value)
                fake_img_candidate.set_pixel_value(x, y, new_pixel_value)

            population.add_img(fake_img_candidate)

        return population
コード例 #14
0
    def generate_population_matrix(self, current_alpha):
        self.old_population_matrix = np.copy(self.population_matrix)

        for row_index in range(0, self.selective_section):
            for col_index in range(0, VariableSetting.No_of_Descriptors):
                if current_alpha< self.velocity_matrix[row_index][col_index] and self.velocity_matrix[row_index][col_index] <= (0.5 * (1+ current_alpha)):
                    self.population_matrix[row_index][col_index] = self.local_best_matrix[row_index][col_index]
                elif ((0.5 * (1+ current_alpha)) < self.velocity_matrix[row_index][col_index]) and (self.velocity_matrix[row_index][col_index] <= (1 - VariableSetting.Beta)):
                    self.population_matrix[row_index][col_index] = self.global_best_row[col_index]
                elif ((1 - VariableSetting.Beta ) < self.velocity_matrix[row_index][col_index]) and (self.velocity_matrix[row_index][col_index] <= 1):
                    self.population_matrix[row_index][col_index] = 1 - self.population_matrix[row_index][col_index]

        for row_index in range(self.selective_section, VariableSetting.Population_Size):
            velocity_object = Velocity()
            random_velocity_row = velocity_object.get_valid_row()
            self.population_matrix[row_index] = Population.create_valid_random_population_row(random_velocity_row)
コード例 #15
0
 def _crossover(self, population, selected_imgs, crossover_prob):
     new_population = Population()
     new_population.phenotype = population.phenotype
     range_of_selected_images = range(len(selected_imgs))
     for i in range_of_selected_images:
         selected_img_index_range = list(range_of_selected_images)
         random.shuffle(selected_img_index_range)
         first_index = selected_img_index_range[0]
         second_index = selected_img_index_range[1]
         parent_1 = selected_imgs[first_index]
         parent_2 = selected_imgs[second_index]
         child = FakeImgCandidate(np.copy(self.img))
         for x, y in population.phenotype:
             r = random.random()
             if r <= crossover_prob:
                 new_pixel_value = (parent_1.get_pixel_value(x, y) + parent_2.get_pixel_value(x, y)) / 2
                 child.set_pixel_value(x, y, new_pixel_value)
         new_population.add_img(child)
     for selected_img in selected_imgs:
         new_population.add_img(selected_img)
     return new_population
コード例 #16
0
 def __init__(self, minfun_, bounds_, p_size):
     self.minfun = minfun_
     self.bounds = bounds_
     self.population = Population(minfun_, bounds_, p_size)
     self.best_genome = None
コード例 #17
0
from FileLoader import FileLoader
from DataManager import DataManager
from src.Population import Population

file_path = "../Dataset/00-91-Drugs-All-In-One-File.csv"
loaded_data = FileLoader.load_file(file_path)

data_manager = DataManager(normalizer=None)
data_manager.create_first_population(loaded_data)
data_manager.split_data_into_train_valid_test_sets(test_split=0.15, train_split=0.70)

population = Population()
population.create_first_population()
for i in range (1,50):
    print("row", i, population.population_matrix[i].sum())
コード例 #18
0
 def create_first_population(self):
     population = Population(velocity_matrix=self.velocity_matrix)
     self.population_matrix = population.create_first_population()
     self.local_best_matrix = np.copy(self.population_matrix)
コード例 #19
0
file_path = "../Dataset/00-91-Drugs-All-In-One-File.csv"
loaded_data = FileManager.load_file(file_path)
output_filename = FileManager.create_output_file()


#rescaling_normalizer = RescalingNormalizer()
#scikit_normalizer = ScikitNormalizer()
#data_manager = DataManager(normalizer=scikit_normalizer)

data_manager = DataManager(normalizer=None)
data_manager.set_data(loaded_data)
data_manager.split_data(test_split=0.15, train_split=0.70)

model = svm.SVR()

population = Population()
population.load_data()


'''

    TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR_DE_BPSO.getAllOfTheData()
    TrainX, ValidateX, TestX = FromDataFileMLR_DE_BPSO.rescaleTheData(TrainX, ValidateX, TestX)

    velocity = createInitVelMat(numOfPop, numOfFea)


    def rescaleTheData(TrainX, ValidateX, TestX):

    # 1 degree of freedom means (ddof) N-1 unbiased estimation
    TrainXVar = TrainX.var(axis = 0, ddof=1)
コード例 #20
0
 def elite_strategy(self, new_best_candidates):
     individuals = np.asarray(list(set(np.append(self.best_individuals, new_best_candidates))))
     return Population.get_n_best_individuals(self.elite_strategy_num, self.searching_value, individuals,
                                              self.fitness_function)
コード例 #21
0
 def create_first_population(self):
     population = Population(velocity_matrix=self.velocity_matrix)
     self.population_matrix = population.create_first_population()
     self.local_best_matrix = np.copy(self.population_matrix)
コード例 #22
0
class EA(object):
    """This class is the entry point to the execution of the algorithm.

    Attributes:
        minfun (function): Function used to calculate the fitness of a genome.
        bounds (list): Contains the minimum and maximum values that each variable can take from a candidate
            solution.
        population (Population): Object which contains a list of genomes.
        best_genome (Genome): Optimal solution calculated by the algorithm.


    Args:
        minfun_ (function): The function to be set.
        bounds_ (list): List of values to be set.
        p_size (int): Maximum size of our population.
    """
    def __init__(self, minfun_, bounds_, p_size):
        self.minfun = minfun_
        self.bounds = bounds_
        self.population = Population(minfun_, bounds_, p_size)
        self.best_genome = None

    def run(self, iterations):
        """Runs the Differential Evolution algorithm.

        Args:
            iterations (int): Number of iterations to be made by the algorithm.
        """
        # print(f'Before:\n {self.population}\n')
        # self.best()
        # print(f'Best Genome before: {self.best_genome.array}, fitness={self.best_genome.fitness} ')

        mutator = Rand1MutationOperator(self.population, self.bounds, 0.2)
        mixer = ExponentialCrossoverOperator(self.minfun)
        replacer = ElitistReplacementOperator()

        for _ in range(iterations):
            candidate_population = Population(None, None, 0)
            for target in self.population.collection:
                # List with genomes who will be the donors
                mutant = mutator.apply(target)
                # Genome modified by replacing a few random positions
                candidate_genome = mixer.apply(target, mutant)

                candidate_population.add(candidate_genome)

            # Targets are replaced by candidates from the population if candidate has less fitness than target
            self.population = replacer.apply(self.population,
                                             candidate_population)

        # print(f'After:\n {self.population}\n')
        # self.best()
        # print(f'Best Genome after: {self.best_genome.array}, fitness={self.best_genome.fitness} ')

    def best(self):
        """Gets the best genome

        Returns:
            best_genome (Genome): Optimal solution calculated by the algorithm.
        """
        self.population.ascendent_sort()
        self.best_genome = self.population.collection[0]
        return self.best_genome
コード例 #23
0
from FileLoader import FileLoader
from DataManager import DataManager
from src.Population import Population

file_path = "../Dataset/00-91-Drugs-All-In-One-File.csv"
loaded_data = FileLoader.load_file(file_path)

data_manager = DataManager(normalizer=None)
data_manager.create_first_population(loaded_data)
data_manager.split_data_into_train_valid_test_sets(test_split=0.15,
                                                   train_split=0.70)

population = Population()
population.create_first_population()
for i in range(1, 50):
    print("row", i, population.population_matrix[i].sum())