R OMPR- Adding a new constraint and dimension to matrix

361 Views Asked by At

I'm using the OMPR package in r in order to solve some constraint problems with my keeper-league soccer/football team. This league is operated very much like a real Premier League team, where I wanted to maximize the total number of goals contributed by each player per game. Essentially I want to pick the 10 best players each day in order to maximize the output (goals) of my team.

But there is a catch! Not only does my fantasy teams play two games a day (and need to maximize production across both games), but I can only use 5 players from last week's team on this week's team. So in more direct terms I need to optimize points where:

  1. Max 10 Players are selected
  2. Players are expected to get different amount of goals in each of the two games per day
  3. Players can play any position (for simplicity)
  4. The total number of players who played last week cannot exceed 5 on the "optimally" selected roster

At first pass this looks very similar to this question with an additional wrinkle. Unlike that question, I need to add an additional constraint to the process where I set a maximum threshold for players who played last week (5).

I'm having trouble both conceptualizing how to add a binary "was used last week" column to the array to be optimized as well as setting the actual constraint in the optimization function. Any wisdom/guidance would be appreciated.

#total player pool
num_players = 20
#total positions
num_positions = 9
#total number of games to optimize over
num_games = 2

# Goal each player will generate at each position per game 
Goal_1 = matrix(runif(20*9)*10, nrow = 20, ncol = 9)
Goal_2 = matrix(runif(20*9)*10, nrow = 20, ncol = 9)

#matrix that generates 1/0 if you were used last week...1=you were used last week
#first number in vector = first row (player) in each Goal_`` matrix
last_week= sample(c(0,1), replace=TRUE, size=20)



# ******How do I add this last_week vector to the below matrix to use in the optimization function???****

Goal_Matrix <- array(c(Goal_1, Goal_2), dim = c(n_players, n_positions, num_games))



#******i need to add an additional constraint where only five players (max) from last week are used******

mip <- ompr::MIPModel() %>% 
  
  # Initialize player/position set of binary options
  ompr::add_variable(x[i, j, k], i = 1:num_players, j = 1:num_positions, k = 1:num_games, type = 'binary') %>%
  
  # Every player/game can only be 0 or 1 across all positions
  ompr::add_constraint(sum_expr(x[i, j, k], j = 1:num_positions) <= 1, i = 1:num_players, k = 1:num_games) %>% 
  
  # Every position/game has to be exactly 1 across all players
  ompr::add_constraint(sum_expr(x[i, j, k], i = 1:num_players) == 1, j = 1:num_positions, k = 1:2) %>%
  
  # Limit to 10 players total via Big M
  ompr::add_variable(u[i], i = 1:num_players, type = 'binary') %>%
  ompr::add_constraint(sum_expr(u[i],i = 1:num_players) <= 10) %>%
  # big M constraint ensuring that is_used is 1 if a player is used
  ompr::add_constraint(2*u[i] >= sum_expr(x[i,j,k],j = 1:num_positions, k = 1:2), i = 1:num_players) %>%
  
  
  # ****** Limit to max 5 players used last week via the `last_week vector` ??? ****
  
  

  # Objective is to maximize Goal
  ompr::set_objective(sum_expr(x[i, j, k] * Goal_Matrix[i, j, k], i = 1:num_players, j = 1:num_positions, k = 1:num_games), 'max') %>% 
  
  # Solve model
  ompr::solve_model(with_ROI(solver = 'symphony', verbosity = -2))

0

There are 0 best solutions below