mrc-ide / individual

R Package for individual based epidemiological models
https://mrc-ide.github.io/individual
Other
30 stars 16 forks source link

Add initial checkpoint and restore functionality. #178

Closed plietar closed 9 months ago

plietar commented 9 months ago

This allows a user to run a simulation for a number of time steps and save the state of the simulation. The state can then be restored to continue the simulation.

This is,for example, useful to compare the impact of an intervention based on different parameters. The simulation can be run up to the point of an intervention, when the parameters' value don't matter. The simulation can then be resumed multiple times but with different intervention parameters each time.

For now, only variables are supported. Events will be added next.

codecov[bot] commented 9 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (3800945) 96.28% compared to head (a329052) 96.37%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## dev #178 +/- ## ========================================== + Coverage 96.28% 96.37% +0.08% ========================================== Files 36 36 Lines 1722 1764 +42 ========================================== + Hits 1658 1700 +42 Misses 64 64 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

plietar commented 9 months ago

~I still need to add some unit tests to make sure the checkpoint/restore works correctly~ DONE, but I'm interested in getting some feedback in the meantime.

The long term goal is to support https://github.com/mrc-ide/malariasimulation/issues/196. There's obviously still a long way to go for this, especially since malariasimulation maintains quite a lot of state that isn't visible to individual, but this is the first step in that direction.

plietar commented 9 months ago

Thanks for the review, I've applied the suggestions.

I think it would be much simpler to have malariasimulation save and restore its state buffers than re-creating them from the Variables (I'm not sure that would even be possible). It should be able to do that before/after calling simulation_loop, and let simulation_loop call restore_state to save the events and variables.

Another option would be to add an argument to simulation_loop for custom checkpoint-able objects, and have restore_state call checkpoint/restore on these automatically. Then the caller only has to define a pair of methods on each object but not worry about calling them.