| Title: | Framework for Specifying and Simulating Individual Based Models |
|---|---|
| Description: | A framework which provides users a set of useful primitive elements for specifying individual based simulation models, with special attention models for infectious disease epidemiology. Users build models by specifying variables for each characteristic of individuals in the simulated population by using data structures exposed by the package. The package provides efficient methods for finding subsets of individuals based on these variables, or cohorts. Cohorts can then be targeted for variable updates or scheduled for events. Variable updates queued during a time step are executed at the end of a discrete time step, and the code places no restrictions on how individuals are allowed to interact. These data structures are designed to provide an intuitive way for users to turn their conceptual model of a system into executable code, which is fast and memory efficient. |
| Authors: | Giovanni Charles [aut, cre] (ORCID: <https://orcid.org/0000-0002-7024-1200>), Sean L. Wu [aut] (ORCID: <https://orcid.org/0000-0002-5781-9493>), Peter Winskill [aut] (ORCID: <https://orcid.org/0000-0003-3001-4959>), Paul Liétar [aut] (ORCID: <https://orcid.org/0009-0000-3813-6227>), Imperial College of Science, Technology and Medicine [cph] |
| Maintainer: | Giovanni Charles <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.19 |
| Built: | 2026-06-08 10:34:57 UTC |
| Source: | https://github.com/mrc-ide/individual |
Simulate a process where individuals in a given from state
advance to the to state each time step with probability rate.
bernoulli_process(variable, from, to, rate)bernoulli_process(variable, from, to, rate)
variable |
a categorical variable. |
from |
a string representing the source category. |
to |
a string representing the destination category. |
rate |
the probability to move individuals between categories. |
a function which can be passed as a process to simulation_loop.
This is a data structure that compactly stores the presence of
integers in some finite set (max_size), and can
efficiently perform set operations (union, intersection, complement, symmetric
difference, set difference).
WARNING: All operations are in-place so please use $copy
if you would like to perform an operation without destroying your current bitset.
This class is defined as a named list for performance reasons, but for most intents and purposes it behaves just like an R6 class.
new()
create a bitset.
Bitset$new(size, from)
sizethe size of the bitset.
frompointer to an existing IterableBitset to use; if NULL
make empty bitset, otherwise copy existing bitset.
insert()
insert into the bitset.
b$insert(v)
van integer vector of elements to insert.
remove()
remove from the bitset.
b$remove(v)
van integer vector of elements (not indices) to remove.
clear()
clear the bitset.
b$clear()
size()
get the number of elements in the set.
b$size()
or()
to "bitwise or" or union two bitsets.
b$or(other)
otherthe other bitset.
and()
to "bitwise and" or intersect two bitsets.
b$and(other)
otherthe other bitset.
not()
to "bitwise not" or complement a bitset.
b$not(inplace)
inplacewhether to overwrite the current bitset, default = TRUE
xor()
to "bitwise xor" get the symmetric difference of two bitset (keep elements in either bitset but not in their intersection).
b$xor(other)
otherthe other bitset.
set_difference()
Take the set difference of this bitset with another
(keep elements of this bitset which are not in other)
b$set_difference(other)
otherthe other bitset.
sample()
sample a bitset.
b$sample(rate)
ratethe success probability for keeping each element, can be a single value for all elements or a vector of unique probabilities for keeping each element.
choose()
choose k random items in the bitset.
b$choose(k)
kthe number of items in the bitset to keep. The selection of
these k items from N total items in the bitset is random, and
k should be chosen such that .
copy()
returns a copy of the bitset.
In cases where a destination bitset already exists, it may be more
performant to use the copy_from method instead.
b$copy()
copy_from()
overwrite the value of the bitset from another bitset.
This is similar to calling other$copy(), but can be more
efficient by reusing the resources of the existing bitset.
b$copy_from(other)
otherthe other bitset.
to_vector()
return an integer vector of the elements stored in this bitset.
b$to_vector()
This non-modifying function returns the number of intersecting
elements between two bitsets a and b. This should be faster than
writing a$copy()$and(b)$size() as it avoids the memory allocations of $copy().
bitset_count_and(a, b)bitset_count_and(a, b)
a |
a |
b |
another |
Renders the number of individuals in each category.
categorical_count_renderer_process(renderer, variable, categories)categorical_count_renderer_process(renderer, variable, categories)
renderer |
a |
variable |
a |
categories |
a character vector of categories to render. |
a function which can be passed as a process to simulation_loop.
Represents a categorical variable for an individual.
This class should be used for discrete variables taking values in
a finite set, such as infection, health, or behavioral state. It should
be used in preference to IntegerVariable
if possible because certain operations will be faster.
new()
Create a new CategoricalVariable
CategoricalVariable$new(categories, initial_values)
categoriesa character vector of possible values
initial_valuesa character vector of the initial value for each individual
get_index_of()
return a Bitset for individuals with the given values
CategoricalVariable$get_index_of(values)
valuesthe values to filter
get_size_of()
return the number of individuals with the given values
CategoricalVariable$get_size_of(values)
valuesthe values to filter
get_categories()
return a character vector of possible values. Note that the order of the returned vector may not be the same order that was given when the variable was intitialized, due to the underlying unordered storage type.
CategoricalVariable$get_categories()
get_values()
return the value of the variable for the given individuals
CategoricalVariable$get_values(index = NULL)
indexthe indices of individuals whose categories will be returned
queue_update()
queue an update for this variable
CategoricalVariable$queue_update(value, index)
valuethe new value
indexthe indices of individuals whose value will be updated
to the one specified in value. This may be either a vector of integers or
a Bitset.
queue_extend()
extend the variable with new values
CategoricalVariable$queue_extend(values)
valuesto add to the variable
queue_shrink()
shrink the variable
CategoricalVariable$queue_shrink(index)
indexa bitset or vector representing the individuals to remove
size()
get the size of the variable
CategoricalVariable$size()
.update()
CategoricalVariable$.update()
.resize()
CategoricalVariable$.resize()
save_state()
save the state of the variable
CategoricalVariable$save_state()
restore_state()
restore the variable from a previously saved state.
CategoricalVariable$restore_state(timestep, state)
timestepthe timestep at which simulation is resumed. This parameter's value is ignored, it only exists to conform to a uniform interface with events.
statethe previously saved state, as returned by the
save_state method. NULL is passed when restoring from a saved
simulation in which this variable did not exist.
clone()
The objects of this class are cloneable with this method.
CategoricalVariable$clone(deep = FALSE)
deepWhether to make a deep clone.
Represents a continuous variable for an individual.
new()
Create a new DoubleVariable.
DoubleVariable$new(initial_values)
initial_valuesa numeric vector of the initial value for each individual.
get_values()
get the variable values.
DoubleVariable$get_values(index = NULL)
indexoptionally return a subset of the variable vector. If
NULL, return all values; if passed a Bitset
or integer vector, return values of those individuals.
get_index_of()
return a Bitset giving individuals
whose value lies in an interval .
DoubleVariable$get_index_of(a, b)
alower bound
bupper bound
get_size_of()
return the number of individuals whose value lies in an interval
Count individuals whose value lies in an interval .
DoubleVariable$get_size_of(a, b)
alower bound
bupper bound
queue_update()
Queue an update for a variable. There are 4 types of variable update:
Subset update: The argument index represents a subset of the variable to
update. The argument values should be a vector whose length matches the size of index,
which represents the new values for that subset.
Subset fill: The argument index represents a subset of the variable to
update. The argument values should be a single number, which fills the specified subset.
Variable reset: The index vector is set to NULL and the argument values
replaces all of the current values in the simulation. values should be a vector
whose length should match the size of the population, which fills all the variable values in
the population
Variable fill: The index vector is set to NULL and the argument values
should be a single number, which fills all of the variable values in
the population.
DoubleVariable$queue_update(values, index = NULL)
valuesa vector or scalar of values to assign at the index.
indexis the index at which to apply the change, use NULL for the
fill options. If using indices, this may be either a vector of integers or
a Bitset.
queue_extend()
extend the variable with new values
DoubleVariable$queue_extend(values)
valuesto add to the variable
queue_shrink()
shrink the variable
DoubleVariable$queue_shrink(index)
indexa bitset or vector representing the individuals to remove
size()
get the size of the variable
DoubleVariable$size()
.update()
DoubleVariable$.update()
.resize()
DoubleVariable$.resize()
save_state()
save the state of the variable
DoubleVariable$save_state()
restore_state()
restore the variable from a previously saved state.
DoubleVariable$restore_state(timestep, state)
timestepthe timestep at which simulation is resumed. This parameter's value is ignored, it only exists to conform to a uniform interface with events.
statethe previously saved state, as returned by the
save_state method. NULL is passed when restoring from a saved
simulation in which this variable did not exist.
clone()
The objects of this class are cloneable with this method.
DoubleVariable$clone(deep = FALSE)
deepWhether to make a deep clone.
Describes a general event in the simulation.
individual::EventBase -> Event
new()
Initialise an Event.
Event$new(restore = TRUE)
restoreif true, the schedule of this event is restored when restoring from a saved simulation.
schedule()
Schedule this event to occur in the future.
Event$schedule(delay)
delaythe number of time steps to wait before triggering the event, can be a scalar or a vector of values for events that should be triggered multiple times.
clear_schedule()
Stop a future event from triggering.
Event$clear_schedule()
.process_listener()
Event$.process_listener(listener)
.process_listener_cpp()
Event$.process_listener_cpp(listener)
.resize()
Event$.resize()
save_state()
save the state of the event
Event$save_state()
restore_state()
restore the event from a previously saved state.
If the event was constructed with restore = FALSE, the state
argument is ignored.
Event$restore_state(timestep, state)
timestepthe timestep at which simulation is resumed.
statethe previously saved state, as returned by the
save_state method. NULL is passed when restoring from a saved
simulation in which this variable did not exist.
clone()
The objects of this class are cloneable with this method.
Event$clone(deep = FALSE)
deepWhether to make a deep clone.
Common functionality shared between simple and targeted events.
add_listener()
Add an event listener.
EventBase$add_listener(listener)
listenerthe function to be executed on the event, which takes a single argument giving the time step when this event is triggered.
.timestep()
EventBase$.timestep()
.tick()
EventBase$.tick()
.process()
EventBase$.process()
clone()
The objects of this class are cloneable with this method.
EventBase$clone(deep = FALSE)
deepWhether to make a deep clone.
This non-modifying function returns a new Bitset
object of the same maximum size as the original but which only contains
those values at the indices specified by the argument other.
Indices in other may be specified either as a vector of logicals, a
vector of integers or as another bitset. If a vector of logicals is
specified, it must be of the same size as the bitset. Please note that
filtering by another bitset is not a "bitwise and" intersection, and will
have the same behavior as providing an equivalent vector of integer indices.
filter_bitset(bitset, other)filter_bitset(bitset, other)
bitset |
the |
other |
the values to keep (may be a vector of integers, logicals, or
another |
Simulates a two-stage process where all individuals
in a given source_state sample whether to leave or not with probability
rate; those who leave go to one of the destination_states with
probabilities contained in the vector destination_probabilities.
fixed_probability_multinomial_process( variable, source_state, destination_states, rate, destination_probabilities )fixed_probability_multinomial_process( variable, source_state, destination_states, rate, destination_probabilities )
variable |
a |
source_state |
a string representing the source state. |
destination_states |
a vector of strings representing the destination states. |
rate |
probability of individuals in source state to leave. |
destination_probabilities |
probability vector of destination states. |
a function which can be passed as a process to simulation_loop.
Simulates infection for age-structured models, where individuals contact each other at a rate given by some mixing (contact) matrix. The force of infection on susceptibles in a given age class is computed as:
Where is the matrix of contact rates, is the probability of infection
per contact. The per-capita probability of infection for susceptible individuals is then:
.
infection_age_process( state, susceptible, exposed, infectious, age, age_bins, p, dt, mixing )infection_age_process( state, susceptible, exposed, infectious, age, age_bins, p, dt, mixing )
state |
a |
susceptible |
a string representing the susceptible state (usually "S"). |
exposed |
a string representing the state new infections go to (usually "E" or "I"). |
infectious |
a string representing the infected and infectious state (usually "I"). |
age |
a |
age_bins |
the total number of age bins (groups). |
p |
the probability of infection given a contact. |
dt |
the size of the time step (in units relative to the contact rates in |
mixing |
a mixing (contact) matrix between age groups. |
a function which can be passed as a process to simulation_loop.
Represents a integer valued variable for an individual.
This class is similar to CategoricalVariable,
but can be used for variables with unbounded ranges, or other situations where part
of an individual's state is better represented by an integer, such as
household or age bin.
new()
Create a new IntegerVariable.
IntegerVariable$new(initial_values)
initial_valuesa vector of the initial values for each individual
get_values()
Get the variable values.
IntegerVariable$get_values(index = NULL)
indexoptionally return a subset of the variable vector. If
NULL, return all values; if passed a Bitset
or integer vector, return values of those individuals.
get_modulo_differences()
Return a vector of individuals with 0 modulo difference from input value and the distance being compared
IntegerVariable$get_modulo_differences(value, difference)
valuethe value to check
differencethe difference to check, e.g. difference = 2 checks whether the difference is even
get_index_of()
Return a Bitset for individuals with some subset of values.
Either search for indices corresponding to values in set, or
for indices corresponding to values in range . Either set
or a and b must be provided as arguments.
IntegerVariable$get_index_of(set = NULL, a = NULL, b = NULL)
seta vector of values (providing set means a,b are ignored)
alower bound
bupper bound
get_size_of()
Return the number of individuals with some subset of values.
Either search for indices corresponding to values in set, or
for indices corresponding to values in range . Either set
or a and b must be provided as arguments.
IntegerVariable$get_size_of(set = NULL, a = NULL, b = NULL)
seta vector of values (providing set means a,b are ignored)
alower bound
bupper bound
queue_update()
Queue an update for a variable. There are 4 types of variable update:
Subset update: The argument index represents a subset of the variable to
update. The argument values should be a vector whose length matches the size of index,
which represents the new values for that subset.
Subset fill: The argument index represents a subset of the variable to
update. The argument values should be a single number, which fills the specified subset.
Variable reset: The index vector is set to NULL and the argument values
replaces all of the current values in the simulation. values should be a vector
whose length should match the size of the population, which fills all the variable values in
the population
Variable fill: The index vector is set to NULL and the argument values
should be a single number, which fills all of the variable values in
the population.
IntegerVariable$queue_update(values, index = NULL)
valuesa vector or scalar of values to assign at the index
indexis the index at which to apply the change, use NULL for the
fill options. If using indices, this may be either a vector of integers or
a Bitset.
queue_extend()
extend the variable with new values
IntegerVariable$queue_extend(values)
valuesto add to the variable
queue_shrink()
shrink the variable
IntegerVariable$queue_shrink(index)
indexa bitset or vector representing the individuals to remove
size()
get the size of the variable
IntegerVariable$size()
.update()
IntegerVariable$.update()
.resize()
IntegerVariable$.resize()
save_state()
save the state of the variable
IntegerVariable$save_state()
restore_state()
restore the variable from a previously saved state.
IntegerVariable$restore_state(timestep, state)
timestepthe timestep at which simulation is resumed. This parameter's value is ignored, it only exists to conform to a uniform interface with events.
statethe previously saved state, as returned by the
save_state method. NULL is passed when restoring from a saved
simulation in which this variable did not exist.
clone()
The objects of this class are cloneable with this method.
IntegerVariable$clone(deep = FALSE)
deepWhether to make a deep clone.
Simulates a Bernoulli process where all individuals
in a given source state from sample whether or not
to transition to destination state to with a
individual probability specified by the DoubleVariable
object rate_variable.
multi_probability_bernoulli_process(variable, from, to, rate_variable)multi_probability_bernoulli_process(variable, from, to, rate_variable)
variable |
a |
from |
a string representing the source state. |
to |
a string representing the destination state. |
rate_variable |
|
a function which can be passed as a process to simulation_loop.
Simulates a two-stage process where all individuals
in a given source_state sample whether to leave or not with a
individual probability specified by the DoubleVariable
object rate_variable; those who leave go to one of the destination_states with
probabilities contained in the vector destination_probabilities.
multi_probability_multinomial_process( variable, source_state, destination_states, rate_variable, destination_probabilities )multi_probability_multinomial_process( variable, source_state, destination_states, rate_variable, destination_probabilities )
variable |
a |
source_state |
a string representing the source state. |
destination_states |
a vector of strings representing the destination states. |
rate_variable |
|
destination_probabilities |
probability vector of destination states. |
a function which can be passed as a process to simulation_loop.
This is a ragged array which stores doubles (numeric values).
new()
Create a new RaggedDouble
RaggedDouble$new(initial_values)
initial_valuesa vector of the initial values for each individual
get_values()
Get the variable values.
RaggedDouble$get_values(index = NULL)
indexoptionally return a subset of the variable vector. If
NULL, return all values; if passed an Bitset
or integer vector, return values of those individuals.
get_length()
Get the lengths of the indiviudal elements in the ragged array
RaggedDouble$get_length(index = NULL)
indexoptionally only get lengths for a subset of persons. If
NULL, return all lengths; if passed an Bitset
or integer vector, return lengths of arrays for those individuals.
queue_update()
Queue an update for a variable. There are 4 types of variable update:
Subset update: The argument index represents a subset of the variable to
update. The argument values should be a vector whose length matches the size of index,
which represents the new values for that subset.
Subset fill: The argument index represents a subset of the variable to
update. The argument values should be a single number, which fills the specified subset.
Variable reset: The index vector is set to NULL and the argument values
replaces all of the current values in the simulation. values should be a vector
whose length should match the size of the population, which fills all the variable values in
the population
Variable fill: The index vector is set to NULL and the argument values
should be a single number, which fills all of the variable values in
the population.
RaggedDouble$queue_update(values, index = NULL)
valuesa list of numeric vectors
indexis the index at which to apply the change, use NULL for the
fill options. If using indices, this may be either a vector of integers or
an Bitset.
queue_extend()
extend the variable with new values
RaggedDouble$queue_extend(values)
valuesto add to the variable
queue_shrink()
shrink the variable
RaggedDouble$queue_shrink(index)
indexa bitset or vector representing the individuals to remove
size()
get the size of the variable
RaggedDouble$size()
.update()
RaggedDouble$.update()
.resize()
RaggedDouble$.resize()
save_state()
save the state of the variable
RaggedDouble$save_state()
restore_state()
restore the variable from a previously saved state.
RaggedDouble$restore_state(timestep, state)
timestepthe timestep at which simulation is resumed. This parameter's value is ignored, it only exists to conform to a uniform interface with events.
statethe previously saved state, as returned by the
save_state method. NULL is passed when restoring from a saved
simulation in which this variable did not exist.
clone()
The objects of this class are cloneable with this method.
RaggedDouble$clone(deep = FALSE)
deepWhether to make a deep clone.
This is a ragged array which stores integers (numeric values).
new()
Create a new RaggedInteger
RaggedInteger$new(initial_values)
initial_valuesa vector of the initial values for each individual
get_values()
Get the variable values.
RaggedInteger$get_values(index = NULL)
indexoptionally return a subset of the variable vector. If
NULL, return all values; if passed an Bitset
or integer vector, return values of those individuals.
get_length()
Get the lengths of the indiviudal elements in the ragged array
RaggedInteger$get_length(index = NULL)
indexoptionally only get lengths for a subset of persons. If
NULL, return all lengths; if passed an Bitset
or integer vector, return lengths of arrays for those individuals.
queue_update()
Queue an update for a variable. There are 4 types of variable update:
Subset update: The argument index represents a subset of the variable to
update. The argument values should be a vector whose length matches the size of index,
which represents the new values for that subset.
Subset fill: The argument index represents a subset of the variable to
update. The argument values should be a single number, which fills the specified subset.
Variable reset: The index vector is set to NULL and the argument values
replaces all of the current values in the simulation. values should be a vector
whose length should match the size of the population, which fills all the variable values in
the population
Variable fill: The index vector is set to NULL and the argument values
should be a single number, which fills all of the variable values in
the population.
RaggedInteger$queue_update(values, index = NULL)
valuesa list of numeric vectors
indexis the index at which to apply the change, use NULL for the
fill options. If using indices, this may be either a vector of integers or
an Bitset.
queue_extend()
extend the variable with new values
RaggedInteger$queue_extend(values)
valuesto add to the variable
queue_shrink()
shrink the variable
RaggedInteger$queue_shrink(index)
indexa bitset or vector representing the individuals to remove
size()
get the size of the variable
RaggedInteger$size()
.update()
RaggedInteger$.update()
.resize()
RaggedInteger$.resize()
save_state()
save the state of the variable
RaggedInteger$save_state()
restore_state()
restore the variable from a previously saved state.
RaggedInteger$restore_state(timestep, state)
timestepthe timestep at which simulation is resumed. This parameter's value is ignored, it only exists to conform to a uniform interface with events.
statethe previously saved state, as returned by the
save_state method. NULL is passed when restoring from a saved
simulation in which this variable did not exist.
clone()
The objects of this class are cloneable with this method.
RaggedInteger$clone(deep = FALSE)
deepWhether to make a deep clone.
Class to render output for the simulation.
new()
Initialise a renderer for the simulation, creates the default state renderers.
Render$new(timesteps)
timestepsnumber of timesteps in the simulation.
set_default()
Set a default value for a rendered output renderers.
Render$set_default(name, value)
namethe variable to set a default for.
valuethe default value to set for a variable.
render()
Update the render with new simulation data.
Render$render(name, value, timestep)
namethe variable to render.
valuethe value to store for the variable.
timestepthe time-step of the data point.
to_dataframe()
Return the render as a data.frame.
Render$to_dataframe()
clone()
The objects of this class are cloneable with this method.
Render$clone(deep = FALSE)
deepWhether to make a deep clone.
Schedules a follow-up event as the result of an event firing.
reschedule_listener(event, delay)reschedule_listener(event, delay)
event |
|
delay |
the delay until the follow-up event. |
Restore the state of one or more simulation objects. The
specified objects are paired up with the relevant part of the state object,
and the restore_state method of each object is called.
If the list of object is named, more objects may be specified than were
originally present in the saved simulation, allowing a simulation to be
extended with more features upon resuming. In this case, the
restore_state method of the new objects is called with a NULL
argument. Conversly, the list of objects may omit certain entries, in which
case their state to be restored is ignored.
restore_object_state(timesteps, objects, state)restore_object_state(timesteps, objects, state)
timesteps |
the number of time steps that have already been simulated |
objects |
a simulation object (eg. a variable or event) or an arbitrarily nested list structure of such objects. |
state |
a saved simulation state for the given objects, as returned by
|
Restore the simulation state from a previous checkpoint. The state of passed events and variables is overwritten to match the state they had when the simulation was checkpointed.
restore_simulation_state(state, variables, events, restore_random_state)restore_simulation_state(state, variables, events, restore_random_state)
state |
the simulation state to restore, as returned by
|
variables |
the list of Variables |
events |
the list of Events |
restore_random_state |
if TRUE, restore R's global random number generator's state from the checkpoint. |
the time step at which the simulation should resume.
Save the state of a simulation object or set of objects.
save_object_state(objects)save_object_state(objects)
objects |
a simulation object (eg. a variable or event) or an arbitrarily nested list structure of such objects. |
the saved states of the objects. This has the same shape as the given
objects: if a list was passed as an argument, this returns the
corresponding list of saved states. If a singular object was passed, this
returns just that particular object's state.
Save the simulation state in an R object, allowing it to be
resumed later using restore_simulation_state.
save_simulation_state(timesteps, variables, events)save_simulation_state(timesteps, variables, events)
timesteps |
the number of time steps that have already been simulated |
variables |
the list of Variables |
events |
the list of Events |
the saved simulation state.
Run a simulation where event listeners take precedence over processes for state changes.
simulation_loop( variables = list(), events = list(), processes = list(), timesteps, state = NULL, restore_random_state = FALSE )simulation_loop( variables = list(), events = list(), processes = list(), timesteps, state = NULL, restore_random_state = FALSE )
variables |
a list of Variables |
events |
a list of Events |
processes |
a list of processes to execute on each timestep |
timesteps |
the end timestep of the simulation. If |
state |
a checkpoint from which to resume the simulation |
restore_random_state |
if TRUE, restore R's global random number generator's state from the checkpoint. |
Invisibly, the saved state at the end of the simulation, suitable for later resuming.
population <- 4 timesteps <- 5 state <- CategoricalVariable$new(c('S', 'I', 'R'), rep('S', population)) renderer <- Render$new(timesteps) transition <- function(from, to, rate) { return(function(t) { from_state <- state$get_index_of(from) state$queue_update( to, from_state$sample(rate) ) }) } processes <- list( transition('S', 'I', .2), transition('I', 'R', .1), transition('R', 'S', .05), categorical_count_renderer_process(renderer, state, c('S', 'I', 'R')) ) simulation_loop(variables=list(state), processes=processes, timesteps=timesteps) renderer$to_dataframe()population <- 4 timesteps <- 5 state <- CategoricalVariable$new(c('S', 'I', 'R'), rep('S', population)) renderer <- Render$new(timesteps) transition <- function(from, to, rate) { return(function(t) { from_state <- state$get_index_of(from) state$queue_update( to, from_state$sample(rate) ) }) } processes <- list( transition('S', 'I', .2), transition('I', 'R', .1), transition('R', 'S', .05), categorical_count_renderer_process(renderer, state, c('S', 'I', 'R')) ) simulation_loop(variables=list(state), processes=processes, timesteps=timesteps) renderer$to_dataframe()
Describes a targeted event in the simulation. This is useful for events which are triggered for a sub-population.
individual::EventBase -> TargetedEvent
new()
Initialise a TargetedEvent.
TargetedEvent$new(population_size)
population_sizethe size of the population.
schedule()
Schedule this event to occur in the future.
TargetedEvent$schedule(target, delay)
targetthe individuals to pass to the listener, this may be either a vector of integers or a Bitset.
delaythe number of time steps to wait before triggering the event, can be a scalar in which case all targeted individuals are scheduled for for the same delay or a vector of values giving the delay for that individual.
get_scheduled()
Get the individuals who are scheduled as a Bitset.
TargetedEvent$get_scheduled()
clear_schedule()
Stop a future event from triggering for a subset of individuals.
TargetedEvent$clear_schedule(target)
targetthe individuals to clear, this may be either a vector of integers or a Bitset.
queue_extend()
Extend the target size.
TargetedEvent$queue_extend(n)
nthe number of new elements to add to the index.
queue_extend_with_schedule()
Extend the target size and schedule for the new population.
TargetedEvent$queue_extend_with_schedule(delays)
delaysthe delay for each new individual.
queue_shrink()
Shrink the TargetedEvent.
TargetedEvent$queue_shrink(index)
indexthe individuals to remove from the event.
.process_listener()
TargetedEvent$.process_listener(listener)
.process_listener_cpp()
TargetedEvent$.process_listener_cpp(listener)
.resize()
TargetedEvent$.resize()
save_state()
save the state of the event
TargetedEvent$save_state()
restore_state()
restore the event from a previously saved state.
TargetedEvent$restore_state(timestep, state)
timestepthe timestep at which simulation is resumed.
statethe previously saved state, as returned by the
save_state method. NULL is passed when restoring from a saved
simulation in which this variable did not exist.
clone()
The objects of this class are cloneable with this method.
TargetedEvent$clone(deep = FALSE)
deepWhether to make a deep clone.
Updates the category of a sub-population as the result of an
event firing, to be used in the TargetedEvent
class.
update_category_listener(variable, to)update_category_listener(variable, to)
variable |
a |
to |
a string representing the destination category. |