Want the ability to stop a simulation
Will use RMetaSim to test

library(rmetasim)

Based on Time elapsed

Stop the repeat after the elapsed time exceeds a certain amount. Set to 1 but does not match 1 second...
Using repeat{}

# repeat and proc.time() ###
runs <- 1
ptm <- proc.time()
  repeat {
      exampleland <- landscape.new.example()
  is.landscape(exampleland)
  simex <- landscape.simulate(exampleland, 4)
  obshet <- landscape.obs.het(simex)
   # print(obshet)
  runs <- runs + 1
 newptm <- proc.time() - ptm
 # print(newptm)
if(newptm[3] >= 1) break()
  }
proc.time()
runs

Learning a shiny to slide the time allowed would be nice.

This seems to measure actual time elapsed

using repeat{} and break()

start <- Sys.time()
repeat{
  exampleland <- landscape.new.example()
  is.landscape(exampleland)
  simex <- landscape.simulate(exampleland, 4)
  obshet <- landscape.obs.het(simex)
   # print(obshet)
  runs <- runs + 1
  end <- Sys.time()
  #Time elapsed
  timel <- as.numeric(end-start)

if(timel >= 2) break()
  }
proc.time()
runs

Try using while() loop

runs <- 1
ptm <- proc.time()
while(newptm[3] < 5) {
  exampleland <- landscape.new.example()
  is.landscape(exampleland)
  simex <- landscape.simulate(exampleland, 4)
  obshet <- landscape.obs.het(simex)
   # print(obshet)
  runs <- runs + 1
 newptm <- proc.time() - ptm
}
runs

Stackoverflow 'tic/toc' like MATLAB

But I can't get it to work.

tic <- function(gcFirst = TRUE, type=c("elapsed", "user.self", "sys.self"))
{
   type <- match.arg(type)
   assign(".type", type, envir=baseenv())
   if(gcFirst) gc(FALSE)
   tic <- proc.time()[type]         
   assign(".tic", tic, envir=baseenv())
   invisible(tic)
}

toc <- function()
{
   type <- get(".type", envir=baseenv())
   toc <- proc.time()[type]
   tic <- get(".tic", envir=baseenv())
   print(toc - tic)
   invisible(toc)
}

runs <- 1
eltime <- 0
 tic()
while(toc() < 5){
exampleland <- landscape.new.example()
  is.landscape(exampleland)
  simex <- landscape.simulate(exampleland, 4)
  obshet <- landscape.obs.het(simex)
  runs <- runs + 1
}
runs


christianparobek/skeleSim documentation built on Feb. 29, 2020, 6:58 p.m.