Introduction to simmer.bricks

Iñaki Ucar

2019-01-09

A motivating example

The simmer package provides a rich and flexible API to build discrete-event simulations. However, there are certain recurring patterns that are typed over and over again. The most common example is probably to spend some time holding a resource. Let us consider the basic example from the Introduction to simmer:

which means that you must have this in mind if you want to use a rollback() to loop over some part of the trajectory.

In summary, the simmer.bricks package is a repository of simmer activity patterns like this one. See help(package="simmer.bricks") for a comprehensive list.

More compelling examples

Delayed release

Some simulations require a resource to become inoperative for some time after a release. It is possible to simulate this with simmer using a technique that we call delayed release. Basically, while an arrival releases the resource and continues the trajectory, a clone of the latter keeps the resource busy for the time required; finally, the clone is removed. The main problem is that this keeping the resource busy must be implemented in different ways depending on the resource type, i.e., whether it is preemptive or not.

This package encapsulates all this logic in a very easy-to-use brick called delayed_release():

If you are curious, you can print the trajectory above to see what happens behind the scenes.

Parallel tasks

Another common pattern is to set up a number of parallel tasks with clone(). This could be challenging if the original arrival had resources seized. Let us consider the following case, in which a doctor and a nurse are visiting patients in a hospital room:

This simulation fails. This is because the original arrival, which seized the room and follows the first path (doctor), finishes its duty in the first place. Given that wait = TRUE for the synchronize() activity, it means that the last clone to arrive there (the nurse in this case) continues, while the others are removed.

Solving this requires ensuring that the original arrival reaches the synchronize() activity in the last place (or in the first place if wait = FALSE), which can be tricky, as some asynchronous programming must be used. However, simmer.bricks provides the do_parallel() brick:

Interleaved resources

Assembly lines are chains of limited resources in which the current resource cannot be released until the next one is available. This class of problems can be solved with a pattern called interleaved resources. Such pattern uses auxiliary resources to guard the access to the second and subsequent resources in the chain, serving as a token to the guarded resource. As a consequence, if a resource is blocked for some reason, its tokens will exhaust eventually, and thus the blockage will propagate backwards.

Let us consider a chain of two machines, A and B, whose service times are 1 and 2 respectively. Then, the chain of resources can be set up as follows:

As can be seen, the interleave brick uses an auxiliary resource called "B_token" that must be defined too. If machine B has capacity=1 and queue_size=1, then "B_token" must have capacity=2 (B’s capacity + queue size) and queue_size=Inf, to avoid dropping arrivals.

In the simuation above, three arrivals are processed in machine A during 1 unit of time. Then the first two successfully seize a token to B, but the last arrival has to wait until one of them leave B before releasing A.

Contributing

If you know about more patterns that you would like to see included in simmer.bricks, please, open an issue or a pull request on GitHub.