Single simulation

In this tutorial, we will demonstrate how to launch a single virtual experiment. This is the default simulation mode in larvaworld and forms the backbone for evry other more complex mode.

There are several preconfigured experiments available that can be launched instantly. We will have a look on them too.

Let’s import the relevant classes :

%load_ext param.ipython
import panel as pn

pn.extension()


import larvaworld as lw
from larvaworld.lib import reg
from larvaworld.lib.sim.single_run import ExpRun
from larvaworld.lib.reg.generators import ExpConf

# Setting the verbosity level to 0 to get more information
lw.VERBOSE = 1

# Tutorial safety switches (avoid GUI/heavy compute by default)
RUN_SIM_DEMO = True
RUN_GUI_DEMO = False
RUN_ANALYZE_DEMO = False

DEMO_EXPERIMENT_ID = "chemorbit"
DEMO_N = 5
DEMO_DURATION_MIN = 0.1
DEMO_SCREEN_KWS = {}  # headless by default

A look at the Exp configuration class makes it easy to get an idea of the involved arguments:

  • General simulation arguments (duration, timestep etc)

  • Environment configuration

  • Parameters to be recorded from agents and their post-simulation analysis

  • Larva groups

BTW one of the preconfigured experiments can be called via the experiment argument.

from larvaworld.lib.reg.larvagroup import LarvaGroup

%params LarvaGroup
# Show the attributes of the ExpConf class
%params ExpConf

# Show the attributes of the ExpConf class as a nested dictionary
ExpConf.param

The preconfigured experiment configurations can be inspected and selected by a unique ID

# Print all available experiment configuration IDs
ids = reg.conf.Exp.confIDs
print(ids)
# Select the experiment configuration with the ID "chemorbit"
conf = reg.conf.Exp.getID("chemorbit")

# Print the keys of the configuration
print(conf.keylist, "\n")

# Print the "larva_groups" key of the configuration
print(conf.larva_groups, "\n")

The simulation launcher accepts also several runtype arguments :

# Show the attributes of the ExpRun class
%params ExpRun

# Show the attributes of the ExpRun class as a nested dictionary
ExpRun.param

A single simulation of a stored experiment configuration can be launched by passing the respective ID to the launcher.

# Define the experiment's ID
id = DEMO_EXPERIMENT_ID
# Default option that provides more control
r = ExpRun(
    experiment=id, N=DEMO_N, duration=DEMO_DURATION_MIN, screen_kws=DEMO_SCREEN_KWS
)

# Print the "larva_groups" parameter of the experiment (as defined in the configuration)
print(r.p.larva_groups, "\n")

if RUN_SIM_DEMO:
    r.simulate()
# Fast option
r = ExpRun.from_ID(id, N=5)
# The screen visualization arguments for the experiment (GUI)
# NOTE: This opens a pygame window; keep it opt-in for docs builds.
screen_kws = {"vis_mode": "video"}

if RUN_GUI_DEMO:
    r = ExpRun(
        experiment=id, N=DEMO_N, duration=DEMO_DURATION_MIN, screen_kws=screen_kws
    )
    r.simulate()

Now that the simulated datasets have been generated they can be analyzed to produce figures.

The analysis is predefined and experiment-specific.

We can inspect the registered plots and proceed to analysis

r.graphgroups
if RUN_ANALYZE_DEMO:
    r.analyze()