Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Evelyn Xue
/
exue
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7a7a745a
authored
Oct 17, 2019
by
Evelyn Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
318 additions
and
0 deletions
pa2
pa2
0 → 100644
View file @
7a7a745a
'''
Schelling Model of Housing Segregation
Program for simulating a variant of Schelling's model of
housing segregation. This program takes five parameters:
filename -- name of a file containing a sample city grid
R - The radius of the neighborhood: home at Location (i, j) is in
the neighborhood of the home at Location (k,l)
if k-R <= i <= k+R and l-R <= j <= l+R
Similarity threshold - minimum acceptable threshold for ratio of the number
of similar neighbors to the number of occupied homes
in a neighborhood.
Occupancy threshold - minimum acceptable threshold for ratio of the number
of occupied homes to the total number of homes
in a neighborhood.
max_steps - the maximum number of passes to make over the city
during a simulation.
Sample:
python3 schelling.py --grid_file=tests/a19-sample-grid.txt --r=1 \
--simil_threshold=0.44 --occup_threshold=0.5 \
--max_steps=1
'''
import os
import sys
import click
import utility
def is_satisfied(grid, R, location, simil_threshold, occup_threshold):
'''
Determine whether or not the homeowner at a specific location is satisfied
using a neighborhood of radius R and specified similarity and occupancy thresholds.
Inputs:
grid: the grid
R: radius for the neighborhood
location: a grid location
simil_threshold: lower bound for similarity score
occup_threshold: lower bound for occupancy score
Returns: Boolean
'''
assert utility.is_grid(grid), ("The grid argument has the wrong type. "
"It should be a list of lists of strings "
"with the same number of rows and columns")
# We recommend adding an assertion to check that the location does
# not contain an open (unoccupied) home.
assert location not in utility.find_opens(grid)
# YOUR CODE HERE
def is_in_neighborhood(grid, R, location):
#Find locations of the specified location's neighborhood households
in_neighborhood = []
for k in range(grid_size):
for l in range(grid_size):
if k < r
if (k-R <= i <= k+R) and (l-R <= j <= l+R):
in_neighborhood.append((k,l))
return in_neighborhood #returns a list of locations in the specified location's neighborhood
def
def similarity_score(grid, R, location):
#Find the similarity score of the specified location
# Returns a value
occupied_households = set(grid) - set(utility.find_opens(grid))
same_color_and_in_neighborhood = []
for households in in_neighborhoods:
if grid[i][j] == grid[k][l]:
same_color_and_in_neighborhood.append(households)
S = len(same_color_and_in_neighborhood)
#number of homes in neighborhood of same color
occupied_homes_in_neighborhood = []
for households in occupied_households:
if households in in_neighborhood
occupied_homes_in_neighborhood.append(households)
H = len(occupied_homes_in_neighborhood)
#number of occupied homes in neighborhood
similarity_score = S/H
return similarity_score
def occupancy_score(grid, R, location):
#Returns a value
T = len(in_neighborhood)
occupancy_score = H/T
return occupancy_score
if similarity_score >= simil_threshold and occupancy_score >= occup_threshold:
return True
else:
return False
# PUT YOUR AUXILIARY FUNCTIONS HERE
def distance_bwt(original_location, new_location):
original_location = []
new_location = []
for a in range(grid_size):
for b in range(grid_size):
if grid[a][b]#??????: #DO I NEED THIS LINE AT ALL??
original_location.append((a,b))
for c in range(grid_size):
for d in range(grid_size):
if grid[c][d]#????: #AND DO I NEED THIS??
new_location.append((c,d))
distance = abs(d-b) + abs(c-a)
return distance
def swap_values(original_location, new_location):
standin = original_location
original_location = new_location
new_location = standin
return (original_location, new_location) ###HOW DO I USE THIS???
opens = utility.find_opens(grid)
def check_if_open_location_satisfies_homeowner(grid, R, simil_threshold, occup_threshold, opens):
for location in opens:
if is_satisfied(grid, R, location, simil_threshold, occup_threshold) == True:
return True
else:
return False
def
# DO NOT REMOVE THE COMMENT BELOW
#pylint: disable-msg=too-many-arguments
def do_simulation(grid, R, simil_threshold, occup_threshold, max_steps, opens):
'''
Do a full simulation.
Inputs:
grid: (list of lists of strings) the grid
R: (int) radius for the neighborhood
simil_threshold: (float) Similarity threshold
occup_threshold: (float) Occupancy threshold
max_steps: (int) maximum number of steps to do
opens: (list of tuples) a list of open locations
Returns:
The total number of relocations completed.
'''
assert utility.is_grid(grid), ("The grid argument has the wrong type. "
"It should be a list of lists of strings "
"with the same number of rows and columns")
# YOUR CODE HERE
# Check whether there are unsatisfied homeowners in the grid at all, if not, return 0
for location in grid:
if is_satisfied(grid, R, location, simil_threshold, occup_threshold) == True:
return 0
else:
check_if_open_location_satisfies_homeowner(grid, R, simil_threshold, occup_threshold, opens) ==
# Check whether any change takes place in this step, if none, stop simulation
if utility.find_mismatch(grid0, grid1) == None:
# REPLACE -1 with an appropriate return value
return -1
@click.command(name="schelling")
@click.option('--grid_file', type=click.Path(exists=True))
@click.option('--r', type=int, default=1, help="neighborhood radius")
@click.option('--simil_threshold', type=float, default=0.44, help="Similarity threshold")
@click.option('--occup_threshold', type=float, default=0.70, help="Occupancy threshold")
@click.option('--max_steps', type=int, default=1)
def go(grid_file, r, simil_threshold, occup_threshold, max_steps):
'''
Put it all together: do the simulation and process the results.
'''
if grid_file is None:
print("No parameters specified...just loading the code")
return
grid = utility.read_grid(grid_file)
opens = utility.find_opens(grid)
if len(grid) < 20:
print("Initial state of city:")
for row in grid:
print(row)
print()
num_relocations = do_simulation(grid, r, simil_threshold, occup_threshold, max_steps, opens)
print("Number of relocations done: " + str(num_relocations))
if len(grid) < 20:
print()
print("Final state of the city:")
for row in grid:
print(row)
if __name__ == "__main__":
go() # pylint: disable=no-value-for-parameter
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment