DimaKudosh / pydfs-lineup-optimizer

Daily Fantasy Sports lineup optimzer for all popular daily fantasy sports sites
MIT License
418 stars 156 forks source link

Python noob struggling with stacking #310

Open Uncle6Pack opened 2 years ago

Uncle6Pack commented 2 years ago

DataSci student, python noob here.

I can get this code to work, for the most part. When I start trying to add the stacking feature, it breaks.

I can get this one to work, but this seems to be the only one and it keeps using WAS: optimizer.add_stack(PositionsStack(['QB', 'WR'], for_teams=['GB', 'KC', 'WAS'])) # stack QB and WR for one of provided teams

Also, max exposure breaks it as well: lineups = optimizer.optimize(n=20, max_exposure=.5)

TYIA.

pip install pydfs-lineup-optimizer

from pydfs_lineup_optimizer import get_optimizer, Site, Sport

from pydfs_lineup_optimizer.stacks import TeamStack, PositionsStack, PlayersGroup, GameStack from pydfs_lineup_optimizer import AfterEachExposureStrategy

optimizer = get_optimizer(Site.DRAFTKINGS, Sport.FOOTBALL)

playerlist = "DKsalaries.csv"

optimizer.load_players_from_csv("DKSalaries.csv")

optimizer.add_stack(TeamStack(2))

optimizer.add_stack(TeamStack(3, for_teams=["GB"])) # stack 3 players from any of specified teams

optimizer.add_stack(GameStack(3))

optimizer.add_stack(PositionsStack(['QB', 'WR'], max_exposure=0.5)) # stack QB and WR with 0.5 exposure for all team stacks

optimizer.add_stack(PositionsStack(['QB', ('WR', 'TE')])) # stack QB and WR or TE from same team

optimizer.add_stack(PositionsStack(['QB', 'WR'], for_teams=['GB', 'KC', 'WAS'])) # stack QB and WR for one of provided teams

optimizer.add_stack(GameStack(5, min_from_team=2))

rodgers_adams_group = PlayersGroup(optimizer.player_pool.get_players('Aaron Rodgers', 'Randall Cobb'))

group = PlayersGroup(optimizer.player_pool.get_players('Aaron Rodgers', 'Randall Cobb'))

optimizer.add_players_group(group)

lineups = optimizer.optimize(n=20)

lineups

for lineup in lineups: print(lineup) print(lineup.players) print(lineup.fantasy_points_projection) print(lineup.salary_costs)


SolverInfeasibleSolutionException Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/pydfs_lineup_optimizer/lineup_optimizer.py in optimize(self, n, max_exposure, randomness, with_injured, exposure_strategy, exclude_lineups) 416 try: --> 417 solved_variables = solver.solve() 418 lineup_players = []

2 frames SolverInfeasibleSolutionException: ['_C3', '_C5', '_C8', '_C17', '_C26', '_C40', '_C52', '_C81', '_C82', '_C87', '_C125', '_C126', '_C127', '_C128', '_C129', '_C130', '_C131', '_C132', '_C133', '_C134', '_C135', '_C136', '_C137', '_C138', '_C139', '_C140', '_C141', '_C142', '_C143', '_C144', '_C145', '_C146', '_C147', '_C148', '_C149', '_C150', '_C151', '_C152', '_C153', '_C154', '_C212', '_C213', '_C214', '_C215', '_C216', '_C217', '_C218', '_C219', '_C220', '_C221', '_C222', '_C223', '_C224', '_C225', '_C226', '_C227', '_C228', '_C231', '_C235', '_C237', '_C238', '_C240']

During handling of the above exception, another exception occurred:

GenerateLineupException Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/pydfs_lineup_optimizer/lineup_optimizer.py in optimize(self, n, max_exposure, randomness, with_injured, exposure_strategy, exclude_lineups) 433 constraint.post_optimize(variables_names) 434 except SolverInfeasibleSolutionException as solver_exception: --> 435 raise GenerateLineupException(solver_exception.get_user_defined_constraints()) 436 self.last_context = context 437

GenerateLineupException: Can't generate lineups. DKSalaries.csv

DimaKudosh commented 2 years ago

I can't reproduce error with PositionsStack and GameStack from your sample. This error happened when solver can't solve problem with used constraints, for example you add stack with QB for GB but removed Rodgers from optimizer or any other discrepancy.