GA implementation

- ADD pretty_printer method
- crossover draft

with Michał Malinowski
This commit is contained in:
Lewy 2021-06-21 01:56:55 +02:00
parent 19680a0139
commit 7a14078390
2 changed files with 13 additions and 6 deletions

View File

@ -76,6 +76,9 @@ def genetic_algorithm_setup(field):
print(parents) print(parents)
# Generating next generation using crossover. # 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)) offspring_crossover = crossover(parents, offspring_size=(pop_size[0] - parents.shape[0], num_weights))
print("Crossover") print("Crossover")
print(offspring_crossover) print(offspring_crossover)
@ -98,12 +101,7 @@ def genetic_algorithm_setup(field):
print("Best solution : ", new_population[best_match_idx, :]) print("Best solution : ", new_population[best_match_idx, :])
print("Best solution fitness : ", fitness[best_match_idx]) print("Best solution fitness : ", fitness[best_match_idx])
import matplotlib.pyplot pretty_printer(best_outputs)
matplotlib.pyplot.plot(best_outputs)
matplotlib.pyplot.xlabel("Iteration")
matplotlib.pyplot.ylabel("Fitness")
matplotlib.pyplot.show()
# return best iteration of field # return best iteration of field
return 0 return 0

View File

@ -1,3 +1,4 @@
import matplotlib
import numpy import numpy
import src.dimensions as D import src.dimensions as D
@ -64,6 +65,9 @@ def population_fitness(population_text, field, population_size):
def crossover(parents, offspring_size): def crossover(parents, offspring_size):
current_parrent = parents[0]
new_part = []
offspring = numpy.empty(offspring_size) offspring = numpy.empty(offspring_size)
# The point at which crossover takes place between two parents. Usually, it is at the center. # The point at which crossover takes place between two parents. Usually, it is at the center.
crossover_point = numpy.uint8(offspring_size[1] / 2) crossover_point = numpy.uint8(offspring_size[1] / 2)
@ -93,3 +97,8 @@ def mutation(offspring_crossover, num_mutations=1):
return offspring_crossover return offspring_crossover
def pretty_printer(best_outputs):
matplotlib.pyplot.plot(best_outputs)
matplotlib.pyplot.xlabel("Iteration")
matplotlib.pyplot.ylabel("Fitness")
matplotlib.pyplot.show()