From ac6a7df4fe6f24f725c619b3be13c2234198270b Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sat, 16 May 2020 14:21:09 +0200 Subject: [PATCH] Added mutation --- src/AI/GA.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/AI/GA.py b/src/AI/GA.py index 941449b..269265c 100644 --- a/src/AI/GA.py +++ b/src/AI/GA.py @@ -29,7 +29,6 @@ def geneticAlgorithm(map, iter, solutions, mutationAmount=0.2): parents = selectMatingPool(population, fitness, int(solutions / 2)) print("Best fitness: {}".format(max(fitness))) offspring = mating(parents, solutions, mutationAmount) - # TODO: Parents selection, mating, offspring def selectMatingPool(population, fitness, count): @@ -66,7 +65,7 @@ def mating(parents, offspringCount, mutationAmount): parent2 = (i + 1) % len(parents) offspring.append(crossover(parents[parent1], parents[parent2])) - # TODO: Add mutation + offspring = mutation(offspring, mutationAmount) return offspring @@ -85,6 +84,13 @@ def crossover(genes1, genes2): return result +def mutation(offspring, mutationAmount): + for player in offspring: + randomGeneIdx = random.randint(len(player)) + player[randomGeneIdx] = player[randomGeneIdx] * random.random() * mutationAmount + return offspring + + def doSimulation(weights, map): """ Runs the simulation. Returns fitness.