Added mutation
This commit is contained in:
parent
e8e36a4420
commit
ac6a7df4fe
10
src/AI/GA.py
10
src/AI/GA.py
@ -29,7 +29,6 @@ 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)
|
||||||
# TODO: Parents selection, mating, offspring
|
|
||||||
|
|
||||||
|
|
||||||
def selectMatingPool(population, fitness, count):
|
def selectMatingPool(population, fitness, count):
|
||||||
@ -66,7 +65,7 @@ def mating(parents, offspringCount, mutationAmount):
|
|||||||
parent2 = (i + 1) % len(parents)
|
parent2 = (i + 1) % len(parents)
|
||||||
offspring.append(crossover(parents[parent1], parents[parent2]))
|
offspring.append(crossover(parents[parent1], parents[parent2]))
|
||||||
|
|
||||||
# TODO: Add mutation
|
offspring = mutation(offspring, mutationAmount)
|
||||||
return offspring
|
return offspring
|
||||||
|
|
||||||
|
|
||||||
@ -85,6 +84,13 @@ def crossover(genes1, genes2):
|
|||||||
return result
|
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):
|
def doSimulation(weights, map):
|
||||||
"""
|
"""
|
||||||
Runs the simulation. Returns fitness.
|
Runs the simulation. Returns fitness.
|
||||||
|
Loading…
Reference in New Issue
Block a user