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)
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])
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
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])
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)
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
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)
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
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
def __init__(self, minfun_, bounds_, p_size): self.minfun = minfun_ self.bounds = bounds_ self.population = Population(minfun_, bounds_, p_size) self.best_genome = None
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())
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)