From f56eedb657f7ce77e3fbeae3e78cf01307a1972c Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sat, 16 May 2020 13:51:09 +0200 Subject: [PATCH] Added mating pool selection function --- src/AI/GA.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/AI/GA.py b/src/AI/GA.py index 160d109..5b6bc21 100644 --- a/src/AI/GA.py +++ b/src/AI/GA.py @@ -28,6 +28,27 @@ def geneticAlgorithm(map, iter, solutions): # TODO: Parents selection, mating, offspring + +def selectMatingPool(population, fitness, count): + """ + Pick best players from a population. + + :param population: Entire population pool + :param fitness: Fitnesses coresponding to each player + :param count: Selection count + """ + result = [] + bestIdxs = [] + for i in range(count): + bestIdx = (numpy.where(fitness == numpy.max(fitness)))[0][0] + fitness[bestIdx] = 0 + bestIdxs.append(bestIdx) + for id in bestIdxs: + result.append(population[id]) + print(result) + return result + + def doSimulation(weights, map): """ Runs the simulation. Returns fitness. @@ -46,7 +67,6 @@ def doSimulation(weights, map): player.kill() del player map.respawn() - print(fitness) return fitness