我有一个数据帧(stats),它的结构如下:
Pos Player.Name TM Sal R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R16 R17 R18 R19 R20 R21 R22 R23 FP
1 MID Blake Acres STK 11200 83 0 0 41 0 126 49 35 0 0 0 71 32 65 46 91 82 99 121 66 71.92
2 FWD Jack Billings STK 12100 74 59 122 113 46 88 81 76 80 0 60 0 0 74 63 85 99 52 105 72 79.35
3 FWD Josh Bruce STK 9250 59 81 72 55 59 69 47 43 112 60 57 59 71 65 26 48 104 49 41 69 62.30
5 DEF Sean Dempster STK 8650 42 47 62 79 44 42 65 57 52 62 24 0 21 48 97 40 80 71 81 54 56.21我可以对此运行一个简单的Rglpk阵容优化,如下所示,没有任何问题,它将根据统计数据$FP给出最佳阵容
num.players <- length(stats$Player.Name)
obj <- stats$FP
var.types <- rep("B", num.players)
names<-unique(stats$Player.Name)
mat<-matrix(0, nrow = length(names), ncol = nrow(stats))
for (i in 1:length(names)){mat[i,]<-as.numeric(stats$Player.Name == names[i])}
matrix <- rbind(as.numeric(stats$Pos == "DEF"),as.numeric(stats$Pos == "MID"),as.numeric(stats$Pos == "RK"),as.numeric(stats$Pos == "FWD"),stats$Sal)
matrix<-rbind(mat,matrix)
direction <- c(rep("<=",length(names)),"==","==","==","==","<=")
rhs <- c(rep(1,length(names)),2,4,1,2,100000)
sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,types = var.types, max = TRUE)
Lineup<-stats[sol$solution==1,]然而,我想修改这个代码,这样我就可以找到在指定轮次(即统计$r1到统计$r23)中达到指定分数的最优方。我已经组合了一个循环方法,理论上是可行的,但太慢了,无法付诸实践:
target<-readline("Enter target score: ")
gms_target<-as.numeric(readline("Enter number of games to reach target score (out of 20): "))
pass<-"N"
avg<-2000
while (pass == "N"){
num.players <- length(stats$Player.Name)
obj <- stats$FP
var.types <- rep("B", num.players)
names<-unique(stats$Player.Name)
mat<-matrix(0, nrow = length(names), ncol = nrow(stats))
for (i in 1:length(names)){mat[i,]<-as.numeric(stats$Player.Name == names[i])}
matrix <- rbind(as.numeric(stats$Pos == "DEF"),as.numeric(stats$Pos == "MID"),as.numeric(stats$Pos == "RK"),as.numeric(stats$Pos == "FWD"),stats$Sal,stats$FP)
matrix<-rbind(mat,matrix)
direction <- c(rep("<=",length(names)),"==","==","==","==","<=","<=")
rhs <- c(rep(1,length(names)),2,4,1,2,100000,avg)
sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,types = var.types, max = TRUE)
Lineup<-stats[sol$solution==1,]
Salary<-sum(Lineup$Sal)
Score<-sum(Lineup$FP)
avg<-Score-.05
sums<-colSums(Lineup[,c(5:24)])
gms<-length(sums[sums >= target])
if(gms>=gms_target){pass="Y"}
}有没有一种简单的方法可以将这个请求构建到标准的Rglpk框架中?例如,找到最佳阵容(基于统计数据$FP),在R1和R23之间的20场比赛中,该球队至少有5场得分?
我对此考虑得更多一些,并在上面的循环中将统计数据$FP从平均得分更新为赛季总得分,从而极大地减少了循环运行时间,然而,我仍然对非循环替代方案非常感兴趣。
发布于 2018-01-06 01:03:12
您可能希望查看R中的分布函数。如果您可以找到优化团队的平均值和标准差,则可以使用pnorm()计算团队达到指定分数的可能性。实际上,我将它用于我的幻想玩家,看看他们为我返回某个分数的可能性有多大。下面是我的代码示例。
players$lk = mapply(function(x,y,z) pnorm(q=z, mean=x, sd=y, lower.tail = FALSE), x=players$points, y=players$sd, z=players$projection) -players$points是他们的平均投影,-players$sd是他们的标准差,-players$projection是我希望他们命中的数字
如果你能计算标准差,你可以对你的团队使用同样的分布函数。
https://stackoverflow.com/questions/42356862
复制相似问题