long time listener first time caller to S.O... I am asking a question that has been asked very similarly before, however I don't believe I am smart enough to decipher how to implement the solution, for this I apologize. Here is the link to the question I found: Constraints in R Multiple Integer Linear Programming
I am maxing over my projected fantasy points(FPTS_PREDICT_RF), subject to a 50,000 salary cap, while minimizing a 'risk' calculation that I have came up with.
Now, the problem lies in the "flex" position. The team needs to be made up of 9 positions, 1 QB 2 RB 3 WR 1 TE 1 DEF 1 FLEX
The flex can be a RB, WR, or TE.
So, we can then have:
1 QB
2-3 RB
3-4 WR
1-2 TE
1 DEF
I am trying to implement the constraint that #RB + #WR + #TE ==7.
Here is the relevant code:
library(Rglpk)
# number of variables
num.players <- length(final$PLAYER)
# objective:
obj <- final$FPTS_PREDICT_RF
# the vars are represented as booleans
var.types <- rep("B", num.players)
# the constraints
matrix <- rbind(as.numeric(final$position == "QB"), # num QB
as.numeric(final$position == "RB"), # num RB
as.numeric(final$position == "WR"), # num WR
as.numeric(final$position == "TE"), # num TE
as.numeric(final$position == "DEF"),# num DEF
diag(final$riskNormalized), # player's risk
final$Salary) # total cost
direction <- c("==",
"<=",
"<=",
"<=",
"==",
rep("<=", num.players),
"<=")
rhs <- c(1, # Quartbacks
3, # Running Backs
2, # Wide Receivers
1, # Tight Ends
1, # Defense
rep(10, num.players), #HERE, you need to enter a number that indicates how
#risk you are willing to be, 1 being low risk,
# 10 being high risk. 10 is max.
50000) # By default, you get 50K to spend, so leave this number alone.
sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,
types = var.types, max = TRUE)
sol #Projected Fantasy Points
Can someone help me implement this constraint? Any help is much, much appreciated!
EDIT: Link to dataset 'final' is csv format: https://www.dropbox.com/s/qp35wc4d380hep1/final.csv?dl=0
SIDE QUESTION: For any of you fantasy footballers out there, I am calculating my 'risk' factor directly from the S.D. of the player's historical fantasy points, and normalizing this number over the support of [0,10]. Can you think of a better way to calculate a given players risk?