pydfs-lineup-optimizer icon indicating copy to clipboard operation
pydfs-lineup-optimizer copied to clipboard

Error When Setting Max Exposure To 0

Open OzzyDFS opened this issue 4 years ago • 4 comments

Hello, I am optimizing lineups for tonight's NBA slate and I wanted to set max exposure for a particular player to zero, in effect removing him from my player pool. However, I get a Key Error for that player when attempting to do so. I don't want to set the projection to zero because my projections column has a formula. Maybe this is a bug in the code?

KeyError Traceback (most recent call last)

----> 7 for lineup in optimizer.optimize(10): 8 print(lineup) 9 #optimizer.export('entries.csv')

~/python/site-packages/pydfs_lineup_optimizer/lineup_optimizer.py in optimize(self, n, max_exposure, randomness, with_injured, exposure_strategy) 375 constraints = [constraint(self, players_dict, context) for constraint in rules] 376 for constraint in constraints: --> 377 constraint.apply(base_solver) 378 previous_lineup = None 379 for _ in range(n):

~/python/site-packages/pydfs_lineup_optimizer/rules.py in apply(self, solver) 561 variable = solver.add_variable('total_game_%s_%s' % (game.home_team, game.away_team)) 562 game_variables.append(variable) --> 563 variables = [self.players_dict[player] for player in game_players] 564 solver.add_constraint(variables, None, SolverSign.LTE, variable * total_players) 565 solver.add_constraint(variables, None, SolverSign.GTE, variable)

~/python3.8/site-packages/pydfs_lineup_optimizer/rules.py in (.0) 561 variable = solver.add_variable('total_game_%s_%s' % (game.home_team, game.away_team)) 562 game_variables.append(variable) --> 563 variables = [self.players_dict[player] for player in game_players] 564 solver.add_constraint(variables, None, SolverSign.LTE, variable * total_players) 565 solver.add_constraint(variables, None, SolverSign.GTE, variable)

OzzyDFS avatar Feb 08 '21 21:02 OzzyDFS

I'm not exactly sure how to set max exposure to 0 but would the drop feature do the trick? It's what I use to exclude players from my player pool.

drop = optimizer.get_player_by_name('Player Name') optimizer.remove_player(drop)

McCampy avatar Feb 10 '21 05:02 McCampy

I'm having the same issue. Works fine with FanDuel, but getting that key error in DraftKings.

chitown88 avatar Feb 23 '21 11:02 chitown88

Find the player with get_player_by_id and then use optimizer.remove_player If you want to remove them all in one fell swoop, then you could use the following:

for p in optimizer.players:
    if p.max_exposure == 0:
        optimizer.remove_player(p)

This method has the benefit of not needing to find a player by name or id, you can just pass the player object itself to remove_player when max_exposure is zero.

sansbacon avatar Mar 24 '21 05:03 sansbacon

Find the player with get_player_by_id and then use optimizer.remove_player If you want to remove them all in one fell swoop, then you could use the following:

for p in optimizer.players:
    if p.max_exposure == 0:
        optimizer.remove_player(p)

This method has the benefit of not needing to find a player by name or id, you can just pass the player object itself to remove_player when max_exposure is zero.

Hmm maybe i did something wrong, but that didn't seem to work for me. I set max_exposure to 0 and then copy and pasted your code as written. Still got 100% of the player.

OzzyDFS avatar Mar 25 '21 00:03 OzzyDFS