{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1: NetLogo interaction through the pyNetLogo connector\n", "\n", "This notebook provides a simple example of interaction between a NetLogo model and the Python environment, using the Wolf Sheep Predation model included in the NetLogo example library (Wilensky, 1999). This model is slightly modified to add additional agent properties and illustrate the exchange of different data types. All files used in the example are available from the pyNetLogo repository at https://github.com/quaquel/pyNetLogo.\n", "\n", "We start by instantiating a link to NetLogo, loading the model, and executing the `setup` command in NetLogo." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "scrolled": true }, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "sns.set_style(\"white\")\n", "sns.set_context(\"talk\")\n", "\n", "import pynetlogo\n", "\n", "netlogo = pynetlogo.NetLogoLink(\n", " gui=True,\n", " jvm_path=\"/Users/jhkwakkel/Downloads/jdk-19.0.2.jdk/Contents/MacOS/libjli.dylib\",\n", ")\n", "\n", "netlogo.load_model(\"./models/Wolf Sheep Predation_v6.nlogo\")\n", "netlogo.command(\"setup\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the `write_NetLogo_attriblist` method to pass properties to agents from a Pandas dataframe -- for instance, initial values for given attributes. This improves performance by simultaneously setting multiple properties for multiple agents in a single function call.\n", "\n", "As an example, we first load data from an Excel file into a dataframe. Each row corresponds to an agent, with columns for each attribute (including the `who` NetLogo identifier, which is required). In this case, we set coordinates for the agents using the `xcor` and `ycor` attributes." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/html": [ "
\n", " | who | \n", "xcor | \n", "ycor | \n", "
---|---|---|---|
0 | \n", "0 | \n", "-24.000000 | \n", "-24.000000 | \n", "
1 | \n", "1 | \n", "-23.666667 | \n", "-23.666667 | \n", "
2 | \n", "2 | \n", "-23.333333 | \n", "-23.333333 | \n", "
3 | \n", "3 | \n", "-23.000000 | \n", "-23.000000 | \n", "
4 | \n", "4 | \n", "-22.666667 | \n", "-22.666667 | \n", "