GA implementation

- ADD comments
- ADD stop function

with Michał Malinowski
This commit is contained in:
Lewy 2021-06-21 03:38:21 +02:00
parent 288d3cf30a
commit ef5c5556ef

View File

@ -43,8 +43,8 @@ def genetic_algorithm_setup(field):
# iterative var # iterative var
generation = 0 generation = 0
# TODO WARUNEK STOPU
while generation < num_generations: while generation < num_generations and stop < 3:
if keyboard.is_pressed('space'): if keyboard.is_pressed('space'):
generation += 1 generation += 1
@ -82,6 +82,7 @@ def genetic_algorithm_setup(field):
offspring_x = random.randint(1, D.GSIZE - 2) offspring_x = random.randint(1, D.GSIZE - 2)
offspring_y = random.randint(1, D.GSIZE - 2) offspring_y = random.randint(1, D.GSIZE - 2)
# TODO OPERATOR KRZYŻOWANIA
offspring_crossover = crossover(parents) offspring_crossover = crossover(parents)
print("Crossover") print("Crossover")
for i in range(0, len(offspring_crossover)): for i in range(0, len(offspring_crossover)):
@ -89,7 +90,7 @@ def genetic_algorithm_setup(field):
for row in offspring_crossover[i]])) for row in offspring_crossover[i]]))
print("") print("")
# Adding some variations to the offspring using mutation. # TODO OPERATOR MUTACJI
offspring_mutation = mutation(population_units, offspring_crossover, population_size - num_parents, offspring_mutation = mutation(population_units, offspring_crossover, population_size - num_parents,
num_mutations=10) num_mutations=10)
print("Mutation") print("Mutation")
@ -105,6 +106,16 @@ def genetic_algorithm_setup(field):
for k in range(0, len(offspring_mutation)): for k in range(0, len(offspring_mutation)):
population_text.append(offspring_mutation[k]) population_text.append(offspring_mutation[k])
# TODO WARUNEK STOPU
stop = 0
if generation > 3:
if best_outputs[-1] / best_outputs[-2] < 1.05:
stop += 1
if best_outputs[-1] / best_outputs[-3] < 1.05:
stop += 1
if best_outputs[-2] / best_outputs[-3] < 1.05:
stop += 1
# final Fitness # final Fitness
fitness = [] fitness = []
for i in range(0, population_size): for i in range(0, population_size):