GA implementation

- DONE best results
- DONE parents selection

with Michał Malinowski
This commit is contained in:
Lewy 2021-06-21 00:38:56 +02:00
parent 301e05268c
commit 19680a0139
2 changed files with 19 additions and 25 deletions

View File

@ -15,7 +15,7 @@ def genetic_algorithm_setup(field):
population_text = [] population_text = []
population_text_single = [] population_text_single = []
population_size = 3 population_size = 10
# Populate the population_text array # Populate the population_text array
for k in range(population_size): for k in range(population_size):
@ -26,9 +26,9 @@ 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 # # printer
for _ in population_text: # for _ in population_text:
print(population_text) # print(population_text)
""" """
Genetic algorithm parameters: Genetic algorithm parameters:
@ -41,7 +41,9 @@ def genetic_algorithm_setup(field):
best_outputs = [] best_outputs = []
num_generations = 10 num_generations = 10
num_parents = 2
# iterative var
generation = 0 generation = 0
while generation < num_generations: while generation < num_generations:
@ -52,18 +54,24 @@ def genetic_algorithm_setup(field):
# Measuring the fitness of each chromosome in the population. # Measuring the fitness of each chromosome in the population.
# population Fitness # population Fitness
fitness = population_fitness(population_text, field, population_size) fitness = []
for i in range(0, population_size):
fitness.append((i, population_fitness(population_text[i], field, population_size)))
print("Fitness") print("Fitness")
print(fitness) print(fitness)
best_outputs.append(best_Output(new_population)) best = sorted(fitness, key=lambda tup: tup[1])[0:num_parents]
# The best result in the current iteration.
print("Best result : ", best_Output(new_population))
# Leaderboard only
best_outputs.append(best[0][1])
# The best result in the current iteration.
print("Best result : ", best[0])
# TODO METODA WYBORU OSOBNIKA - RANKING
# Selecting the best parents in the population for mating. # Selecting the best parents in the population for mating.
parents = select_mating_pool(new_population, fitness, parents = [population_text[i[0]] for i in best]
num_parents_mating)
print("Parents") print("Parents")
print(parents) print(parents)

View File

@ -24,7 +24,6 @@ def local_fitness(field, x, y, plants_case):
plant_value = 1 plant_value = 1
neighbour_bonus = 1 neighbour_bonus = 1
print(D.GSIZE, x, y)
if x - 1 >= 0: if x - 1 >= 0:
if plants_case[x][y] == plants_case[x - 1][y]: if plants_case[x][y] == plants_case[x - 1][y]:
neighbour_bonus += 1 neighbour_bonus += 1
@ -60,21 +59,10 @@ def population_fitness(population_text, field, population_size):
for i in range(D.GSIZE): for i in range(D.GSIZE):
fitness_row.append(sum(population_values_single[i])) fitness_row.append(sum(population_values_single[i]))
fitness.append(sum(fitness_row)) fitness = sum(fitness_row)
return fitness return fitness
def select_mating_pool(pop, fitness, num_parents):
# Selecting the best individuals in the current generation as parents for producing the offspring of the next generation.
parents = numpy.empty((num_parents, pop.shape[1]))
for parent_num in range(num_parents):
max_fitness_idx = numpy.where(fitness == numpy.max(fitness))
max_fitness_idx = max_fitness_idx[0][0]
parents[parent_num, :] = pop[max_fitness_idx, :]
fitness[max_fitness_idx] = -99999999999
return parents
def crossover(parents, offspring_size): def crossover(parents, offspring_size):
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.
@ -105,5 +93,3 @@ def mutation(offspring_crossover, num_mutations=1):
return offspring_crossover return offspring_crossover
def best_Output(new_population):
return numpy.max(numpy.sum(new_population * equation_inputs, axis=1))