R OMPR软件包-限制所选唯一变量组件的数量 [英] R OMPR package - Limiting the number of unique variable components chosen

查看:62
本文介绍了R OMPR软件包-限制所选唯一变量组件的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ompr 包来创建和解决整数编程问题.为简单起见,我将以NFL足球奇幻球员为例.

我想最大化两场比赛的得分,同时每场比赛只在每个位置上玩 1 名球员.(为简单起见,这里假设任何玩家都可以担任任何位置.)

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

任何帮助将不胜感激!

  n_players = 25n_positions = 11n_games = 2每个玩家在每个游戏的每个位置得分的#分点游戏1 =矩阵(runif(25 * 11),nrow = 25,ncol = 11)points_game2 =矩阵(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()%>%#初始化播放器/位置的二进制选项集ompr :: add_variable(x [i,j,k],i = 1:n_players,j = 1:n_positions,k = 1:n_games,type ='binary')%&%;%#每个玩家/游戏在所有位置上只能为0或1ompr :: add_constraint(sum_expr(x [i,j,k],j = 1:n_positions)< = 1,i = 1:n_players,k = 1:n_games)%&%;%#所有玩家的每个位置/游戏都必须正好为1ompr :: add_constraint(sum_expr(x [i,j,k],i = 1:n_players)== 1,j = 1:n_positions,k = 1:2)%>%#******最多可容纳15位玩家???****#目标是最大化积分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')%>%#解决模型ompr :: solve_model(with_ROI(solver ='symphony',verbosity = -2)) 

解决方案

您可以添加一组跨玩家索引的二进制变量,以跟踪玩家是否在任何游戏的任何职位中使用.然后,您可以将这些变量的总和限制为极限(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约束,如果使用玩家,则确保is_used为1ompr :: add_constraint(2 * is_used [i]> = sum_expr(x [i,j,k],j = 1:n_positions,k = 1:2),i = 1:n_players)%>% 

I'm using the ompr package for creating and solving an integer programming problem. For simplicity's sake, I will use NFL football fantasy players as my example.

I want to maximize the number of points scored across the 2 games, while only playing 1 player at each position per game. (For simplicity's sake, here assume that any player can play any position.)

The part I'm having trouble with is that of the 25 possible players, I want to limit the total number of players chosen across the two games to 15. The i component of the added ompr variable represents the player indices, but I'm not sure how to add a constraint that limits the total unique i's chosen.

Any help would be greatly appreciated!

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

解决方案

You can add a set of binary variables indexed across the players that tracks whether or not a player is used in any postition in any game. Then you can limit the sum of those variables to your limit (15). This lets you only count a player once even if they get used in both games. Then you can add a big M constraint that forces the new binary variables to be 1 if a player is used in any position in any game, but lets the variable be 0 if the player is not used. Since we have two games and a player can be in at most 1 position in each game, we can set the big M to be 2 for all players

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

这篇关于R OMPR软件包-限制所选唯一变量组件的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆