Esempio n. 1
0
def helper(input_filename, expected_filename, R, M_threshold, B_threshold,
           max_num_steps, expected_num_relocations):
    '''
    Do one simulation with the specified parameters (R, threshold,
    max_num_steps) starting from the specified input file.  Match
    actual grid generated with the expected grid and match expected
    steps and actual steps.

    Inputs:
        input_filename: (string) name of the input grid file
        expected_filename: (string) name of the expected grid file.
        R: (int) radius for the neighborhood
        M_threshold: lower bound for similarity score for maroon homeowners
        B_threshold: lower bound for similarity score for blue homeowners
        max_steps: (int) maximum number of steps to do
        expected_num_relocations: (int) expected number of relocations
            performed during the simulation
    '''

    input_filename = os.path.join(BASE_DIR, input_filename)
    actual_grid = utility.read_grid(input_filename)
    expected_num_homeowners = count_homeowners(actual_grid)
    opens = utility.find_opens(actual_grid)

    actual_num_relocations = do_simulation(actual_grid, R, M_threshold,
                                           B_threshold, max_num_steps, opens)
    actual_num_homeowners = count_homeowners(actual_grid)

    expected_filename = os.path.join(BASE_DIR, expected_filename)
    expected_grid = utility.read_grid(expected_filename)

    if actual_num_relocations != expected_num_relocations:
        s = ("actual and expected values number of relocations do not match\n"
             "  got {:d}, expected {:d}")
        s = s.format(actual_num_relocations, expected_num_relocations)
        pytest.fail(s)

    if actual_num_homeowners != expected_num_homeowners:
        if actual_num_homeowners <= expected_num_homeowners:
            s = "Homeowners are fleeing the city!\n"
        else:
            s = ("The city is gaining homeowners.\n")
        s += ("  Actual number of homeowners: {:d}\n"
              "  Expected number of homeowners: {:d}\n")
        s = s.format(actual_num_homeowners, expected_num_homeowners)

        pytest.fail(s)

    mismatch = utility.find_mismatch(actual_grid, expected_grid)
    if mismatch:
        (i, j) = mismatch
        s = ("actual and expected grid values do not match "
             "at location ({:d}, {:d})\n")
        s = s.format(i, j)
        s = s + "  got {}, expected {}".format(actual_grid[i][j],
                                               expected_grid[i][j])
        pytest.fail(s)
Esempio n. 2
0
def empty_homes(grid):
    '''
    Based on the provided grid, create a list of tuples with the 
    empty homes.
    Inputs: 
        grid: the grid
    
    Returns:
        list of tuples with coordinates of empty homes    
    '''

    # utilize read_grid function from utility module to import grid
    grid = read_grid(grid)

    # Define an empty list of homes
    empty_homes_list = []

    # procedure to obtain a list of empty homes from the grid
    # iterate through every element of the rows
    for i in range(len(grid)):
        # iterate through each of the columns
        for j in range(len(grid[0])):
            # if the element is 
            if grid[i][j] == 'O':
                #store the index
                empty_homes_list.append((i,j))

    return empty_homes_list
Esempio n. 3
0
def go(grid_file, r, 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)

    if len(grid) < 20:
        print("Initial state of city:")
        for row in grid:
            print(row)
        print()

    num_relocations = do_simulation(grid, r, threshold, max_steps)
    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)
Esempio n. 4
0
def helper(filename, R, i, j, num_homes, same, empty, expected):
    # set threshold to be just a bit below (when expected is true) or
    # above (when expected is false) the ratio of the satisfaction
    # score and the number of homes.
    threshold = (same + 0.5*empty)/num_homes + (-0.01 if expected else 0.01)
    grid = utility.read_grid(TEST_DIR + filename)
    actual = is_satisfied(grid, R, threshold, (i, j))
    assert(actual == expected)
Esempio n. 5
0
def helper_check_steps(input_filename, R, threshold, max_num_steps, expected_num_steps):
    '''
    Do one simulation with the specified parameters (R, threshold,
    max_num_steps) starting from the specified input file.  Compare
    the actual number of steps taken to the expected number of steps.
    '''

    grid = utility.read_grid(TEST_DIR + input_filename)
    actual = do_simulation(grid, R, threshold, max_num_steps)
    assert(actual == expected_num_steps)
Esempio n. 6
0
def helper(input_filename, expected_filename, R, threshold,
           max_num_steps, expected_num_steps):
    '''
    Do one simulation with the specified parameters (R, threshold,
    max_num_steps) starting from the specified input file.  Match
    actual grid generated with the expected grid and match expected
    steps and actual steps.
    '''

    input_filename = os.path.join(BASE_DIR, input_filename)
    actual_grid = utility.read_grid(input_filename)
    actual_num_steps = do_simulation(actual_grid, R, threshold, max_num_steps)
    actual_num_homeowners = count_homeowners(actual_grid)

    expected_filename = os.path.join(BASE_DIR, expected_filename)
    expected_grid = utility.read_grid(expected_filename)
    expected_num_homeowners = count_homeowners(expected_grid)

    if actual_num_homeowners != expected_num_homeowners:
        if actual_num_homeowners <= expected_num_homeowners:
            s = "Homeowners are fleeing the city!\n"
        else:
            s = "City is gaining homeowners.\n"
        s += "    Actual number of homeowners: {:d}\n".format(actual_num_homeowners)
        s += "    Expected number of homeowners: {:d}\n".format(expected_num_homeowners)
        pytest.fail(s)

    mismatch = utility.find_mismatch(actual_grid, expected_grid)
    if mismatch:
        s = "actual and expected grid values do not match at location ({:d}, {:d})\n"
        s = s.format(mismatch[0], mismatch[1])

        actual = actual_grid[mismatch[0]][mismatch[1]]
        expected = expected_grid[mismatch[0]][mismatch[1]]
        s += "    got {}, expected {}".format(actual, expected)
        pytest.fail(s)

    # check steps only when expected steps >= 0
    if expected_num_steps >= 0 and actual_num_steps != expected_num_steps:
        s = "actual and expected values number of steps do not match\n"
        s = s + "    got {:d}, expected {:d}".format(actual_num_steps, expected_num_steps)
        pytest.fail(s)
Esempio n. 7
0
def go(args):
    '''
    Put it all together: parse the arguments, do the simulation and
    process the results.

    Inputs:
        args: (list of strings) the command-line arguments
    '''

    usage = "usage: python schelling.py <grid file name> <R > 0> <0 < threshold <= 1.0> <max steps >= 0>\n"
    grid = None
    threshold = 0.0
    R = 0
    max_steps = 0
    MAX_SMALL_GRID = 20

    if (len(args) != 5):
        print(usage)
        sys.exit(0)

    # parse and check the arguments
    try:
        grid = utility.read_grid(args[1])

        R = int(args[2])
        if R <= 0:
            print("R must be greater than zero")
            sys.exit(0)

        threshold = float(args[3])
        if (threshold <= 0.0 or threshold > 1.0):
            print("threshold must satisfy: 0 < threshold <= 1.0")
            sys.exit(0)

        max_steps = int(args[4])
        if max_steps <= 0:
            print("max_steps must be greater than or equal to zero")
            sys.exit(0)

    except:
        print(usage)
        sys.exit(0)

    num_steps = do_simulation(grid, R, threshold, max_steps)
    if len(grid) < MAX_SMALL_GRID:
        for row in grid:
            print(row)
    else:
        print("Result grid too large to print")

    print("Number of steps simulated: " + str(num_steps))
Esempio n. 8
0
def helper_check_relocations(input_filename, expected_filename, R, threshold, max_num_steps):
    '''
    Do one simulation with the specified parameters (R, threshold,
    max_num_steps) starting from the specified input file.  Match
    actual grid generated with the expected grid.
    '''

    grid = utility.read_grid(TEST_DIR + input_filename)
    do_simulation(grid, R, threshold, max_num_steps)

    expected_grid = utility.read_grid(TEST_DIR + expected_filename)

    # compare the actual state of the city against the expected state
    for i in range(len(grid)):
        for j in range(len(grid[0])):
            expected = expected_grid[i][j]
            # strip unsatisfied indicator from the expected file,
            # if necessary
            expected = expected if "U" not in expected else expected[1]
            if grid[i][j] != expected:
                s = "actual and expected values do not match at location ({:d}, {:d})\n".format(i, j)
                s = s + "got {}, expected {}".format(grid[i][j], expected)
                pytest.fail(s)
Esempio n. 9
0
def go(args):
    usage = "usage: python schelling.py <grid file name> <R > 0> <0 < threshold <= 1.0> <max steps >= 0>\n"
    grid = None
    threshold = 0.0
    R = 0
    max_steps = 0
    MAX_SMALL_GRID = 20

    
    if (len(args) != 5):
        print(usage)
        sys.exit(0)

    # parse and check the arguments
    try:
        grid = utility.read_grid(args[1])

        R = int(args[2])
        if R <= 0:
            print("R must be greater than zero")
            sys.exit(0)

        threshold = float(args[3])
        if (threshold <= 0.0 or threshold > 1.0):
            print("threshold must be between 0.0 and 1.0 not inclusive")
            sys.exit(0)

        max_steps = int(args[4])
        if max_steps <= 0:
            print("max_steps must be greater than or equal to zero")
            sys.exit(0)

    except:
        print(usage)
        sys.exit(0)
        

    num_steps = do_simulation(grid, R, threshold, max_steps)
    if len(grid) < MAX_SMALL_GRID:
        for row in grid:
            print(row)
    else:
        print("Result grid too large to print")

    print("Number of steps simulated: " + str(num_steps))
Esempio n. 10
0
def helper_test_is_satisfied(filename, R, location, threshold, expected):
    '''
    Check result of calling is_satisfied on the specified location with
    in an R-neighborhood with the specified threshold.

    Inputs:
        filename: (string) name of the input grid file
        R: (integer) neighborhood parameter
        location: (pair of integers) location in the grid to be tested
        threshold: (float) satisfaction threshold
        expected: (boolean) expected result.
    '''
    grid = utility.read_grid(filename)

    actual = schelling.is_satisfied(grid, R, threshold, location)
    if actual != expected:
        s = "Actual value ({}) is not equal to the expected value ({}).\n"
        s = s + "    @ location {} with R={:d} and threshold={:.2f}.\n"
        pytest.fail(s.format(actual, expected, location, R, threshold))
def helper_test_compute_similarity_score(filename, R, location, expected):
    '''
    Check result of calling compute_similarity_score on the specified
    location with in an R-neighborhood with the specified threshold.

    Inputs:
        filename: (string) name of the input grid file
        R: (integer) neighborhood parameter
        location: (pair of integers) location in the grid to be tested
        expected: (float) expected result.
    '''
    grid = utility.read_grid(filename)

    actual = schelling.compute_similarity_score(grid, R, location)
    if abs(actual - expected) > EPS or \
       (math.isnan(actual) and not math.isnan(expected)) or \
       (not math.isnan(actual) and math.isnan(expected)):

        s = "Actual value ({}) is not equal to the expected value ({}).\n"
        s = s + "    @ location {} with R-{:d} neighborhoods.\n"
        pytest.fail(s.format(actual, expected, location, R))
Esempio n. 12
0
def helper_test_is_satisfied(filename, R, location, M_threshold, B_threshold,
                             expected):
    '''
    Check result of calling is_satisfied on the specified
    location with in an R-neighborhood with the specified threshold.

    Inputs:
        filename: (string) name of the input grid file
        R: (integer) neighborhood parameter
        location: (pair of integers) location in the grid to be tested
        M_threshold: lower bound for similarity score for maroon homeowners
        B_threshold: lower bound for similarity score for blue homeowners
        expected: (float) expected result.
    '''
    grid = utility.read_grid(filename)

    actual = schelling.is_satisfied(grid, R, location, M_threshold,
                                    B_threshold)
    if actual != expected:

        s = "Actual value ({}) is not equal to the expected value ({}).\n"
        s = s + "    @ location {} with R-{:d} neighborhoods.\n"
        pytest.fail(s.format(actual, expected, location, R))
Esempio n. 13
0
import os
import sys
import utility


# Define lists
empty_homes = []

R = 2

grid2 = utility.read_grid("grid4.txt", strip_Us=True)

grid = [['R', 'R', 'O', 'R', 'R', 'R'], ['O', 'B', 'B', 'B', 'B', 'R'], ['R', 'R', 'R', 'R', 'R', 'R'], ['B', 'B', 'B', 'O', 'B', 'R'], ['R', 'O', 'R', 'R', 'O', 'R'], ['R', 'O', 'R', 'R', 'O', 'R']]


print(grid)

# obtain a list of empty homes
# iterate through each element of a row
for i in range(len(grid)):
    # iterate through each of the columns
    for j in range(len(grid[0])):
        # if the element is 
        if grid[i][j] == 'O':
            #store the index
            empty_homes.append((i,j))

#print(empty_homes)

location = (2, 2)