Methods for creating monitor
objects for simulation environments.
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))
A monitor
object.
an identifier to show when printed.
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.
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
).
a function to retrieve the attributes table. It must accept
the xptr
as a first argument, even if it is not needed.
a function to retrieve the resources table. It must accept
the xptr
as a first argument, even if it is not needed.
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.
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.
directory where files will be created (must exist).
whether to keep files on exit. By default, files are removed.
separator character.
file extension to use.
function that will be used to read the files.
a list of further arguments for reader
.
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"
.
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
Run the code above in your browser using DataLab