2017-12-07 3 views
1

Ich bin ein Anfänger in R. Ich arbeite an linearer Programmierung mit R Studio Cplex, um ein Modell zu lösen. Eine der Einschränkungen in meinem Modell ist Xl (i, j, t) < = D (i, j, t). Ich kann dies mit geschachtelten for-Schleife mit einer kleinen Dimension (16X16X6) tun. Aber ich möchte mein Modell ein viel größeres Modell betreiben, eher wie 2500X2500X60. Ich muss Speicher sparen und es schneller als die geschachtelte for-Schleife ausführen. Ich habe über die Anwendung nachgedacht, aber ich weiß nicht, wie ich es zum Laufen bringen kann. Jede Hilfe würde sehr geschätzt werden!Ersetzen Sie eine verschachtelte For-Schleife mit Mapply

location <-16 
horizon <-6 
Amat <- NULL 
Xe_null <- array(0, dim = c(locations, locations, horizon)) 
Xl_null <- array(0, dim = c(locations, locations, horizon)) 
Xl  <- array(0, dim = c(locations, locations, horizon)) 
Xl <- Xl_null 
for (t in 1:horizon) { 
    for (j in 1:locations) { 
    for (i in 1:locations) { 
     Xl[i,j,t] <- 1 
     Amat <- rbind(Amat, c(as.vector(Xe_null), as.vector(Xl))) 
     Xl <- Xl_null 
} } } 
dim(Amat) # 1536 3072 

Hier ist eine weitere Einschränkung.

R  <- array(, dim=c(locations, horizon, horizon)) 
R_null <- array(, dim=c(locations, horizon, horizon)) 
R <- R_null 
Xe <- Xe_null 
Xl <- Xl_null 
# 
for (t in 1:(horizon-1)) { 
    for (tp in (t+1):horizon) { 
    for (j in 1:locations)  { 
     for (i in 1:locations)  { 
     if ((tp-t) ==Travel_time[i,j]) 
     { 
      Xe[i,j,t]=1 
      Xl[i,j,t]=1 
     } 
     } 
     R[j,tp,t] = 1 
     R[j,tp,t+1] = -1 
     Amat <- rbind(Amat, c(as.vector(Xe), as.vector(Xl),as.vector(R))) 
     } 
    } 
} 

Ich versuche, dies zu tun:

Xl = function(ii,jj,tt){1} 
t =c(1:horizon) 
i =c(1:locations) 
j =c(1:locations) 
output_Xl = apply(expand.grid(i,j,t),1,function(x,y,h) Xl(x[1],x[2],x[3])) 
Xl_new <- array(output_Xl, dim = c(locations, locations, horizon)) 
Amat <- rbind(Amat, c(as.vector(Xe_null), as.vector(Xl_new))) 
dim(Amat) # 1 3072 
+0

Ist 'Xl_null' und' Xe_null' das gleiche Objekt zu verwenden, oder ist 'Xe_null' die ursprünglichen Daten? – Llopis

+0

Ja, Xl_null und Xe_null sind leer mit Null. – pkjli

Antwort

2

Sie können die gleiche Leistung mit

T <- horizon*locations*locations 
Bmat <- cbind(matrix(0, nrow=T, ncol=T), diag(1, nrow=T, ncol=T)) 
identical(Amat, Bmat) 
# TRUE 
2

bekommen denke ich, was Sie brauchen eine vektorisiert Funktion zu machen, ist die gleiche Leistung zu geben (siehe ?Vectorize). Der folgende Code ist fünfhundertmal so schnell wie deiner.
Bei Ihrer wirklichen Probleme, vielleicht brauchen Sie <<- statt <- (siehe ?"<<-")

my_func <- function(a, b, c){ 
    Xl[a, b, c] <- 1 
    c(as.vector(Xe_null), as.vector(Xl)) 
} 

vectorized_my_func <- Vectorize(my_func, c("a", "b", "c")) 

arg_df <- expand.grid(1:locations, 1:locations, 1:horizon) 
res <- vectorized_my_func(arg_df[,1], arg_df[,2], arg_df[,3]) 

identical(Amat, t(res)) # TRUE 

# your code 
## user system elapsed 
## 77.538 18.293 97.056 

# my code 
## user system elapsed 
### 0.137 0.051 0.189 
+0

Vielen Dank für die Hilfe und den Vorschlag, meinen Beitrag zu bearbeiten. Sehr hilfreich! Danke :) – pkjli

+0

Hallo, ich habe gerade eine Follow-up-Frage. Wenn ich die Orte <- 2500, Horizont <- 60 ändere. Ich hatte keinen Speicher mehr. Soll ich in Sparse Matrix schauen? Oder haben Sie bessere Vorschläge? – pkjli

Verwandte Themen