1

我正在使用OMPRr 中的包来解决我的守门员联赛足球/足球队的一些约束问题。这个联赛的运作方式非常像一支真正的英超球队,我想最大化每位球员每场比赛贡献的总进球数。从本质上讲,我想每天挑选 10 名最佳球员,以最大限度地提高球队的产出(目标)。

但是有一个问题!我的梦幻球队不仅每天打两场比赛(并且需要最大限度地提高两场比赛的产量),而且我本周的球队只能使用上周球队的 5 名球员。因此,更直接地说,我需要优化以下几点:

  1. 最多选择 10 名玩家
  2. 预计球员每天在两场比赛的每一场比赛中都会获得不同数量的进球
  3. 玩家可以玩任何位置(为简单起见)
  4. 在“最佳”选择的名单上,上周出场的球员总数不能超过 5 人

乍一看,这看起来与这个问题非常相似,但有一个额外的皱纹。与那个问题不同,我需要在为上周比赛的玩家设置最大阈值的过程中添加一个额外的限制 (5)。

我在概念化如何将二进制“上周使用”列添加到要优化的数组以及在优化函数中设置实际约束时都遇到了麻烦。任何智慧/指导将不胜感激。

#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))

4

0 回答 0