Skip to content

Population

Junling Ma edited this page Mar 8, 2023 · 7 revisions

Population Class

This is an R6 class that represents a population. A population is a collection of agents. There are two important tasks for a population:

  1. to manage the agents in it
  2. to define the contact patterns of the agents

The contact patterns are defined by objects of the Contact class that are associated with the population. A population may have multiple Contact objects, for example, one for random mixing, one for close contacts represented by a contact netowrk, and another for social network.

This class inherits the Agent class, i.e., a population is itself an agent, and can thus have its own state and events.

Constructor

Usage

    initialize = function(population=0, initializer=NULL)

Argument

  • population: either an external pointer pointing to a population object returned from newPopulation, or an integer specifying the population size, or a list

  • initializer: a function or NULL

Details

If population 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), and in this case, the initializer is ignored.

Add an agent

Usage

    addAgent = function(agent)

Argument

  • agent: either an object of the Agent class, or an external pointer returned from [newAgent](#Create an agent).

Return value

the Population object itself for chaining actions.

Details

The agent is scheduled in the population. If the population is already added to a simulation, the agent will report its state to the simulation.

Add a contact pattern

Usage

    addContact = function(contact)

Argument

Details

If the contact has already been added, this call does nothing.

return a specific agent by index

Usage

    getAgent = function(i)

Argument

  • i: the index of the agent (starting from 1)

Return value

an external pointer pointing to the agent

Set the state of an agent

Usage

    setState = function(i, state)

Arguments

  • i: the index of the agent (starting from 1)
  • state: a list holding the state to set, an a non-list value (equivalent to list(state))

Return value

The population object itself for chaining actions.

Active Field

The population size

The $size field retrieves the population size, which is an integer

Exposed C++ Functions

Create a new population

Usage

    newPopulation = function(n)

Argument

  • n: an integer specifying the population size.

Return value

An external pointer pointing to the created C++ object of the Population class. Note that it is not the [R6 Population class] (#Population-Class) described above. Instead, the Population class' constructor, if called with a population size as the argument, calls this function to create a C++ object, which can be retrieved by the $get active field.

Details

The population will be created with n individuals in it. These individuals have an empty state upon created. Note that individuals can be added later by the "add" method, the initial population size is for convenience, not required

Retrieve the population size

Usage

   getSize = function(population)

Argument

  • population: an external pointer to a population, for example, one returned by newPopulation