Search code examples
roptimizationsolver

R optimization to find best fantasy football lineup


For my fantasy league, I've always manually done this in excel, but I'd like to code it in R so I can do it easier year to year.

Given my league specific roster requirements of 1 QB, 2 RB, 2 WR, 1 TE, who should I draft to maximize the Points column (projected fantasy points scored), constrained needing the above positions and on spending $200 or less in Value (Value column)?

I usually do this in excel solver to tell me who to draft. I then try to draft those players and if I can't (for example if a player gets bid too high for me to afford), I rerun the solver to show me the next best strategy.

Here is my data in a df:

    > dput(players)
structure(list(Player = c("John1", "John2", "John3", "John4", 
"John5", "John6", "John7", "John8", "John9", "John10", "John11", 
"John12", "John13", "John14", "John15", "John16"), Position = c("QB", 
"QB", "QB", "QB", "RB", "RB", "RB", "RB", "WR", "WR", "WR", "WR", 
"TE", "TE", "TE", "TE"), FantasyPoints = c(10, 8, 6, 4, 20, 15, 
10, 5, 30, 20, 10, 5, 50, 30, 20, 10), DraftValue = c(15, 10, 
8, 2, 50, 30, 25, 20, 40, 30, 20, 10, 50, 35, 20, 5)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -16L))

and here is my code so far:

# Create objective function to maximize fantasy points
obj <- players$FantasyPoints

# Create constraints for number of players at each position
qb_constraint <- players$Position == "QB"
rb_constraint <- players$Position == "RB"
wr_constraint <- players$Position == "WR"
te_constraint <- players$Position == "TE"

# Create constraint for total draft budget
budget_constraint <- players$DraftValue <= 200

# Run linear programming problem
lp_model <- lp("max", obj, qb_constraint, "=", 1, rb_constraint, "=", 2, wr_constraint, "=", 2, te_constraint, "=", 1, budget_constraint, "<=", 200)

# Print optimal solution
print(players[lp_model$solution == 1, ])

But I'm getting an error when running the LP model: "Error in lp("max", obj, qb_constraint, "=", 1, rb_constraint, "=", 2, : unused argument (200)"


Solution

  • this is the fix

        # Define the objective function (maximize fantasy points)
    obj <- players$FantasyPoints
    
    # Prompt user to input number of QBs
    num_qb <- as.numeric(readline("Enter the number of QBs: "))
    num_rb <- as.numeric(readline("Enter the number of RBs: "))
    num_wr <- as.numeric(readline("Enter the number of WRs: "))
    num_te <- as.numeric(readline("Enter the number of TEs: "))
    num_value <- as.numeric(readline("Enter your draft budget: "))
    num_players <- as.numeric(readline("Adding in your flex spots, enter the total number of starters: "))
    
    # Create a new column indicating the player's position
    players$QB <- ifelse(players$Position == "QB", 1, 0)
    players$RB <- ifelse(players$Position == "RB", 1, 0)
    players$WR <- ifelse(players$Position == "WR", 1, 0)
    players$TE <- ifelse(players$Position == "TE", 1, 0)
    players$Total <- 1
    
    # Define the constraints (position limits and draft value limit)
    con <- matrix(c(
      # QB constraint
      players$QB,
      # RB constraint
      players$RB,
      # WR constraint
      players$WR,
      # TE constraint
      players$TE,
      # Draft value constraint
      players$DraftValue,
      #Total players constraint
      players$Total
    ), ncol = nrow(players), byrow = TRUE)
    
    # Define the variables for the lp
    dir <- c("<=", rep(">=",3),"<=","<=")
    rhs <- c(num_qb, num_rb, num_wr, num_te, num_value, num_players)
    
    # Solve the linear program
    result <- lp("max", obj, con, dir, rhs, all.bin = TRUE)
    
    # Print the optimal team
    print(players[result$solution == 1,])