R OMPR- 向矩阵添加新的约束和维度

问题描述

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

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

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

乍一看,这看起来与 this question 非常相似,但有额外的皱纹。与那个问题不同的是,我需要在流程中添加一个额外的约束条件,即我为上周比赛的玩家设置了最大阈值 (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,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 = 1:num_positions) <= 1,k = 1:num_games) %>% 
  
  # Every position/game has to be exactly 1 across all players
  ompr::add_constraint(sum_expr(x[i,i = 1:num_players) == 1,k = 1:2) %>%
  
  # Limit to 10 players total via Big M
  ompr::add_variable(u[i],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,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,k] * Goal_Matrix[i,k = 1:num_games),'max') %>% 
  
  # Solve model
  ompr::solve_model(with_ROI(solver = 'symphony',verbosity = -2))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)