Dynamic scheduling

Author: Lukas Breitwieser
This tutorial demonstrates that behaviors and operations can be added and removed during the simulation. This feature provides maximum flexibility to control which functions will be executed during the lifetime of a simulation.

Let's start by setting up BioDynaMo notebooks.

Define a helper variable

We define a standalone operation TestOp which prints out that it got executed and which removes itself from the list of scheduled operations afterwards. The same principles apply also for agent operations.

Let's define a little helper function which creates a new instance of TestOp and adds it to the list of scheduled operations.

Let's define a new behavior b2 which prints out when it gets executed and which adds a new operation with name OP2 to the simulation if a condition is met.

In this scenario the condition is defined as simulation time step == 1.

We define another behavior b1 which prints out when it gets executed, removes itself from the agent, and which adds behavior b2 to the agent.

Now all required building blocks are ready. Let's define the initial model: a single agent with behavior b1.

We also add a new operation to the simulation.

Let's simulate one iteration and think about the expected output.

Let's simulate another iteration.
This time we only expect output from B2. Remember that B1 and OP1 have been removed in the last iteration.

This time the condition in B2 is met and we expect to see an output line to tell us that a new instance of TestOp with name OP2 has been added to the simulation.

Let's simulate another iteration. This time we expect an output from B2 whose condition is not met in this iterations, and from OP2 that it got executed and removed from the simulation.

Let's simulate one last iteration. OP2 removed itself in the last iteration. Therefore, only B2 should be left. The condition of B2 is not met.

In summary: We initialized the simulation with B1 and OP1.

In iteration:

  1. B1 removed, B2 added, OP1 removed
  2. OP2 added
  3. OP2 removed