from matching import Player
= [
suitors "Bingley"),
Player("Collins"),
Player("Darcy"),
Player("Wickham"),
Player(
]
= [
reviewers "Charlotte"),
Player("Elizabeth"),
Player("Jane"),
Player("Lydia"),
Player( ]
Pairing up suitors and reviewers
In this tutorial we will be setting up and solving an instance of SM.
In particular, we will be using an example adapted from the great literary work, Pride and Prejudice (Austen 1813), where four women (Charlotte, Elizabeth, Jane and Lydia) are being courted by four male suitors (Bingley, Collins, Darcy, and Wickham).
From here on out, we’ll refer to the men and women as suitors and reviewers, respectively.
Creating the players and their preferences
To begin, we create an instance of the Player
class for each suitor and reviewer:
To set a player’s preferences, we use the Player.set_prefs()
method.
Each player’s preferences must be a list of all the Player
instances in the other party ordered according to how much they like them. That is, put your favourite first, followed by your second, and so on until you’ve put your least favourite last.
The preference lists below are based on some very loose interpretations of the original text and the need to create full lists. Please do not come for me about them.
A nice way to do this is by unpacking suitors
and reviewers
:
= suitors
bingley, collins, darcy, wickham = reviewers
charlotte, elizabeth, jane, lydia
bingley.set_prefs([jane, elizabeth, lydia, charlotte])
collins.set_prefs([elizabeth, jane, lydia, charlotte])
darcy.set_prefs([elizabeth, jane, charlotte, lydia])
wickham.set_prefs([lydia, jane, elizabeth, charlotte])
charlotte.set_prefs([collins, darcy, bingley, wickham])
elizabeth.set_prefs([wickham, darcy, bingley, collins])
jane.set_prefs([bingley, wickham, darcy, collins]) lydia.set_prefs([wickham, bingley, darcy, collins])
Running the game
With our now complete Player
instances, we pass the lists of players to the StableMarriage
class to create a game:
from matching.games import StableMarriage
= StableMarriage(suitors, reviewers) game
Then we can find a suitor-optimal, stable matching using the solve
method:
game.solve()
{Bingley: Jane, Collins: Charlotte, Darcy: Elizabeth, Wickham: Lydia}
Huzzah! We have shown (with some inventive but necessary subtext) that Jane Austen successfully created a stable matching between her characters over a century before the maths had been figured out.