From 5ca916873d582f72ce05fafbdd1f1797dc03a68e Mon Sep 17 00:00:00 2001 From: Lewy Date: Mon, 21 Jun 2021 04:43:13 +0200 Subject: [PATCH] GA FIX MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - FIX bug where parents would not go to next gen with MichaƂ Malinowski --- AI/GeneticAlgorithm.py | 45 ++++++++++++++++++------------------------ AI/ga_methods.py | 17 ++++++++-------- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/AI/GeneticAlgorithm.py b/AI/GeneticAlgorithm.py index 5851350..5d45d24 100644 --- a/AI/GeneticAlgorithm.py +++ b/AI/GeneticAlgorithm.py @@ -24,10 +24,6 @@ def genetic_algorithm_setup(field): population_text_single[row].append(random.choice(population_units)) population_text.append(population_text_single) - # # printer - # for _ in population_text: - # print(population_text) - """ Genetic algorithm parameters: Mating pool size @@ -35,8 +31,6 @@ def genetic_algorithm_setup(field): """ # units per population in generation - num_parents_mating = 4 - best_outputs = [] num_generations = 10 num_parents = 4 @@ -56,13 +50,12 @@ def genetic_algorithm_setup(field): fitness = [] for i in range(0, population_size): - print('POP_TEXT', len(population_text), i) fitness.append((i, population_fitness(population_text[i], field, population_size))) print("Fitness") print(fitness) - best = sorted(fitness, key=lambda tup: tup[1])[0:num_parents] + best = sorted(fitness, key=lambda tup: tup[1], reverse=True)[0:num_parents] # Leaderboard only best_outputs.append(best[0][1]) @@ -72,12 +65,14 @@ def genetic_algorithm_setup(field): # TODO METODA WYBORU OSOBNIKA - RANKING # Selecting the best parents in the population for mating. + print(best) parents = [population_text[i[0]] for i in best] + parents_copy = copy.deepcopy(parents) print("Parents") - for i in range(0, len(parents)): - print('\n'.join([''.join(['{:4}'.format(item) for item in row]) - for row in parents[i]])) - print("") + # for i in range(0, len(parents)): + # print('\n'.join([''.join(['{:4}'.format(item) for item in row]) + # for row in parents[i]])) + # print("") # Generating next generation using crossover. offspring_x = random.randint(1, D.GSIZE - 2) @@ -86,26 +81,26 @@ def genetic_algorithm_setup(field): # TODO OPERATOR KRZYĆ»OWANIA offspring_crossover = crossover(parents) print("Crossover") - for i in range(0, len(offspring_crossover)): - print('\n'.join([''.join(['{:4}'.format(item) for item in row]) - for row in offspring_crossover[i]])) - print("") + # for i in range(0, len(offspring_crossover)): + # print('\n'.join([''.join(['{:4}'.format(item) for item in row]) + # for row in offspring_crossover[i]])) + # print("") # TODO OPERATOR MUTACJI offspring_mutation = mutation(population_units, offspring_crossover, population_size - num_parents, num_mutations=10) print("Mutation") - for i in range(0, len(offspring_mutation)): - print('\n'.join([''.join(['{:4}'.format(item) for item in row]) - for row in offspring_mutation[i]])) - print("") + # for i in range(0, len(offspring_mutation)): + # print('\n'.join([''.join(['{:4}'.format(item) for item in row]) + # for row in offspring_mutation[i]])) + # print("") population_text_copy = copy.deepcopy(population_text) unused_indexes = [i for i in range(0, population_size) if i not in [j[0] for j in best]] # Creating next generation population_text = [] - for k in range(0, len(parents)): - population_text.append(parents[k]) + for k in parents_copy: + population_text.append(k) for k in range(0, len(offspring_mutation)): population_text.append(offspring_mutation[k]) while len(population_text) < population_size: @@ -113,11 +108,9 @@ def genetic_algorithm_setup(field): population_text.append(population_text_copy[x]) unused_indexes.remove(x) - print("END LEN", len(population_text)) - # TODO WARUNEK STOPU stop = 0 - if generation > 3: + if generation > 10: if best_outputs[-1] / best_outputs[-2] < 1.05: stop += 1 if best_outputs[-1] / best_outputs[-3] < 1.05: @@ -142,7 +135,7 @@ def genetic_algorithm_setup(field): pretty_printer(best_outputs) - # return best iteration of field + # TODO REALLY return best iteration of field return 0 diff --git a/AI/ga_methods.py b/AI/ga_methods.py index 9cebedb..266671c 100644 --- a/AI/ga_methods.py +++ b/AI/ga_methods.py @@ -43,7 +43,6 @@ def local_fitness(field, x, y, plants_case): if plants_case[x][y] == plants_case[x][y + 1]: neighbour_bonus += 1 - # TODO * multiculture_bonus local_fitness_value = (soil_value + plant_value) * (0.5 * neighbour_bonus + 1) return local_fitness_value @@ -53,7 +52,6 @@ def population_fitness(population_text_local, field, population_size): # The fitness function calulates the sum of products between each input and its corresponding weight. fitness = [] - print('LOCAL', len(population_text_local)) for k in range(population_size): population_values_single = [] population_values_single_row = [] @@ -70,16 +68,17 @@ def population_fitness(population_text_local, field, population_size): return fitness -def crossover(parents): +def crossover(local_parents): ret = [] - for i in range(0, len(parents)): - child = copy.deepcopy(parents[i]) + for i in range(0, len(local_parents)): + child = copy.deepcopy(local_parents[i]) # Vertical randomization - width = random.randint(1, D.GSIZE // len(parents)) # width of stripes - indexes_parents = numpy.random.permutation(range(0, len(parents))) # sorting of stripes - beginning = random.randint(0, len(parents[0]) - width * len(parents)) # point we start putting the stripes from + width = random.randint(1, D.GSIZE // len(local_parents)) # width of stripes + indexes_parents = numpy.random.permutation(range(0, len(local_parents))) # sorting of stripes + beginning = random.randint(0, len(local_parents[0]) - width * len( + local_parents)) # point we start putting the stripes from for x in indexes_parents: - child[beginning:beginning + width] = parents[x][beginning:beginning + width] + child[beginning:beginning + width] = local_parents[x][beginning:beginning + width] beginning += width ret.append(child) return ret