Simulation | R Documentation |
The Simulation class inherits the Population class. So a simulation
manages agents and their contact. Thus, the class also inherits the
Agent class. So a simulation can have its own state, and events attached
(scheduled) to it. In addition, it also manages all the transitions, using
its addTransition
method. ASt last, it maintains loggers, which
record (or count) the state changes, and report their values at specified
times.
ABM::R6Agent
-> ABM::R6Population
-> R6Simulation
new()
Simulation$new(simulation = 0, initializer = NULL)
simulation
can be either an external pointer pointing to a population object returned from newSimulation, or an integer specifying the population size, or a list
initializer
a function or NULL
If simulation is a number (the population size), then initializer can be a function that take the index of an agent and return its initial state. If it is a list, the length is the population size, and each element corresponds to the initial state of an agent (with the same index). Run the simulation
run()
Simulation$run(time)
time
the time points to return the logger values.
the returned list can be coerced into a data.frame object which first column is time, and other columns are logger results, each row corresponds to a time point.
The Simulation object first collect and log the states from all agents in the simulation, then set the current time to the time of the first event, then call the resume method to actually run it.
Continue running the simulation
a list of numeric vectors, with time and values reported by all logger.
resume()
Simulation$resume(time)
time
the time points to return the logger values.
the returned list can be coerced into a data.frame object which first column is time, and other columns are logger results, each row corresponds to a time point.
The Simulation object repetitively handle the events until the the last time point in "time" is reached. ASt each time point, the logger states are collected in put in a list to return. Add a logger to the simulation
a list of numeric vectors, with time and values reported by all logger.
addLogger()
Simulation$addLogger(logger)
logger,
an external pointer returned by functions like newCounter or newStateLogger.
without adding a logger, there will be no useful simulation results returned. Add a transition to the simulation
the simulation object itself (invisible)
addTransition()
Simulation$addTransition( rule, waiting.time, to_change_callback = NULL, changed_callback = NULL )
rule
is a formula that gives the transition rule
waiting.time
either an external pointer to a WaitingTime object such as one returned by newExpWaitingTime or newGammaWaitingTime, or a function (see the details section)
to_change_callback
the R callback function to determine if the change should occur. See the details section.
changed_callback
the R callback function after the change happened. See the details section.
If waiting.time is a function then it should take exactly one argument time, which is a numeric value holding the current value, and return a single numeric value for the waiting time (i.e., should not add time).
Formula can be used to specify either a spontaneous transition change, or a transition caused by a contact.
A spontaneous transition has the form from -> to, where from and to are state specifications. It is either a variable name holding a state (R list) or the list itself. The list can also be specified by state(...) instead of list(...)
For a spontaneous transition, the callback functions take the following two arguments
time: the current time in the simulation
agent: the agent who initiate the contact, an external pointer
A transition caused by contact, the formula needs to specify the states of both the agent who initiate the contact and the contact agent. The two states are connected by a + sign, the one before the
sign is the initiator, and the one after the sign is the contact. The transition must be associated with a Contact object, using a ~ operator. The Contact object must be specified by a variable name that hold the external pointer to the object (created by e.g., the newRandomMixing function) For example, suppose S=list("S"), I=list("I"), and m=newRandomMixing(sim), then a possible rule specifying an infectious agent contacting a susceptible agent causing it to become exposed can be specified by
I + S -> I + list("E") ~ m
For a transition caused by a contact, the callback functions take the third argument: 3. contact: the contact agent, an external pointer
the simulation object itself (invisible)
clone()
The objects of this class are cloneable with this method.
Simulation$clone(deep = FALSE)
deep
Whether to make a deep clone.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.