具有 lpsolve 的 R Team Roster 约束 - 必须从 Team 中至少挑选 x 名球员
R Team Roster constraint with lpsolve - must pick at least x players from Team
我在尝试构建
的基础时遇到问题
我想优化,让至少有 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,]
如果我用错了某些术语,请原谅我,但这是我最终想出的解决方案。每个球员都被视为一列,我也为每支球队都有一列。我为每个 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),])
我在尝试构建
我想优化,让至少有 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,]
如果我用错了某些术语,请原谅我,但这是我最终想出的解决方案。每个球员都被视为一列,我也为每支球队都有一列。我为每个 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),])