Message Boards Message Boards

How to save the Mathematica execution state so that I can stop and later continue from where it was?

I have some functions that take days to execute. I need to find a way to save the Mathematica execution state so that I can turn off the computer and later resume and continue with the calculations from the state where I stopped or at least close to it so that I don't have to start from the beginning of the calculation. I'm trying with DumpSave but I don't find the way to restore (Get) correctly. To reproduce in a simple example (attached) I use an artificially delayed NMinimize function and save it every 3 seconds with a StepMonitor:

bestSolution = 
  NMinimize[{objective[Sequence @@ vars], constraints}, vars,
   StepMonitor :> (If[AbsoluteTime[] - startTime > 3,
      DumpSave["SavedState.mx", "Global`"];
      startTime = AbsoluteTime[];
      Print[iter, " Saved"];
      iter++];
     Pause[0.1]),
   Method -> "SimulatedAnnealing"];

Then I check if there is a file with a previous execution state saved and load it:

If[! FileExistsQ["SavedState.mx"], 
 Print[ "Doesn't exist a saved interrupted calculation"], 
 Print["Loaded previous calculation"]; Get["SavedState.mx"]]

And then I execute the code (the call to customNMinimize includes the bestSolution code above):

result = customNMinimize[objectiveFunction, vars, constraints]

If I execute the code for the first time an abort the execution when it is, for example, in the third iteration (to simulate turning off the computer) the DumpSave creates a file. But when I execute it for a second time, the file is loaded and I get an error NMInimize: 2.3911` is not a valid variable because the Get is not loading the execution state but the variables' values.

How can I restore to the execution state where I left and continue with the NMinimize calculation without having to start from the beginning every time?

Attachments:
POSTED BY: Juan Barandiaran
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract