Sorry, here is what I tried with code. The LP
works for creating 2 teams. If LP
is not the way to go, I am open to different directions. I want to create all possible fantasy combinations about a specified point total. In this case it is 150. Data I used can be found HERE.
> library(Rglpk)
maxpts = 10000 # Set a max point limit to loop
> obj <- final$FPts # Objective Funtion
matrix <- rbind(as.numeric(final$Pos == "QB"), # num QB
+ as.numeric(final$Pos == "RB"), # num RB
+ as.numeric(final$Pos == "RB"), # num RB
+ as.numeric(final$Pos == "WR"), # num WR
+ as.numeric(final$Pos == "WR"), # num WR
+ as.numeric(final$Pos == "TE"), # num TE
+ as.numeric(final$Pos == "TE"), # num TE
+ as.numeric(final$Pos %in% c("RB", "WR", "TE")), # Num RB/WR/TE
+ as.numeric(final$Pos == "DST"),# num DEF
+ final$Sal, # total cost
+ final$FPts)
> direction <- c("==",
+ ">=",
+ "<=",
+ ">=",
+ "<=",
+ ">=",
+ "<=",
+ "==",
+ "==",
+ "<=",
+ "<=")
> rhs <- c(1, # Quartbacks
+ 2, # RB Min
+ 3, # RB Max
+ 3, # WR Min
+ 4, # WR Max
+ 1, # TE Min
+ 2, # TE Max
+ 7, # RB/WR/TE
+ 1, # Defense
+ 50000,
+ maxpts)
> sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,
+ types = var.types, max = TRUE)
> teamlp <- function(){
+ sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,
+ types = var.types, max = TRUE)
+ finalteam <- final[sol$solution==1,]
+ teams <- rbind(teams, finalteam)
+ maxpts = sum(finalteam$FPts) - .001
+ } # Put LP in a funtion
getteams <- function(){
+ while(maxpts > 150) {
+ teamlp
+ }
+ maxpts = sum(finalteam$FPts) - .001
+ } # Created a while loop to create teams until Total Team points dropped below 150
Was hoping to figure this out and have it print out like this
Team QB RB RB WR WR WR TE FLEX(RB/WR/TE) DEF Totoal PTS
1 QB1 RB1 RB1 WR1 WR1 WR1 TE1 FLEX1 DEF1 190
2 QB2 RB2 RB2 WR2 WR2 WR2 TE2 FLEX2 DEF2 189.95
.
.
.
N QBn RBn RBn WRn WRn WRN TEn FLEXn DEFn 150.01
I think the code I wrote is an infinite loop. Program has been running for 30 minutes. I ran the LP independently and that works. I think I also messed up the function that contains the LP. I got this error when I ran that by itself.
Error in rbind(teams, finalteam) : object 'teams' not found
Again my goal is to find all possible combinations of teams that have points above 150. Teams are comprised of 1 QB
, 2 RB
, 3 WR
, 1 TE
, 1 FLEX (RB/WR/TE)
, and 1 DEF
. I tried to use combination (comb
) first but seemed like I was using too much of the memory and was getting a error so I went to a LP in a loop path after seeing an example, but it was written to only produce 20 teams. Any suggestion will be much appreciated.