Hey everyone, getting my feet wet with R and am running into a wall.
Currently I have some R code that will look through my data set that includes (Player, Position, Projection, Salary, Team, Opponent). This is for FanDuel so I currently have it selecting 1 QB, 2 RBs, 3 WRs, 1 TE, 1 K, and 1 Defense while staying under the $60K salary cap. So I can get it to spit out the optimal line up for me, can also loop it to get say the best 10 line ups for me.
The problem I'm running into is that I want the lineup it spits out to have a few more constraints.
For example I want just 1 of the WRs/TE to be on the same team as the QB.
Anyone got any advice? I have been searching all over for this answer and can't find anything.
Here is the code I have so far
Code:
name <- myData$Name
pos <- myData$Pos
pts <- myData$Projection
cost <- myData$Salary
team <- myData$Team
num.players <- length(name)
f <- pts
var.types <- rep("B", num.players)
maxPts <- 1000
lineup_no <- 10
Lineups <- vector("list", length(lineup_no))
for(i in 1:lineup_no)
{
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
,f)
dir <- c("=="
,"=="
,"=="
,"=="
,"=="
,"=="
,"<="
,"<=")
b <- c(1
, 2
, 3
, 1
, 1
, 1
, 60000
, maxPts)
library(Rglpk)
sol <- Rglpk_solve_LP(obj = f
, mat = A
, dir = dir
, rhs = b
, types = var.types
, max=TRUE)
score <- sum(pts[sol$solution >0])
Lineup <- myData[sol$solution == 1,]
Lineup<-Lineup[order(Lineup$Pos),]
print(Lineup)
print(score)
lineupNumber <- i
print(lineupNumber)
Lineups[[i]] <-Lineup
maxPts <- score - .01
}
Thanks!