This examples demonstrates the behavior of two types of agents (type A and type B) in a urban neighborhood. The agents get along with each other but every agent wants to live near at least some of the agents of its own type. This example is inspired by the Schelling's model.

Read carefully what you need to accomplish for this exercise. You need to submit your source code and 4 plots all-together. The instruction for plots are written in bold in the text below. For plotting use the provided functions in the Python template.

For this example you first need to create and randomly initialize a square grid:

  1. Implement a 2d regular square lattice (i.e. a grid) with 100x100 cells.
  2. Initialize 40% of cells with agents of type A and place them randomly in the grid.
  3. Initialize 40% of cells with agents of type B and place them randomly in the grid.
Please note, 20% of cells remain empty, i.e. nobody lives in these cells.

You will simulate people relocating around city in the search for a neighborhood in which they feel happy. Agents feel happy if at least 50% of their neighbors are of the same type. Each (central) agent has 8 neighboring cells: a cell to the East (E), to the West (W), to the South (S), to the North (N), to the Southeast (SE), to the Southwest (SW), to the Northeast (NE) and to the Northwest (NW). The agents that live on the grid borders have some neighbors missing. For example, the agent living in the cell at position (0,0), i.e. in the upper left corner has only three neighbors: E, SE, and S. The empty cells do not count for calculation of happiness. For example, an agent who lives among otherwise empty cells is 100% happy, or an agent who lives near 5 empty cells, 2 cells of its own type and 1 cell of the other type is 66% happy (2 own type agents / 3 occupied cells in total).

Your simulation runs as follows:

  1. Plot the initial grid as a heat-map.
  2. Plot the initial distribution of the happiness in your city.
  3. Iterate over 4) 20 times
  4. Calculate happiness for each agent. Relocate an unhappy agent to a random empty cell.
  5. Plot the final grid as a heat-map.
  6. Plot the final distribution of the happiness in your city.

Submit your source code and 4 plots. You will need to understand what do you see in your plots and most importantly you will need to understand why do you see that in your plots.