Skip to content

Contact

junlingm edited this page Mar 7, 2023 · 2 revisions

Contact Class

This is an R6 class that implements a contact pattern in R

The main task of the class is to return the contacts of a given agent. Each object of this class is associated to a population. A population may have multiple contacts attached, e.g., a random mixing contact pattern and a network contact pattern.

This class must be subclassed in order to implement specific functionality. To subclass, we must implement three methods, namely contact, addAgent, and build. See more details in the documentation of each method.

Constructor

Usage

    initialize = function()

The contacts of an agent

Usage

    contact = function(time, agent)

Arguments

  • time: the current time in the simulation, a number
  • agent: the agent whose contacts are requested. An external pointer

Return value

a list of external pointers pointing to the contacting agents

Details

This method must

Add an agent

Usage

    addAgent = function(agent)

Argument

  • agent: the agent whose contacts are requested. An external pointer

Details

When an agent is added to a population, it is added to each of the contact patterns. When a contact pattern is added to a population, all agents in a population is added to the contact pattern, one by one.

Note that, immediately before the simulation is run, while reporting the states to the simulation object, the population will call the build method for each Contact object. Thus a contact object may choose to ignore addin agents before build is called, and handle all agents within the finalize method. However, the contact object must handle adding an agent after build is called.

Build the contact pattern

Usage

    build = function()

Details

This method is called immediately before the simulation is run, when the attached population reports the states to the simulation object. Thus this method can be considered as a callback function to notify the contact object the population state, such as its agents, states, events, and contact patterns are all initialized, so the contact pattern should finish initialization, for example, building the contact network.

This is needed because some contact patterns, such as a configuration-model contact network, cannot be built while adding agents one by one. It must be generated when all agents are present. This is unlike the Albert-Barabasi networkm which can be built while adding the agents.

When the function is called, the active field private$population field is available, which is the population that it is attached to.

Active Fields

The C++ Pointer

$get is the external pointer pointing to the C++ RContact object.

Attached to a population

$attached is a logical value indicating whether the object has been attached to a population

Exposed C++ Functions

Creates a random mixing pattern

Usage

   newRandomMixing = function()

Return value

an external pointer.

Details

The returned external pointer can then be passed to the addContact method of a population (or simulation).

examples

# creates a simulation with 100 agents
sim = Simulation$new(100)
# add a random mixing contact pattern for these agents.
sim$addContact(newRandomMixing())

Creates a random network using the configuration model

Usage

  newConfigurationModel = function(rng)

Argument

  • rng: a function that generates random degrees. Please see the Details sub-sub-section below.

Return value

an external pointer.

Details

The function rng should take exactly one argument n for the number of degrees to generate, and should return an integer vector of length n.

Example

The following example generates the degrees for a Poisson random network with a mean degree 5.

# creates a simulation with 100 agents
sim = Simulation$new(100)
# add a Poisson network with a mean degree 5
sim$addContact(newConfigurationModel(function(n) rpois(n, 5)))