Question: Help with the assign_inspectors, you can use the function above the assign_inspectors to help build assign_inspectors, and the require is here. Assigning Inspectors (function assign_inspectors)

Help with the assign_inspectors, you can use the function above the assign_inspectors to help build assign_inspectors, and the require is here.

Assigning Inspectors (function assign_inspectors)

Bridge condition indexes (BCI) represent the condition of a bridge on a past inspection, and we use the most recent BCI to prioritize certain bridges. There are three levels of priorities, namely 'urgent', 'high priority' and 'low priority'. 'Urgent' consists of a certain number of bridges which have had the lowest BCIs in their last inspection. The number of urgent bridges is a parameter of the inspector assignment mechanism and can be provided as an input. The highest BCI index among the urgent bridges is referred to as the critical BCI. The high and low priority bridges are determined based on their corresponding upper limits on BCI which are represented by the constants HIGH_PRIORITY_BCI, and LOW_PRIORITY_BCI, constrained by HIGH_PRIORITY_BCI < LOW_PRIORITY_BCI.

For example, if HIGH_PRIORITY_BCI is 60, then all bridges with their most recent BCI less than or equal to 60 and above the critical BCI are considered 'high priority'. Similarly, if LOW_PRIORITY_BCI is 70, then all bridges with a BCI less than or equal to 70 (but > 60) are considered 'low priority'.

When assigning bridges to inspectors, we want first prioritize the urgent bridges by assigning each of them to their nearest inspector. Once all the urgent bridges are assigned, we prioritize nearby high priority bridges within a large radius of the inspector over low priority bridges that are closer.

The radiuses are specified by the constants HIGH_PRIORITY_RADIUS, and LOW_PRIORITY_RADIUS.

You are told the maximum number of bridges to assign per inspector. The way we want to assign bridges to inspectors is as follows:

  1. First assign all the urgent bridges, each to its closest inspector.
  2. For each inspector assign high priority bridges with a distance <= HIGH_PRIORITY_RADIUS from the inspector.
  3. For each inspector if (1) and (2) still assigned fewer than the given maximum number of bridges, then we go on to assign low priority bridges with a distance <= LOW_PRIORITY_RADIUS from the inspector.

You should assign all the urgent bridges first. In this assignment you can always assume the total number of urgent bridges will be less than or equal to the maximum number of bridges each inspector can be assigned.

Afterwards, you can assign more bridges to each inspector based on the order they are listed in the inspectors list. In this step, one inspector at a time, we assign the maximum number of bridges to each inspector (or fewer than the maximum number of bridges, if all bridges have already been assigned). Inspectors should be assigned bridges based on the order they appear in the list (e.g., the first inspector in the list should be assigned up to the maximum number of bridges first, the second inspector should be assigned up to the maximum number of bridges next, and so on).

Bridges are assigned to an inspector using as many bridges that fulfill (1) as possible, followed by (2), and then (3) if there are still fewer than the maximum number of bridges assigned to that inspector. If there are multiple bridges with the same priority and radius, we choose the bridge with the lowest ID (e.g., if there are two low priority bridges with IDs 3 and 4, we would assign the bridge with ID 3 first). Each bridge should be assigned to at most one inspector.

The data used for this assignment is a subset of the data found in: https://www.ontario.ca/data/bridge-conditions """ import csv import math from typing import List, TextIO ID_INDEX = 0 NAME_INDEX = 1 HIGHWAY_INDEX = 2 LAT_INDEX = 3 LON_INDEX = 4 YEAR_INDEX = 5 LAST_MAJOR_INDEX = 6 LAST_MINOR_INDEX = 7 NUM_SPANS_INDEX = 8 SPAN_LENGTH_INDEX = 9 LENGTH_INDEX = 10 LAST_INSPECTED_INDEX = 11 BCIS_INDEX = 12 MAX_BCIS = 5 HIGH_PRIORITY_BCI = 60 LOW_PRIORITY_BCI = 100 HIGH_PRIORITY_RADIUS = 500 LOW_PRIORITY_RADIUS = 100 EARTH_RADIUS = 6371 ### SAMPLE DATA TO USE IN DOCSTRING EXAMPLES #### THREE_BRIDGES_UNCLEANED = [ ['1 - 32/', 'Highway 24 Underpass at Highway 403', '403', '43.167233', '-80.275567', '1965', '2014', '2009', '4', 'Total=64 (1)=12;(2)=19;(3)=21;(4)=12;', '65', '04/13/2012', '72.3', '', '72.3', '', '69.5', '', '70', '', '70.3', '', '70.5', '', '70.7', '72.9', ''], ['1 - 43/', 'WEST STREET UNDERPASS', '403', '43.164531', '-80.251582', '1963', '2014', '2007', '4', 'Total=60.4 (1)=12.2;(2)=18;(3)=18;(4)=12.2;', '61', '04/13/2012', '71.5', '', '71.5', '', '68.1', '', '69', '', '69.4', '', '69.4', '', '70.3', '73.3', ''], ['2 - 4/', 'STOKES RIVER BRIDGE', '6', '45.036739', '-81.33579', '1958', '2013', '', '1', 'Total=16 (1)=16;', '18.4', '08/28/2013', '85.1', '85.1', '', '67.8', '', '67.4', '', '69.2', '70', '70.5', '', '75.1', '', '90.1', ''] ] THREE_BRIDGES = [[1, 'Highway 24 Underpass at Highway 403', '403', 43.167233, -80.275567, '1965', '2014', '2009', 4, [12.0, 19.0, 21.0, 12.0], 65.0, '04/13/2012', [72.3, 69.5, 70.0, 70.3, 70.5, 70.7, 72.9]], [2, 'WEST STREET UNDERPASS', '403', 43.164531, -80.251582, '1963', '2014', '2007', 4, [12.2, 18.0, 18.0, 12.2], 61.0, '04/13/2012', [71.5, 68.1, 69.0, 69.4, 69.4, 70.3, 73.3]], [3, 'STOKES RIVER BRIDGE', '6', 45.036739, -81.33579, '1958', '2013', '', 1, [16.0], 18.4, '08/28/2013', [85.1, 67.8, 67.4, 69.2, 70.0, 70.5, 75.1, 90.1]] ] ####### BEGIN HELPER FUNCTIONS #################### def read_data(csv_file: TextIO) -> List[List[str]]: """Read and return the contents of the open CSV file csv_file as a list of lists, where each inner list contains the values from one line of csv_file. Docstring examples not given since results depend on csv_file. """ data = [] lines = csv.reader(csv_file) for line in lines: data.append(line) data = data[2:] return data def calculate_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> float: """Return the distance in kilometers between the two locations defined by (lat1, lon1) and (lat2, lon2), rounded to the nearest meter. >>> calculate_distance(43.659777, -79.397383, 43.657129, -79.399439) 0.338 >>> calculate_distance(43.42, -79.24, 53.32, -113.30) 2713.226 """ # This function uses the haversine function to find the # distance between two locations. You do NOT need to understand why it # works. You will just need to call on the function and work with what it # returns. # Based on code at goo.gl/JrPG4j # convert decimal degrees to radians lon1, lat1, lon2, lat2 = (math.radians(lon1), math.radians(lat1), math.radians(lon2), math.radians(lat2)) # haversine formula t lon_diff = lon2 - lon1 lat_diff = lat2 - lat1 a = (math.sin(lat_diff / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(lon_diff / 2) ** 2) c = 2 * math.asin(math.sqrt(a)) return round(c * EARTH_RADIUS, 3) ####### END HELPER FUNCTIONS ####################
def get_bridge(bridge_data: List[list], bridge_id: int) -> list: """Return the data for the bridge with id bridge_id from bridge_data. If there is no bridge with the given id, return an empty list. 
def find_bridges_in_radius(bridge_data: List[list], lat: float, long: float, distance: float) -> List[int]: """Return the IDs of the bridges that are within radius distance from (lat, long).
def find_critical_bci(bridge_data: List[list], num_urgents: int) -> float: """Returns the critical bci indext such that there are only nm_urgent bridges with bci less than or equal to the return value. All bridges with bci bellow or equal to the return value are in the urgent state. Precondition: num_urgents > 0 >>> find_critical_bci(THREE_BRIDGES, 2) 72.3 """
def get_distance_to_inspector(bridge1: list, inspector: list) -> float: """Return the distance in kilometres, rounded to the nearest metre (i.e., 3 decimal places), between the two bridges bridge1 and bridge2. >>> get_distance_to_inspector(get_bridge(THREE_BRIDGES, 1), [43.10, -80.15]) 12.638 """ 
def find_closest_inspector(bridge_data: List[list], bridge_id: int, inspectors: List[list]) -> int: """Return the index of the inspector in the list of inspectors that has the shortest distance to the bridge with id bridge_id from bridge_data. Precondition: a bridge with bridge_id is in bridge_data, and there is at least one inspector in the inspectors list. >>> find_closest_inspector(THREE_BRIDGES, 2, [[43.20, -80.35], \ [43.10, -80.15]]) 0 """

def assign_inspectors(bridge_data: List[list], inspectors: List[List[float]], max_bridges: int) -> List[List[int]]: """Return a list of bridge IDs to be assigned to each inspector in inspectors. inspectors is a list containing (latitude, longitude) pairs representing each inspector's location. At most max_bridges bridges should be assigned to an inspector, and each bridge should only be assigned once (to the first inspector that can inspect that bridge). See the "Assigning Inspectors" section of the handout for more details. >>> assign_inspectors(THREE_BRIDGES, [[43.10, -80.15]], 1) [[1]] >>> assign_inspectors(THREE_BRIDGES, [[43.10, -80.15]], 2) [[1, 2]] >>> assign_inspectors(THREE_BRIDGES, [[43.10, -80.15]], 3) [[1, 2]] >>> assign_inspectors(THREE_BRIDGES, [[43.20, -80.35], [43.10, -80.15]], 1) [[1], [2]] >>> assign_inspectors(THREE_BRIDGES, [[43.20, -80.35], [43.10, -80.15]], 2) [[1, 2], []] >>> assign_inspectors(THREE_BRIDGES, [[43.20, -80.35], [45.0368, -81.34]], 2) [[1, 2], [3]] >>> assign_inspectors(THREE_BRIDGES, [[38.691, -80.85], [43.20, -80.35]], 2) [[], [1, 2]] """ # TODO

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!