matching
matching copied to clipboard
HospitalResident: ValueError when using optimal="hospital"
Hi there! Thanks for this great library.
I'd like to know whether the following is expected behaviour. Using the HospitalResident algorithm, I have the scenario below. All hospitals rank all residents. The hospitals do not have enough capacity, though: there are 8 residents and only 6 spots.
When using the optimal="resident" option, the algorithm succeeds and says that residents S3 and S1 were not placed — which is expected.
Using the optimal="hospital" option, however, an exception is raised:
File "<...snip...>\Lib\site-packages\matching\algorithms\hospital_resident.py", line 141, in hospital_optimal
successors = resident.get_successors()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "<...snip...>\Lib\site-packages\matching\players\player.py", line 52, in get_successors
idx = self.prefs.index(self.matching)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: H2 is not in list
The code is as follows:
resident_preferences = {
"S1": ["H1", "H2"],
"S2": ["H3", "H1"],
"S3": ["H1", "H3"],
"S4": ["H3", "H2"],
"S5": ["H1", "H3"],
"S6": ["H2", "H1"],
"S7": ["H3", "H1"],
"S8": ["H2", "H1"],
}
hospital_preferences = {
"H1": ["S5", "S6", "S3", "S4", "S1", "S2", "S7", "S8"],
"H2": ["S8", "S2", "S3", "S4", "S5", "S6", "S7", "S1"],
"H3": ["S7", "S2", "S3", "S4", "S5", "S6", "S1", "S8"],
}
hospital_capacities = {
"H1": 2,
"H2": 2,
"H3": 2,
}
game = HospitalResident.create_from_dictionaries(
resident_preferences, hospital_preferences, hospital_capacities
)
# solution = game.solve(optimal="resident") <-- this works
solution = game.solve(optimal="hospital") # <-- this doesn't