Exemple #1
0
def test_check_stability():
    """ Test that StudentAllocation can recognise whether a matching is stable
    or not. """

    students = [Student("A"), Student("B"), Student("C")]
    projects = [Project("P", 2), Project("Q", 2)]
    supervisors = [Supervisor("X", 2), Supervisor("Y", 2)]

    a, b, c = students
    p, q = projects
    x, y = supervisors

    p.set_supervisor(x)
    q.set_supervisor(y)

    a.set_prefs([p, q])
    b.set_prefs([q])
    c.set_prefs([q, p])

    x.set_prefs([c, a])
    y.set_prefs([a, b, c])

    game = StudentAllocation(students, projects, supervisors)

    matching = game.solve()
    assert game.check_stability()

    matching[p] = [c]
    matching[q] = [a, b]

    assert not game.check_stability()
Exemple #2
0
def test_create_from_dictionaries(student_names, project_names,
                                  supervisor_names, capacities, seed):
    """ Test that StudentAllocation is created correctly when passed
    dictionaries of preferences and affiliations for each party. """

    stud_prefs, sup_prefs, proj_sups, sup_caps = make_connections(
        student_names, project_names, supervisor_names, capacities, seed)

    proj_caps = dict(zip(project_names, capacities))
    game = StudentAllocation.create_from_dictionaries(stud_prefs, sup_prefs,
                                                      proj_sups, proj_caps,
                                                      sup_caps)

    for student in game.students:
        assert student.pref_names == stud_prefs[student.name]
        assert student.matching is None

    for project in game.projects:
        assert project.supervisor.name == proj_sups[project.name]
        assert project.matching == []

    for supervisor in game.supervisors:
        assert supervisor.pref_names == sup_prefs[supervisor.name]
        assert supervisor.matching == []

    assert game.matching is None
def test_example_in_docs():
    """ Verify the example used in the discussion page of SA. """

    student_prefs = {
        "A": ["X1", "X2"],
        "B": ["Y2", "X2", "Y1"],
        "C": ["X1", "Y1", "X2"],
        "D": ["Y2", "X1", "Y1"],
        "E": ["X1", "Y2", "X2", "Y1"],
    }

    supervisor_prefs = {
        "X": ["B", "C", "A", "E", "D"],
        "Y": ["B", "C", "E", "D"],
    }

    project_supervisors = {"X1": "X", "X2": "X", "Y1": "Y", "Y2": "Y"}
    project_capacities = {p: 2 for p in project_supervisors}
    supervisor_capacities = {sup: 3 for sup in supervisor_prefs}

    game = StudentAllocation.create_from_dictionaries(
        student_prefs,
        supervisor_prefs,
        project_supervisors,
        project_capacities,
        supervisor_capacities,
    )
    a, b, c, d, e = game.students
    (x1, x2, y1, y2), (x, y) = game.projects, game.supervisors

    matching = student_allocation(game.students, game.projects,
                                  game.supervisors)
    assert matching == {x1: [c, a], x2: [], y1: [d], y2: [b, e]}
Exemple #4
0
def make_game(student_names, project_names, supervisor_names, capacities, seed):
    """ Make all of the players and the game itself. """

    np.random.seed(seed)
    students, projects, supervisors = make_players(
        student_names, project_names, supervisor_names, capacities
    )
    game = StudentAllocation(students, projects, supervisors)

    return students, projects, supervisors, game