2021-06-21 03:24:07 +02:00
|
|
|
import copy
|
|
|
|
import random
|
|
|
|
|
2021-06-21 01:56:55 +02:00
|
|
|
import matplotlib
|
2021-06-21 04:04:26 +02:00
|
|
|
import matplotlib.pyplot
|
2021-06-20 13:24:12 +02:00
|
|
|
import numpy
|
|
|
|
|
2021-06-20 18:02:11 +02:00
|
|
|
import src.dimensions as D
|
|
|
|
|
|
|
|
|
2021-06-20 13:24:12 +02:00
|
|
|
# Genetic Algorithm methods
|
|
|
|
|
2021-06-20 23:43:57 +02:00
|
|
|
def local_fitness(field, x, y, plants_case):
|
2021-06-20 18:02:11 +02:00
|
|
|
soil_value = 0
|
|
|
|
if field[x][y].field_type == "soil":
|
|
|
|
soil_value = 1
|
|
|
|
else:
|
|
|
|
soil_value = 0.5
|
|
|
|
|
2021-06-20 23:43:57 +02:00
|
|
|
if plants_case[x][y] == "":
|
2021-06-20 18:02:11 +02:00
|
|
|
plant_value = 0
|
2021-06-20 23:43:57 +02:00
|
|
|
elif plants_case[x][y] == "w":
|
2021-06-20 18:02:11 +02:00
|
|
|
plant_value = 1
|
2021-06-20 23:43:57 +02:00
|
|
|
elif plants_case[x][y] == "p":
|
2021-06-20 18:02:11 +02:00
|
|
|
plant_value = 2
|
2021-06-20 23:43:57 +02:00
|
|
|
elif plants_case[x][y] == "s":
|
2021-06-20 18:02:11 +02:00
|
|
|
plant_value = 3
|
2021-06-20 23:43:57 +02:00
|
|
|
else:
|
|
|
|
plant_value = 1
|
2021-06-20 18:02:11 +02:00
|
|
|
|
|
|
|
neighbour_bonus = 1
|
2021-06-21 04:04:26 +02:00
|
|
|
|
2021-06-20 18:02:11 +02:00
|
|
|
if x - 1 >= 0:
|
2021-06-20 23:43:57 +02:00
|
|
|
if plants_case[x][y] == plants_case[x - 1][y]:
|
2021-06-20 18:02:11 +02:00
|
|
|
neighbour_bonus += 1
|
|
|
|
if x + 1 < D.GSIZE:
|
2021-06-20 23:43:57 +02:00
|
|
|
if plants_case[x][y] == plants_case[x + 1][y]:
|
2021-06-20 18:02:11 +02:00
|
|
|
neighbour_bonus += 1
|
|
|
|
if y - 1 >= 0:
|
2021-06-20 23:43:57 +02:00
|
|
|
if plants_case[x][y] == plants_case[x][y - 1]:
|
2021-06-20 18:02:11 +02:00
|
|
|
neighbour_bonus += 1
|
|
|
|
if y + 1 < D.GSIZE:
|
2021-06-20 23:43:57 +02:00
|
|
|
if plants_case[x][y] == plants_case[x][y + 1]:
|
2021-06-20 18:02:11 +02:00
|
|
|
neighbour_bonus += 1
|
2021-06-20 13:24:12 +02:00
|
|
|
|
2021-06-20 18:02:11 +02:00
|
|
|
local_fitness_value = (soil_value + plant_value) * (0.5 * neighbour_bonus + 1)
|
|
|
|
return local_fitness_value
|
|
|
|
|
|
|
|
|
2021-06-21 04:04:26 +02:00
|
|
|
def population_fitness(population_text_local, field, population_size):
|
2021-06-20 13:24:12 +02:00
|
|
|
# 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.
|
2021-06-20 23:43:57 +02:00
|
|
|
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):
|
2021-06-21 04:04:26 +02:00
|
|
|
population_values_single_row.append(local_fitness(field, i, j, population_text_local))
|
2021-06-20 23:43:57 +02:00
|
|
|
population_values_single.append(population_values_single_row)
|
|
|
|
|
|
|
|
for i in range(D.GSIZE):
|
|
|
|
fitness_row.append(sum(population_values_single[i]))
|
2021-06-21 00:38:56 +02:00
|
|
|
fitness = sum(fitness_row)
|
2021-06-20 13:24:12 +02:00
|
|
|
return fitness
|
|
|
|
|
|
|
|
|
2021-06-21 04:43:13 +02:00
|
|
|
def crossover(local_parents):
|
2021-06-21 03:24:07 +02:00
|
|
|
ret = []
|
2021-06-21 04:43:13 +02:00
|
|
|
for i in range(0, len(local_parents)):
|
|
|
|
child = copy.deepcopy(local_parents[i])
|
2021-06-21 03:24:07 +02:00
|
|
|
# Vertical randomization
|
2021-06-21 04:43:13 +02:00
|
|
|
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
|
2021-06-21 03:24:07 +02:00
|
|
|
for x in indexes_parents:
|
2021-06-21 04:43:13 +02:00
|
|
|
child[beginning:beginning + width] = local_parents[x][beginning:beginning + width]
|
2021-06-21 03:24:07 +02:00
|
|
|
beginning += width
|
|
|
|
ret.append(child)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def mutation(population_units, offspring_crossover, num_mutants, num_mutations=10):
|
|
|
|
for case in range(0, len(offspring_crossover)):
|
|
|
|
for mutation in range(0, num_mutations):
|
|
|
|
mutation_x = random.randint(0, D.GSIZE - 1)
|
|
|
|
mutation_y = random.randint(0, D.GSIZE - 1)
|
|
|
|
mutation_value = random.choice(population_units)
|
|
|
|
offspring_crossover[case][mutation_x][mutation_y] = mutation_value
|
|
|
|
num_mutants -= 1
|
|
|
|
|
2021-06-20 13:24:12 +02:00
|
|
|
return offspring_crossover
|
2021-06-20 18:02:11 +02:00
|
|
|
|
|
|
|
|
2021-06-21 01:56:55 +02:00
|
|
|
def pretty_printer(best_outputs):
|
|
|
|
matplotlib.pyplot.plot(best_outputs)
|
|
|
|
matplotlib.pyplot.xlabel("Iteration")
|
|
|
|
matplotlib.pyplot.ylabel("Fitness")
|
|
|
|
matplotlib.pyplot.show()
|