-
Notifications
You must be signed in to change notification settings - Fork 10
Agent Multigroup
# the transmission rate matrix
beta = matrix(c(0.4, 0.2, 0.2, 0.3), nrow=2)
# the recovery rate
gamma = newExpWaitingTime(0.2)
# the rate leaving the latent stage and becoming infectious
sigma=0.5
# the population size
N = 10000
# initialize a simulation with N agents
sim = Simulation$new(N)
for (i in 1:n) {
sim$setState(i, list(if (i <= 20) "I" else "S", group=i %% 2))
}
A counter counts a specific type of event changes. It take three arguments
- name: the name of the counter, which is the name of the observation variable
- from: its meaning depends on the following argument.
- to: optional. If to is NULL, then counter counts the number of agents in the state that matches from. Otherwise, it logs the transitions that agent jumped from a state that matched "from" to one matched "to".
A state matches if the the corresponding domains in "from" (or "to") have identical values as the agent's state.
sim$addLogger(newCounter("S1", from=list("S", group=0)))
sim$addLogger(newCounter("S2", list("S", group=1)))
sim$addLogger(newCounter("E1", list("E", group=0)))
sim$addLogger(newCounter("E2", list("E", group=1)))
sim$addLogger(newCounter("I1", list("I", group=0)))
sim$addLogger(newCounter("I2", list("I", group=1)))
sim$addLogger(newCounter("R1", list("R", group=0)))
sim$addLogger(newCounter("R2", list("R", group=1)))
We create a random mixing object and add it to the simulation
m = newRandomMixing()
sim$addContact(m)
There are two types of transitions.
- Spontaneous transitions, e.g., showing system or recovery.
- Transitions caused by contacts.
The transition rules are specified by a formula:
- "->" denotes the direction of transition.
- A contact transition needs to have two agents state specified, the agent who initiates the contact, and the contacting agent. There states are specified by the "+" operator. The first is the state of the initiator, the second is the state of the contact.
- A contact transition is always associated with a contact object, which determines the contacts of an agent. The contact is specified after "~".
Each transition has to have a waiting time, which must be exponentially distributed. It can be a number specifying the rate. Though not used in the example, a transition can have a to_change_callback and a changed_callback. The first callback is a function that return a logical value. If TRUE, the transition will occur. Otherwise the transition will be prevented. The latter callback is executed after the state change happened.
sim$addTransition("E"->"I", sigma)
sim$addTransition("I"->"R", gamma)
sim$addTransition(list("I", group=0) + list("S", group=0) ->
list("I", group=0) + list("E", group=0) ~ m, beta[1, 1])
sim$addTransition(list("I", group=0) + list("S", group=1) ->
list("I", group=0) + list("E", group=1) ~ m, beta[1, 2])
sim$addTransition(list("I", group=1) + list("S", group=0) ->
list("I", group=1) + list("E", group=0) ~ m, beta[2, 1])
sim$addTransition(list("I", group=1) + list("S", group=1) ->
list("I", group=1) + list("E", group=1) ~ m, beta[2, 2])
result = sim$run(0:250)
print(result)