def compute_similarity_score(grid, R, location): ''' Compute the similarity score for the homeowner at the specified location using a neighborhood of radius R. Inputs: grid (list of lists of strings): the grid R (int): radius for the neighborhood location (pair of ints): a grid location Returns: float ''' # I have bypassed writing an assertion that a spot is open assert utility.is_grid(grid), "Grid argument is the wrong type" # First figure out what color this home is home_color = grid[location[0]][location[1]] # Next get the neighborhood around this home neighborhood = generate_neighborhood(grid, R, location) # Next build a counter for each type of home same_color_homes = 0.0 different_color_homes = 0.0 # Next use an equation to return the similarity score for other_home_color in neighborhood: if other_home_color == home_color: same_color_homes += 1.0 elif other_home_color != "O": different_color_homes += 1.0 return (same_color_homes) / (different_color_homes + same_color_homes)
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. # YOUR CODE HERE # Replace None with correct return value return None
def do_simulation(grid, R, threshold, max_steps): ''' Do a full simulation. Inputs: grid: (list of lists of strings) the grid R: (int) radius for the neighborhood threshold: (float) satisfaction threshold max_steps: (int) maximum number of steps to do Returns: The function number of steps executed. ''' open_homes = open_locations_original(grid) for i in range(max_steps): if one_iteration(grid, R, threshold, open_homes) == False: return i + 1 return max_steps 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 # REPLACE 0 with an appropriate return value return 0
def do_simulation(grid, R, M_threshold, B_threshold, max_steps, opens): ''' Do a full simulation. Inputs: grid: (list of lists of strings) the grid R: (int) radius for the neighborhood M_threshold: (float) satisfaction threshold for maroon homeowners B_threshold: (float) satisfaction threshold for blue homeowners 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 # REPLACE -1 with an appropriate return value num_relocations = 0 relocations = 1 steps = 0 while relocations > 0 and steps < max_steps: grid, relocations, opens = one_step(grid, R, M_threshold, B_threshold, opens) steps += 1 num_relocations += relocations return num_relocations
def compute_similarity_score(grid, R, location): ''' Compute the similarity score for the homeowner at the specified location using a neighborhood of radius R. Inputs: grid (list of lists of strings): the grid R (int): radius for the neighborhood location (pair of ints): a grid location Returns: float ''' 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") assert unoccupied_home(grid, location), \ ("The location does not contain an occupied home.") (row, column) = location row_list = list(range(row - R, row + (R + 1))) column_list = list(range(column - R, column + (R + 1))) rl_length = len(row_list) cl_length = len(column_list) S = 0 H = 0 if R == 0: return 1.0 if R > 0: for r in row_list: if row_list[0] < 0: del row_list[0] if row_list[-1] > len(grid) - 1: del row_list[-1] for c in column_list: if column_list[0] < 0: del column_list[0] if column_list[-1] > len(grid) - 1: del column_list[-1] for x in row_list: for y in column_list: if grid[x][y] == "B" or "R": H += 1 if grid[x][y] == "O": H -= 1 if grid[row][column] == grid[x][y]: S += 1 similarity_score = S / H return similarity_score
def do_simulation(grid, R, M_threshold, B_threshold, max_steps, opens): ''' Do a full simulation. Inputs: grid: (list of lists of strings) the grid R: (int) radius for the neighborhood M_threshold: (float) satisfaction threshold for maroon homeowners B_threshold: (float) satisfaction threshold for blue homeowners 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 # Initialize stopping condition variable count_steps = 0 # Initialize output variable count_relocation = 0 relocation_one_step = 10 # Simulate steps until maximun steps is achieved or no one can or want to relocate while ((count_steps < max_steps) and (relocation_one_step != 0)): relocation_one_step = 0 # Store the number of relocations of a single step in relocation_one_step while running the step relocation_one_step = simulate_one_step(grid, R, M_threshold, B_threshold, opens) # Add the number of this step's relocations into the total count_relocation += relocation_one_step # Count the number of steps count_steps += 1 # REPLACE -1 with an appropriate return value return count_relocation
def R1_direct_neighbor(grid, location): ''' Determine the number of R-1 direct neighbors of a particular location on the grid. Inputs: grid: the grid location: a grid location tuple Returns: the number of R-1 direct neighbors of a particular location on the grid ''' assert utility.is_grid(grid) # Extracting row and column index from the tuple lrow = location[0] lcol = location[1] # Initialize output variable occupied_count = 0 # Establishing boundaries for R-1 neighborhood, logic similar to is_satisfied() r_low = lrow - 1 r_upp = lrow + 2 c_low = lcol - 1 c_upp = lcol + 2 if (r_low < 0): r_low = 0 if (r_upp > len(grid)): r_upp = len(grid) if (c_low < 0): c_low = 0 if (c_upp > len(grid)): c_upp = len(grid) # Count the number of occupied home in the R-1 neighborhood for r in range(r_low, r_upp): for c in range(c_low, c_upp): if (grid[r][c] != "O"): occupied_count += 1 return occupied_count
def do_simulation(grid, R, threshold, max_steps): ''' Do a full simulation. Inputs: grid: (list of lists of strings) the grid R: (int) radius for the neighborhood threshold: (float) satisfaction threshold max_steps: (int) maximum number of steps to do Returns: (int) The number of relocations completed. ''' assert utility.is_grid(grid), "Grid argument is the wrong type" # Steps 1-5 outline the logic of the do_simulation method # 1. Create and initialize the list of open locations open_locations = [] for i in range(0, len(grid)): for j in range(0, len(grid[i])): if grid[i][j] == "O": open_locations.append((i, j)) # 4. Simulate one step of the simulation relocations = 0 steps = 0 # Keep track of the relocations of one step while(steps != max_steps): relocations += one_step(grid, R, threshold, open_locations) # 5. Run steps until one of the stopping conditions is met: No relocations in a step if relocations == 0: return relocations else: steps += 1 # 5. Run steps until one of the stopping conditions is met: Maximum steps reached if steps == max_steps: return relocations
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 # REPLACE -1 with an appropriate return value return -1
def do_simulation(grid, R, threshold, max_steps): ''' Do a full simulation. Inputs: grid: (list of lists of strings) the grid R: (int) radius for the neighborhood threshold: (float) satisfaction threshold max_steps: (int) maximum number of steps to do Returns: (int) The 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") step_count = 0 num_relocations = 0 if max_steps == 0: return num_relocations grid_list = [copy.deepcopy(grid)] while True: (new_grid, num_new_relocations) = relocate \ (copy.deepcopy(grid_list[0]), R, threshold) (grid, n) = relocate(grid, R, threshold) grid_list.append(new_grid) step_count += 1 num_relocations += num_new_relocations if step_count == max_steps: return num_relocations mismatch = utility.find_mismatch(grid_list[0], grid_list[1]) if mismatch == None: return num_relocations del grid_list[0] if step_count < max_steps: continue
def is_satisfied(grid, R, location, M_threshold, B_threshold): ''' Determine whether or not the homeowner at a specific location is satisfied using a neighborhood of radius R and specified M and B thresholds. Inputs: grid: the grid R: radius for the neighborhood location: a grid location M_threshold: lower bound for similarity score for maroon homeowners B_threshold: lower bound for similarity score for blue homeowners 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") # Storing the location tuple into row and column lrow = location[0] lcol = location[1] assert grid[lrow][lcol] != "O" assert R <= len(grid) # We recommend adding an assertion to check that the location does # not contain an open (unoccupied) home. # YOUR CODE HERE # Initializing output variable B_count = 0 M_count = 0 occupied_count = 0 # Establishing the R neighborhood boundaries r_low = lrow - R r_upp = lrow + R + 1 c_low = lcol - R c_upp = lcol + R + 1 # Making sure the boundaries don't extend outside of the grid if (r_low < 0): r_low = 0 if (r_upp > len(grid)): r_upp = len(grid) if (c_low < 0): c_low = 0 if (c_upp > len(grid)): c_upp = len(grid) # Count the number of M, B and non-O in the neighborhood for r in range(r_low, r_upp): for c in range(c_low, c_upp): if (grid[r][c] == "B"): B_count += 1 if (grid[r][c] == "M"): M_count += 1 if (grid[r][c] != "O"): occupied_count += 1 # Calculate the percentage and check if it satisfies the condition B_percent = B_count / occupied_count M_percent = M_count / occupied_count if (grid[lrow][lcol] == "B"): if B_percent >= B_threshold: return True if (grid[lrow][lcol] == "M"): if M_percent >= M_threshold: return True # Replace False with correct return value return False