Code to optimize fantasy lineup with the ability for restrictions (currently for FanDuel and DraftKings). Essentially, this solves a variant of the knapsack problem using linear programming.
Code follows the FanDuel structure (D, K, WR, WR, WR, RB, RB, TE, QB) or the DraftKings structure (D, QB, WR, WR, WR, RB, RB, TE, FLEX) where FLEX is an additional WR/TE/RB.
This function returns a number of top teams (in terms of expected points), given a set salary cap. The input file currently should be in the form of the .csv file 'fantasy_points.csv'. Replace the column 'Expected Points' with your own expected points. You will also need to include the player/defense's team and their opponent for that week. This information is all contained in the FanDuel export file (see the screenshot on where to download that). For DraftKings, currently you will need to do some editing on their export file to get it into the format here.
Functionality currently includes two possible restrictions:
-
No two players are from the same team AND that the chosen defense is not playing against any of the offensive players.
-
At most two players from the same team (including defense) AND offensive players don't face each other (for example, if NE is playing NYG, you can't have Odell Beckham and Tom Brady).
Players can also be 'forced' into the lineup, meaning you can build an optimal lineup around a given set of players of your choice.
Need to add:
-
Support for DraftKings style lineup and custom style lineups -
Support for custom style lineups
-
Custom restrictions (for example, running back and wide receiver cannot be on same team)
There is now an R Shiny app available for this: http://geekman1.shinyapps.io/FF_Lineup_Optimizer
Why is this function needed? Brute force to find the best lineup would require searching through 32x32x(32C3)x(32C2)x32x32 = 3.2876 x 10^12 combinations. This is about 3 trillion lineups, assuming only 32 starting players at each position (in reality there are more than 32 starters, so the number of real possible lineups may easily balloon into the quadrillions).
And here is the top team for week 4 of the 2015 season, with an expected 178.2 points, with a $59,800 salary. Details for the top ten teams are in the file 'output_top_teams.csv'.
Position | FirstName | LastName | ExpectedPoints | Salary | Team | Opponent | TeamSalary | TotalPoints |
---|---|---|---|---|---|---|---|---|
WR | Julio | Jones | 28.3 | 9400 | ATL | HOU | 59800 | 178.2 |
QB | Andy | Dalton | 23.9 | 7600 | CIN | KC | 59800 | 178.2 |
RB | Latavius | Murray | 16.4 | 7500 | OAK | CHI | 59800 | 178.2 |
WR | Larry | Fitzgerald | 24.9 | 7400 | ARI | STL | 59800 | 178.2 |
RB | Devonta | Freeman | 20.0 | 7200 | ATL | HOU | 59800 | 178.2 |
WR | Travis | Benjamin | 19.3 | 6000 | CLE | SD | 59800 | 178.2 |
TE | Austin | Seferian-Jenkins | 14.7 | 5200 | TB | CAR | 59800 | 178.2 |
K | Josh | Brown | 13.0 | 4800 | NYG | BUF | 59800 | 178.2 |
D | Denver | Broncos | 17.7 | 4700 | DEN | MIN | 59800 | 178.2 |