Skip to content

Commit

Permalink
updated logic to improve no centers within given threshold
Browse files Browse the repository at this point in the history
[LOGIC_IMPROVEMENT] Handle no centers are within the absolute distance threshold while ensuring that the same center is not chosen every time if there are other options available.

Resolves moest-np#31
  • Loading branch information
nirvikBasnet committed Apr 23, 2024
1 parent b654b2e commit 366bdc9
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions school_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,21 @@ def center_to_dict(c, distance):
return {'cscode': c['cscode'], 'name': c['name'], 'address': c['address'], 'capacity': c['capacity'], 'lat': c['lat'], 'long': c['long'], 'distance_km': distance}

def sort_key(c):
# intent: sort by preference score DESC then by distance_km ASC
# leaky abstraction - sorted requires a single numberic value for each element
return c['distance_km'] * random.uniform(1,5) - get_pref(school['scode'], c['cscode'])*100
return c['distance_km'] * random.uniform(1, 5) - get_pref(school['scode'], c['cscode']) * 100

school_lat = school.get('lat')
school_long = school.get('long')
if len(school_lat) == 0 or len(school_long) == 0:
return []

within_distance = []
nearest_distance = None;
nearest_distance = None
nearest_center = None
for c in centers:
distance = haversine_distance(float(school_lat), float(school_long), float(c.get('lat')), float(c.get('long')))
if school['scode'] == c['cscode']:
continue
if nearest_center == None or distance < nearest_distance:
if nearest_center is None or distance < nearest_distance:
nearest_center = c
nearest_distance = distance

Expand All @@ -83,8 +81,30 @@ def sort_key(c):

if len(within_distance) > 0:
return sorted(within_distance, key=sort_key)
else: # if there are no centers within given threshold, return one that is closest
return [center_to_dict(nearest_center, nearest_distance)]
else:

# Get all centers with their distances from the school
all_centers_with_distance = [
center_to_dict(c, haversine_distance(float(school_lat), float(school_long), float(c.get('lat')), float(c.get('long'))))
for c in centers
if school['scode'] != c['cscode']
]

# Filter centers based on preference cutoff
qualified_centers = [
center for center in all_centers_with_distance
if get_pref(school['scode'], center['cscode']) > PREF_CUTOFF
]

# If qualified centers are found, sort them by preference score and return
if qualified_centers:
sorted_centers = sorted(qualified_centers, key=sort_key)
return sorted_centers
else:
# If no qualified centers, return the nearest center
return [center_to_dict(nearest_center, nearest_distance)]



def read_tsv(file_path: str) -> List[Dict[str, str]]:
data = []
Expand Down

0 comments on commit 366bdc9

Please sign in to comment.