如何在一段时间后打破R中的循环?

时间:2018-05-14 11:29:38

标签: r

我正在尝试使用模拟退火和最速下降方法来优化Schaffer函数。我想在一个循环中添加一个终止条件,这个循环会在让我们说5秒后打破循环。不幸的是,我失去了我想在这样一个更复杂的循环中放置这样一个条件的地方:

currIter <- 0
finished <- FALSE
x_old <- x

while(finished == FALSE){
StartT <- Sys.time()
x_new <- lineSearch(f, x_old, x_old - a*numGradient(f, x_old, 10^-6), 10)
if(currIter <= maxIter & abs(f(x_new)-f(x_old))>e & f(x_new)<f(x_old)){
  x_old <- x_new
  result$x_opt  <- x_new
  result$f_opt  <- f(x_new)
  result$x_hist <- rbind(result$x_hist, x_new)
  result$f_hist <- rbind(result$f_hist, f(x_new))
  result$iter   <- currIter
  result$tEval   <- rbind(result$tEval, Sys.time() - StartT)
}else{
  finished <- TRUE
}
currIter <- currIter + 1
}
return(result)
}

我可以这样设置一个条件吗?

while(sys.time() < 3)

0 个答案:

没有答案