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