def test_check_validity(player_names, seed): """ Test that StableMarriage finds no errors when the game is solved. """ suitors, reviewers = make_players(player_names, seed) game = StableMarriage(suitors, reviewers) game.solve() assert game.check_validity()
def test_matching_consistent(player_names, seed): """ Test that StableMarriage recognises a valid matching requires there to be consistency between the game's matching and its players'. """ suitors, reviewers = make_players(player_names, seed) game = StableMarriage(suitors, reviewers) game.solve() game.suitors[0].matching = None game.reviewers[0].matching = None with pytest.raises(Exception): game._check_matching_consistent()
def test_check_for_unmatched_players(player_names, seed): """Test that StableMarriage recognises a valid matching requires all players to be matched as players.""" suitors, reviewers = make_players(player_names, seed) game = StableMarriage(suitors, reviewers) game.solve() player = game.suitors[0] player.matching = None with pytest.raises(MatchingError) as e: game.check_validity() error = e.unmatched_players[0] assert error.startswith(player.name)
def test_check_stability(): """ Test that StableMarriage can recognise whether a matching is stable. """ from matching import Player suitors = [Player("A"), Player("B")] reviewers = [Player("X"), Player("Y")] (a, b), (x, y) = suitors, reviewers a.set_prefs(reviewers) b.set_prefs(reviewers[::-1]) x.set_prefs(suitors) y.set_prefs(suitors[::-1]) game = StableMarriage(suitors, reviewers) matching = game.solve() assert game.check_stability() (a, b), (x, y) = game.suitors, game.reviewers matching[a] = y matching[b] = x assert not game.check_stability()
def test_solve(player_names, seed): """ Test that StableMarriage can solve games correctly when passed players. """ for optimal in ["suitor", "reviewer"]: suitors, reviewers = make_players(player_names, seed) game = StableMarriage(suitors, reviewers) matching = game.solve(optimal) assert isinstance(matching, Matching) assert set(matching.keys()) == set(suitors) assert set(matching.values()) == set(reviewers)
def test_all_matched(player_names, seed): """ Test that StableMarriage recognises a valid matching requires all players to be matched as players and as part of the game. """ suitors, reviewers = make_players(player_names, seed) game = StableMarriage(suitors, reviewers) matching = game.solve() matching[game.suitors[0]].matching = None game.matching[game.suitors[0]] = None with pytest.raises(Exception): game._check_all_matched()
def test_solve(player_names, seed): """Test that StableMarriage can solve games correctly when passed players.""" for optimal in ["suitor", "reviewer"]: suitors, reviewers = make_players(player_names, seed) game = StableMarriage(suitors, reviewers) matching = game.solve(optimal) assert isinstance(matching, Matching) suitors = sorted(suitors, key=lambda s: s.name) reviewers = sorted(reviewers, key=lambda r: r.name) matching_keys = sorted(matching.keys(), key=lambda k: k.name) matching_values = sorted(matching.values(), key=lambda v: v.name) for game_suitor, suitor in zip(matching_keys, suitors): assert game_suitor.name == suitor.name assert game_suitor.pref_names == suitor.pref_names for game_reviewer, reviewer in zip(matching_values, reviewers): assert game_reviewer.name == reviewer.name assert game_reviewer.pref_names == reviewer.pref_names
def test_check_stability(): """ Test that StableMarriage can recognise whether a matching is stable. """ from matching import Player suitors = [Player("A"), Player("B")] reviewers = [Player(1), Player(2)] suitors[0].set_prefs(reviewers) suitors[1].set_prefs(reviewers[::-1]) reviewers[0].set_prefs(suitors) reviewers[1].set_prefs(suitors[::-1]) game = StableMarriage(suitors, reviewers) matching = game.solve() assert game.check_stability() (s_a, s_b), (r_1, r_2) = game.suitors, game.reviewers matching[s_a] = r_2 matching[s_b] = r_1 assert not game.check_stability()
#except KeyError: # row_str = row_str + ", " + str(0) print(row_str) # future strategy # GS algo fine for small enough dataset & guarantees stable solution that optimizes max allocations # but since it's iterative & evaluates entire solution space, grows exponential # so use these pairings & scores & feedback to develop an ML soluted # also use ML to develop more sophisticated ranking & explore different scoring (objective) functions # pseudo-global matching based on ranked preferences suitors = [Player(name="A"), Player(name="B"), Player(name="C")] reviewers = [Player(name="D"), Player(name="E"), Player(name="F")] (A, B, C), (D, E, F) = suitors, reviewers #print(type(suitors)) A.set_prefs([D, E, F]) B.set_prefs([D, F, E]) C.set_prefs([F, D, E]) D.set_prefs([B, C, A]) E.set_prefs([A, C, B]) F.set_prefs([C, B, A]) from matching.games import StableMarriage game = StableMarriage(suitors, reviewers) print(game.solve()) # {A: E, B: D, C: F}