luca-scr / GA

An R package for optimization using genetic algorithms
http://luca-scr.github.io/GA/
91 stars 29 forks source link

Persisting state for partial runs #40

Closed neverfox closed 4 years ago

neverfox commented 4 years ago

I've been thinking about how one might go about saving partial state (say as of the last successful iteration) if the run is interrupted early (manually or otherwise, but mostly manually). It's often a shame to want or need to interrupt a long run only to have nothing to show for it. I'm not certain how that might best be implemented (or even if the current package already has a way to do this). Thoughts?

luca-scr commented 4 years ago

Here is an example that implements something close to what you need:

f <- function(x) (x^2+x)*cos(x)  # -10 < x < 10
GA <- ga(type = "real-valued", fitness = f, lower = -10, upper = 10, maxiter = 1e4,
         postFitness = function(object) assign("obj", object, envir = .GlobalEnv))
obj@iter
plot(obj, xlim = c(1,obj@iter))

Thus, obj is an object of class GA which is saved in the working space. You can restart ga()using the current solutions. Note, however, that this is not equivalent to restart the search from the iteration where the break took place.