09. Understanding & Customizing NetLogo

NetLogo is a the world’s most commonly used Agent-Based Modeling system. Recall that the term “modeling” and “model” are the same as “simulating” and “simulating.” NetLogo is a free service that is used daily from 3rd grade through advanced research. What is so special about it? There are four characteristics that, combined, make it so valuable for everybody:

  • Animated, visual display of results. Many other model systems output static numbers as “answers” to problems. Numerical output can’t capture dynamic interactions among variables like climate change or virus spread. Even 3rd graders can understand animated visual displays of the relationships of wolves, sheep, and grass.
  • NetLogo is free to download or to use on the web. This makes it accessible to laptops, tablets, and anything that can display a web page. Schools love that it runs on Chromebooks, that there is no installation needed, no requisitions, approvals, or budget impacts. It’s as available to a 3rd grader at home as to a scientist in a research lab.
  • The NetLogo Programming Language. NetLogo code can be used by anyone to create new simulations and modify existing ones. Unlike great educational simulations like PhET sims, NetLogo’s flexibility allows it to be a long-term investment rather than a one-shot, one-topic model. Anyone from elementary school to retired professionals can create and customize NetLogo models. With the emergence of AI systems able to program, the entry to the creation and customization of NetLogo now allows those with no programming skills to more easily build their own simulation systems for fun, education, and research.
  • Massive Library of Pre-Built and Shared Models. Over 200 simulations come with NetLogo’s built-in Models Library. Many more are available through models shared on NetLogo’s website, contributed to the Models Common. They cover a vast range of disciplines and levels from simple predator-prey models to complex molecular interactions.
  • Modeling Complexity. Traditional science, sociology, economics, and statistics classes explore the relationship between two variables, occasionally three. A 2-dimensional graph easily shows the relationship of two variables like “education” and “wealth.” The real world is never so simple. NetLogo’s animations visually show the complex interaction of multiple variables and the change over time like the way a new virus begins with just a few sick people but progressively may affect everyone through an understandable process. Climate change is a perfect example of complexity that makes it hard for many people to understand the process and how to interact with that process. Modeling complexity is a key to understanding the world.

Changing Code for a NetLogo Model (hands-on for you to do)

Use ChatGPT or other AI system of your choice. You’ll use a very simple NetLogo model to follow these steps:

  • Pick a simple NetLogo model whose code is short and somewhat understandable. We’ll pick our Virus Spread model.
  • Decide what enhancement you want to make. The Virus Spread model demonstrates how a virus infection slowly spreads at first since only a few people are infected. Once a critical mass are infected, the rate of infection increases exponentially until most are infected. The only variables are how infectious the virus is. One enhancement could be to add “vaccination” to the model. In real epidemics, some people recover from the virus, and some people die. The model could be enhanced to show how a virus epidemic dies out after most are immune because of past infection or vaccination.
  • Open ChatGPT and tell it that you want to modify NetLogo code, and I will post the code and then explain the changes I want.
  • Select ALL of the NetLogo Virus Spread code and paste it into your ChatGPT conversation. (If you use a NetLogo model in the Models Library, ChatGPT can find this code itself.) Upload the code to ChatGPT.
  • Next, in ChatGPT describe your model and the enhancement you wish to make. For example:
    • I created a basic NetLogo virus spread simulation, and I want to add a variable for vaccination. There sould be an initial slider to specify the percent population that is vaccinated. Can you add this feature to the code I posted? Thank you.
  • ChatGPT will modify and display the code to add to NetLogo. Pay attention to ChatGPT’s directions, to add it or to replace old code.
  • Add any interface elements according to ChatGPT’s direction (sliders, monitors, buttons).
  • The checkmark icon in the code window (top left) can tell if the code seems right. Try running it. If it runs, buy ChatGPT a coffee. If not …
  • If the code doesn’t run, copy the code containing the error and any NetLogo error message. Paste those into ChatGPT and request it to fix the errors.
  • Rinse and repeat if needed.
  • note: most models are too complex for non-programmers to enhance this way. Practice with very simple models or ASK ChatGPT to CREATE a whole NetLogo model that you want! Again, keep it simple for now.

Illustrative Classic NetLogo Models

The Fire model simulates the spread of a forest fire based on the density of trees in the forest. From this model you can learn whether there are specific thresholds below which a forest fire would simply burn itself out, or vice versa, burn and spread uncontrollably. The model does not include obvious real-world variables like wind, dryness, and human residences within the forest. The model could be more realistic with the addition of those elements. (Earth Sciences → Fire)

Several Sheep & Wolf Predation models that simulate factors in the population ecology of predators and prey. Some models include grass that the sheep eat, reproduction rates, and how much each have to eat for health. Like the Fire model, there may be variable values that support a population equilibrium vs. condemnation to population collapse of wolves and/or sheep. This model is easy for 2nd graders to understand even though they will feel sorry for the sheep. With the addition of grass and reproduction variables, it becomes a complex model that is easy to follow because the visual animation clearly shows the population changes. (Biology → Sheep & Wolf Predation)

The Flocking model simulates the grouping behavior of flocks of birds. We see birds magically arising as a group, all flying off in the same direction, sometimes even in a V-shape they maintain with an apparent leader at the front. NetLogo excels in modeling group behavior by establishing rules just for each individual, yet those individuals behave as though there was some central leader. Flocking defines just three variables: how far ahead a bird looks, how close it wants to get to another bird, and how much it will turn based on the behavior of other birds. Together those individual rules create the great, apparently organized flocks of birds we see, explaining how birds create complex flocking behavior by following each one’s simple rules. There’s a separate Flocking model that produces V-formation behavior. There’s also a wonderful Flocking 3D model where you can follow a bird in 3D as it finds, joins, and perhaps leaves a flock. (Biology → Flocking)

Climate Change is the poster child for the behavior of a Complex System. It’s a critically-important concept that few people understand. Sure, they know that CO2 has something to do with warmer weather, but they can’t imagine why. CO2 is an odorless, invisible gas that every one of our cells produce. We get rid of it every time we exhale. How can this be harmful? It turns out that NetLogo’s Climate Change model makes visible all of the key elements in the “greenhouse effect” whereby CO2 warms the air, oceans, and earth. This is one of the most popular and important NetLogo models. Below are the factors teachers or kids can visualize or control to see their effects: (Earth Sciences → Climate Change)

  • sun brightness. Think of season with winter sun hitting at an angle, being absorbed by more atmosphere and reflecting more off the earth’s surface.
  • albedo. The reflectivity of the earth’s surface at this place. Snow and water reflect more sunlight than dark soil or dense foliage. Loss of Greenland’s ice means that Greenland absorbs more light, heating soil and rock.
  • visible “heat” radiating from earth, more as the earth warms. The heat is ultraviolet light waves/particles that interact with CO2 molecules.
  • CO2 molecules “trap” radiated heat waves (where heat is leaving the earth) and retransmit it back down to earth, reheating earth’s surface. The simulations shows individual CO2 molecules retransmitting heat rays back to the earth’s surface that it just left! This powerful visualization is clear enough for 3rd graders to understand (most adults, too).
  • Interface controls allow students to add or subtract CO2 and add or subtract cloud cover. A temperature graph shows the results of each of the variables.

HIV and other STD diseases prey on young adults who need to know how to protect themselves. The HIV model is especially appropriate for high school and college students to both see demonstrated in the classroom AND to explore on their own at home. It clearly shows the dangers and protections related to sexual activity, number of partners, length of relationships, condom use, and HIV/STD tests. This is a non-judgement, interactive simulation that has a powerful message for sexually active (or soon-to-be-active) teens and young adults. This simulation transforms the academic experience of other virus spread models into a powerful, personal model for this vulnerable age group. (Biology → HIV)

The Segregation Model simulates changes in housing patterns that relate to different races, nationalities, religions, and wealth. It is such an important social issue, that its effects have been widely studies and discussed, but the causes are missing a critical element. This model is based on Scheller’s famous model of residential preference. It’s one of the most famous sociological models, and it provides insight that’s rarely discussed. Think of what kind of neighborhood a family wants to live in, and think of why they would want to leave their current neighborhood, especially if their own neighbors have been moving in and out. A valuable model for high school social studies classes. Here’s a video of Michigan Professor Scott Page in an online Model Thinking class explaining this beautifully: https://www.youtube.com/watch?v=42STyM7RfrU (Social Science → Segregation)

The Simple Economy Model simulates a very simple economy starting with a population of people with equal wealth ($100 each) who each give $1 to the nearest person over and over. A series of simple graphs show how average and individual wealth changes (or stays the same). Try it and be surprised. Next try to explain it. This is a pure example of serious complexity arising from very, very simple models. (Social Science → Economics → Simple Economy)

Heroes and Cowards. Here’s a final model that works best in a field or a gym with a dozen or more kids or adults. The Heroes and Cowards model in NetLogo shows the logic and outcome, but it’s not nearly as interesting as a real live class-in-the-playground. Enough said, find out for yourself what happens. The directions for the activity are in the “Info” tab of the Heroes and Cowards model in NetLogo. (IABM Textbook → Chapter 2 → Heroes and Cowards)