93 lines
3.4 KiB
Python
93 lines
3.4 KiB
Python
import numpy
|
|
|
|
import src.dimensions as D
|
|
|
|
|
|
# Genetic Algorithm methods
|
|
|
|
def local_fitness(field, x, y, plants):
|
|
soil_value = 0
|
|
if field[x][y].field_type == "soil":
|
|
soil_value = 1
|
|
else:
|
|
soil_value = 0.5
|
|
|
|
if plants[x][y] == "":
|
|
plant_value = 0
|
|
elif plants[x][y] == "w":
|
|
plant_value = 1
|
|
elif plants[x][y] == "p":
|
|
plant_value = 2
|
|
elif plants[x][y] == "s":
|
|
plant_value = 3
|
|
|
|
neighbour_bonus = 1
|
|
if x - 1 >= 0:
|
|
if plants[x][y] == plants[x - 1][y]:
|
|
neighbour_bonus += 1
|
|
if x + 1 < D.GSIZE:
|
|
if plants[x][y] == plants[x + 1][y]:
|
|
neighbour_bonus += 1
|
|
if y - 1 >= 0:
|
|
if plants[x][y] == plants[x][y - 1]:
|
|
neighbour_bonus += 1
|
|
if y + 1 < D.GSIZE:
|
|
if plants[x][y] == plants[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 cal_pop_fitness(pop):
|
|
# 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 = sum(map(sum, pop))
|
|
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):
|
|
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
|
|
|
|
|
|
def best_Output(new_population):
|
|
return numpy.max(numpy.sum(new_population * equation_inputs, axis=1))
|