From 7a14078390f1363d4305ae77c495fecbe2cdbcef Mon Sep 17 00:00:00 2001 From: Lewy Date: Mon, 21 Jun 2021 01:56:55 +0200 Subject: [PATCH] GA implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ADD pretty_printer method - crossover draft with MichaƂ Malinowski --- AI/GeneticAlgorithm.py | 10 ++++------ AI/ga_methods.py | 9 +++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/AI/GeneticAlgorithm.py b/AI/GeneticAlgorithm.py index 01569dd..15ff7c3 100644 --- a/AI/GeneticAlgorithm.py +++ b/AI/GeneticAlgorithm.py @@ -76,6 +76,9 @@ def genetic_algorithm_setup(field): print(parents) # Generating next generation using crossover. + offspring_x = random.randint(1, D.GSIZE - 2) + offspring_y = random.randint(1, D.GSIZE - 2) + offspring_crossover = crossover(parents, offspring_size=(pop_size[0] - parents.shape[0], num_weights)) print("Crossover") print(offspring_crossover) @@ -98,12 +101,7 @@ def genetic_algorithm_setup(field): print("Best solution : ", new_population[best_match_idx, :]) print("Best solution fitness : ", fitness[best_match_idx]) - import matplotlib.pyplot - - matplotlib.pyplot.plot(best_outputs) - matplotlib.pyplot.xlabel("Iteration") - matplotlib.pyplot.ylabel("Fitness") - matplotlib.pyplot.show() + pretty_printer(best_outputs) # return best iteration of field return 0 diff --git a/AI/ga_methods.py b/AI/ga_methods.py index ef54051..f511de7 100644 --- a/AI/ga_methods.py +++ b/AI/ga_methods.py @@ -1,3 +1,4 @@ +import matplotlib import numpy import src.dimensions as D @@ -64,6 +65,9 @@ def population_fitness(population_text, field, population_size): def crossover(parents, offspring_size): + current_parrent = parents[0] + new_part = [] + offspring = numpy.empty(offspring_size) # The point at which crossover takes place between two parents. Usually, it is at the center. crossover_point = numpy.uint8(offspring_size[1] / 2) @@ -93,3 +97,8 @@ def mutation(offspring_crossover, num_mutations=1): return offspring_crossover +def pretty_printer(best_outputs): + matplotlib.pyplot.plot(best_outputs) + matplotlib.pyplot.xlabel("Iteration") + matplotlib.pyplot.ylabel("Fitness") + matplotlib.pyplot.show()