我正在使用该ompr
包来创建和解决整数规划问题。为简单起见,我将使用 NFL 足球幻想球员作为我的示例。
我想最大化两场比赛的得分,同时每场比赛每个位置只玩一名球员。(为简单起见,这里假设任何玩家都可以玩任何位置。)
我遇到问题的部分是 25 名可能的球员,我想将两场比赛中选择的球员总数i
限制为 15 名。添加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))