Skip to content

Commit

Permalink
Update individual dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar committed Feb 13, 2024
1 parent 409fc7a commit 969e50a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Remotes:
Additional_repositories:
https://rstub.r-universe.dev
Imports:
individual (>= 0.1.12),
individual (>= 0.1.13),
malariaEquilibrium (>= 1.0.1),
Rcpp,
statmod,
Expand Down
9 changes: 6 additions & 3 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ run_simulation <- function(
#' @param parameters a named list of parameters to use
#' @param correlations correlation parameters
#' @param initial_state the state from which the simulation is resumed
#' @param restore_random_state if TRUE, restore the random number generator's state from the checkpoint.
#' @return a list with two entries, one for the dataframe of results and one for the final
#' simulation state.
run_resumable_simulation <- function(
timesteps,
parameters = NULL,
correlations = NULL,
initial_state = NULL
initial_state = NULL,
restore_random_state = FALSE
) {
random_seed(ceiling(runif(1) * .Machine$integer.max))
if (is.null(parameters)) {
Expand All @@ -130,7 +132,7 @@ run_resumable_simulation <- function(
lagged_infectivity <- create_lagged_infectivity(variables, parameters)

stateful_objects <- unlist(list(
RandomState$new(),
RandomState$new(restore_random_state),
correlations,
vector_models,
solvers,
Expand All @@ -157,7 +159,8 @@ run_resumable_simulation <- function(
variables = variables,
events = unlist(events),
timesteps = timesteps,
state = initial_state$individual
state = initial_state$individual,
restore_random_state = restore_random_state
)

final_state <- list(
Expand Down
10 changes: 9 additions & 1 deletion R/stateful.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ restore_state <- function(state, objects) {
#' @noRd
RandomState <- R6::R6Class(
'RandomState',
private = list(
restore_random_state = NULL
),
public = list(
initialize = function(restore_random_state) {
private$restore_random_state <- restore_random_state
},
save_state = function() {
random_save_state()
},
restore_state = function(state) {
random_restore_state(state)
if (private$restore_random_state) {
random_restore_state(state)
}
}
)
)
6 changes: 5 additions & 1 deletion tests/testthat/test-resume.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ test_that('Simulation can be resumed', {

set.seed(1)
first_phase <- run_resumable_simulation(initial_timesteps, parameters)
second_phase <- run_resumable_simulation(total_timesteps, parameters, initial_state=first_phase$state)
second_phase <- run_resumable_simulation(
total_timesteps,
parameters,
initial_state=first_phase$state,
restore_random_state=TRUE)

set.seed(1)
uninterrupted_run <- run_simulation(total_timesteps, parameters=parameters)
Expand Down

0 comments on commit 969e50a

Please sign in to comment.