我正在尝试使用foreach在并行代码中转换我的嵌套循环。我不明白该怎么做。 按照我的代码: 提前谢谢!!
setwd("simul")
IntervalTime <- c(10,15,20)
ThinIntesity <- c(10,15,20,25)
MINSTOCKGROW <- c(200,250,300)
meteoScen <- c("rcp4p5", "rcp8p5")
Co2mod <- c("CO2_rcp4p5", "CO2_rcp8p5")
fileSettings <- c("set_co2_on_man_off.txt", "set_co2_on_man_on.txt")
mapUnc <- data.frame()
uncerOut <- data.frame()
mapUncNoMan <- data.frame()
validation <- list()
Sample <- getSample(resDE,thin=700,start=0.3*niter,coda=F)
nSample <- nrow(Sample)
ValidationOutput <- "./output/"
#FOR MANAGEMENT CASES
这里是并行化的代码,但我尝试了不同的方法但只有一个核心工作
for (i in 1:length(IntervalTime)){
for(p in 1:length(MINSTOCKGROW)){
for (j in 1:length(ThinIntesity)){
for(z in 1:length(meteoScen)){
for (k in 1:nSample){
# MAP BAYESIAN REFERENCE
ps <- createMixWithDefaults(pars = Sample[k, ],defaults = paramDefault, parind)
if (meteoScen[z]=="M45"){
#running the model
unlink(list.files("./output/Validation",full.names = TRUE),recursive=TRUE)
validation <- run3DCMCC(modelrun)
validation$annual$MINSTOCKGROW <- MINSTOCKGROW[p]
validation$annual$Management <- paste0(meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j])
validation$annual$CODE <- paste0(meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j],"_",MINSTOCKGROW[p],"_",validation$annual$YEAR)
validation$annual$Thinintensity <- ThinIntesity[j]
validation$annual$intervTime <- IntervalTime[i]
validation$annual$Meteo <- meteoScen[z]
validation$annual$Manage <- "MANAGEMENT"
validation$annual$Harv_Wood <- cumsum(validation$annual$C_HW)
validation$annual$StockCarbon <- validation$annual$Harv_Wood+validation$annual$STANDING_WOOD
# validation$monthly$Date <- as.yearmon(paste0(validation$monthly$YEAR, "-", validation$monthly$MONTH))
# validation$monthly$Management <- paste0("METEO_",meteoScenario[z],"INTERVAL_",IntervalTime[i],"INTENSITY_",ThinIntesity[j])
validationDF <- data.frame(validation$annual)
write.table(validationDF, paste0("./dataOut/",meteoScen[z],"_IV_",IntervalTime[i],"_IT_",ThinIntesity[j],"_MINSTCK_",MINSTOCKGROW[p],"_MAN_ON",".txt"),quote = F, sep = "\t",row.names = F,col.names = T)
mapUnc <- rbind(mapUnc, validation$annual)
} else {
嵌套循环中的条件
ps <- createMixWithDefaults(pars = Sample[k, ],defaults = paramDefault,parind)
#running the model
unlink(list.files("./output/",full.names = TRUE),recursive=TRUE)
validation <- run3DCMCC(modelrun)
validation$annual$MINSTOCKGROW <- MINSTOCKGROW[p]
validation$annual$Management <- paste0(meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j])
validation$annual$CODE <- paste0(meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j],"_",validation$annual$YEAR)
validation$annual$Thinintensity <- ThinIntesity[j]
validation$annual$intervTime <- IntervalTime[i]
validation$annual$Meteo <- meteoScen[z]
validation$annual$Manage <- "MANAGEMENT"
validation$annual$Harv_Wood <- cumsum(validation$annual$C_HW)
validation$annual$StockCarbon <- validation$annual$Harv_Wood+validation$annual$STANDING_WOOD
validationDF <- data.frame(validation$annual)
write.table(validationDF, paste0("./dataOut/",meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j],"_MAN_ON",".txt"),quote = F, sep = "\t",row.names = F,col.names = T)
所有运行模型的rbind
mapUnc <- rbind(mapUnc, validation$annual)
}
}
}
}
}
}
答案 0 :(得分:0)
实际上,您可能不需要任何for
循环。考虑输入向量上的expand.grid
,它返回所有可能组合的data.frame
。然后,运行Map
(mapply
的变体)通过构建 vaildationDF 的函数迭代地和逐个元素地传递列值。然后Map()
将返回一个等于展开网格df行的数据帧列表(即所有组合)。
当然,由于您没有提供可重现的示例,未知库和函数调用,因此下面只是该过程的草图。此外,if/else
条件看起来几乎相同,但下面是if
条款的重写。此外,某些行似乎是多余的,因为从未使用 ps 对象(因此 Sample 未在下面合并)。可能 ps 与 modelrun 相关联?并且unlink()
不会影响循环中的项目:
IntervalTime <- c(10,15,20) # passed as i variable in fct. via Map()
ThinIntesity <- c(10,15,20,25) # passed as t variable in fct. via Map()
MINSTOCKGROW <- c(200,250,300) # passed as g variable in fct. via Map()
meteoScen <- c("rcp4p5", "rcp8p5") # passed as m variable in fct. via Map()
loopdf <- expand.grid(IntervalTime=IntervalTime,
ThinIntensity=ThinIntesity,
MINSTOCKGROW=MINSTOCKGROW,
meteoScen=meteoScen)
createValidationDF <- function (i, t, g, m) {
validation <- run3DCMCC(modelrun)
validation$annual$MINSTOCKGROW <- g
validation$annual$Management <- paste0(m,"_","IV_", i,"_","IT_", t)
validation$annual$CODE <- paste0(m,"_","IV_",i,"_","IT_", t,"_",g,"_",
validation$annual$YEAR)
validation$annual$Thinintensity <- t
validation$annual$intervTime <- i
validation$annual$Meteo <- m
validation$annual$Manage <- "MANAGEMENT"
validation$annual$Harv_Wood <- cumsum(validation$annual$C_HW)
validation$annual$StockCarbon <- validation$annual$Harv_Wood+validation$annual$STANDING_WOOD
validationDF <- data.frame(validation$annual)
write.table(validationDF, paste0("./dataOut/", m ,"_IV_", i,"_IT_", t,"_MINSTCK_", g,"_MAN_ON",".txt"),
quote = F, sep = "\t",row.names = F,col.names = T)
return(validationDF)
}
df_List <- Map(createValidationDF, loopdf$IntervalTime, loopdf$ThinIntensity,
loopdf$MINSTOCKGROW, loopdf$meteoScen)
mapUnc <- do.call(rbind, df_List)