【问题标题】:R OMPR package - Limiting the number of unique variable components chosenR OMPR 包 - 限制选择的唯一变量组件的数量
【发布时间】:2020-12-18 03:32:36
【问题描述】:

我正在使用ompr 包来创建和解决整数规划问题。为简单起见,我将以 NFL 梦幻足球运动员为例。

我想最大限度地提高两场比赛的得分,同时每场比赛每个位置只玩 1 名球员。 (为简单起见,这里假设任何玩家都可以玩任何位置。)

我遇到问题的部分是 25 名可能的玩家,我想将两场比赛中选择的玩家人数限制为 15 名。i 的组件添加了ompr 变量代表玩家索引,但我不确定如何添加限制唯一i 选择总数的约束。

任何帮助将不胜感激!

n_players = 25
n_positions = 11
n_games = 2

# Points each player will score at each position per game
points_game1 = matrix(runif(25*11), nrow = 25, ncol = 11)
points_game2 = matrix(runif(25*11), nrow = 25, ncol = 11)
points_array <- array(c(points_game1, points_game2), dim = c(n_players, n_positions, 2))

mip <- ompr::MIPModel() %>% 
  
  # Initialize player/position set of binary options
  ompr::add_variable(x[i, j, k], i = 1:n_players, j = 1:n_positions, k = 1:n_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:n_positions) <= 1, i = 1:n_players, k = 1:n_games) %>% 
  
  # Every position/game has to be exactly 1 across all players
  ompr::add_constraint(sum_expr(x[i, j, k], i = 1:n_players) == 1, j = 1:n_positions, k = 1:2) %>%
  
  # ****** Limit to 15 players total ??? ****
  
  # Objective is to maximize points
  ompr::set_objective(sum_expr(x[i, j, k] * points_array[i, j, k], i = 1:n_players, j = 1:n_positions, k = 1:n_players), 'max') %>% 

  # Solve model
  ompr::solve_model(with_ROI(solver = 'symphony', verbosity = -2))

【问题讨论】:

    标签: r optimization linear-programming mixed-integer-programming ompr


    【解决方案1】:

    您可以添加一组跨球员索引的二进制变量,以跟踪球员是否在任何比赛的任何位置上使用。然后,您可以将这些变量的总和限制在您的极限 (15) 内。这让您只计算一次玩家,即使他们在两个游戏中都使用过。然后,您可以添加一个大 M 约束,如果玩家在任何游戏中的任何位置使用,则强制新的二进制变量为 1,但如果不使用玩家,则让变量为 0。由于我们有两场比赛,一名球员在每场比赛中最多可以处于 1 个位置,我们可以将所有球员的大 M 设置为 2

    ompr::add_variable(is_used[i], i = 1:n_players, type = 'binary') %>%
    ompr::add_constraint(sum_expr(is_used[i],i = 1:n_players) <= 15) %>%
    # big M constraint ensuring that is_used is 1 if a player is used
    ompr::add_constraint(2*is_used[i] >= sum_expr(x[i,j,k],j = 1:n_positions, k = 1:2), i = 1:n_players) %>%
    

    【讨论】:

    • 非常感谢!我对混合整数编程和 ompr 比较陌生,所以仍然围绕着功能,但这很有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2018-04-15
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多