mrc-ide / individual

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

Create a DoubleVectorVariable #52

Open giovannic opened 3 years ago

giovannic commented 3 years ago

DoubleVariables can only store one number per individual. But sometimes you want several numbers to represent a variable. So that you can access and update them together. e.g.

# there are 4 aspects of diarrhoea we want to model
diarrhoea_variables <- list(
  DoubleVariable$new(rep(0, parameters$population)), # diarrhoea_bacteria_prior 
  DoubleVariable$new(rep(0, parameters$population)), # diarrhoea_virus_prior
  DoubleVariable$new(rep(0, parameters$population)), # diarrhoea_parasite_prior
  DoubleVariable$new(rep(0, parameters$population)) # diarrhoea_rotavirus_prior
)

# in process...
for (v in diarrhoea_variables) {
  prior <- v$get_values()
  # do something with this aspect of diarrhoea (or perhaps combine with another aspect)
  v$queue_update(new_prior, changed_individuals)
}

It would be much easier to write:

diarrhoea_priors <- DoubleVectorVariable$new(matrix(0, ncol=4, nrow=parameters$population))

# in process...
priors <- diarrhoea_priors$get_values(
  individual_index = NULL, # get all individuals
  column_index = NULL # get all columns
)
# process your priors
diarrhoea_priors$queue_update(
  values = new_priors # matrix of updated priors
  individual_index = NULL, # update all individuals
  column_index = NULL # update all columns
) # will update the priors matrix at the end of the timestep
giovannic commented 3 years ago

There's also the possibility for a different design. Something like:

diarrhoea_priors <- DoubleVectorVariable$new(matrix(0, ncol=4, nrow=parameters$population))

# in process...
priors <- diarrhoea_priors$current() # gets the current matrix
# process your priors
diarrhoea_priors$set_next(new_priors) # will update the priors matrix at the end of the timestep