I am running a large genetic simulation in the cloud.
The cost of the cloud resources can be drastically reduced by configuring the vm to be preemptible.
In order to recover from a preemption, I save the state of the agents of my population at the beginning of each generation, and when restarting, I load the state, and continue from the generation last completed, rather than having to evolve the population from scratch every time.
This has been working well, but now, for test purposes, I want to have repeatable simulations.
I seed my random number generator (currently using std::mt19937
) with a known seed value, and this works until such time as my vm is preempted.
When I reload the current generation, I need to reload the random number generator's state at the same time, so that it too "picks up where it left off".
I have found that I can just run the generator through a tight loop, discarding all the values it would have generated until I get to the iteration I want to resume from:
const std::size_t generation = load_state_from_file();
std::mt19937 engine;
engine.seed(constant_seed_value);
// discard the first N generated values
for (std::size_t i = 0; i < generation; ++i)
engine();
Running the random number generator through a tight loop feels a bit ugly, and potentially slow if the number of generations is very high, and I'm wondering if there is a better way to do this?
Aucun commentaire:
Enregistrer un commentaire