用mapply替换嵌套的for循环 [英] replace a nested for loop with mapply

查看:82
本文介绍了用mapply替换嵌套的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的初学者.我正在使用R studio的Cplex进行线性编程来求解模型.在我的模型中,约束之一是Xl(i,j,t)≤D(i,j,t).我可以使用小尺寸(16X16X6)的嵌套for循环来做到这一点.但是我想运行更大的模型,例如2500X2500X60.我需要节省内存并比嵌套的for循环更快地运行它.我考虑过使用Apply,但是我不知道如何使它工作.任何帮助将不胜感激!

I am a beginner in R. I am working on linear programming using R studio's Cplex to solve a model. One of the constraint in my model is Xl(i,j,t) <= D(i,j,t). I am able to do this with nested for loop with a small dimension (16X16X6). But I want to run my model a lot bigger model, more like 2500X2500X60. I need to save memory and run it faster than the nested for loop. I thought about using apply but I don't know how to make it work. Any help would be greatly appreciated!

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

这是另一个约束.

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)))
      }
  }
}

我尝试这样做:

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

推荐答案

我认为您需要做的是提供相同输出的矢量化函数(请参见?Vectorize ).下面的代码是您的代码的五百倍.
在遇到实际问题时,也许您需要使用<<-而不是<-(请参阅?<<-" )

I think what you need is to make a vectorized function giving the same output (see ?Vectorize). Below code is five hundred times as fast as yours.
At your real problems, maybe you need to use <<- instead of <- (see ?"<<-")

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 

这篇关于用mapply替换嵌套的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆