我有一個數據幀(數據),其結構如下:Rglpk包(R) - 幻想運動優化工具 - 高級步驟
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框架一個簡單的方法? 例如,找到此團隊在最短的R1和R23之間的20場比賽打進5 500點的最佳陣容(根據統計$ FP)?
------------------------------ UPDATE --------------- ---------------
我想多一點關於這一點,並更新統計$ FP從平均分總得分賽季在上述循環大大減少了循環運行時間但是,我仍然對非循環替代非常感興趣。