Skip to content
junlingm edited this page Mar 7, 2023 · 1 revision

Event Class

This is an R6 class to create and represent an event.

Constructor

Usage

    initialize = function(time, handler)

Arguments

  • time: the time that this event will occur. A length-1 numeric vector.
  • handler: an R function that handles the event when it occurs.

Details

The R handler function should take exactly 3 arguments

  • time: the current time in the simulation
  • sim: the simulation object, an external pointer
  • agent: the agent to whom this event is attached to. The return value of the handler function is ignored.

Active Fields

Event time

The field $time retrieves the event time

The C++ pointer

The field $get retrieves the C++ pointer to the C++ Event object. This can be used in the schedule or ``unschedule``` function below.

C++ Functions

Create a new event

Usage

    newEvent = function(time, handler)

Arguments

  • time: the time that this event will occur. A number.
  • handler: an R function that handles the event when it occurs.

Return value

An external pointer, which can then be passed to functions such as schdule and unschedule.

Details

The R handler function should take exactly 3 arguments

  • time: the current time in the simulation
  • sim: the simulation object, an external pointer
  • agent: the agent to whom this event is attached to.

The return value of the handler function is ignored.

This function avoids the overhead of an R6 class, and is thus faster. This is the recommended method to create an event in an event handler.

Example

# This handler prints increases a counter in the state of the 
# Simulation object, and schedule another event every 0.1 time unit.
handler = function(time, sim, agent) {
  x = getState(sim)
  x$counter = x$counter + 1
  setState(sim, x)
  schedule(agent, newEvent(time + 0.1, handler))
}
# create a new simulation with no agents. but the simulation itself is
# an agent. So we can use all the methods of agent
sim = Simulation$new()
# set the state of the simulation, initialize the counter
sim$state = list(counter = 0)
# schedule a new event at time 0
sim$schedule(Event$new(0, handler))
# add a logger for the counter. Note that, because sim is an R6 class
# to use it in the newStateLogger function, we need to access the 
# external pointer using its $get method
sim$addLogger(newStateLogger("counter", sim$get, "counter"))
# run the simulation for 10 time units.
print(sim$run(0:10))

Interestingly, the counts are not exactly in 10 event time unit. Firstly, report always happen before event, so event at time 0 is not counted in the time interval 0 to 1. Secondly, the event time is stored as a numeric value with increments of 0.1, which is subject to rounding errors. So some the the integer tiome eventsmay be before the reporting and some may be after.