Homework #4 - implementing the simple 2D Schelling model

In this homework assignment, we're going to build upon our in-class experiments with Nicky Case's Parable of the Polygons, which is a 2D version of the Schelling model we've been working on in class. You'll also want to look at the 2D Numpy array tutorial that we have provided!

Your name

Put your name here!

Section 1: The simple 2D Schelling model

In this section, you will implement a simple version of the 2D Schelling model. Create a 2D Numpy array that is 20x20 cells. In our implementation, the number 0 will represent an empty cell, 1 will represent a triangle, and 2 will represent a square. Initialize the array with approximately 1/3 empty cells, 1/3 triangles, and 1/3 squares, all of which are placed in random cells. Then, evolve this model using the following rules:

  • Neighbors are defined as polygons that are in any cell that's immediately adjacent (either left/right/up/down or diagonal) to a given polygon.
  • A polygon (triangle or square) is happy if at least 1/3 of its neighbors are like it
  • A polygon is meh if all of its neighbors are like it, or if it has no neighbors.
  • A polygon is considered to be segregated if all of its neighbors are like it, or if it has no neighbors. The "segregation measure" is the fraction of polygons that are considered to be segregated.

First, plot your initial setup so that it's clear which polygons are which. Then, evolve the board forward by randomly selecting a single unhappy polygon and moving it to an empty cell. Continue doing so until every polygon is either meh or happy. Keep track of how your segregation index evolves with each step, as well as the fraction of polygons that are meh and happy. Finally, plot both the board and (separately), the evolution of the segregation, meh fraction, and happy fraction over time.

IMPORTANT NOTE: Create functions that do the tasks that are complex or that you will need to do repeatedly, such as creating the initial array, determining if polygons are sad/meh/happy, moving polygons, and calculating segregation. Also put comments in your code to indicate what it is you're doing, and make sure these comments are both clear and easy to read!


In [ ]:
# put your code here.  Add additional cells as necessary!

Section 2: Some questions relating to this model

Question 1: How would you change your code so that the fraction of cells that was empty, triangle, and square could be specified by the user?

put your answer here!

Question 2: How would you change your code so that the conditions for happiness were different - in other words, if the polygons were happy if at least some arbitrary fraction of their nieghbors, $f_{like}$, were like them, and if a second fraction, $f_{unlike}$, were unlike them?

put your answer here!

Question 3: Does using functions to create this model make it less easy, or easier, to make these sorts of changes? Explain your answer.

put your answer here!


Section 3: Feedback (required!)

How long did you spend on the homework?

Write your answer here

What questions do you have after this assignment?

Write your answer here


Congratulations, you're done!

How to submit this assignment

Log into the course Desire2Learn website (d2l.msu.edu) and go to the "Homework assignments" folder. There will be a dropbox labeled "Homework 4". Upload this notebook there.