Exemple #1
0
def test_readme_example():
    """ Verify the example used in the repo README. """

    resident_prefs = {
        "A": ["C"],
        "S": ["C", "M"],
        "D": ["C", "M", "G"],
        "J": ["C", "G", "M"],
        "L": ["M", "C", "G"],
    }

    hospital_prefs = {
        "M": ["D", "L", "S", "J"],
        "C": ["D", "A", "S", "L", "J"],
        "G": ["D", "J", "L"],
    }

    capacities = {hosp: 2 for hosp in hospital_prefs}

    game = HospitalResident.create_from_dictionaries(resident_prefs,
                                                     hospital_prefs,
                                                     capacities)
    (A, S, D, J, L), (M, C, G) = game.residents, game.hospitals

    matching = hospital_resident(game.residents, game.hospitals)
    assert matching == {M: [L, S], C: [D, A], G: [J]}
Exemple #2
0
def test_example_in_issue():
    """ Verify that the matching found is consistent with the example in #67.
    """

    group_prefs = {
        "Group 1": ["Intellectual property", "Privacy"],
        "Group 2": ["Privacy", "Fairness in AI"],
        "Group 3": ["Privacy", "Social media"],
    }

    topic_hospital_prefs = {
        "Fairness in AI": ["Group 2"],
        "Intellectual property": ["Group 1"],
        "Privacy": ["Group 3", "Group 2", "Group 1"],
        "Social media": ["Group 3"],
    }

    capacities = {t: 2 for t in topic_hospital_prefs}

    game = HospitalResident.create_from_dictionaries(group_prefs,
                                                     topic_hospital_prefs,
                                                     capacities)
    (G1, G2, G3), (F, I, P, S) = game.residents, game.hospitals

    matching = hospital_resident(game.residents, game.hospitals)
    assert matching == {I: [G1], P: [G3, G2], F: [], S: []}
def test_hospital_optimal(resident_names, hospital_names, capacities, seed):
    """ Verify that the hospital-resident algorithm produces a valid,
    hospital-optimal matching for an instance of HR. """

    np.random.seed(seed)
    residents, hospitals = make_players(resident_names, hospital_names,
                                        capacities)
    matching = hospital_resident(residents, hospitals, optimal="hospital")

    assert set(hospitals) == set(matching.keys())

    for hospital, matches in matching.items():
        old_idx = -np.infty
        for resident in matches:
            idx = hospital.prefs.index(resident)
            assert idx >= old_idx
            old_idx = idx
def test_resident_optimal(resident_names, hospital_names, capacities, seed):
    """ Verify that the hospital-resident algorithm produces a valid,
    resident-optimal matching for an instance of HR. """

    np.random.seed(seed)
    residents, hospitals = make_players(resident_names, hospital_names,
                                        capacities)
    matching = hospital_resident(residents, hospitals, optimal="resident")

    assert set(hospitals) == set(matching.keys())
    assert all([
        r in set(residents) for r in
        {r_match
         for matches in matching.values() for r_match in matches}
    ])

    for resident in residents:
        if resident.matching:
            assert resident.prefs.index(resident.matching) == 0