# COMS W1002 Computing in Context

COMS W1002 Computing in Context: DigitalHumanities / FinanceSchelling’s Model of SegregationAdapted from Frank McCown and Stanford’s Nifty Assignments repository! Many thanks toFrank and Nifty!OverviewRacial segregation…

COMS W1002 Computing in Context: DigitalHumanities / FinanceSchelling’s Model of SegregationAdapted from Frank McCown and Stanford’s Nifty Assignments repository! Many thanks toFrank and Nifty!OverviewRacial segregation has always been a pernicious social problem in the United States.Although much effort has been extended to desegregate our schools, churches, andneighborhoods, the US continues to remain segregated(https://tcf.org/content/commentary/racial-segregation-is-still-a-problem/) by race andeconomic lines. Why is segregation such a difficult problem to eradicate?In 1971, the American economist Thomas Schelling(https://en.wikipedia.org/wiki/Thomas_Schelling) created an agent-based model that mighthelp explain why segregation is so difficult to combat. His model of segregation showed thateven when individuals (or “agents”) didn’t mind being surrounded or living by agents of adifferent race, they would still choose to segregate themselves from other agents over time!Although the model is quite simple, it gives a fascinating look at how individuals might selfsegregate, even when they have no explicit desire to do so.In this assignment, you will create a simulation of Schelling’s model. The user should be ableto set a number of parameters of the model and watch it go.How the Model WorksSchelling’s model will now be explained with some minor changes. Suppose there are twotypes of agents: X and O. The two types of agents might represent different races, ethnicity,economic status, etc. Two populations of the two agent types are initially placed into randomlocations of a neighborhood represented by a grid. After placing all the agents in the grid,each cell is either occupied by an agent or is empty as shown below.Now we must determine if each agent is satisfied with its current location. A satisfied agentis one that is surrounded by at least a fractoin t of agents that are like itself. This threshold tis one that will apply to all agents in the model, even though in reality everyone might have adifferent threshold they are satisfied with. Note that the higher the threshold, the higher thelikelihood the agents will not be satisfied with their current location.For example, if t = .3, agent X is satisfied if at least 30% of its neighbors are also X. If fewerthan 30% are X, then the agent is not satisfied, and it will want to change its location in thegrid. For the remainder of this explanation, let’s assume a threshold t of .3. This means everyagent is fine with being in the minority as long as there are at least 30% of similar agents inadjacent cells.The picture below shows a satisfied agent because 50% of X’s neighbors are also X (.5 > t).In the pciture below, agent X is not satisfied because only 25% of its neighbors are X (.25 <t). Notice that in this example empty cells are not counted when calculating similarity.When an agent is not satisfied, it can be moved to any vacant location in the grid. Anyalgorithm can be used to choose this new location. For example, a randomly selected cellmay be chosen, or the agent could move to the nearest available location.In the image below, all dissatisfied agents have an asterisk next to them.The next image shows the new configuration after all the dissatisfied agents have beenmoved to unoccupied cells at random. Note that the new configuration may cause someagents which were previously satisfied to become dissatisfied!All dissatisfied agents must be moved in the same round. After the round is complete, a newround begins, and dissatisfied agents are once again moved to new locations in the grid.These rounds continue until all agents in the neighborhood are satisfied with their location.Okay, so what do I have to do?You will create code that simulates the Schelling model for two distinct populations.Consider the following parameters for the Schelling model:grid size: tuple of integers (n,m)fraction of population 1: float between 0 and 1.fraction of vacant sites in the grid: float betweein 0 and 1.tolerance: float between 0 and 1Write a function start(grid_size,pop,vacancy) that creates and returns a 2-Dnumpy array of the appropriate size consisting of integers where each element of the array isassigned as follows:part of first demographic: 1part of second demographic: -1vacant: 0The array your function returns should have the appropriate characteristics as specified bythe input parameters provided.In [1]:But wait, there’s more!Write a second function next_round(arr,tolerance) that takes as input an n by marray like the one you just created and returns a new 2-D array one step in the future whereall dissatisfied agents have been moved. Remember, since there may be more dissatisfiedagents than vacancies in the grid, you need to move them one at a time, this way each timean agent is randomly assigned to a vacant site, a vacancy at a new location is created.In [2]:Last bit! (almost)Finally write a function make_list(n) that returns a list of n numpy arrays where the firstarray is created using start and the second using next_round on the first array andthe third using next_round on the second, and so on.File “<ipython-input-1-2c7a386a02a2>”, line 4^SyntaxError: unexpected EOF while parsingFile “<ipython-input-2-25bb51c91c51>”, line 4^SyntaxError: unexpected EOF while parsing# write your function here:def start(grid_size,pop,vacancy):# write your function here:def next_round(arr,tolerance):In [3]:Really last bit!Try out your code. Create a list of length 100 using the parameter values:size=(30,30)pop=.5vacant = .1tolerance = .3In class we will see how to visualize this evolution of the grid using matplotlib. Try it out! I’veincluded some code to help with this in the hw_files folder on Courseworks if you want toget started early.And finally, are there still dissatisfied agents in the grid? How could you write an alternativefunction to make_list that makes a list exactly big enough so that in the final grid thereare no more dissatisfied agents. You don’t have to do this, but it wouldn’t be too hard.In [ ]:File “<ipython-input-3-8048fee1353a>”, line 5^SyntaxError: unexpected EOF while parsing# write your function here:def make_list(n,grid_size,pop,vacancy,tolerance):