代写Homework | 代写Oop | Gui代写 | Assignment | Haskhell – Schellings Model of Housing Segregation

代写Homework | 代写Oop | Gui代写 | Assignment | Haskhell – 这是一个涉及GUI编程的haskhell的代写任务

homework #6: Schellings Model of Housing Segregation

Prelimniaries

Make sure you have installed the Gloss library: Gloss-Hackage. I provide instructions on installing the library on Piazza.

This is required to be installed before leaving Lecture 9 (May 22nd 2018). If you are not able to run/install the library after this date then you will receive a 0 for the graphics a portion of this assignment. No exceptions.

Introduction

The New York Times has an interesting tool for viewing the distribution of racial and ethnic groups in cities and states around the country. If you look at the map for the Greater Chicago area, you will see that many areas are heavily segregated, but that some, such as the suburb of Glendale Heights, are not. The forces that led to this arrangement of people are complex and it is unlikely that any simple model can truly answer the questions of how segregation arises and why it persists. Keeping that in mind, one can still ask simple questions that may shed some light on the situation. In his 1978 book Micromotive and Macrobehavior, Thomas Schelling considered the following question: is it possible that a group of individuals would prefer to live in integrated neighborhoods and yet, as the result of their collective choices end up in segregated neighborhoods?

Schelling examined this question using a very simple model: A city has two kinds of people, Cubs fans and White Sox fans, for example, and each person in the city requires a certain number of his neighbors to be like him to feel comfortable in his location. Schelling used this model to study the patterns that emerge if, over time, people who are unsatisfied with their initial locations or become unsatisfied over time relocate to new locations that satisfy their criteria. What happens depends on the ratio of the two populations and on the satisfaction criteria used by the individuals, but segregation is a frequent outcome.

assignment Overview

Your task in this assignment will be to implement a variant of Schellings model. Before you can do so, we need to specify the details of the model. In particular, we need to specify the:

shape and composition of a city,
boundaries of a homes neighborhood,
definition for satisfaction,
rule for relocating unsatisfied homeowners,
composition of a step in the simulation, and
stopping conditions for the simulation.

Cities: A city is represented as an grid (where the value of can be different for each city). Each grid location (or cell) represents a home, which can be in one of three states: open (that is, unoccupied), occupied by a red homeowner, or occupied by a blue homeowner. You can assume that a city has at least one open location.

N N N

In this assignment, you will always process the grid in row major order. That is, you will use a doubly-nested l oop in which the index variable in the outer loop starts at zero and is used to indicate the current row, and the index variable in the inner loop starts at zero and is used to indicate the current column.

Neighborhood: The neighborhood of a home will be defined by a parameter R (this radius value will be an input to the model, so the value of R may vary from one simulation to another). The R-neighborhood of the home at Location contains all Locations such that:

and
and

Note that the Location itself is considered part of its own neighborhood. So, every homeowner will have at least one neighbor (himself).

We will refer to a neighborhood with parameter R=x as an R-x neighborhood. The following figure shows the neighborhoods around Locations (2,2) and (3,0) for different values of R. Cells colored yellow are included in the specified neighborhood.

Neighborhood around (2,2) Neighborhood around (3,0)
R = 0 R = 1 R = 1 R = 2

Notice that Location (3,0), which is closer to the boundaries of the city, has fewer neighboring homes for the same value of R than Location (2,2), which is the middle of the city. We use the term boundary neighborhood to refer to a neighborhood that is near the boundary of the city and thus does not have a full set of neighbors.

Satisfaction: For each homeowner, we are going to compute a similarity score S/H where S is the number of homes in the neighborhood with occupants of the same color as the homeowner and H is the number of occupied homes in the neighborhood. Since a homeowner is included in his own neighborhood, S will always be at least one and H will be at least one. A homeowner is satisfied with her location, if her similarity score is greater than or equal to a specified threshold.

The figure below illustrates this concept using a city with R-1 neighborhoods. It shows homeowners and their similarity scores. White cells depict unoccupied homes/locations. Blue and red cells hold blue and red homeowners respectively. Each grid cell contains its location (in parentheses) and, for occupied homes, the homeowners R-1 similarity score (in square brackets). We rounded the similarity scores in the figure to three digits merely for clarity. Do not round them in your computation.

Similarity scores
R=
( i , j )
( k , l )
0 k < N i R k i + R
0 l < N j R l j + R
( i , j )
Similarity scores
R=

The next two figures illustrate the effect of the threshold on satisfaction. The figure on the left was computed using a threshold of 0.33, while the figure on the right was computed using a threshold of 0.60. As in the figure above, white locations are unoccupied. In these figures, blue (red) locations are occupied by satisfied blue (red) homeowners, while light blue (pink) locations are occupied by unsatisfied blue (red) homeowners.

Satisfaction threshold = 0.33Satisfaction threshold = 0.
R=1 R=

The threshold is a parameter of the model, and will be the same for all the homeowners in a given simulation.

Relocation rule: If possible, an unsatisfied homeowner should be relocated to an open location that meets the satisfaction criteria. Our homeowners are picky: they want to move to satisfactory locations, but would prefer to stay in integrated neighborhoods. Also, they are looking for a good deal and so, prefer homes that have been on the market longer. To state these preferences more precisely: given multiple (unoccupied) homes that meet the satisfaction threshold, an unsatisfied homeowner will choose the home with the most diversity, that is, the lowest satisfaction score that is at or above the threshold. In case of ties, the homeowner will choose the home that has been on the market longest. If none of the available homes are satisfactory, then the homeowner will not relocate.

We will model the homes that are currently available with a list of their locations. Once a homeowner chooses a home, the location of his new home should be removed from the list and the location of his previous home should be added to the end of the list of available homes. That is, we will be keeping this list of homes in order from oldest listing to newest listing.

Here is an example relocation of the unsatisfied homeowner at Location (1,1) to Location (1, 4):

Before relocation After relocation
Before relocation After relocation

On the left, we show a city with R-1 neighborhoods with a satisfaction threshold of 0.44 along with a depiction of the locations that are open in the neighborhood (shown below the grid). The homeowner in Location (1,1) is unsatisfied with his location. He considers all five open locations and finds the following:

Location Similarity Score Satisfactory Threshold Difference
Location (0,
2)
0.6 Yes 0.
Location (1,
0)
0.2 No
Location (1,
4)
0.5 Yes 0.
Location (3,
3)
0.5 Yes 0.
Location (4,
4)
0.6666666666666666Yes 0.

All the open locations except Location (1, 0) have acceptable similarity scores. Locations (1, 4) and (3, 3) have the scores that are closest to the threshold. Of these, the homeowner will choose Location (1,4) because it is closer to the front of the list (that is, it came on the market sooner).

The frame on the right shows the state of the neighborhood after this homeowner has been relocated. Notice that Location (1,4) has been removed from the list of open locations and Location (1,1) has been added to the end of the list. Also, notice that the satisfaction states of the homeowners at Locations (0,3), (1,3), and (2,0) have changed. Can you figure out why?

Simulation Step: During a step in the simulation, your implementation should make a full pass over the city in row major order. As your implementation visits each home, you will check to see if the home is occupied and if so, whether the homeowner is satisfied with her location at the time of the visit. If she is satisfied, then you will move on to the next home in the traversal. If not, you should attempt to relocate her to a more satisfactory location using the relocation rules described above.

To recap: a visit to a particular home will trigger a relocation in a given step if, at the time of the visit:

1. the home is occupied;
2. the homeowner is unsatisfied with his location; and
3. there is an open location that meets the satisfaction criteria.

Stopping conditions: Your simulation should stop when:

1. it has executed a specified maximum number of steps or
2. no relocations occur in a step.

Sample simulation: step 1

In the figure below, the first frame represents the initial state of a city. The next five frames illustrate the relocations that occur in the first step of the simulation.

Step 1
Initial state After 1st relocation After 2nd relocation After 3rd relocation
After 4th relocation After 5th relocation

At the start of step 1, all of the homeowners in row 0 (that is, in Locations (0,0), (0,1), (0,3), and (0,4)) are satisfied. The homeowner in Location (1,1) is the first unsatisfied homeowner encountered during our traversal of the city. As discussed above in the relocation example, the homeowner will move to Location (1,4), because

the neighborhood around this location is satisfactory,
this home tied with one other home (at Location (3,3)) for the best similarity score, and
it has been on the market longer than the home at Location (3,3) (i.e., it appears earlier in the list of open homes).

Notice in the figure labelled After 1st relocation that Location (1, 4) has been removed from the list of open locations and Location (1,1) has been added to the end of the list.

Relocating the blue homeowner at Location (1,1) causes the blue homeowner at Location (1,2) to become unsatisfied. And as it happens, this location is the next in our row-major traversal. Three of the five open homes (Locations (0,2), (3,3), and (4,4)) are suitable for the blue homeowner at (1,2). The homeowner will move to Location (0,2) because it ties with Location (3,3) for the best similarity score and it appears earlier in the list of open homes.

The traversal continues past satisfied homeowners in Location (1,3), Location (1,4), etc., until it reaches an unsatisfied red homeowner at Location (2,3). All five open homes are suitable for this homeowner, but Location (3,3) has the best similarity score. And so, the homeowner in Location (2,3) will move to Location (3,3).

The traversal then continues past satisfied homeowners at Locations (2,4), Location (3,0), etc., until it reaches the unsatisfied homeowner at Location (3,2). Again, all the currently open locations are satisfactory, but Location (4,4) has the best similarity score.

The next three spots in the traversal (Locations (3,3), (3,4) and (4,0) have satisfied homeowners. The fourth, the red homeowner at Location (4,1), is unsatisfied. Four of the five currently available homes are satisfactory (all but Location (2,3)) and of those, Location (1,2) has the best similarity score and so, the homeowner moves from Location (4,1) to Location (1,2).

The homeowners in the remaining locations in the traversal are all satisfied and so no further relocations occur in this step.

Sample simulation: step 2

The first frame in the figure below represents the state of a city at the start of the second step. Notice that both the state of the grid and the open locations list are the same as they were at the end of the first step.

Step 2
Initial state After 1st relocation After 2nd relocation

The traversal restarts at Location (0,0) and does not encounter an unsatisfied homeowner until Location (0,2). This homeowner is moved to Location (2,3), because it has the best similarity score of the two homes that are suitable (Locations (2,3) and (4,1)).

Next, the traversal passes 13 homes before it gets to an unsatisfied homeowner at Location (3,1). Only one currently available location (Location (4,1)) has an acceptable similarity score and so, the homeowner moves to it.

All the rest of the homes encountered during the traversal are either open or have satisfied homeowners, so no further relocations occur.

Sample simulation: step 3

The first frame in the figure below represents the state of a city at the start of the third step. At this point, all the homeowners are satisfied, so no relocations happen during the third step traversal and the simulation halts when the traversal is completed.

Step 4
Initial state Final state
Step 4
Initial state Final state

Data Representation

Your implementation will need a representation for the current state of a homeowner, a representation for the city (aka the grid), and a representation for the locations that are currently open (unoccupied). Here are a few suggestions:

Representing Homeowners/Homes: Homeowners can be represented by the a data type that contains the constructors B and R, and open homes are represented by O.

Representing cities: A city is represented as a grid of locations. You can define this grid as a list, list of lists, etc. It may be easier for some of you to think about having a flatten list versus a list of lists.

Traversals of the city should always be done in row-major order.

Representing Locations: You can use pairs (tuples) of integers to represent locations.

Tracking open locations You can use use a list of pairs (locations) to track open locations.

Your implementation should initialize the list of open locations in the grid exactly once at the start of a simulation, using a row- major ordering. All subsequent changes to the list will occur as the result of a relocation.

When looking for a new location for a homeowner, your implementation will walk through the list of open locations and consider them as possible new homes for the homeowner. If this process evaluating new homes for a homeowner determines that he should move to a new location, you should remove this location from the list and append his old location to the end of the list.

We encourage you to look carefully at the open locations list in the sample simulation above. In particular, notice that we do not recompute the list of open locations between steps.

The Project

Your task is to implement a Gloss application of Schellings Model as described in the overview section.

Below is the usage for the application

Usage: smhs max_steps (grid_size || grid.txt) red_percentage blue_percentage empty_percentage

The program will launch given the parameters described below. The second parameter ("(grid_size || grid.txt)") means either the user enters in the grid size or requests to load in file. This means this parameter will either be an integer or the name of the file to

load. I will provide a description on the file loading later.

The GUI application will display the grid along with the following simulation parameters that a user defines on the command line or can change during the execution of the program:

1. maximum steps (max_steps, integer value): The maximum number of steps an application can simulate.
2. grid size (grid_size, integer value): The user specifies the grid size (NxN) on the command line. The user is not able to
change the size during the running of the application. The grid size should only between 5 to 15. If the user enters in a
value outside this range then the program should exit with an error message indicating the grid size was invalid. You can
choose the size of the rectangles that will represent the blue/red/empty homes but it needs to be easily visible to the user.
3. R (neighbor size, integer value): Specifies the neighborhood size for a given home. This value can only be changed at the
start of a simulation and not after the first round.
4. percentage of empty homes (empty_percentage, integer value): The user should be allowed to specify the percentage of
empty homes the grid should contain. This number should range between 0-100. The program should exit and print an
error message if this value is not within this range. For example, if we have a 5x5 grid then we have 25 homes. If we
choose the empty percentage to be 25% then at least ~6 spots should be empty. This command line argument is not
allowed to be changed during the execution of the program.
5. percentage of red and blue homes (red_percentage blue_percentage, integer values): The user should be allowed to
specify the percentage of red and blue homes the grid should contain. This should be based off of the remaining spots
after assigning the percentage of empty homes. For example, if we have a 5x5 grid then we have 25 homes. If the empty
percentage is 25% then at least ~6 spots should be empty. This leaves 19 homes to assign. if rest should be split between
red and blue spots (i.e., red_percentage = 50 and blue_percentage = 50) then there should be (9 reds and 10 blues) or (
reds and 9 blues). You can determine which homes get the remaining extra homes if the split is not an even split. This
command line argument is not allowed to be changed during the execution of the application. These values should range
between 0-100. The program should exit and print an error message if these values are not within this range.
Note: You can assume the "red_percentage" and "blue_percentage" when entered on the command line will add up to
100%.
6. similarity threshold (integer value): the threshold percetange described in the overview section. This value should be able
to change during the simulation. If the value is changed in the middle of the simulation then the simulation should use that
new threshold value during the future steps. This number should range between 0-100.
7. reset request : The user should be able reset the simulation at any point and to start a brand new simulation.
8. step request : A step request allows the user to move through the simulation step by step.
9. The simulation needs to display the percentage of satisfied home owners and the current step of the simulation.
10. The simulation needs to display the current round of the simulation. It should also state the number of maximum steps. For
example, ("Round 3 of 300") indicates that current round (i.e. step) is 3 and there are 300 maximum steps
11. There should be an indication to the user when the user has reached the threshold statisfaction.
12. The current values for all the parameters indicated above should be display to the user, regardless if they remain constant
throughout the execution of the program.

The following image is an example of how you could represent the city in your GUI:

Note: This image does not include all of the features/parameters you are required to implement. Also, some of these features
are specified on the command-line or are not required to be implemented. This image just shows an example of how potentially
your application could look.

Obviously, the above application uses widgets such as sliders and buttons to allow the user to interact with the application. Instead, your application needs to assign keys to adjust the parameters of the window. The key assignments are as follows:

Threshold : Up arrow key (increasing +5), Down arrow key (decreasing -5)
Step request : 's' key
Reset request : 'r' key
R (neighbor size) :Left arrow key (increasing +1), Right arrow key (decreasing -1).

Here is a quick overview again on when these parameters are able to be modified:

R (neighbor size) : Change only possible at the beginning of a simulation. During the simulation, this parameter should be
disabled.
similarity threshold : Change is possible at the beginning and during the stepping of the simulation.
reset request : Possible to execute at the beginning and during the stepping of the simulation.
step request : Possible at any time to allow the user to perform one step of the simulation.
Note: You are allowed to specify default values of your choosing for the parameters above.

Remember at the end of the simulation, the grid should reflect the relocations that took place during the simulation.

When designing your function decomposition, keep in mind that you need to do the following subtasks:

1. compute a homeowners similarity score;
2. create and initialize the list of open locations;
3. swap the values at two locations;
4. evaluate the open locations as potential new homes for a homeowner;
5. simulate one step of the simulation;
6. run steps until one of the stopping conditions are met (i.e., by the user requesting to stop or you have reached the
threshold satification value).
7. implement the -f optional flag

Do not combine the last three tasks into one mega-task, because the resulting code would be hard to read and debug. You will lose significant style points if you have a mega-task.

Loading a Grid

On the command line, you should be able to load in a grid configuration filepath. There should only be one grid configuration per file. You need to read in the grid configuration and show the initial grid to the user in the gui. The valid format for the file should look like the following:

NRows NCols DATA

The first line should specify the number of rows in the grid (NRows).
The second line shoud specify the number of columns in the grie (NCols).
The third line (and beyond) will contains lines, where in each line (a row) should only contain "B", "R", or "O" (Empty) to
represent the homeowners (i.e. columns). For each line, these homeowners values will be seperate by whitespace.

The following is an example of a valid grid configuration file:

3 3 R O B O R B B B R

From this file, you can determine the grid size, percentage of red, blue, and empty homes parameters. All other paremeters can be default values chosen by you. You can assume the given configuration file is valid and won’t be misformatted.

Graduating Students Requirements ONLY

If you are a graduating student then you are only required to have the following parameters:

1. Display the grid of homes
2. grid size
3. R (neighbor size)
4. percentage of red,blue, and empty homes
5. similarity threshold

You are only required to have a key assigned to run the simulation through its entirety. You do not need to reset the grid. The user will need to instead rerun the simulation with a new grid. You do need to implement the loading of grid option above.

Submission

Make sure to submit all your files in a directory called: hw6 and your files to your repository. Here are the steps that you could perform to submit the files:

1. Make sure your inside your repository at the root that contains the hw6 directory.
2. git add hw
3. git commit -m "Adding hw6 directory with files"
4. git push origin master

Also make sure to provide a little comment for each function. Just a one line comment before the type signature will suffice.

Acknowledgments

I would like to thank the CS121 team for providing the description of the Introduction and Data representation for this assignment.

发表评论

电子邮件地址不会被公开。 必填项已用*标注