GA FIX
- FIX bug where parents would not go to next gen with Michał Malinowski
This commit is contained in:
parent
361a733102
commit
5ca916873d
@ -24,10 +24,6 @@ def genetic_algorithm_setup(field):
|
||||
population_text_single[row].append(random.choice(population_units))
|
||||
population_text.append(population_text_single)
|
||||
|
||||
# # printer
|
||||
# for _ in population_text:
|
||||
# print(population_text)
|
||||
|
||||
"""
|
||||
Genetic algorithm parameters:
|
||||
Mating pool size
|
||||
@ -35,8 +31,6 @@ def genetic_algorithm_setup(field):
|
||||
"""
|
||||
|
||||
# units per population in generation
|
||||
num_parents_mating = 4
|
||||
|
||||
best_outputs = []
|
||||
num_generations = 10
|
||||
num_parents = 4
|
||||
@ -56,13 +50,12 @@ def genetic_algorithm_setup(field):
|
||||
fitness = []
|
||||
|
||||
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)))
|
||||
|
||||
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
|
||||
best_outputs.append(best[0][1])
|
||||
@ -72,12 +65,14 @@ def genetic_algorithm_setup(field):
|
||||
|
||||
# TODO METODA WYBORU OSOBNIKA - RANKING
|
||||
# Selecting the best parents in the population for mating.
|
||||
print(best)
|
||||
parents = [population_text[i[0]] for i in best]
|
||||
parents_copy = copy.deepcopy(parents)
|
||||
print("Parents")
|
||||
for i in range(0, len(parents)):
|
||||
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
|
||||
for row in parents[i]]))
|
||||
print("")
|
||||
# for i in range(0, len(parents)):
|
||||
# print('\n'.join([''.join(['{:4}'.format(item) for item in row])
|
||||
# for row in parents[i]]))
|
||||
# print("")
|
||||
|
||||
# Generating next generation using crossover.
|
||||
offspring_x = random.randint(1, D.GSIZE - 2)
|
||||
@ -86,26 +81,26 @@ def genetic_algorithm_setup(field):
|
||||
# TODO OPERATOR KRZYŻOWANIA
|
||||
offspring_crossover = crossover(parents)
|
||||
print("Crossover")
|
||||
for i in range(0, len(offspring_crossover)):
|
||||
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
|
||||
for row in offspring_crossover[i]]))
|
||||
print("")
|
||||
# for i in range(0, len(offspring_crossover)):
|
||||
# print('\n'.join([''.join(['{:4}'.format(item) for item in row])
|
||||
# for row in offspring_crossover[i]]))
|
||||
# print("")
|
||||
|
||||
# TODO OPERATOR MUTACJI
|
||||
offspring_mutation = mutation(population_units, offspring_crossover, population_size - num_parents,
|
||||
num_mutations=10)
|
||||
print("Mutation")
|
||||
for i in range(0, len(offspring_mutation)):
|
||||
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
|
||||
for row in offspring_mutation[i]]))
|
||||
print("")
|
||||
# for i in range(0, len(offspring_mutation)):
|
||||
# print('\n'.join([''.join(['{:4}'.format(item) for item in row])
|
||||
# for row in offspring_mutation[i]]))
|
||||
# print("")
|
||||
|
||||
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]]
|
||||
# Creating next generation
|
||||
population_text = []
|
||||
for k in range(0, len(parents)):
|
||||
population_text.append(parents[k])
|
||||
for k in parents_copy:
|
||||
population_text.append(k)
|
||||
for k in range(0, len(offspring_mutation)):
|
||||
population_text.append(offspring_mutation[k])
|
||||
while len(population_text) < population_size:
|
||||
@ -113,11 +108,9 @@ def genetic_algorithm_setup(field):
|
||||
population_text.append(population_text_copy[x])
|
||||
unused_indexes.remove(x)
|
||||
|
||||
print("END LEN", len(population_text))
|
||||
|
||||
# TODO WARUNEK STOPU
|
||||
stop = 0
|
||||
if generation > 3:
|
||||
if generation > 10:
|
||||
if best_outputs[-1] / best_outputs[-2] < 1.05:
|
||||
stop += 1
|
||||
if best_outputs[-1] / best_outputs[-3] < 1.05:
|
||||
@ -142,7 +135,7 @@ def genetic_algorithm_setup(field):
|
||||
|
||||
pretty_printer(best_outputs)
|
||||
|
||||
# return best iteration of field
|
||||
# TODO REALLY return best iteration of field
|
||||
return 0
|
||||
|
||||
|
||||
|
@ -43,7 +43,6 @@ def local_fitness(field, x, y, plants_case):
|
||||
if plants_case[x][y] == plants_case[x][y + 1]:
|
||||
neighbour_bonus += 1
|
||||
|
||||
# TODO * multiculture_bonus
|
||||
local_fitness_value = (soil_value + plant_value) * (0.5 * neighbour_bonus + 1)
|
||||
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.
|
||||
fitness = []
|
||||
|
||||
print('LOCAL', len(population_text_local))
|
||||
for k in range(population_size):
|
||||
population_values_single = []
|
||||
population_values_single_row = []
|
||||
@ -70,16 +68,17 @@ def population_fitness(population_text_local, field, population_size):
|
||||
return fitness
|
||||
|
||||
|
||||
def crossover(parents):
|
||||
def crossover(local_parents):
|
||||
ret = []
|
||||
for i in range(0, len(parents)):
|
||||
child = copy.deepcopy(parents[i])
|
||||
for i in range(0, len(local_parents)):
|
||||
child = copy.deepcopy(local_parents[i])
|
||||
# Vertical randomization
|
||||
width = random.randint(1, D.GSIZE // len(parents)) # width of stripes
|
||||
indexes_parents = numpy.random.permutation(range(0, len(parents))) # sorting of stripes
|
||||
beginning = random.randint(0, len(parents[0]) - width * len(parents)) # point we start putting the stripes from
|
||||
width = random.randint(1, D.GSIZE // len(local_parents)) # width of stripes
|
||||
indexes_parents = numpy.random.permutation(range(0, len(local_parents))) # sorting of stripes
|
||||
beginning = random.randint(0, len(local_parents[0]) - width * len(
|
||||
local_parents)) # point we start putting the stripes from
|
||||
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
|
||||
ret.append(child)
|
||||
return ret
|
||||
|
Loading…
Reference in New Issue
Block a user