{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "_In this notebook, we create a dropdown menu to pre-configure the various experiments we might want to run in Netlogo. This notebook was originally built by Eric Shook (UMN) and modified by Colin D. Wren (UCCS)._\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Running Netlogo\n", "\n", "We cannot unfortunately run Netlogo interactively with its graphical user interface in this Notebook, served via Binder. What we _can_ do is run Netlogo in 'headless' mode - that is to say, without the graphics! Why would we do that? It can be faster, but it also allows us to run experiments, sweeping through various parameter settings, and getting the output in a csv file that we can then import into other programs for analysis. Running Netlogo in headless mode requires some rather complicated command-line commands. Here, we're using the cell below to create some drop-down menus to allow us to execute our experiments by hitting the 'Run ABM Experiment' button.\n", "\n", "We'll discuss how to set up your own experiments in a moment. For now, run the next code block:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "609419b42f6b4669bf4d285b97692c4e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Dropdown(description='Model path and name:', options=('Virus.nlogo',), value='Virus.nlogo')" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ddeefe788ab24edbb686436120e75dba", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Dropdown(description='BehaviorSpace experiment name:', options=('exp01', 'experiment1.xml'), value='exp01')" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3d2acb16c909429a9b6f3fc6bf3a5b75", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Text(value='test.csv', description='Output table name:')" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6e2e3e94ff03442daac64713910bc834", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Button(description='Run ABM Experiment', style=ButtonStyle())" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "ABM experiment running ...\n", "ABM experiment complete.\n" ] } ], "source": [ "import subprocess\n", "import ipywidgets as widgets\n", "from IPython.display import display\n", "\n", "def runabm(model,experiment,tablename):\n", " subprocess.check_output(\"./run-abm-script \"+model+\" \"+experiment+\" \"+tablename,shell=True)\n", "\n", "wmodel = widgets.Dropdown(description='Model path and name:',\n", " options=['Virus.nlogo'])\n", "\n", "wexp = widgets.Dropdown(description=\"BehaviorSpace experiment name:\",\n", " options=['exp01', 'experiment1.xml'])\n", "\n", "wtable = widgets.Text(description=\"Output table name:\",\n", " value='test.csv')\n", "\n", "def on_run_click(b):\n", " print(\"ABM experiment running ...\")\n", " runabm(wmodel.value,wexp.value,wtable.value)\n", " print(\"ABM experiment complete.\")\n", " \n", "wrun = widgets.Button(description=\"Run ABM Experiment\")\n", "wrun.on_click(on_run_click)\n", "\n", "display(wmodel,wexp,wtable,wrun)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...add hit that `Run ABM Experiment` button! Check the home page for the binder to see the results. If you'd like to see the Virus model running in Netlogo Web, right-click [this link](http://netlogoweb.org/launch#http://netlogoweb.org/assets/modelslib/Sample%20Models/Biology/Virus.nlogo) in a new browser window. \n", "\n", "## Adding other models & Experiments\n", "\n", "Do you see what you would have to do to add more models, or more experiments, to the options? You'd just upload them to this folder and add their name:\n", "```\n", "wmodel = widgets.Dropdown(description='Model path and name:',\n", " options=['Virus.nlogo', 'your-model-name-here.nlogo'])\n", "```\n", "\n", "There are a number of models already loaded with netlogo - you can find them in this notebook by navigating: NetLogo6.0.4 -> app -> models -> Sample_Models. You'd add them like so:\n", "\n", "```\n", " options=['Virus.nlogo', 'your-model-name-here.nlogo', '/home/jovyan/NetLogo 6.0.4/app/models/Sample_Models/Biology/Wolf_Sheep_Predation.nlogo'])\n", "```\n", "\n", "The experimental design is included in the .nlogo model code; we'll talk more about that in a moment. A model might have more than one experiments in it; each experiment has its own name and these can be added to the block above the same way the model names are.\n", "\n", "Finally, you can write in the box the name of the output csv file for your experiment run." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Designing another experiment\n", "\n", "The experiment is marked up with xml at the end of the [Virus.nlogo](Virus.nlogo) file:\n", "\n", "```\n", " \n", " setup\n", " go\n", " (ticks / 52) = 10\n", " count turtles with [ sick? ]\n", " count turtles with [ immune? ]\n", " count turtles with [ not sick? and not immune? ]\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "```\n", "\n", "To add another experiment to Virus, you copy and paste this code at the end of file just after the closing `` tag, making sure that your new experiment has a new name:\n", "\n", "```\n", " \n", "```\n", "\n", "But how do you know which values you can set, which parameters you can sweep through? If you look at the code for the model (or the [graphical representation](http://netlogoweb.org/launch#http://netlogoweb.org/assets/modelslib/Sample%20Models/Biology/Virus.nlogo)) you can see the names of all the variables that can be modified. In our original experiment, `infectiousness` had four separate settings - 45, 55, 65, 75. If instead we wanted to sweep through from a starting level to a finishing level in say increments of ten, we would need to specify the start, finish, and increment like this:\n", "```\n", "\n", "```\n", "\n", "Notice also the `` tag. Inside this tag we put whatever Netlogo code we want that does the measuring. As archaeologists, one thing that we are often concerned with is how these complex systems evolve over time. How 'time' is defined in a model is another question altogether, but we often will want to count the 'ticks' of the NetLogo cycle. Open [Virus.nlogo](https://hub.mybinder.org/user/o-date-abm3-ir95qw2z/edit/Virus.nlogo) and add the following metric to the experiment:\n", "\n", "```\n", "ticks\n", "```\n", "\n", "And then re-run the model!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Telling the experiment when to stop\n", "\n", "Finally, notice also the `` tag. We have to tell the model when to stop!\n", "\n", "Try to define and add a second experiment to [Virus.nlogo](https://hub.mybinder.org/user/o-date-abm3-ir95qw2z/edit/Virus.nlogo). What combination of parameters would it be interesting to explore? Note that once you have more than one experiment, your tags will look like this:\n", "```\n", "\n", " \n", " setup\n", " ...\n", " ...\n", " \n", " \n", " setup\n", " ...\n", " ...\n", " \n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## External experiment definition file\n", "\n", "It is possible to put the experiment in an external file, and call that from our dropdowns above. In this binder you'll find the `experiment.xml` file which shows the extra bit of xml necessary to make the standalone file. **Warning** this currently is not working correctly in this binder, so define your experiment parameters *in* the nlogo file itself." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }