我在尝试建立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,]
答案 0 :(得分:0)
如果我的某些条款有误,请原谅我,但这就是我最终提出的解决方案。每个玩家都被视为一个列,我也为每个团队都有一个列。我为每个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),])