-
Notifications
You must be signed in to change notification settings - Fork 10
Agent
This is an R6 class that represent an agent. The key task of an agent is to maintain events, and handle them in the chronological order. Agents also maintain their states, which is a list of values. The events, when handled, operate on the state of the agent (or other agents).
During the simulation the agent with the earliest event in the simulation is picked out, unscheduled, then its earliest event is handled, which potentially causes the state change of the agent (or another agent in the simulation). The state change is then logged by loggers that recognize the state change.
An agent itself cannot handle the event. Instead, it has to be added to a simulation (or a population that itself is added to a simulation).
initialize = function(agent=NULL)
- agent: either an external pointer to an agent such as one returned by newAgent, or a list representing the initial state for creating a new agent, or NULL (an empty state)
schedule = function(event)
- event: an object of the R6 class Event, or an external pointer returned by newEvent
The agent itself to chain actions.
unschedule = function(event)
- event: an object of the R6 class Event, or an external pointer returned by newEvent
The agent itself to chain actions.
clearEvents = function()
The agent itself to chain actions.
leave = function()
the agent itself
When an agent leaves a population, it is not in the simulation any more. If it does not enter another population, it is then considered dead.
setDeathTime = function(time)
- time: the time of death, a numeric value
the agent itself
At the time of death, the agent is removed from the simulation. Calling it multiple times causes the agent to die at the earliest time.
The field $state
can access or change the state of an agent
The field $id
retrieves the ID of an agent. Each agent in a population gets a unique ID in the population.
The field $get
retrieves the C++ pointer to the C++ Agent object.
In this framework, a state is an R list, each named component is called a domain. The value of a domain can be any R value. The list can be at most one unnamed value, which corresponds to a domain with no name. This is useful if there is only one domain.
A state can be matched to an R list (called a rule in this case). The state matches the rule if and only if each domain (names of the list) in rule has the same value as in state. The domains in domains of the state not listed in rule are not matched. In addition, to match to a rule, the domain values must be either a number or a character. This is usefulfor identifying state changes. See the newCounter function and the Simulation class' addTransition method for more details.
newAgent = function(state, death_time)
- state: a list giving the initial state of the agent, or NULL (an empty list)
- death_time: the death time for the agent, an optional numeric value.
an external pointer pointing to the agent
Setting death_time is equivalent to calling the setDeathTime function.
- agent: an external pointer pointing to the agent, e.g., returned by the newAgent function or by the getAgent method of a
an integer value
Before an agent is added to a population, its id is 0. After it is added, its id is the index in the population
(starting from 1). If agent is an R6 object, then we should either use agent$id
or use getID(agent$get)
Get the sate of an agent. In this framework, a state is a list, each named component is called a domain. This function only updates the values of the domain given in the "value" list, while leave the other components not in the "value" list unchanged.
getState = function(agent)
- agent: an external pointer pointing to the agent, e.g., returned by the newAgent function or by the getAgent method of a
a list holding the state
If agent is an R6 object, then we should either use agent$schedule(event)
, or use schedule(agent$get, event)
set the state of an agent. In this framework, a state is a list, each named component is called a domain. This function only updates the values of the domain given in the "value" list, while leave the other components not in the "value" list unchanged.
setState = function(agent, state)
- agent: an external pointer pointing to the agent, e.g., returned by the newAgent function or by the getAgent method of a
- state: a list giving the components of the state to be undated.
the agent itself
If agent is an R6 object, then we should either use agent$schedule
, or use schedule(agent$get, event)
Check if the state of the agent matches the rule.
matchState = function(agent, rule)
- agent: an external pointer pointing to the agent, e.g., returned by the newAgent function or by the getAgent method of a
- rule a list holding the state to match against
@return a logical value
list) in rule has the same value as in state. The domains in domains of the state not listed in rule are not matched
schedule = function(agent, event)
- agent: an external pointer pointing to the agent, e.g., returned by the newAgent function or by the getAgent method of a
- event: an external pointer returned by the newEvent function
the agent itself
If agent is an R6 object, then we should use either agent$schedule(event)
or schedule(agent$get, event)
. Similarly, if event is an R6 object, then we should use
schedule(agent, event$get)
unschedule = function(agent, event)
- agent: an external pointer pointing to the agent, e.g., returned by the newAgent function or by the getAgent method of a
- event: an external pointer returned by the newEvent function
the agent itself
If agent is an R6 object, then we should use either agent$unschedule(event)
or unschedule(agent$get, event)
. Similarly, if event is an R6 object, then we should use unschedule(agent, event$get)
clearEvents = function(agent)
- agent: an external pointer pointing to the agent, e.g., returned by the newAgent function or by the getAgent method of a
the agent itself
If agent is an R6 object, then we should use either agent$clearEvents()
or clearEvents(agent$get)
Cause the agent to leave the population it was in. Without entering another population, it leaves the simulation.
leave = function(agent)
- agent: an external pointer pointing to the agent, e.g., returned by the newAgent function or by the getAgent method of a
setDeathTime = function(agent, time)
- agent: an external pointer pointing to the agent, e.g., returned by the newAgent function or by the getAgent method of a population.
- time: the death time for the agent, a numeric value.