monitor: Create a Monitor

View source: R/monitor-methods.R

monitorR Documentation

Create a Monitor

Description

Methods for creating monitor objects for simulation environments.

Usage

monitor(name, xptr, get_arrivals, get_attributes, get_resources,
  handlers = NULL, finalizer = NULL)

monitor_mem()

monitor_delim(path = tempdir(), keep = FALSE, sep = " ", ext = ".txt",
  reader = read.delim, args = list(stringsAsFactors = FALSE))

monitor_csv(path = tempdir(), keep = FALSE, reader = read.csv,
  args = list(stringsAsFactors = FALSE))

Arguments

name

an identifier to show when printed.

xptr

an external pointer pointing to a C++ object derived from the abstract class simmer::Monitor. See C++ API for further details and, in particular, the simmer/monitor.h header.

get_arrivals

a function to retrieve the arrivals tables. It must accept the xptr as a first argument, even if it is not needed, and a boolean per_resource as a second argument (see get_mon_arrivals).

get_attributes

a function to retrieve the attributes table. It must accept the xptr as a first argument, even if it is not needed.

get_resources

a function to retrieve the resources table. It must accept the xptr as a first argument, even if it is not needed.

handlers

an optional list of handlers that will be stored in a slot of the same name. For example, monitor_mem does not use this slot, but monitor_delim and monitor_csv store the path to the created files.

finalizer

an optional one-argument function to be called when the object is destroyed. For example, monitor_mem does not require any finalizer, but monitor_delim and monitor_csv use this to remove the created files when the monitor is destroyed.

path

directory where files will be created (must exist).

keep

whether to keep files on exit. By default, files are removed.

sep

separator character.

ext

file extension to use.

reader

function that will be used to read the files.

args

a list of further arguments for reader.

Details

The monitor method is a generic function to instantiate a monitor object. It should not be used in general unless you want to extend simmer with a custom monitor.

The in-memory monitor is enabled by default (memory_mem), and it should the fastest.

For large simulations, or if the RAM footprint is an issue, you may consider monitoring to disk. To that end, monitor_delim stores the values in flat delimited files. The usual get_mon_* methods retrieve data frames from such files using the reader provided. By default, read.delim is used, but you may consider using faster alternatives from other packages. It is also possible to keep the files in a custom directory to read and post-process them in a separate workflow.

monitor_csv is a special case of monitor_delim with sep="," and ext=".csv".

Value

A monitor object.

Examples

mon <- monitor_csv()
mon

env <- simmer(mon=mon) %>%
  add_generator("dummy", trajectory() %>% timeout(1), function() 1) %>%
  run(10)
env

read.csv(mon$handlers$arrivals) # direct access
get_mon_arrivals(env)           # adds the "replication" column


Bart6114/simmer documentation built on Oct. 2, 2024, 4:52 p.m.