dipping my feet into R after using excel for many years and have a question. I am thoroughly impressed with how much faster R is, it used to take Excel over an hour to do 10,000 simulations and R did 25,000 of the same sim in 4 mins. Awesome.
This is fantasy football related as I am trying to create a lineup optimizer in R and found the RGLPK library to be a good option. There are multiple other questions on SO that helped me get to where I am today however I have hit a road block. Here are some of the other topics.
Fantasy football linear programming in R with RGLPK
Rglpk - Fantasy Football Lineup Optimiser - Rbind of For Loop Output
Rglpk - Fantasy Football Lineup Optimiser - Forcing the Inclusion of a Player
Here is my stock optimizer
#stock optimal linups solver
name <- myData$Name
pos <- myData$Pos
pts <- myData$Projection
cost <- myData$Salary
team <- myData$Team
opp <- myData$Opp
num.players <- length(name)
f <- pts
var.types <- rep("B", num.players)
A <- rbind(as.numeric(pos=="QB")
, as.numeric(pos=="RB")
, as.numeric(pos=="WR")
, as.numeric(pos=="TE")
, as.numeric(pos=="K")
, as.numeric(pos=="D")
,cost)
dir <- c("=="
,"=="
,"=="
,"=="
,"=="
,"=="
,"<=")
b <- c(1
, 2
, 3
, 1
, 1
, 1
, 60000)
library(Rglpk)
sol <- Rglpk_solve_LP(obj = f
, mat = A
, dir = dir
, rhs = b
, types = var.types
, max=TRUE)
myData[sol$solution == 1,]
sprintf('Cost is:$%i', sum(cost[sol$solution > 0]))
sprintf('Projected Points is: %f', sol$optimum)
Here is a link to the data I'm using.
https://www.dropbox.com/s/d5m8jjnq32f0cpe/Week6NFLProjections.csv?dl=0
I'm also to the point where I can loop the code to create multiple lineups by setting the objective = to the previous score - .01. As a side note this process slows down significantly as it keeps going on(say by lineup #50), is this normal and is there a more efficient way to loop this?
My real question is how can I add some more extensive constraints. In Fantasy football it is useful to "pair" players from the same team together and I can't figure out how I would put that into the constraints.
For a simple pairing example how could I add a constraint so that my "optimal lineup" would have the D and K from the same team? I actually have been able to work around this question by just combining the D+K in the CSV file but am interested in how I would code that into R.
A more complex pairing scenario would be to have my QB and just 1 of the (3)WR/(1)TE be on the same team.
Another would be to make sure none of the offensive players is playing vs my own defense.
Any help would be greatly appreciated. Can't seem to find an answer to this anywhere.