任何帮助将不胜感激! 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 = 25n_positions = 11n_games = 2# Points each player will score at each position per gamepoints_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 playersompr::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 usedompr::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软件包-限制所选唯一变量组件的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-11 18:12