Ejemplo n.º 1
0
def run(
             BenchmarkTeam,
             StatsToMaximize,
             TeamsToInclude=[x for x in range(1,13)],
             Population=1000,
             Generations=60,
             PlayersDictionary = team.readPickle('players'),
             NumbersPerPositionAsList = [5,5,5,6,3],
             TimeFrame = 'stats_lastmonth'
            ):
   playersDic = PlayersDictionary
   rwList = team.makePositionList('RW',playersDic,TeamsToInclude)
   lwList = team.makePositionList('LW',playersDic,TeamsToInclude)
   cList = team.makePositionList('C',playersDic,TeamsToInclude)
   dList = team.makePositionList('D',playersDic,TeamsToInclude)

   creator.create("FitnessMax", base.Fitness, weights=(1.0,) )
   #creator.create("FitnessMax", base.Fitness, weights=tuple( [x/10 for x in range(10,(10-len(StatsToMaximize)),-1)] ) )
   creator.create("Team", list, fitness=creator.FitnessMax)

   toolbox = base.Toolbox()
   toolbox.register("genteam", generateTeam, c=cList, rw=rwList, lw=lwList, d=dList, npos=NumbersPerPositionAsList)
   toolbox.register("population", tools.initRepeat, list, toolbox.genteam)
   toolbox.register("evaluate", evalTeamFitness, BT=BenchmarkTeam, stats=StatsToMaximize, p=playersDic, time=TimeFrame)
   toolbox.register("mate", tools.cxTwoPoint)
   toolbox.register("mutate", mutateTeam, indpb=0.05, c=cList, rw=rwList, lw=lwList, d=dList, npos=NumbersPerPositionAsList)
   #toolbox.register("select", tools.selNSGA2)
   toolbox.register("select", tools.selTournament, tournsize=10)
   toolbox.decorate("mate", checkCrossover(c=cList, rw=rwList, lw=lwList, d=dList, npos=NumbersPerPositionAsList))

   random.seed()
   pop = toolbox.population(n=Population)
   hof = tools.HallOfFame(1)
   algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=Generations,  halloffame=hof, verbose=True)

   return( hof[0] , hof[0].fitness.values )
import random
import numpy
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
import team

playersDic = team.readPickle('players')
teamsToInclude=[0,6]
rwList = team.makePositionList('RW',playersDic,teamsToInclude)
lwList = team.makePositionList('LW',playersDic,teamsToInclude)
cList = team.makePositionList('C',playersDic,teamsToInclude)
dList = team.makePositionList('D',playersDic,teamsToInclude)
statsToMaximize = [14,16,1,2,31,32]

#Define Player and Player's Fitness
creator.create("FitnessMax", base.Fitness, weights=tuple([1.0]*len(statsToMaximize)) )
creator.create("Team", list, fitness=creator.FitnessMax)

#Register Player Operations
def generateTeam(c,rw,lw,d):
   return creator.Team(team.makeRandomTeam(c,rw,lw,d))

def evalTeamFitness(individual):
   listOfPlayerStatsForIndividual = [[playersDic[player]['stats_lastmonth'][statid] for statid in statsToMaximize]for player in individual]
   combinedStatsForIndividual = tuple([sum(totalstats) for totalstats in zip(*listOfPlayerStatsForIndividual)])
   return combinedStatsForIndividual

toolbox = base.Toolbox()
toolbox.register("genteam", generateTeam, c=cList, rw=rwList, lw=lwList, d=dList)