GA implementation

- DONE fitness function
This commit is contained in:
Lewy 2021-06-20 23:43:57 +02:00
parent 6c905621ca
commit 301e05268c
2 changed files with 43 additions and 28 deletions

View File

@ -13,12 +13,18 @@ def genetic_algorithm_setup(field):
# new_population to be # new_population to be
population_text = [] population_text = []
population_text_single = []
population_size = 3
# Populate the population_text array # Populate the population_text array
for row in range(D.GSIZE): for k in range(population_size):
population_text.append([]) population_text_single = []
for column in range(D.GSIZE): for row in range(D.GSIZE):
population_text[row].append(random.choice(population_units)) population_text_single.append([])
for column in range(D.GSIZE):
population_text_single[row].append(random.choice(population_units))
population_text.append(population_text_single)
# printer # printer
for _ in population_text: for _ in population_text:
@ -31,18 +37,8 @@ def genetic_algorithm_setup(field):
""" """
# units per population in generation # units per population in generation
sol_per_pop = 8
num_parents_mating = 4 num_parents_mating = 4
population_values = []
fitness_row = []
# population Fitness
for i in range(0, D.GSIZE):
for j in range(D.GSIZE):
fitness_row.append(local_fitness(field, i, j, population_text))
population_values.append(fitness_row)
best_outputs = [] best_outputs = []
num_generations = 10 num_generations = 10
@ -55,13 +51,15 @@ def genetic_algorithm_setup(field):
print("Generation : ", generation) print("Generation : ", generation)
# Measuring the fitness of each chromosome in the population. # Measuring the fitness of each chromosome in the population.
fitness = cal_pop_fitness(population_values) # population Fitness
fitness = population_fitness(population_text, field, population_size)
print("Fitness") print("Fitness")
print(fitness) print(fitness)
# best_outputs.append(best_Output(new_population)) best_outputs.append(best_Output(new_population))
# The best result in the current iteration. # The best result in the current iteration.
# print("Best result : ", best_Output(new_population)) print("Best result : ", best_Output(new_population))
# 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 = select_mating_pool(new_population, fitness,

View File

@ -5,34 +5,37 @@ import src.dimensions as D
# Genetic Algorithm methods # Genetic Algorithm methods
def local_fitness(field, x, y, plants): def local_fitness(field, x, y, plants_case):
soil_value = 0 soil_value = 0
if field[x][y].field_type == "soil": if field[x][y].field_type == "soil":
soil_value = 1 soil_value = 1
else: else:
soil_value = 0.5 soil_value = 0.5
if plants[x][y] == "": if plants_case[x][y] == "":
plant_value = 0 plant_value = 0
elif plants[x][y] == "w": elif plants_case[x][y] == "w":
plant_value = 1 plant_value = 1
elif plants[x][y] == "p": elif plants_case[x][y] == "p":
plant_value = 2 plant_value = 2
elif plants[x][y] == "s": elif plants_case[x][y] == "s":
plant_value = 3 plant_value = 3
else:
plant_value = 1
neighbour_bonus = 1 neighbour_bonus = 1
print(D.GSIZE, x, y)
if x - 1 >= 0: if x - 1 >= 0:
if plants[x][y] == plants[x - 1][y]: if plants_case[x][y] == plants_case[x - 1][y]:
neighbour_bonus += 1 neighbour_bonus += 1
if x + 1 < D.GSIZE: if x + 1 < D.GSIZE:
if plants[x][y] == plants[x + 1][y]: if plants_case[x][y] == plants_case[x + 1][y]:
neighbour_bonus += 1 neighbour_bonus += 1
if y - 1 >= 0: if y - 1 >= 0:
if plants[x][y] == plants[x][y - 1]: if plants_case[x][y] == plants_case[x][y - 1]:
neighbour_bonus += 1 neighbour_bonus += 1
if y + 1 < D.GSIZE: if y + 1 < D.GSIZE:
if plants[x][y] == plants[x][y + 1]: if plants_case[x][y] == plants_case[x][y + 1]:
neighbour_bonus += 1 neighbour_bonus += 1
# TODO * multiculture_bonus # TODO * multiculture_bonus
@ -40,10 +43,24 @@ def local_fitness(field, x, y, plants):
return local_fitness_value return local_fitness_value
def cal_pop_fitness(pop): def population_fitness(population_text, field, population_size):
# Calculating the fitness value of each solution in the current population. # Calculating the fitness value of each solution in the current population.
# 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 = sum(map(sum, pop)) fitness = []
for k in range(population_size):
population_values_single = []
population_values_single_row = []
fitness_row = []
for i in range(0, D.GSIZE):
for j in range(0, D.GSIZE):
population_values_single_row.append(local_fitness(field, i, j, population_text))
population_values_single.append(population_values_single_row)
for i in range(D.GSIZE):
fitness_row.append(sum(population_values_single[i]))
fitness.append(sum(fitness_row))
return fitness return fitness