Loading 2023__housing_allocations.ipynb +144 −71 Changes for 2023__housing_allocations.ipynb: 144 added lines, 71 removed lines. Original line number Original line Diff line number Diff line %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python # import numpy as np # import numpy as np # import pandas as pd # import pandas as pd import random import random import copy import copy import datetime import datetime # Set RNG seed using Aug 1, 2023 9am # Set RNG seed using Aug 1, 2023 9am random.seed(int(datetime.datetime(2023, 8, 1, 9, 0, 0, 0).timestamp())) random.seed(int(datetime.datetime(2023, 8, 1, 9, 0, 0, 0).timestamp())) ``` ``` %% Cell type:markdown id: tags: %% Cell type:markdown id: tags: A simple CSV/XLSX file with at least the following columns (it can have additional columns, doesn't matter): A simple CSV/XLSX file with at least the following columns (it can have additional columns, doesn't matter): Name, Year of study, Student or Postdoc, Female or not, Preferences, .. Name, Year of study, Student or Postdoc, Female or not, Preferences, .. A sample entry would be: A sample entry would be: Albert Einstein, 1, student, no, [0, 1, 2, 3, 4, 5, 6, 8, 7], Albert Einstein, 1, student, no, [0, 1, 2, 3, 4, 5, 6, 8, 7], Total number of available housing in each category. Total number of available housing in each category. %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python # inputs = pd.read_csv("") # inputs = pd.read_csv("") inputs = {} inputs = {} inputs["name"] = [ inputs["name"] = ["person 1", "person 2", "person 3"] "person 1", inputs["year"] = [1, 3, 2] "person 2", inputs["student_or_postdoc"] = ["student", "student", "postdoc"] "person 3" inputs["course"] = ["iphd", "phd", "postdoc"] ] inputs["female"] = [1, 0, 0] inputs["year"] = [ inputs["preferences"] = [[0, 5, 1, 2, 3], [0, 5, 1, 2, 3], [0, 5, 1, 2, 3]] 1, 3, 2 ] inputs["student_or_postdoc"] = [ "student", "student", "postdoc" ] inputs["course"] = [ "iphd", "phd", "postdoc" ] inputs["female"] = [ 1, 0, 0 ] inputs["preferences"] = [ [0,5,1,2,3], [0,5,1,2,3], [0,5,1,2,3] ] ``` ``` %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python availability = { availability = {} 0: 15, availability["student"] = {0: 15, 1: 23, 2: 25, 3: 35, 4: 40, 5: 18} 1: 23, availability["postdoc"] = {0: 4, 1: 23, 2: 0, 3: 0, 4: 0, 5: 10} 2: 25, 3: 35, 4: 40, 5: 18 } ``` ``` %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python def get_info_for_person(person_name, col_name): def get_info_for_person(person_name, col_name): if person_name not in inputs["name"]: if person_name not in inputs["name"]: raise IOError(f"We do not recognize name: {person_name}") raise IOError(f"We do not recognize name: {person_name}") if col_name not in inputs: if col_name not in inputs: raise IOError(f"We were not provided with info: {col_name}") raise IOError(f"We were not provided with info: {col_name}") idx_of_person = inputs["name"].index(person_name) idx_of_person = inputs["name"].index(person_name) return inputs[col_name][idx_of_person] return inputs[col_name][idx_of_person] def get_pool_number(person_name): def get_pool_number_for_student(person_name): pool_number = 3 pool_number = 3 if (get_info_for_person(person_name, "female") == 1) and ( if (get_info_for_person(person_name, "female") == 1) and ( get_info_for_person(person_name, "year") == 1 get_info_for_person(person_name, "year") == 1 ): ): pool_number = 1 pool_number = 1 elif (get_info_for_person(person_name, "year") == 1) or ( elif (get_info_for_person(person_name, "year") == 1) or ( (get_info_for_person(person_name, "year") == 5 and get_info_for_person( ( person_name, "course") == "phd") or ( get_info_for_person(person_name, "year") == 5 get_info_for_person(person_name, "year") == 6 and get_info_for_person( and get_info_for_person(person_name, "course") == "phd" person_name, "course") == "iphd") ) ): or ( get_info_for_person(person_name, "year") == 6 and get_info_for_person(person_name, "course") == "iphd" ) ): pool_number = 2 pool_number = 2 return pool_number return pool_number def get_pool_number_for_postdoc(person_name): # FIXME: Everyone is equal? return 1 def get_possible_pools(): def get_possible_pools(): return [1, 2, 3] return [1, 2, 3] ``` ``` %% Cell type:markdown id: tags: Check if inputs look okay %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python total_number_of_people = len(inputs["name"]) total_number_of_people = len(inputs["name"]) for col_name in inputs: for col_name in inputs: if len(inputs[col_name]) != total_number_of_people: if len(inputs[col_name]) != total_number_of_people: raise IOError("There are {} entries for column {}. We need {}.".format( raise IOError( len(inputs[col_name]), col_name, total_number_of_people "There are {} entries for column {}. We need {}.".format( )) len(inputs[col_name]), col_name, total_number_of_people ) ) print(f"Inputs look good. We got {total_number_of_people} entries.") print(f"Inputs look good. We got {total_number_of_people} entries.") ``` ``` %% Output %% Output Inputs look good. We got 3 entries. Inputs look good. We got 3 entries. %% Cell type:markdown id: tags: %% Cell type:markdown id: tags: Assign people to lottery pools Assign people to lottery pools %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python all_pools = {} ``` %% Cell type:code id: tags: ``` python housing_pools = {} housing_pools = {} for pool_id in get_possible_pools(): for pool_id in get_possible_pools(): _pool = [] _pool = [] for person_name in inputs["name"]: for person_name in inputs["name"]: if get_pool_number(person_name) == pool_id: # FIXME: Change the next line for postdocs _pool.append(person_name) if get_info_for_person(person_name, "student_or_postdoc") == "student": if get_pool_number_for_student(person_name) == pool_id: _pool.append(person_name) housing_pools[pool_id] = _pool housing_pools[pool_id] = _pool print("Here are our assignments:") print("Here are our assignments:") for pool_id in housing_pools: for pool_id in housing_pools: print(f"In pool {pool_id}, we have:\n {housing_pools[pool_id]}") print(f"In pool {pool_id}, we have:\n {housing_pools[pool_id]}") all_pools["student"] = copy.deepcopy(housing_pools) ``` ``` %% Output %% Output Here are our assignments: Here are our assignments: In pool 1, we have: In pool 1, we have: ['person 1'] ['person 1'] In pool 2, we have: In pool 2, we have: [] [] In pool 3, we have: In pool 3, we have: ['person 2', 'person 3'] ['person 2'] %% Cell type:code id: tags: ``` python housing_pools = {} for pool_id in get_possible_pools(): _pool = [] for person_name in inputs["name"]: # FIXME: Change the next line for postdocs if get_info_for_person(person_name, "student_or_postdoc") == "postdoc": if get_pool_number_for_postdoc(person_name) == pool_id: _pool.append(person_name) housing_pools[pool_id] = _pool print("Here are our assignments:") for pool_id in housing_pools: print(f"In pool {pool_id}, we have:\n {housing_pools[pool_id]}") all_pools["postdoc"] = copy.deepcopy(housing_pools) ``` %% Output Here are our assignments: In pool 1, we have: ['person 3'] In pool 2, we have: [] In pool 3, we have: [] %% Cell type:code id: tags: ``` python all_pools ``` %% Output {'student': {1: ['person 1'], 2: [], 3: ['person 2']}, 'postdoc': {1: ['person 3'], 2: [], 3: []}} %% Cell type:markdown id: tags: %% Cell type:markdown id: tags: Draw lottery and assign housing Draw lottery and assign housing %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python housing_allocations = {} housing_allocations = {} current_availability = copy.deepcopy(availability) ``` for pool_id in get_possible_pools(): %% Cell type:code id: tags: # randomly assign ranks, and sort according to it this_pool = housing_pools[pool_id] ``` python random.shuffle(this_pool) for pool_type in ["student", "postdoc"]: print("------------------------") print(f"\nAs per a randomized lottery, we will allocate in the following order:" print(f"Processing housing allotments for: {pool_type}") f"{this_pool}") current_availability = copy.deepcopy(availability[pool_type]) current_pool = all_pools[pool_type] # now we go over the ranked list, and assign housing based on availability for person_name in this_pool: for pool_id in get_possible_pools(): their_preferences = get_info_for_person(person_name, "preferences") # randomly assign ranks, and sort according to it for pref in their_preferences: this_pool = current_pool[pool_id] if current_availability[pref] > 0: random.shuffle(this_pool) # Allot NOW! print(f"""...assigning housing {pref} to { print( person_name} as it has { f"\nAs per a randomized lottery, we will allocate pool {pool_id} in the following order:" current_availability[pref]} vacancies still.""") f"{this_pool}" housing_allocations[person_name] = pref ) current_availability[pref] = current_availability[pref] - 1 break # now we go over the ranked list, and assign housing based on availability else: for person_name in this_pool: print(f"For {person_name}, acco {pref} is full. Moving on...") their_preferences = get_info_for_person(person_name, "preferences") continue for pref in their_preferences: if current_availability[pref] > 0: # Allot NOW! print( f"""...assigning housing {pref} to { person_name} as it has { current_availability[pref]} vacancies still.""" ) housing_allocations[person_name] = pref current_availability[pref] = current_availability[pref] - 1 break else: print(f"For {person_name}, acco {pref} is full. Moving on...") continue print("------------------------") ``` ``` %% Output %% Output ------------------------ Processing housing allotments for: student As per a randomized lottery, we will allocate in the following order:['person 1'] As per a randomized lottery, we will allocate pool 1 in the following order:['person 1'] ...assigning housing 0 to person 1 as it has 15 vacancies still. ...assigning housing 0 to person 1 as it has 15 vacancies still. As per a randomized lottery, we will allocate in the following order:[] As per a randomized lottery, we will allocate pool 2 in the following order:[] As per a randomized lottery, we will allocate in the following order:['person 2', 'person 3'] As per a randomized lottery, we will allocate pool 3 in the following order:['person 2'] ...assigning housing 0 to person 2 as it has 14 vacancies still. ...assigning housing 0 to person 2 as it has 14 vacancies still. ...assigning housing 0 to person 3 as it has 13 vacancies still. ------------------------ ------------------------ Processing housing allotments for: postdoc As per a randomized lottery, we will allocate pool 1 in the following order:['person 3'] ...assigning housing 0 to person 3 as it has 4 vacancies still. As per a randomized lottery, we will allocate pool 2 in the following order:[] As per a randomized lottery, we will allocate pool 3 in the following order:[] ------------------------ %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python print(f"Here are proposed housing allocations:\n{housing_allocations}") print(f"Here are proposed housing allocations:\n{housing_allocations}") ``` ``` %% Output %% Output Here are proposed housing allocations: Here are proposed housing allocations: {'person 1': 0, 'person 2': 0, 'person 3': 0} {'person 1': 0, 'person 2': 0, 'person 3': 0} %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python ``` ``` Loading
2023__housing_allocations.ipynb +144 −71 Changes for 2023__housing_allocations.ipynb: 144 added lines, 71 removed lines. Original line number Original line Diff line number Diff line %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python # import numpy as np # import numpy as np # import pandas as pd # import pandas as pd import random import random import copy import copy import datetime import datetime # Set RNG seed using Aug 1, 2023 9am # Set RNG seed using Aug 1, 2023 9am random.seed(int(datetime.datetime(2023, 8, 1, 9, 0, 0, 0).timestamp())) random.seed(int(datetime.datetime(2023, 8, 1, 9, 0, 0, 0).timestamp())) ``` ``` %% Cell type:markdown id: tags: %% Cell type:markdown id: tags: A simple CSV/XLSX file with at least the following columns (it can have additional columns, doesn't matter): A simple CSV/XLSX file with at least the following columns (it can have additional columns, doesn't matter): Name, Year of study, Student or Postdoc, Female or not, Preferences, .. Name, Year of study, Student or Postdoc, Female or not, Preferences, .. A sample entry would be: A sample entry would be: Albert Einstein, 1, student, no, [0, 1, 2, 3, 4, 5, 6, 8, 7], Albert Einstein, 1, student, no, [0, 1, 2, 3, 4, 5, 6, 8, 7], Total number of available housing in each category. Total number of available housing in each category. %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python # inputs = pd.read_csv("") # inputs = pd.read_csv("") inputs = {} inputs = {} inputs["name"] = [ inputs["name"] = ["person 1", "person 2", "person 3"] "person 1", inputs["year"] = [1, 3, 2] "person 2", inputs["student_or_postdoc"] = ["student", "student", "postdoc"] "person 3" inputs["course"] = ["iphd", "phd", "postdoc"] ] inputs["female"] = [1, 0, 0] inputs["year"] = [ inputs["preferences"] = [[0, 5, 1, 2, 3], [0, 5, 1, 2, 3], [0, 5, 1, 2, 3]] 1, 3, 2 ] inputs["student_or_postdoc"] = [ "student", "student", "postdoc" ] inputs["course"] = [ "iphd", "phd", "postdoc" ] inputs["female"] = [ 1, 0, 0 ] inputs["preferences"] = [ [0,5,1,2,3], [0,5,1,2,3], [0,5,1,2,3] ] ``` ``` %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python availability = { availability = {} 0: 15, availability["student"] = {0: 15, 1: 23, 2: 25, 3: 35, 4: 40, 5: 18} 1: 23, availability["postdoc"] = {0: 4, 1: 23, 2: 0, 3: 0, 4: 0, 5: 10} 2: 25, 3: 35, 4: 40, 5: 18 } ``` ``` %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python def get_info_for_person(person_name, col_name): def get_info_for_person(person_name, col_name): if person_name not in inputs["name"]: if person_name not in inputs["name"]: raise IOError(f"We do not recognize name: {person_name}") raise IOError(f"We do not recognize name: {person_name}") if col_name not in inputs: if col_name not in inputs: raise IOError(f"We were not provided with info: {col_name}") raise IOError(f"We were not provided with info: {col_name}") idx_of_person = inputs["name"].index(person_name) idx_of_person = inputs["name"].index(person_name) return inputs[col_name][idx_of_person] return inputs[col_name][idx_of_person] def get_pool_number(person_name): def get_pool_number_for_student(person_name): pool_number = 3 pool_number = 3 if (get_info_for_person(person_name, "female") == 1) and ( if (get_info_for_person(person_name, "female") == 1) and ( get_info_for_person(person_name, "year") == 1 get_info_for_person(person_name, "year") == 1 ): ): pool_number = 1 pool_number = 1 elif (get_info_for_person(person_name, "year") == 1) or ( elif (get_info_for_person(person_name, "year") == 1) or ( (get_info_for_person(person_name, "year") == 5 and get_info_for_person( ( person_name, "course") == "phd") or ( get_info_for_person(person_name, "year") == 5 get_info_for_person(person_name, "year") == 6 and get_info_for_person( and get_info_for_person(person_name, "course") == "phd" person_name, "course") == "iphd") ) ): or ( get_info_for_person(person_name, "year") == 6 and get_info_for_person(person_name, "course") == "iphd" ) ): pool_number = 2 pool_number = 2 return pool_number return pool_number def get_pool_number_for_postdoc(person_name): # FIXME: Everyone is equal? return 1 def get_possible_pools(): def get_possible_pools(): return [1, 2, 3] return [1, 2, 3] ``` ``` %% Cell type:markdown id: tags: Check if inputs look okay %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python total_number_of_people = len(inputs["name"]) total_number_of_people = len(inputs["name"]) for col_name in inputs: for col_name in inputs: if len(inputs[col_name]) != total_number_of_people: if len(inputs[col_name]) != total_number_of_people: raise IOError("There are {} entries for column {}. We need {}.".format( raise IOError( len(inputs[col_name]), col_name, total_number_of_people "There are {} entries for column {}. We need {}.".format( )) len(inputs[col_name]), col_name, total_number_of_people ) ) print(f"Inputs look good. We got {total_number_of_people} entries.") print(f"Inputs look good. We got {total_number_of_people} entries.") ``` ``` %% Output %% Output Inputs look good. We got 3 entries. Inputs look good. We got 3 entries. %% Cell type:markdown id: tags: %% Cell type:markdown id: tags: Assign people to lottery pools Assign people to lottery pools %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python all_pools = {} ``` %% Cell type:code id: tags: ``` python housing_pools = {} housing_pools = {} for pool_id in get_possible_pools(): for pool_id in get_possible_pools(): _pool = [] _pool = [] for person_name in inputs["name"]: for person_name in inputs["name"]: if get_pool_number(person_name) == pool_id: # FIXME: Change the next line for postdocs _pool.append(person_name) if get_info_for_person(person_name, "student_or_postdoc") == "student": if get_pool_number_for_student(person_name) == pool_id: _pool.append(person_name) housing_pools[pool_id] = _pool housing_pools[pool_id] = _pool print("Here are our assignments:") print("Here are our assignments:") for pool_id in housing_pools: for pool_id in housing_pools: print(f"In pool {pool_id}, we have:\n {housing_pools[pool_id]}") print(f"In pool {pool_id}, we have:\n {housing_pools[pool_id]}") all_pools["student"] = copy.deepcopy(housing_pools) ``` ``` %% Output %% Output Here are our assignments: Here are our assignments: In pool 1, we have: In pool 1, we have: ['person 1'] ['person 1'] In pool 2, we have: In pool 2, we have: [] [] In pool 3, we have: In pool 3, we have: ['person 2', 'person 3'] ['person 2'] %% Cell type:code id: tags: ``` python housing_pools = {} for pool_id in get_possible_pools(): _pool = [] for person_name in inputs["name"]: # FIXME: Change the next line for postdocs if get_info_for_person(person_name, "student_or_postdoc") == "postdoc": if get_pool_number_for_postdoc(person_name) == pool_id: _pool.append(person_name) housing_pools[pool_id] = _pool print("Here are our assignments:") for pool_id in housing_pools: print(f"In pool {pool_id}, we have:\n {housing_pools[pool_id]}") all_pools["postdoc"] = copy.deepcopy(housing_pools) ``` %% Output Here are our assignments: In pool 1, we have: ['person 3'] In pool 2, we have: [] In pool 3, we have: [] %% Cell type:code id: tags: ``` python all_pools ``` %% Output {'student': {1: ['person 1'], 2: [], 3: ['person 2']}, 'postdoc': {1: ['person 3'], 2: [], 3: []}} %% Cell type:markdown id: tags: %% Cell type:markdown id: tags: Draw lottery and assign housing Draw lottery and assign housing %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python housing_allocations = {} housing_allocations = {} current_availability = copy.deepcopy(availability) ``` for pool_id in get_possible_pools(): %% Cell type:code id: tags: # randomly assign ranks, and sort according to it this_pool = housing_pools[pool_id] ``` python random.shuffle(this_pool) for pool_type in ["student", "postdoc"]: print("------------------------") print(f"\nAs per a randomized lottery, we will allocate in the following order:" print(f"Processing housing allotments for: {pool_type}") f"{this_pool}") current_availability = copy.deepcopy(availability[pool_type]) current_pool = all_pools[pool_type] # now we go over the ranked list, and assign housing based on availability for person_name in this_pool: for pool_id in get_possible_pools(): their_preferences = get_info_for_person(person_name, "preferences") # randomly assign ranks, and sort according to it for pref in their_preferences: this_pool = current_pool[pool_id] if current_availability[pref] > 0: random.shuffle(this_pool) # Allot NOW! print(f"""...assigning housing {pref} to { print( person_name} as it has { f"\nAs per a randomized lottery, we will allocate pool {pool_id} in the following order:" current_availability[pref]} vacancies still.""") f"{this_pool}" housing_allocations[person_name] = pref ) current_availability[pref] = current_availability[pref] - 1 break # now we go over the ranked list, and assign housing based on availability else: for person_name in this_pool: print(f"For {person_name}, acco {pref} is full. Moving on...") their_preferences = get_info_for_person(person_name, "preferences") continue for pref in their_preferences: if current_availability[pref] > 0: # Allot NOW! print( f"""...assigning housing {pref} to { person_name} as it has { current_availability[pref]} vacancies still.""" ) housing_allocations[person_name] = pref current_availability[pref] = current_availability[pref] - 1 break else: print(f"For {person_name}, acco {pref} is full. Moving on...") continue print("------------------------") ``` ``` %% Output %% Output ------------------------ Processing housing allotments for: student As per a randomized lottery, we will allocate in the following order:['person 1'] As per a randomized lottery, we will allocate pool 1 in the following order:['person 1'] ...assigning housing 0 to person 1 as it has 15 vacancies still. ...assigning housing 0 to person 1 as it has 15 vacancies still. As per a randomized lottery, we will allocate in the following order:[] As per a randomized lottery, we will allocate pool 2 in the following order:[] As per a randomized lottery, we will allocate in the following order:['person 2', 'person 3'] As per a randomized lottery, we will allocate pool 3 in the following order:['person 2'] ...assigning housing 0 to person 2 as it has 14 vacancies still. ...assigning housing 0 to person 2 as it has 14 vacancies still. ...assigning housing 0 to person 3 as it has 13 vacancies still. ------------------------ ------------------------ Processing housing allotments for: postdoc As per a randomized lottery, we will allocate pool 1 in the following order:['person 3'] ...assigning housing 0 to person 3 as it has 4 vacancies still. As per a randomized lottery, we will allocate pool 2 in the following order:[] As per a randomized lottery, we will allocate pool 3 in the following order:[] ------------------------ %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python print(f"Here are proposed housing allocations:\n{housing_allocations}") print(f"Here are proposed housing allocations:\n{housing_allocations}") ``` ``` %% Output %% Output Here are proposed housing allocations: Here are proposed housing allocations: {'person 1': 0, 'person 2': 0, 'person 3': 0} {'person 1': 0, 'person 2': 0, 'person 3': 0} %% Cell type:code id: tags: %% Cell type:code id: tags: ``` python ``` python ``` ```