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”.
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:
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:
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?
Start with a blank model and build a model of your choosing! In your write-up, document the model you built, including:
If you’ve never used NetLogo before, or you’re new to coding, try this tutorial to get comfy first. The first Tutorial covers working with the NetLogo interface, and then it digs into the coding side of NetLogo in Tutorials 2 and 3.
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:
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:
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.
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:
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!).
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:
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).N = 1000
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:
corner
x
and y
listRemember 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.
If you have time and interest, try changing the Chaos Game! A few things you can try:
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?
What are you considering for your final project? Give an idea or two (definitely not final, just want you to start thinking about possibilities!)