Modify Globals
You can add a new or additional value to the globals
struct with:
Vahana.set_global!
— Functionset_global!(sim::Simulation, name, value)
Set the value of the field name
of the globals
struct for simulation sim
.
In parallel simulations, set_global!
must be called on all processes, and with identical value
across all processes.
set_global!
must not be called within a transition function.
See also create_simulation
, mapreduce
, modify_global!
push_global!
and get_global
Vahana.push_global!
— Functionpush_global!(sim::Simulation, name, value)
In the case that a field of the globals
struct from the Simulation constructor is a vector (e.g. for time series data), push_global!
can be used to add a value to this vector, instead of writing set_global!(sim, name, push!(get_global(sim, name), value)
.
In parallel simulations, push_global!
must be called on all processes, and with identical value
across all processes.
push_global!
must not be called within a transition function.
See also create_model
, mapreduce
, set_global!
and get_global
Vahana.modify_global!
— Functionmodify_global!(sim::Simulation, name, f)
Modify the value of the name
field of the globals
structure for the simulation sim
using the f
function, which receives the current value as an argument.
This is a combination of set_global!
and get_global
: set_global!(sim, name, f(get_global(sim, name)))
.
modify_global!
must not be called within a transition function.
In parallel simulations, push_global!
must be called on all processes, and with identical value
across all processes.
See also create_simulation
, mapreduce
, set_global!
push_global!
and get_global
Base.mapreduce
— Functionmapreduce(sim, f, op, ::Type{T}; [kwargs ...])
Calculate an aggregated value, based on the state of all agents or edges of type T.
f
is applied to all of these agents or edges and then op
is used to reduce (aggregate) the values returned by f
.
mapreduce is calling Base.mapreduce, f
, op
and kwargs
are passed directly to mapreduce, while sim
and T
are used to determine the iterator.
The state of the globals struct (the globals
argument of the create_simulation
function can not be changed inside of a transition function, as a transition function is calculated on a per agent basis with many evaluations in parallel, and changes to the global layer must be a single, synchronized operation.