This commit is contained in:
Marcin Kostrzewski 2020-05-16 14:46:02 +02:00
parent ac6a7df4fe
commit 66ac27a6fe

View File

@ -6,13 +6,14 @@ from src.entities.Enums import Classifiers
from src.entities.Player import Player from src.entities.Player import Player
def geneticAlgorithm(map, iter, solutions, mutationAmount=0.2): def geneticAlgorithm(map, iter, solutions, mutationAmount=0.05):
""" """
This algorithm will attempt to find the best affinities for player's goal choices. This algorithm will attempt to find the best affinities for player's goal choices.
:param map: Map with all entities :param map: Map with all entities
:param iter: Generations count :param iter: Generations count
:param solutions: Solutions per generation :param solutions: Solutions per generation
:param mutationAmount: Mutation strength
""" """
# Based on 4 weights, that are affinities tied to the player # Based on 4 weights, that are affinities tied to the player
weightsCount = 4 weightsCount = 4
@ -29,6 +30,7 @@ def geneticAlgorithm(map, iter, solutions, mutationAmount=0.2):
parents = selectMatingPool(population, fitness, int(solutions / 2)) parents = selectMatingPool(population, fitness, int(solutions / 2))
print("Best fitness: {}".format(max(fitness))) print("Best fitness: {}".format(max(fitness)))
offspring = mating(parents, solutions, mutationAmount) offspring = mating(parents, solutions, mutationAmount)
population = offspring
def selectMatingPool(population, fitness, count): def selectMatingPool(population, fitness, count):
@ -86,8 +88,8 @@ def crossover(genes1, genes2):
def mutation(offspring, mutationAmount): def mutation(offspring, mutationAmount):
for player in offspring: for player in offspring:
randomGeneIdx = random.randint(len(player)) randomGeneIdx = random.randrange(0, len(player))
player[randomGeneIdx] = player[randomGeneIdx] * random.random() * mutationAmount player[randomGeneIdx] = player[randomGeneIdx] + random.uniform(-1.0, 1.0) * mutationAmount
return offspring return offspring