In this assignment you will build up the Gale-Shapely matching algorithm presented in class to be a reusable function and use simulation to investigate its properties.
-
In a file called
helper.pywrite a function calledget_ranks(prefs)that takes a dictionary of hospital preference lists and returns a dictionary of rank dictionaries. (4 points)You can test your function as following:
>>> import helper >>> hospital_prefs = { ... 'X': ['B', 'A', 'C'], ... 'Y': ['A', 'B', 'C'], ... 'Z': ['A', 'B', 'C'] ... } >>> helper.get_ranks(hospital_prefs) {'Y': {'A': 1, 'C': 3, 'B': 2}, 'X': {'A': 2, 'C': 3, 'B': 1}, 'Z': {'A': 1, 'C': 3, 'B': 2}}
(Remember that dictionaries are unordered so the order of your output may differ.)
Hint: Iterate over each hospital
hinprefs. Then iterateifrom 0 tolen(prefs[h])(use therangefunction). Now you know thathranksprefs[h][i]withi+1. -
In
helper.pywrite a functionmatch(student_prefs, hospital_prefs, students)that returns a stable match dictionary. (4 points)The included
test_match.pytests yourmatch()function using the same example from class. You can run it at the command line using$ python test_match.py.Hint: take the code included
lecture_example.pyand remove the hard-coded data (students, hospitals, student_prefs, hospital_prefs, hospital_ranks). Call yourget_ranksfunction to create thehospital_ranksdictionary. Return the resulting match instead of printing it. -
Add code to
match()to validate thatstudent_prefs,hospital_prefs, andstudentshave the following properties:- The sudents specified in
studentsare the same as those instudent_prefs. - The number of students and hospitals are the same
- Each preference list (both student and hospital) is the right length
- Each student preference list contains all hospitals
- Each hospital preference list contains all students
If any of these conditions is false raise a
ValueErrorwith an informative message. - The sudents specified in
-
Write a function
random_preferences(n)inhelper.pythat generates a preference dictionary of lengthnwhere the keys are the numbers0ton-1and the values are i.i.d. random preferences. (4 points)The included
test_match_random.pyrunsmatch()with random preferences generated by yourrandom_preferences()function withn=5. You can run it at the command line using$ python test_match_random.py.Hint: use
range(n)to iterate and uselist(np.random.permutation(n))to generate a random preference list. (after importing NumPy:import numpy as np) -
Write a function
get_hospital_match_ranks(M, hospital_prefs)that, given a matchingMand hospital preferences dictionaryhospital_prefsreturns a dictionary whose keys are hospitals and values are the rank of the hospital's match. Write an analogous functionget_student_match_ranks(M, student_prefs). (4 points)For example, you can test
get_hospital_match_ranksin then=3example from class as follows:>>> import helper >>> helper.get_hospital_match_ranks({'Y': 'B', 'X': 'A', 'Z': 'C'}, {'X': ['B', 'A', 'C'], 'Y': ['A', 'B', 'C'], 'Z': ['A', 'B', 'C']}) {'Y': 2, 'X': 2, 'Z': 3}
-
Write a script called
simulate.pythat simulates the the matching problem one thousand times withn=100. For each one store the average hospital and student match ranks. Print the average hospital match rank and student match rank across all simulations.For example, your script might look like this:
$ python simulate.py Average hospital match rank: ??? Average student match rank: ???
Hint: Initialize empty lists
student_match_avg_rankandhospital_match_avg_rank. Iterate overrange(1000). Simulate the matching problem (seetest_match_random.py). Get the hospital and student rank dictionaries using the functions you wrote in B.3. Calculate the average hospital and student ranks by taking the mean over thevalues()of each dictionary. Append the averages to the respective lists (student_match_avg_rankorhospital_match_avg_rank). Finally, take the average over each list.