Practicum 1: Setting up Netlogo and Python

Wim Pouw

Practicum 1: Objectives

  • Overview: Introduction to the practicum.
  • Setup: NetLogo and Python Mesa environment setup.
  • Coding Style: Study coding style differences in NetLogo and Python.
  • Basics: Initialize basic setups and models, and tweaking and breaking them.

Agenda

  • Intro and live setup and how to with Wim
  • Self-work with quiz-questions
  • Self-work annotate code for future self, by breaking and fixing code

Overview of the practicum

  • We have one landing page: Practicum Landing Page
  • Models, slides, and literature.
  • Slides will be available before the practicum
  • Living document (I hope to build out more over time based on your feedback)

Tips

  • Start to build your own notes and annotated model library
  • The info page in netlogo is formatted in the markup language Markdown
  • Jupyter notebooks allow you to preface your code in Markdown too
  • Rmarkdown is similar to jupyter as it mix R code with… well you get it.
  • Markdown Cheat Sheet

Downloading netlogo models

  • Lets download the basic Netlogo models
  • Unpack using 7zip, Winrar, or your OS built-in unpacker

Python: Installing Mesa (our go to ABM python package)

conda create -n mesa-env python=3.11
conda activate mesa-env
pip install mesa[all]==3.2.0
pip install jupyter notebook seaborn pandas numpy matplotlib
python -c "import mesa; print(f'Mesa version: {mesa.__version__}')"
  • Of course feel free to use your own Python environment, or clone repo and install locally (pip install .). This is the recommended and tested procedure however.

  • Tested on the practicum PC (and my own Windows laptop)

Jupyter Notebooks

  • We will be using Jupyter Notebooks for our Python code with Visual Studio Code (and our environment setup initialized in VS code).
  • But feel free to use your own IDE and organization.

Downloading models

  • Lets download the basic Mesa models

We can run models now

Run all netlogo and python basic models and inspect them carefully. What do these simple models do?

Runthrough of netlogo things and mesa code

  • with some highlights by Wim

Do it yourself assignment (general)

  • For each practicum I want you to organize your own model library
  • This model library has well-annotated markdown-edited notebooks (Python) or info tabs (Netlogo)
  • What does this model do, what are chunks of code that are complicated, and what could I use this for? Use the online documentation for explanations.
  • A personal model library will serve future you well

Grid Model Questions

In the NetLogo grid model, what initializes the turtles position randomly?

  • let direction one-of [0 90 180 270]
  • setxy random-pxcor random-pycor
  • ask patches [if (pxcor + pycor) mod 2 = 0 [ set pcolor white ]]
  • set heading direction

Grid Model Questions

What characterizes the agents move posibility? (google terms if needed)

  • The agent can only move to a cell in a Von Neumann neighborhood
  • The agent can only move to a cell in a Moore neighborhood
  • The number of move options of the agents changes randomly
  • None of these

Grid Model Questions

What does possible_moves = [(x, (y+1)%h), ((x+1)%w, y), (x, (y-1)%h), ((x-1)%w, y)] represent?

  • We move the agent to a random position
  • The directions that the agent could go to with torus wrapping
  • Diagonal movement options

Grid Model Questions

Compare the implementation of the agent movement in NetLogo and Mesa.

  • NetLogo sets direction and moves, Mesa selects coordinates directly
  • Both use the same movement implementation
  • Only Mesa calculates possible moves

Grid Model Questions (Open Question)

Lets say we now want to have the agents be able to move to any patch surrounding the current patch, including the diagonals. What would you change in netlogo? What would you change in Python? Impress your neighbor, and explain what you did to them (and vice versa).

Grid Model Questions (Use your own imagination)

Make a copy of the grid models. And try to think of a simple thing you can add that make your model behave slightly more interesting from your perspective. Try to implement the change both in Netlogo and in Python.

Continuous Model Questions

Which Mesa components are essential for a basic continuous space model? (Select all correct)

  • ContinuousSpace to define the simulation space
  • ContinuousSpaceAgent as the base class for agents
  • OrthogonalMooreGrid for discrete cells
  • mesa.Model as the base class for the simulation model

Continuous Model Questions

What happens if you don’t call self.agents.add(agent) after creating an agent?

  • The agent moves randomly anyway
  • The model crashes immediately
  • The agent exists but won’t be included in model.agents.do(“step”)
  • The visualization will not work

Continuous Model Questions (Use your own imagination)

Modify the Mesa continuous model so that the model behavior is slightly different or more complex. Impress your neighbor, and explain what you did to them (and vice versa).

Network Model Questions

Check out the network basic models.

Network Model Questions

For the python implementation, what’s the key difference between this network visualization and the previous grid/continuous visualizations?

  • Network visualization is faster to render
  • Network uses custom matplotlib plotting while grid/continuous use Mesa’s make_space_component
  • Network models can’t use Mesa’s visualization system
  • Custom plotting only works for small networks

Self-study

During this first practicum you were likely lost because many operations are foreign. The only way to learn is to look up things in the python code you do not understand. Then once you understand you write it out in your own words in the jupyter notebook Markdown sections and save it for future reference (in your personal model library).