def geneticAlgorithm(populationSize, maxCost, nbFights, **kwargs): if 'initChampions' in kwargs: population = kwargs['initChampions'] population += [createRandomShip(maxCost) for _ in range(populationSize-len(kwargs['initChampions']))] else: population = [createRandomShip(maxCost) for _ in range(populationSize)] tournamentResult = tournament(population, nbFights) nbChampions = 2 if 'nbChampions' in kwargs: nbChampions = kwargs['nbChampions'] nbGeneration = 2 if 'nbGeneration' in kwargs: nbChampions = kwargs['nbGeneration'] nbMutations = 2 if 'nbMutations' in kwargs: nbChampions = kwargs['nbMutations'] for nbG in range(nbGeneration): # Determine the parents champions = [(population[i], tournamentResult[population[i]]) for i in range(len(population[:nbChampions]))] for shipResult in tournamentResult: mini = min([result[1] for result in champions]) if tournamentResult[shipResult] > mini: for indexR in range(len(champions)): if champions[indexR] == mini: champions[indexR] = (shipResult, tournamentResult[shipResult]) break # Create new population with crossovers of the parents for shipD in population: if shipD not in [champ[0] for champ in champions]: deleteShip(shipD) population = [shipC[0] for shipC in champions] for cantFindName in range(populationSize-len(champions)): parent1 = randrange(nbChampions) # We'll assume that there's at least 2 champions parent2 = randrange(nbChampions) while parent2 == parent1: parent2 = randrange(nbChampions) population += [crossover(population[parent1], population[parent2], maxCost)] # Mutation for nbM in range(nbMutations): population[randrange(len(population))] = mutation(population[randrange(len(population))], maxCost) # Determine the fitness of the population tournamentResult = tournament(population, nbFights) bestIndividual = population[0] for key in tournamentResult: if tournamentResult[key] > tournamentResult[bestIndividual]: bestIndividual = key return bestIndividual
async def tournament(ctx, *args): global currentTournaments if len(args) == 0: if len(currentTournaments) == 0: currentTournaments = tournament() else: ctx.send("There is always a game night") else: if args[0] == "clear": if int(args[1]).isDigit(): try: currentTournaments.remove(int(args[1]) - 1) except IndexError: ctx.send("Index out of bounds") elif args[1] == "all": currentTournaments = None else: ctx.send(f"Cannot clear {args[1]}") elif len(args) > 4: del args[4:] #truncate args newTournament = tournament.tournament(args) currentTournaments.append(newTournament) ctx.send(newTournament.getAnnouncement()) else: newGamesNight = gamesnight.gamesnight(args) currentTournaments.append(newGamesNight) ctx.send(newGamesNight.getAnnouncement())
def autoStopGeneticAlgorithm(populationSize, maxCost, nbFights, **kwargs): """ Stops when then champions aren't really differents from the rest of the population. """ champions = [] oldChampions = [] minGenerations = 5 if keepMemoryOfShipsGAlgo: ls = os.listdir() nameF = 'shipsGAlgo' while nameF in ls: nameF += '2' resMemory = '' def handler(signum, _): if signum == signal.SIGTERM: print('\033[32m'+str(champions)+'\033[0m') signal.signal(signal.SIGTERM, handler) def muchDifferences(championsL): victoriesPoints = [ch[1] for ch in championsL] for x in victoriesPoints: if x > (populationSize-1) * (2/3): return True return False if 'initChampions' in kwargs: population = kwargs['initChampions'] population += [createRandomShip(maxCost) for _ in range(populationSize-len(kwargs['initChampions']))] else: population = [createRandomShip(maxCost) for _ in range(populationSize)] tournamentResult = tournament(population, nbFights) nbChampions = 2 if 'nbChampions' in kwargs: nbChampions = kwargs['nbChampions'] nbMutations = 2 if 'nbMutations' in kwargs: nbChampions = kwargs['nbMutations'] nbGenerations = 0 if keepMemoryOfShipsGAlgo: for shipM in population: resMemory += shipM + ' ' resMemory += '\n' while (minGenerations > nbGenerations or muchDifferences(champions)) and nbGenerations < 30: # Determine the parents champions = [(population[i], tournamentResult[population[i]]) for i in range(len(population[:nbChampions]))] for shipResult in tournamentResult: mini = min([result[1] for result in champions]) if tournamentResult[shipResult] > mini: for indexR in range(len(champions)): if champions[indexR] == mini: champions[indexR] = (shipResult, tournamentResult[shipResult]) break oldChampions += [c[0] for c in champions if c[0] not in oldChampions] # Create new population with crossovers of the parents # for shipD in population: # if shipD not in [champ[0] for champ in champions]: # deleteShip(shipD) population = [shipC[0] for shipC in champions] for cantFindName in range(populationSize-len(champions)): parent1 = randrange(nbChampions) # We'll assume that there's at least 2 champions parent2 = randrange(nbChampions) while parent2 == parent1: parent2 = randrange(nbChampions) population += [crossover(population[parent1], population[parent2], maxCost)] # Mutation for nbM in range(nbMutations): population[randrange(len(population)-nbChampions)+nbChampions] = mutation(population[randrange(len(population))], maxCost) # Determine the fitness of the population tournamentResult = tournament(population, nbFights) nbGenerations += 1 if keepMemoryOfShipsGAlgo: for shipM in population: resMemory += shipM + ' ' resMemory += '\n' if keepMemoryOfShipsGAlgo: with open(nameF, 'w') as keepMemory: keepMemory.write(resMemory) bestIndividual = population[0] for key in tournamentResult: if tournamentResult[key] > tournamentResult[bestIndividual]: bestIndividual = key print('\033[32m'+'Best individual : '+bestIndividual+'\033[0m') champions = [(population[i], tournamentResult[population[i]]) for i in range(len(population[:nbChampions]))] for shipResult in tournamentResult: mini = min([result[1] for result in champions]) if tournamentResult[shipResult] > mini: for indexR in range(len(champions)): if champions[indexR][1] == mini: champions[indexR] = (shipResult, tournamentResult[shipResult]) break oldChampions += [c[0] for c in champions if c[0] not in oldChampions] resChamps = [bestIndividual] + [champs[0] for champs in champions if champs != bestIndividual] return resChamps, [badShip for badShip in population if badShip not in resChamps], oldChampions