2

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.

Community
  • 1
  • 1
  • I'm thinking I could create a vector for teams i.e `code` teams <- myData$Team `code` But how can I use that to make sure my selected D = selected K – NxtWrldChamp Oct 28 '16 at 18:22

1 Answers1

0

Try doing something similar to this, you'll just need to modify it to suit your situation. I've taken this direct from my own code, but basically, input the players I want and create a separate data frame with these. Then I optimise the left over positions and rbind together to create the final lineup. This loops through and gives as many lineups as the user wants.

Inclusions<-readline("Enter players to include into optimal lineups: ")
Inclusions <- as.character(unlist(strsplit(Inclusions, ",")))
Inclusions_table<-Data[  Data$Player.Name %in% Inclusions, ]
Inclusions_no<-nrow(Inclusions_table)
Data<-Data[ ! Data$Player.Name %in% Inclusions, ]

Lineup_no<-readline("How many lineups to be generated?: ")

num.players <- length(Data$Player.Name)
obj<-Data$fpts
var.types<-rep("B",num.players)
subscore<-1000

Lineups <- list()
for(i in 1:Lineup_no)
{
matrix <- rbind(as.numeric(Data$Position == "QB"), # num QB
       as.numeric(Data$Position == "RB"), # num RB
       as.numeric(Data$Position == "RB"), # num RB
       as.numeric(Data$Position == "WR"), # num WR
       as.numeric(Data$Position == "WR"), # num WR
       as.numeric(Data$Position == "TE"), # num TE
       as.numeric(Data$Position == "TE"), # num TE
       as.numeric(Data$Position %in% c("RB", "WR", "TE")),  # Num RB/WR/TE
       as.numeric(Data$Position == "DEF"),# num DEF
       Data$Salary,Data$fpts)
direction <- c("==",
     ">=",
     "<=",
     ">=",
     "<=",
     ">=",
     "<=",
     "==",
     "==",
     "<=","<")
opt_var<-subscore-0.01         
rhs<-c(1-sum(Inclusions_table$Position=="QB"),max(0,2-sum(Inclusions_table$Position=="RB")),4-sum(Inclusions_table$Position=="RB"),max(0,2-sum(Inclusions_table$Position=="WR")),4-sum(Inclusions_table$Position=="WR"),max(0,1-sum(Inclusions_table$Position=="TE")),2-sum(Inclusions_table$Position=="TE"),7-sum(Inclusions_table$Position=="RB")-sum(Inclusions_table$Position=="WR")-sum(Inclusions_table$Position=="TE"),1-sum(Inclusions_table$Position=="DEF"),100000-sum(Inclusions_table$Salary),opt_var)
sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,
                  types = var.types, max = TRUE)
Lineup<-data.frame(Data[sol$solution==1,])
subscore<-sum(Lineup$fpts)
Lineup<-rbind(Lineup,Inclusions_table)
Lineup<-Lineup[order(Lineup$Position),]
Salary<-sum(Lineup$Salary)
Score<-sum(Lineup$fpts)
print(Lineup)
print(Salary)
print(Score)
Lineups[[i]]<-Lineup
}

Data is my data set and looks like this for reference:

     Position             Player.Name       Team   Opponent Salary  PPG    fpts Pos_Rank   upper   lower Off_Snaps Pct_Off
1056       TE              A.J. Derby   Patriots      Bills   5000    0  0.0000       82       0       0        NA    <NA>
462        RB         Aaron Ripkowski    Packers    Falcons   6000  1.8  1.3116       75  1.8852    0.01        22     25%
78         QB           Aaron Rodgers    Packers    Falcons  19350 20.6 18.4292        1 19.9689    17.2        87    100%
1466       WR          Adam Humphries Buccaneers    Raiders   7650  8.1  9.4808       46 11.2125  7.5664        38     51%
1808       WR           Albert Wilson     Chiefs      Colts   5000  4.3  5.6673       74  6.2438    4.78        11     21%
1252       WR        Aldrick Robinson    Falcons    Packers   5000  3.8  2.9114       96  3.2836  2.0152        10     15%
636        RB            Alex Collins   Seahawks     Saints   6000  2.7  1.5992       69  2.1513    0.41         1      2%

Hopefully you can modify this example to suit you.

Morts81
  • 419
  • 3
  • 13
  • Thanks, I have found your questions on here and used a lot of this already as a basis for what I'm doing. I can lock players in but I am more wondering if there is a way for me to tell the code to spit out the best team where the DEF and K are on the same team. I've been able to loop through every DEF/K combo to see the best teams but wasn't sure if there was a more efficient way. – NxtWrldChamp Oct 31 '16 at 17:21
  • Do you want the optimal lineup for each team with the same defense and kicker? Or are you selecting a K and DEf yourself and then wanting to see the optimal lineup? – Morts81 Nov 01 '16 at 01:54
  • Would prefer not to do any manual selecting at first. i.e. ask code to give me best lineup where the defense and K are from the same team. – NxtWrldChamp Nov 01 '16 at 13:29
  • It should be easy enough to do. Loop through the optimiser for all other positions (not K and DEF) and modify the price to be minus the cost of that teams K and DEF. You could then rbind the K and DEF with the optimised lineup. This would give 32 teams, one for each of the NFL Teams. – Morts81 Nov 02 '16 at 11:44