- FIX bug where parents would not go to next gen

with Michał Malinowski
This commit is contained in:
Lewy 2021-06-21 04:43:13 +02:00
parent 361a733102
commit 5ca916873d
2 changed files with 27 additions and 35 deletions

View File

@ -24,10 +24,6 @@ def genetic_algorithm_setup(field):
population_text_single[row].append(random.choice(population_units)) population_text_single[row].append(random.choice(population_units))
population_text.append(population_text_single) population_text.append(population_text_single)
# # printer
# for _ in population_text:
# print(population_text)
""" """
Genetic algorithm parameters: Genetic algorithm parameters:
Mating pool size Mating pool size
@ -35,8 +31,6 @@ def genetic_algorithm_setup(field):
""" """
# units per population in generation # units per population in generation
num_parents_mating = 4
best_outputs = [] best_outputs = []
num_generations = 10 num_generations = 10
num_parents = 4 num_parents = 4
@ -56,13 +50,12 @@ def genetic_algorithm_setup(field):
fitness = [] fitness = []
for i in range(0, population_size): 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))) fitness.append((i, population_fitness(population_text[i], field, population_size)))
print("Fitness") print("Fitness")
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 # Leaderboard only
best_outputs.append(best[0][1]) best_outputs.append(best[0][1])
@ -72,12 +65,14 @@ def genetic_algorithm_setup(field):
# TODO METODA WYBORU OSOBNIKA - RANKING # TODO METODA WYBORU OSOBNIKA - RANKING
# Selecting the best parents in the population for mating. # Selecting the best parents in the population for mating.
print(best)
parents = [population_text[i[0]] for i in best] parents = [population_text[i[0]] for i in best]
parents_copy = copy.deepcopy(parents)
print("Parents") print("Parents")
for i in range(0, len(parents)): # for i in range(0, len(parents)):
print('\n'.join([''.join(['{:4}'.format(item) for item in row]) # print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in parents[i]])) # for row in parents[i]]))
print("") # print("")
# Generating next generation using crossover. # Generating next generation using crossover.
offspring_x = random.randint(1, D.GSIZE - 2) offspring_x = random.randint(1, D.GSIZE - 2)
@ -86,26 +81,26 @@ def genetic_algorithm_setup(field):
# TODO OPERATOR KRZYŻOWANIA # 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)):
print('\n'.join([''.join(['{:4}'.format(item) for item in row]) # print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in offspring_crossover[i]])) # for row in offspring_crossover[i]]))
print("") # print("")
# TODO OPERATOR MUTACJI # 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")
for i in range(0, len(offspring_mutation)): # for i in range(0, len(offspring_mutation)):
print('\n'.join([''.join(['{:4}'.format(item) for item in row]) # print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in offspring_mutation[i]])) # for row in offspring_mutation[i]]))
print("") # print("")
population_text_copy = copy.deepcopy(population_text) 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]] unused_indexes = [i for i in range(0, population_size) if i not in [j[0] for j in best]]
# Creating next generation # Creating next generation
population_text = [] population_text = []
for k in range(0, len(parents)): for k in parents_copy:
population_text.append(parents[k]) population_text.append(k)
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])
while len(population_text) < population_size: while len(population_text) < population_size:
@ -113,11 +108,9 @@ def genetic_algorithm_setup(field):
population_text.append(population_text_copy[x]) population_text.append(population_text_copy[x])
unused_indexes.remove(x) unused_indexes.remove(x)
print("END LEN", len(population_text))
# TODO WARUNEK STOPU # TODO WARUNEK STOPU
stop = 0 stop = 0
if generation > 3: if generation > 10:
if best_outputs[-1] / best_outputs[-2] < 1.05: if best_outputs[-1] / best_outputs[-2] < 1.05:
stop += 1 stop += 1
if best_outputs[-1] / best_outputs[-3] < 1.05: if best_outputs[-1] / best_outputs[-3] < 1.05:
@ -142,7 +135,7 @@ def genetic_algorithm_setup(field):
pretty_printer(best_outputs) pretty_printer(best_outputs)
# return best iteration of field # TODO REALLY return best iteration of field
return 0 return 0

View File

@ -43,7 +43,6 @@ def local_fitness(field, x, y, plants_case):
if plants_case[x][y] == plants_case[x][y + 1]: if plants_case[x][y] == plants_case[x][y + 1]:
neighbour_bonus += 1 neighbour_bonus += 1
# TODO * multiculture_bonus
local_fitness_value = (soil_value + plant_value) * (0.5 * neighbour_bonus + 1) local_fitness_value = (soil_value + plant_value) * (0.5 * neighbour_bonus + 1)
return local_fitness_value 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. # The fitness function calulates the sum of products between each input and its corresponding weight.
fitness = [] fitness = []
print('LOCAL', len(population_text_local))
for k in range(population_size): for k in range(population_size):
population_values_single = [] population_values_single = []
population_values_single_row = [] population_values_single_row = []
@ -70,16 +68,17 @@ def population_fitness(population_text_local, field, population_size):
return fitness return fitness
def crossover(parents): def crossover(local_parents):
ret = [] ret = []
for i in range(0, len(parents)): for i in range(0, len(local_parents)):
child = copy.deepcopy(parents[i]) child = copy.deepcopy(local_parents[i])
# Vertical randomization # Vertical randomization
width = random.randint(1, D.GSIZE // len(parents)) # width of stripes width = random.randint(1, D.GSIZE // len(local_parents)) # width of stripes
indexes_parents = numpy.random.permutation(range(0, len(parents))) # sorting of stripes indexes_parents = numpy.random.permutation(range(0, len(local_parents))) # sorting of stripes
beginning = random.randint(0, len(parents[0]) - width * len(parents)) # point we start putting the stripes from beginning = random.randint(0, len(local_parents[0]) - width * len(
local_parents)) # point we start putting the stripes from
for x in indexes_parents: 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 beginning += width
ret.append(child) ret.append(child)
return ret return ret