I am working on some code in R to optimize my fantasy football line, but I have some difficulties with one limitation. I basically have a list of players, their position, expected points and cost.
The composition should include:
1 QB 2 RB 2 WR 1 TE 1 FLEX (either RB, WR, or TE) Total cost less than $ 200
My problem is that my code wants to select the position of FLEX as a player that he has already selected as WR, RB or TE. Here is the code that I use, I have a table that I imported using columns for the player, position, points and value. In the table, any RB, WR, or TE are duplicated with the FLEX position. I tried changing the line that sets pos == "FLEX" to pos == "WR" || pos == "RB" || pos == "TE" and it didn’t work, my only idea is to run the code, and if it duplicates the FLEX player, I delete it from the original table. Although this is a bit of a pain.
Any ideas are welcome.
name <- mydata$name pos <- mydata$pos pts <- mydata$pts cost <- mydata$cost 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=="FLEX") ,cost) dir <- c("==" ,"==" ,"==" ,"==" ,"==" ,"<=") b <- c(1 , 2 , 2 , 1 , 1 , 200) library(Rglpk) sol <- Rglpk_solve_LP(obj = f , mat = A , dir = dir , rhs = b , types = var.types , max=TRUE) sol name[sol$solution == 1]
optimization r constraints linear-programming
user1723699
source share