2021-06-21 03:24:07 +02:00
|
|
|
import copy
|
|
|
|
import random
|
|
|
|
|
2021-06-21 01:56:55 +02:00
|
|
|
import matplotlib
|
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 03:24:07 +02:00
|
|
|
print(x, y)
|
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
|
|
|
# TODO * multiculture_bonus
|
|
|
|
local_fitness_value = (soil_value + plant_value) * (0.5 * neighbour_bonus + 1)
|
|
|
|
return local_fitness_value
|
|
|
|
|
|
|
|
|
2021-06-20 23:43:57 +02:00
|
|
|
def population_fitness(population_text, 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):
|
|
|
|
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]))
|
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 03:24:07 +02:00
|
|
|
def crossover(parents):
|
|
|
|
ret = []
|
|
|
|
for i in range(0, len(parents)):
|
|
|
|
child = copy.deepcopy(parents[i])
|
|
|
|
# Vertical randomization
|
|
|
|
width = random.randint(1, D.GSIZE / len(parents)) # width of stripes
|
|
|
|
indexes_parents = numpy.random.permutation(range(0, len(parents))) # sorting of stripes
|
|
|
|
beginning = random.randint(0, len(parents[0]) - width * len(parents)) # point we start putting the stripes from
|
|
|
|
for x in indexes_parents:
|
|
|
|
child[beginning:beginning + width] = parents[x][beginning:beginning + width]
|
|
|
|
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
|
|
|
|
|
|
|
|
while num_mutants > 0:
|
|
|
|
case = random.randint(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()
|