DimaKudosh / pydfs-lineup-optimizer

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

Creating 4-3 Stacks in MLB with Fanduel #257

Open lightninglarry opened 3 years ago

lightninglarry commented 3 years ago

Can anyone chime in on what im doing incorrectly? Heres the error: SolverInfeasibleSolutionException Traceback (most recent call last) ~\anaconda3\lib\site-packages\pydfs_lineup_optimizer\lineup_optimizer.py in optimize(self, n, max_exposure, randomness, with_injured, exposure_strategy) 383 try: --> 384 solved_variables = solver.solve() 385 lineup_players = []

~\anaconda3\lib\site-packages\pydfs_lineup_optimizer\solvers\pulp_solver.py in solve(self) 55 invalid_constraints = [name for name, constraint in self.prob.constraints.items() if not constraint.valid()] ---> 56 raise SolverInfeasibleSolutionException(invalid_constraints)

SolverInfeasibleSolutionException: ['_C393', '_C507', '_C511', 'min_teams', 'total_players', 'budget']

During handling of the above exception, another exception occurred:

GenerateLineupException Traceback (most recent call last)

in 32 lineups = optimizer.optimize(n=10, max_exposure=.4) 33 optimizer.set_min_salary_cap(34700) ---> 34 for lineup in lineups: 35 print(lineup) 36 optimizer.export(r"C:\Users\brand\Desktop\MLB\FD.csv") ~\anaconda3\lib\site-packages\pydfs_lineup_optimizer\lineup_optimizer.py in optimize(self, n, max_exposure, randomness, with_injured, exposure_strategy) 399 constraint.post_optimize(variables_names) 400 except SolverInfeasibleSolutionException as solver_exception: --> 401 raise GenerateLineupException(solver_exception.get_user_defined_constraints()) 402 self.last_context = context 403 GenerateLineupException: Can't generate lineups. Following constraints are not valid: min_teams,total_players,budget heres the code creating it: **from** pydfs_lineup_optimizer import get_optimizer, Site, Sport, CSVLineupExporter from pydfs_lineup_optimizer.stacks import TeamStack, PositionsStack, PlayersGroup from pydfs_lineup_optimizer import AfterEachExposureStrategy optimizer = get_optimizer(Site.FANDUEL, Sport.BASEBALL) optimizer.load_players_from_csv(r"C:\Users\brand\Downloads\FanDuel-MLB-2021 ET-04 ET-01 ET-56054-players-list.csv") optimizer.restrict_positions_for_opposing_team(['P'], ['C', 'SS', 'OF', '1B', '2B', '3B']) optimizer.add_stack(TeamStack(4, for_teams=['DET', 'PHI', 'NYY', 'CHC'])) # stack 4 players from any of specified teams optimizer.add_stack(TeamStack(3, spacing=2)) # stack 3 players close to each other in range of 2 spots.** devers_jd_xander = PlayersGroup([optimizer.get_player_by_name(Nickname) for Nickname in ('Rafael Devers', 'J.D. Martinez', 'Xander Boegarts')], max_exposure=0.5) optimizer.add_players_group(devers_jd_xander) bieber = optimizer.get_player_by_name("Shane Bieber") optimizer.add_player_to_lineup(bieber) bieber.max_exposure = .5 #cole = optimizer.get_player_by_name("Gerrit Cole") #optimizer.add_player_to_lineup(cole) #cole.max_exposure = .1 #darvish = optimizer.get_player_by_name("Yu Darvish") #optimizer.add_player_to_lineup(darvish) #darvish.max_exposure = .2 #giolito = optimizer.get_player_by_name("Lucas Giolito") #optimizer.add_player_to_lineup(giolito) #giolito.max_exposure = .1 #scherzer = optimizer.get_player_by_name("Max Scherzer") #optimizer.add_player_to_lineup(scherzer) #scherzer.max_exposure = .1 lineups = optimizer.optimize(n=10, max_exposure=.4) optimizer.set_min_salary_cap(34700) for lineup in lineups: print(lineup) optimizer.export(r"C:\Users\brand\Desktop\MLB\FD.csv") import pandas as pd df= pd.read_csv(r"C:\Users\brand\Desktop\MLB\FD.csv") Changing my stack to a 3-3 works, or a 4-2 works, but not 4-3, which still meets the roster requirement of 4 different teams if my Pitcher is a diff player. Wanting 4 man, 3 man, 1 off, and my P type lineups. also a secondary Q. I had to comment my pitchers out as it didnt like have all the exposures like that. Is there a way to do that?
bigboyz commented 3 years ago

I'm having the same issues as you. I have seen that setting multiple max or min exposure to over 5-6 players that compete for the same position usually gives a Unable To Solve error. I run into this setting lineups and doing the same wheel of exposures over defense often where I really don't have a favorite. I tried TotalExposureStrategy, AfterExposureStrategy. I went through the code line by line trying to see where setting too many min or max might toss the error. I just can't figure it out. Just seems to be no way to do more or five min or max for any single player. I find min.exposure to be more picky than max where I often can get only 3-4 and never try to total them up to 100% as you did above. I get the best luck when I have 20% or so leftover.

As far as the stacks, I also had the exact error today on FanDuel stacking. Seems like anything that effects 7 players is a fail. It's solvable and I can come up with my own lineups that meet all requirements, but getting the error as you.

Such as huge part of baseball so I haven't given up searching for a fix. Hoping its all related to some constraint that we can modify. I'll continue to update if I find anything, but if anyone else out there can pinpoint what causes this to fail, it would be greatly appreciated!

bigboyz commented 3 years ago

Hey, @lightninglarry! We just got an update that fixed a position bug with FanDuel. Since that update my 4-3 stacks and any other stacks I attempt work fine. I am certain that position bug is what affected the stack bug for FanDuel baseball. I think you should update PYDFS and try again and I bet you you'll see it work.

lightninglarry commented 3 years ago

will try today my man. Thanks @bigboyz

Owen-Slater commented 3 years ago

I also can confirm that the update fixed these errors.

lightninglarry commented 3 years ago

Thanks guys. Still having issues setting exposure to more than 1 Pitcher on a slate, LineupOptimizerException: You're already select all P's

what are your thoughts on this?

bigboyz commented 3 years ago

Personally, what I do is use the min and max exposure ability on the CSV file. Then just put .30 Cole or whatever. Sounds like the error is FanDuel and you locked a pitcher, but than tried to add another even though 1 is limit.

lightninglarry commented 3 years ago

@bigboyz how do i message you on here. Id like to learn more if you got time?
feel free to email me larry.mcdonald002@gmail.com.

lightninglarry commented 3 years ago

LineupOptimizerException: Unable to build lineup. was working for a tad.

Owen-Slater commented 3 years ago

@lightninglarry Hi ,so I partially retract my statement regarding the original issue. It is fixed with the update, but it looks like when you create no Pitcher vs Batters optimizer.restrict_positions_for_opposing_team(['P'], ['C', 'SS', 'OF', '1B', '2B', '3B']) there may sometimes be an issue. I'm not 100% sure though. Try removing this constraint if you have it and let me know if it works.

EdCarGoes commented 3 years ago

LineupOptimizerException: Unable to build lineup. was working for a tad.

Can also confirm the issue seemed to be fixed for a day or two. Then I think a second update was made and the issue seems to be back. I have a very large pool of players and right now only 23 lineups are being generated before it throws the "unable to build lineup" error.

lightninglarry commented 3 years ago

still issue comes up. I was able to create 7 lineups before unable to build lineup occurred.

DimaKudosh commented 3 years ago

How do you run optimizer? What stacks do you use? I tried to generate 150 lineups with 4-3 team stacks for today's contest and it didn't throw me error

lightninglarry commented 3 years ago

@DimaKudosh, ive tried just to leave it empty and let projections dictate my stacks. Tried tonight and it spit out 11 until it faulted out.

from pydfs_lineup_optimizer import get_optimizer, Site, Sport, CSVLineupExporter from pydfs_lineup_optimizer.stacks import TeamStack, PositionsStack, PlayersGroup from pydfs_lineup_optimizer import AfterEachExposureStrategy

optimizer = get_optimizer(Site.FANDUEL, Sport.BASEBALL)

optimizer.load_players_from_csv(r"C:\Users\brand\Downloads\FanDuel-MLB-2021 ET-04 ET-19 ET-57413-players-list.csv")

optimizer.restrict_positions_for_opposing_team(['P'], ['C', 'SS', 'OF', '1B', '2B', '3B']) optimizer.add_stack(TeamStack(4, spacing=1, max_exposure=0.3)) optimizer.add_stack(TeamStack(3, spacing=1, max_exposure=0.3))

lineups = optimizer.optimize(n=150, max_exposure=.5, exposure_strategy = AfterEachExposureStrategy) optimizer.set_min_salary_cap(34200) optimizer.set_max_repeating_players(7) for lineup in lineups: print(lineup)

bigboyz commented 3 years ago

I noticed you are using spacing=1. I have 57413 as I played that slate today, but can you post the exact file you used? I want to compare it to mine. I noticed when I download a file early the Batting Order is omitted from many teams. If your copy did not have it for all teams it may very well be unsolvable with those constraints.

lightninglarry commented 3 years ago

no, on that i waited till after lock to use the file just to test it out. All lineups were in.

Owen-Slater commented 3 years ago

@lightninglarry @bigboyz is it possible that the dual positions on FD are creating this issue? I haven't investigated it in the script yet, but I noticed I do miss some stacks that should be generated. So for instance if you have a 4 stack, spacing =1 for ['C', '2B', 'OF', '1B', '3B', 'SS'] ..... and the only way you can achieve a spacing of 1 is by using a player designated as 3B/1B or 2B/SS ... then this will throw an error (?) or stop the optimization? ... Not sure here, just throwing out a suggestion. I know that this issue was fixed in an update, but maybe the spacing needs to be addressed. Again, I haven't gone through the script yet to verify this.

lightninglarry commented 3 years ago

@lightninglarry @bigboyz is it possible that the dual positions on FD are creating this issue? I haven't investigated it in the script yet, but I noticed I do miss some stacks that should be generated. So for instance if you have a 4 stack, spacing =1 for ['C', '2B', 'OF', '1B', '3B', 'SS'] ..... and the only way you can achieve a spacing of 1 is by using a player designated as 3B/1B or 2B/SS ... then this will throw an error (?) or stop the optimization? ... Not sure here, just throwing out a suggestion. I know that this issue was fixed in an update, but maybe the spacing needs to be addressed. Again, I haven't gone through the script yet to verify this.

So if i leave spacing out, your thinking it works @Owen-Slater ? If so thats cool, but still spacing would need to be addressed I think.

lightninglarry commented 3 years ago

I took the spacing code part out, only created 1 lineup before it crashed tonight using the above code

lightninglarry commented 3 years ago

I think ive got it working, thanks for all the help. One more question though. Wanting to add randomness say by like 10 pct, or 25 pct to my projections. 1- is this syntax correct, and 2- how do u incorporate randomness as a percentage?

optimizer.add_stack(TeamStack(4, for_teams=['DET', 'PHI', 'NYY', 'CHC'] spacing=1, max_exposure=0.3)) # stack 4 players from any of specified teams optimizer.add_stack(TeamStack(4, for_teams=[‘ARI', 'STL', 'NYM', 'BOS'] spacing=1, max_exposure=0.6)) # stack 4 players from any of specified teams optimizer.add_stack(TeamStack(4, for_teams=['ATL', 'SF', 'LAD', 'BAL'] spacing=1, max_exposure=0.8)) # stack 4 players from any of specified teams

Also for anyone who uses Fanduel, if the team lineup hasnt been out yet, and you want them in your stack, do you put a # for batting order for player? or if u leave it blank does it still spit it out at you anyways if there is a projection? @bigboyz