Sztuczna_Inteligencja-projekt/AI/ga_methods.py
Lewy 19680a0139 GA implementation
- DONE best results
- DONE parents selection

with Michał Malinowski
2021-06-21 00:38:56 +02:00

96 lines
3.4 KiB
Python

import numpy
import src.dimensions as D
# Genetic Algorithm methods
def local_fitness(field, x, y, plants_case):
soil_value = 0
if field[x][y].field_type == "soil":
soil_value = 1
else:
soil_value = 0.5
if plants_case[x][y] == "":
plant_value = 0
elif plants_case[x][y] == "w":
plant_value = 1
elif plants_case[x][y] == "p":
plant_value = 2
elif plants_case[x][y] == "s":
plant_value = 3
else:
plant_value = 1
neighbour_bonus = 1
if x - 1 >= 0:
if plants_case[x][y] == plants_case[x - 1][y]:
neighbour_bonus += 1
if x + 1 < D.GSIZE:
if plants_case[x][y] == plants_case[x + 1][y]:
neighbour_bonus += 1
if y - 1 >= 0:
if plants_case[x][y] == plants_case[x][y - 1]:
neighbour_bonus += 1
if y + 1 < D.GSIZE:
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
def population_fitness(population_text, field, population_size):
# 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.
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 = sum(fitness_row)
return fitness
def crossover(parents, offspring_size):
offspring = numpy.empty(offspring_size)
# The point at which crossover takes place between two parents. Usually, it is at the center.
crossover_point = numpy.uint8(offspring_size[1] / 2)
for k in range(offspring_size[0]):
# Index of the first parent to mate.
parent1_idx = k % parents.shape[0]
# Index of the second parent to mate.
parent2_idx = (k + 1) % parents.shape[0]
# The new offspring will have its first half of its genes taken from the first parent.
offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crossover_point]
# The new offspring will have its second half of its genes taken from the second parent.
offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]
return offspring
def mutation(offspring_crossover, num_mutations=1):
mutations_counter = numpy.uint8(offspring_crossover.shape[1] / num_mutations)
# Mutation changes a number of genes as defined by the num_mutations argument. The changes are random.
for idx in range(offspring_crossover.shape[0]):
gene_idx = mutations_counter - 1
for mutation_num in range(num_mutations):
# The random value to be added to the gene.
random_value = numpy.random.uniform(-1.0, 1.0, 1)
offspring_crossover[idx, gene_idx] = offspring_crossover[idx, gene_idx] + random_value
gene_idx = gene_idx + mutations_counter
return offspring_crossover