【问题标题】:R Team Roster constraint with lpsolve - must pick at least x players from Team具有 lpsolve 的 R 团队名册约束 - 必须从团队中选择至少 x 名球员
【发布时间】:2016-12-24 11:07:12
【问题描述】:

我在尝试在 previous question 的基础上构建时遇到了麻烦

我想优化,至少有 3 名球员来自同一支球队,但我不在乎是哪支球队。

在下面的代码中,我可以强制它从熊队(或我指定的另一支球队)中挑选 3 名球员。您将如何选择来自同一支球队的 3 名球员的最佳阵容?

library(Rglpk)
DF <- data.frame(Team=c(rep("Bears",5), rep("Jets",5), rep("49ers", 5)), Player=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"), Role=c(rep(c("WR", "RB", "TE"),5)), Avgpts=c(22, 19, 30, 25, 20, 21, 26, 14, 21, 13, 11, 8, 4, 3, 5), Salary=c(930, 900, 1300, 970, 910, 920, 980, 720, 650, 589, 111, 1239, 145, 560, 780))
obj = DF$Avgpts
con = rbind(as.numeric(DF$Role=="WR"), as.numeric(DF$Role=="RB"), as.numeric(DF$Role=="TE"), as.numeric(DF$Team == "Bears"), DF$Salary)
dir = c("==","==","==","==","<=")
rhs = c(1,1,1,3,100000)
sol <- Rglpk_solve_LP(obj = obj
                , mat = con
                , dir = dir
                , rhs = rhs
                , types = rep("B", length(DF$Team))
                , max=TRUE)

solution <- DF[sol$solution==1,]

【问题讨论】:

  • 一般来说,有链接是可以的,但你的代码应该自己运行。这意味着创建DF 并调用library(lpSolve) 等等。
  • 抱歉,我更改了示例以便它运行。目前硬编码的条件是从熊队中挑选 3 名球员,但我想同时挑选出最好的 3 名球员,无论是哪支球队。

标签: r optimization solver knapsack-problem lpsolve


【解决方案1】:

如果我弄错了一些术语,请原谅我,但这是我最终想出的解决方案。每个球员都被视为一个专栏,我也为每支球队设置了一个专栏。我为每个 Team=Team 输入了一个虚拟变量,该变量等于我希望单个团队中的最少玩家数。

library("lpSolveAPI")
DF <- data.frame(Team=c(rep("Bears",5), rep("Jets",5), rep("49ers", 5)), Player=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"), Role=c(rep(c("WR", "RB", "TE"),5)), Avgpts=c(22, 19, 30, 25, 20, 21, 26, 14, 21, 13, 11, 8, 4, 3, 5), Salary=c(930, 900, 1300, 970, 910, 920, 980, 720, 650, 589, 111, 1239, 145, 560, 780))

ncol <- nrow(DF) # of players in DF
nteams <- length(unique(DF$Team))
teams <- unique(DF$Team)

lp_rowpicker <- make.lp(ncol=(ncol+nteams))

obj_vals <- DF[, "Avgpts"]
set.objfn(lp_rowpicker, c(obj_vals, rep(0, nteams))) #dummy 0s for team variable
lp.control(lp_rowpicker,sense='max')
set.type(lp_rowpicker, columns=1:(ncol+nteams), type = "binary")
add.constraint(lp_rowpicker, xt=c(DF$Salary, rep(0, nteams)), type="<=", rhs=35000)
add.constraint(lp_rowpicker, xt=c(as.numeric(DF$Role=="WR"), rep(0, nteams)), type="=", rhs=1)
add.constraint(lp_rowpicker, xt=c(as.numeric(DF$Role=="RB"), rep(0, nteams)), type="=", rhs=1)
add.constraint(lp_rowpicker, xt=c(as.numeric(DF$Role=="TE"), rep(0, nteams)), type="=", rhs=1)

然后我设置了一个约束,即设置为 1 的团队列数等于团队总数减去我想要的最佳解决方案中的团队数。在这种情况下,由于我在数据框中的 3 支球队中寻找 1 支球队,因此 2 支球队将被设置为 1,而设置为 0 的球队将需要至少 3 名球员才能满足该行的最小限制级别。

#3 players total
add.constraint(lp_rowpicker, xt=c(rep(1, ncol), rep(0, nteams)), type="=", rhs=3)

# add a constraint that every team must have between 3 and 6 players.
# put a dummy value of 3 in for each team
# if the flag for the team column is 0 then 3 players must be selected (each with a value of 1 in that team's column.
for (i in 1:nteams){
    team <- teams[i]
    add.constraint(lp_rowpicker, lhs=3, xt=c(as.numeric(DF$Team==team), rep(0, i-1), 3, rep(0, nteams-i)), type="<=", rhs=7)
}

# one team will not have the dummy value in the team column, forcing at     least 3 players picked from the same team to meet the lhs of the above constraint
add.constraint(lp_rowpicker, xt=c(rep(0, ncol), rep(1, nteams)), type="=", rhs=(nteams-1))

solve(lp_rowpicker)
get.objective(lp_rowpicker)
soln <- get.variables(lp_rowpicker)>0
solution <- DF[soln[0:ncol],]
print(solution[order(solution$Team),])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2021-05-03
    • 2014-11-12
    • 1970-01-01
    • 2019-07-10
    相关资源
    最近更新 更多