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.