Saving the Simulation for Running It Later

225 views
Skip to first unread message

ikazi...@gmail.com

unread,
Mar 15, 2021, 7:53:10 PM3/15/21
to python-simpy

Hi Everyone,

Is there anyway to save the simulation to run it later? I am guessing that would mean saving the state of the Simpy Environment and Resource  objects in memory / disk. Is there any standard way to do that?

Thanks in Advance!

Joseph Anderson

unread,
Mar 16, 2021, 12:40:14 PM3/16/21
to ikazi...@gmail.com, python-simpy
I'm assuming you don't just mean saving the .py file rather than running a simulation in the REPL? Rather, you want to interrupt a simulation that is in process, save the current state, and then continue running the simulation at a later time from where it left off?

I don't know how to do that, and I'm more than a little curious why you would even want to do that. Are you unable to run the full simulation in one sitting? Cause most models shouldn't take more than a couple minutes to complete. I think the most complicated one I've ever made only took maybe 5 minutes.

A possible workaround (depending on your reason) is that you can set the random seed for the simulation. Because the random number generator isn't truly random, the numbers it generates are completely deterministic if you know the random seed. So by controlling the random seed, you can run the same simulation file over and over and get the exact same output every time. Using the random standard library, just add:

    random.seed(42)  # Pick your own number

This would allow you to restart the simulation from scratch but still get the same results as the first partial run you did. I make a habit of setting the random seed in all my simulations just to have reproducible results. But it would also work if you had to stop the simulation for any reason and wanted to run it exactly the same way.


-Joe



--
You received this message because you are subscribed to the Google Groups "python-simpy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-simpy...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-simpy/68c40b4f-c387-430b-aeef-acdb696b05bbn%40googlegroups.com.

Peter Grayson

unread,
Mar 16, 2021, 1:13:23 PM3/16/21
to 'Naveen Chauhan' via python-simpy
There exist SimPy based models where simulation runtime can be on the order of many hours, simulating many millions of events. I work on one such model.

Being able to serialize/deserialize the state of a simulation would be an enormously useful feature. A couple examples:

1. Ability to stop/serialize and then later deserialize/resume a simulation would enable long-runtime simulations to be run on lowest-cost cloud compute resources (EC2 Spot Instances, Preemptable VMs on GCP, etc). I.e. by resuming your in-progress simulation later on a different node after the node the simulation started on is preempted.

2. Create periodic save points in long running simulations to reduce the time needed to reproduce an interesting simulation dynamic or bug.

3. Resume and extend the duration of a simulation that is producing interesting data and/or exhibiting interesting phenomenon.

If we could figure out a way to do this with SimPy, that would be amazing. That said, one of the key obstacles to doing this with snowpy is that Python generators are not pickle-able[1]. I'm sure there would be other challenges to pickling SimPy's state, not to mention appropriately capturing application-specific state.

Regarding seeds, I agree with Joe that managing the PRNG seed of a simulation is a good technique. It's both useful for the stated purpose of having deterministic simulations and for being able to run multiple differently-seeded simulations (often in parallel) to help measure variability of the system being modeled. This second technique can sometimes help avoid some of the problems with very long runtime simulations by replacing with several shorter runtime simulations.

Pete

Reply all
Reply to author
Forward
0 new messages