ich einen Datenrahmen (Statistiken) haben, die wie folgt strukturiert:Rglpk Package (R) - Fantasy Sports Optimiser - Erweiterte Schritte
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
ich eine einfache Rglpk Lineup Optimierung auf diese laufen kann wie folgt mit keine Probleme auf alle, und es wird mir die optimale Aufstellung auf Statistik basierend geben $ 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,]
Allerdings mag ich diesen Code ändern, so dass ich eine optimale Seite finden kann, die eine bestimmte Punktzahl in einer bestimmten Anzahl erfüllte von Runden (dh Statistik $ R1 bis Statistik $ R23). Ich habe zusammen eine Schleife Methode setzen, die theoretisch funktioniert, aber ist einfach viel zu langsam praktikabel sein: eine einfache Möglichkeit, diese Anforderung in den Standard Rglpk Rahmen zu bauen
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"}
}
Gibt es? Zum Beispiel finden Sie die optimale Aufstellung (basierend auf Statistiken $ FP), wo dieses Team 500 Punkte in mindestens 5 der 20 Spiele zwischen R1 und R23 erzielt hat?
------------------------------ UPDATE --------------- ---------------
Ich habe ein wenig mehr darüber nachgedacht und die Aktualisierung der Statistik $ FP von der durchschnittlichen Punktzahl zur gesamten Saison Punktzahl in der Schleife oben drastisch reduziert die Schleife Laufzeit Allerdings bin ich immer noch sehr an einer Non-Loop-Alternative interessiert.