{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Single simulation\n", "\n", "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.\n", "\n", "There are several preconfigured experiments available that can be launched instantly. We will have a look on them too.\n", "\n", "Let's import the relevant classes :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext param.ipython\n", "import panel as pn\n", "\n", "pn.extension()\n", "\n", "\n", "import larvaworld as lw\n", "from larvaworld.lib import reg\n", "from larvaworld.lib.sim.single_run import ExpRun\n", "from larvaworld.lib.reg.generators import ExpConf\n", "\n", "# Setting the verbosity level to 0 to get more information\n", "lw.VERBOSE = 1\n", "\n", "# Tutorial safety switches (avoid GUI/heavy compute by default)\n", "RUN_SIM_DEMO = True\n", "RUN_GUI_DEMO = False\n", "RUN_ANALYZE_DEMO = False\n", "\n", "DEMO_EXPERIMENT_ID = \"chemorbit\"\n", "DEMO_N = 5\n", "DEMO_DURATION_MIN = 0.1\n", "DEMO_SCREEN_KWS = {} # headless by default" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A look at the Exp configuration class makes it easy to get an idea of the involved arguments:\n", "\n", "- General simulation arguments (duration, timestep etc)\n", "- Environment configuration\n", "- Parameters to be recorded from agents and their post-simulation analysis \n", "- Larva groups \n", " \n", "BTW one of the preconfigured experiments can be called via the *experiment* argument." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from larvaworld.lib.reg.larvagroup import LarvaGroup\n", "\n", "%params LarvaGroup" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Show the attributes of the ExpConf class\n", "%params ExpConf\n", "\n", "# Show the attributes of the ExpConf class as a nested dictionary\n", "ExpConf.param" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The preconfigured experiment configurations can be inspected and selected by a unique ID" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Print all available experiment configuration IDs\n", "ids = reg.conf.Exp.confIDs\n", "print(ids)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Select the experiment configuration with the ID \"chemorbit\"\n", "conf = reg.conf.Exp.getID(\"chemorbit\")\n", "\n", "# Print the keys of the configuration\n", "print(conf.keylist, \"\\n\")\n", "\n", "# Print the \"larva_groups\" key of the configuration\n", "print(conf.larva_groups, \"\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The simulation launcher accepts also several runtype arguments :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Show the attributes of the ExpRun class\n", "%params ExpRun\n", "\n", "# Show the attributes of the ExpRun class as a nested dictionary\n", "ExpRun.param" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A single simulation of a stored experiment configuration can be launched by passing the respective ID to the launcher. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define the experiment's ID\n", "id = DEMO_EXPERIMENT_ID" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Default option that provides more control\n", "r = ExpRun(\n", " experiment=id, N=DEMO_N, duration=DEMO_DURATION_MIN, screen_kws=DEMO_SCREEN_KWS\n", ")\n", "\n", "# Print the \"larva_groups\" parameter of the experiment (as defined in the configuration)\n", "print(r.p.larva_groups, \"\\n\")\n", "\n", "if RUN_SIM_DEMO:\n", " r.simulate()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Fast option\n", "r = ExpRun.from_ID(id, N=5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The screen visualization arguments for the experiment (GUI)\n", "# NOTE: This opens a pygame window; keep it opt-in for docs builds.\n", "screen_kws = {\"vis_mode\": \"video\"}\n", "\n", "if RUN_GUI_DEMO:\n", " r = ExpRun(\n", " experiment=id, N=DEMO_N, duration=DEMO_DURATION_MIN, screen_kws=screen_kws\n", " )\n", " r.simulate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that the simulated datasets have been generated they can be analyzed to produce figures. \n", "\n", "The analysis is predefined and experiment-specific.\n", "\n", "We can inspect the registered plots and proceed to analysis" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r.graphgroups" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if RUN_ANALYZE_DEMO:\n", " r.analyze()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "larvaworld_autoversioning", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" } }, "nbformat": 4, "nbformat_minor": 2 }