Part 1: Emoji-ABMs

For this part, we’ll be using Nicky Case’s agent-based modeling simulator. When you make your write-ups with this, you can include screenshots for your plots and as your model “code”.


Figure 1. Demographic patterns in Detroit (graphic by Eric Fischer).

Problem 1: The Schelling Model

During the 1960s the economist Thomas Schelling researched segregation and racial preferences, and showed individuals can have only relatively mild preferences for the same type and yet these subtle preferences can lead to societal segregation.

The rules for this model are simple: each person is happy if at least a certain percentage of their neighbors are the same type as them. If a person is happy, they stay in their current location. If they are unhappy, they move to a randomly chosen new location. This continues until all the people are happy (or possibly forever).

Start by doing the following:

  • Open a blank model: https://ncase.me/sim/?s=blank
  • Make two new emoji agent types. They can be any emoji’s you want (emojipedia.org). I highly recommend choosing emojis with contrasting colors. I used whales and foxes in my implementation, so I’ll refer to them as whales and foxes below.
  • Choose a square grid of at least 30 x 30.
  • Set the initialization so that there are about equal numbers of empty spaces, whales, and foxes (or whatever emoji you chose).

For each problem, include the written answer to the problem, a screenshot of the model behavior, and the “code” (a screenshot or listing of the rules and model/environment setup).

Problem 1A) From the blank model, code up a version of the Schelling model with the following rules:

  • Each type of animal (whales and foxes) wants at least X of its neighbors to be like itself–in other words:
  • If if less than X of its neighbors are like itself, it will move to a random location anywhere on the board
  • Consider neighbors to be the 8 squares that share an edge or corner with the current location.

Code the model, supposing that each animal will move if less than 2 (25%) of its neighbors to be the same as it. Run the model a few times. What behavior do you observe?


Problem 1B) Adjust the number of neighbors required to be the same. How does the model behavior vary as this number varies from 1 to 8?


Problem 1C) Now try out adjusting the balance between empty cells and animals. How does the model behavior change as the density of whales and foxes increases or decreases? Do you always observe separation between the two types? Why do you think this is?


Problem 1D) Lastly, let’s illustrate the importance of being careful about how we specify our rules. Re-run problems 1A - 1C with a slightly different rule: each type of animal wants no more than Y of its neighbors to be the opposite type. In other words, if there are more than 6 (75%) neighbors of the other type, it will move. Does this change the behavior of the model? If so, how does the behavior change? Why do you think this is?


Problem 2: Build your own model!

Start with a blank model and build a model of your choosing! In your write-up, document the model you built, including:

  • The overall process your are modeling and/or question you are trying to address
  • Agents: the types of agents in your model
  • Rules/interactions: the rules they follow (including what configuration of neighbors they look at)
  • Environment: the environmental specifications you used (grid size, etc.)


Part 2: NetLogo Models

Problem 1: Ants model

Open NetLogo, and go to the Models Library. Under Biology, choose the model “Ants.” Take a look at the “Info” tab of the Ants model and then work through the problems below.

Problem 1A) Interface. Explore the Ants model. Using different parameter settings:

  • Make the ants consume from multiple piles simultaneously.
  • Figure out how to make the ant colony really inefficient at consuming food.
  • Figure out how to make the ant colony really efficient at consuming food.

For the lab write-up, you can just include your notes on what parameter settings you used to make each condition happen.


Problem 1B) Code. Adjust the original Ants model code to do the following:

  • Adjust the two of the food sources to a) make them farther away from the nest, and b) make one bigger and another one smaller
  • Add a new food source between the nest and another food source (Bonus: add the new food source to the plot output)

What do you observe? How does the model behavior change?


Optional Problem 1C) Ants with multiple pheromones. (Taken from Wilensky and Rand, Chp. 3 #8) This problem is optional only! If you’re interested in learning more NetLogo, this will give you good practice. As mentioned in its Info tab, the Ants model focuses on the problem of food foraging, and assumes that ants can always find their own way directly back to the nest using a perfectly distributed predefined nest-scent gradient, once they have found the food. To make the model more physically plausible, change it so that the nest-scent is a new kind of pheromone that is released by ants for some limited time period after they have left the nest. This nest-scent pheromone should diffuse and evaporate just like the food-carrying pheromone does.



Part 3: Python

Optional Problem

If you’ve never used Python before, or you don’t know how to build loops and/or functions in Python, here are some tutorials to get started with:

  • 10-minute Python - very short tutorial with just the basics (often useful if you’ve coded in other things or have used python but not in a while)
  • The Python Tutorial - the official tutorial, gives more details and a good reference. Good to start with if you’ve never used python. Check out the resources section of the course website for more tutorials also.
  • Think Python - our book is a good reference too—you may want to start with Chapters 2 and 3.

Finally, we’ll be using a few modules/packages for Python in this class, such as numpy, scipy, and matplotlib. To get familiar with those, here’s a quick cheatsheet that covers many of the packages we’ll need (also useful as a reference even if you’ve used them before!).


Problem 1: The Chaos Game

Next, let’s code up the Chaos Game in Python. This will give us some practice building functions, using for loops, and using if statements. We’ll code it up step by step, and then put it all together.

First, let’s import some of the libraries we need (you may need to install these libraries first!):

from math import *                  # useful math functions
import numpy as np                  # useful array objects 
                                    # (also a core scientific computing library)
import matplotlib.pyplot as plt     # nice plotting commands
from random import random, randint  # random number generation functions

Problem 1A: Functions) Write a function called midpoint that takes in two tuples of the form (x,y) as inputs, and returns their midpoint as a new tuple. We can calculate the midpoint of two points \((x_1, y_1)\) and \((x_2,y_2)\) like this: \[ midpoint = \left(\frac{1}{2}(x_1 + x_2) \, ,\, \frac{1}{2}(y_1 + y_2) \right)\] We will use this function later to calculate add new points as we draw the chaos game. Add this function to the beginning of your script.


Problem 1B: Setup) Next, let’s setup some of what we need for the Chaos Game:

  • Make a list called corner that contains three tuples, each with the coordinates of one vertex on your equilateral triangle. In case you’ve forgotten your Pythagorean theorem from way back when, here’s a set of coordinates you can use: \((0,0)\), \((1,0)\), and \((0.5, \sqrt{3}/2)\) (python uses the function sqrt for square roots).
  • Set the number of total points we will plot as N = 1000
  • Make two vectors of length 1000 filled with zeros using the np.zeros function. We will use these to store all the x and y coordinates we will plot on the Chaos Game. You can make these arrays like this: x = np.zeros(N).

Problem 1C: Starting position) Now we have everything we need, and we can start the Chaos Game! Choose a random starting position for your x and y coordinates, and store this starting position in your x and y arrays as the first entry. (Remember that Python indexes from zero so you’ll store these in x[0] and y[0]!) To choose the random location, you can use the random() function. (This will pick a random value between 0 and 1, which works out since those are the maximum size for the triangle we’re using. If you picked a different size triangle, be sure to adjust the range for your random draw!)


Problem 1D: For loops) Now that we have our starting position, we need to choose the rest of our 1000 points. For this, we’ll use a loop. Write a loop that iterates from 1 to N. On each iteration of the loop, you should:

  • Choose a random vertex from your list corner
  • Calculate the midpoint between this vertex and the previous point in your x and y list
  • Save this midpoint to the list of x and y coordinates

Remember that for Python, spacing is important! Make sure you indent each line in the for loop the same amount, or you’ll run into trouble.


Problem 1E: Plot the results!) Okay, we should have a nice Chaos Game going now! We can plot our results like this:

plt.figure()
plt.scatter(x, y)   # plot our chaos game points
plt.scatter((0,1,0.5),(0,0,sqrt(3)/2),color='red') # plot the triangle vertices
plt.show()

When you put all this code together, you should get something that looks roughly like this! Include your final plot and your code when you turn in your lab.


Optional Problem 2

If you have time and interest, try changing the Chaos Game! A few things you can try:

  • Change the number of vertices you consider (you can make squares or lots of other shapes)
  • Change the fraction of the distance that you traverse with each new point (e.g. go 1/3 of the way instead of halfway to the vertex)
  • Change the rules for the vertex order that you choose! (e.g. make it so you can’t choose the same vertex twice)

If you want to try some pre-worked out ideas, there are some nice examples on Wolfram Mathworld. If you try a few different things, what do you notice? Are there particular configurations that work or don’t work? Why do you think this is?



Part 4: Project Ideas

What are you considering for your final project? Give an idea or two (definitely not final, just want you to start thinking about possibilities!)