![CI status](https://github.com/daffidwilde/matching/actions/workflows/ci.yml/badge.svg?branch=main)
[![Code style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/ambv/black)
[![Zenodo archive](https://zenodo.org/badge/DOI/10.5281/zenodo.2553125.svg)](https://doi.org/10.5281/zenodo.2553125)
[![JOSS paper](https://joss.theoj.org/papers/10.21105/joss.02169/status.svg)](https://doi.org/10.21105/joss.02169)
# A package for solving matching games
Matching games allow for the allocation of resources and partnerships in
a fair way. Typically, a matching game is defined by two sets of players
that each have preferences over at least some of the elements of the
other set. The objective of the game is then to find a mapping between
the sets of players in which everyone is happy enough with their match.
In `matching`, we deal with four types of matching game:
- the stable marriage problem (SM);
- the hospital-resident assignment problem (HR);
- the student-allocation problem (SA);
- the stable roommates problem (SR).
## Installation
Matching requires Python 3.5 or above, and relies only on
[NumPy](http://www.numpy.org/) for general use.
The library is most easily installed using `pip`:
```bash
$ python -m pip install matching
```
However, if you would like to install it from source then go ahead and
clone the GitHub repository:
```bash
$ git clone https://github.com/daffidwilde/matching.git
$ cd matching
$ python -m pip install .
```
## Documentation
Full documentation (including tutorials and discussion material) is
available here: <https://daffidwilde.github.io/matching/>
An academic paper on this library has been included in the Journal of
Open Source Software (JOSS) and is available here:
<https://joss.theoj.org/papers/10.21105/joss.02169>
## Playing a simple game
With all games, Matching uses a `Player` class to represent the members
of the "applying" party, i.e. residents and students. For HR and SA,
there are specific classes to represent the roles of `Hospital`,
`Project` and `Supervisor`.
Consider the following instance of SM which is represented on a
bipartite graph where the suitors and reviewers are along the left and
right respectively.
![image](./tex/stable_marriage.png){.align-center width="10cm"}
We can construct these preferences using dictionaries:
```python
>>> suitor_preferences = {
... "A": ["D", "E", "F"], "B": ["D", "F", "E"], "C": ["F", "D", "E"]
... }
>>> reviewer_preferences = {
... "D": ["B", "C", "A"], "E": ["A", "C", "B"], "F": ["C", "B", "A"]
... }
```
Then to solve this matching game, we make use of the `StableMarriage`
class, like so:
```python
>>> from matching.games import StableMarriage
>>> game = StableMarriage.create_from_dictionaries(
... suitor_preferences, reviewer_preferences
... )
>>> game.solve()
{A: E, B: D, C: F}
```
## The `Matching` object
This matching is not a standard Python dictionary, though it does
largely look and behave like one. It is in fact an instance of the
`SingleMatching` class:
```python
>>> matching = game.matching
>>> type(matching)
<class 'matching.matchings.SingleMatching'>
```
This dictionary-like object is primarily useful as a teaching device
that eases the process of manipulating a matching after a solution has
been found.
## `Player` classes
Despite passing dictionaries of strings here, the matching displays
instances of `matching.player.Player`:
```python
>>> matching = game.matching
>>> for suitor in matching:
... print(type(suitor))
<class 'matching.players.player.Player'>
<class 'matching.players.player.Player'>
<class 'matching.players.player.Player'>
```
This is because `create_from_dictionaries` creates instances of the
appropriate player classes first and passes them to the game class.
Using dictionaries like this can be an efficient way of creating large
games but it does require the names of the players in each party to be
unique.
With all games, Matching uses a `Player` class to represent the members
of the "applying" party, i.e. residents and students. For HR and SA,
there are specific classes to represent the roles of `Hospital`,
`Project` and `Supervisor`.
## A note on performance
One of the limitations of this library is the time complexities of the
algorithm implementations. In practical terms, the running time of any
of the algorithms in Matching is negligible but the theoretic complexity
of each has not yet been attained. For example, an instance of HR with
400 applicants and 20 hospitals is solved in less than one tenth of a
second:
```python
>>> from matching.games import HospitalResident
>>> import numpy as np
>>> prng = np.random.default_rng(0)
>>> num_residents, num_hospitals = 400, 20
>>> resident_prefs = {
... r: np.argsort(prng.random(size=num_hospitals))
... for r in range(num_residents)
... }
>>> hospital_prefs = {
... h: np.argsort(prng.random(size=num_residents))
... for h in range(num_hospitals)
... }
>>> capacities = {h: num_hospitals for h in hospital_prefs}
>>> game = HospitalResident.create_from_dictionaries(
... resident_prefs, hospital_prefs, capacities
... )
>>> _ = game.solve() # 48.6 ms ± 963 µs per loop
```
## Get in contact!
I hope this package is useful, and feel free to contact me here with any
issues or recommendations. Pull requests are always welcome!
Raw data
{
"_id": null,
"home_page": "",
"name": "matching",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.5",
"maintainer_email": "",
"keywords": "game-theory,gale-shapley,matching-games,stable-marriage,hospital-resident,stable-roommates,project-allocation",
"author": "Vince Knight",
"author_email": "Henry Wilde <henrydavidwilde@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/a3/d2/0c0802609e9f3fd7029ece2a1fc7145be669c6713f3be1da353ac4cdb8ce/matching-1.4.3.tar.gz",
"platform": null,
"description": "![CI status](https://github.com/daffidwilde/matching/actions/workflows/ci.yml/badge.svg?branch=main)\n[![Code style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/ambv/black)\n[![Zenodo archive](https://zenodo.org/badge/DOI/10.5281/zenodo.2553125.svg)](https://doi.org/10.5281/zenodo.2553125)\n[![JOSS paper](https://joss.theoj.org/papers/10.21105/joss.02169/status.svg)](https://doi.org/10.21105/joss.02169)\n\n\n# A package for solving matching games\n\nMatching games allow for the allocation of resources and partnerships in\na fair way. Typically, a matching game is defined by two sets of players\nthat each have preferences over at least some of the elements of the\nother set. The objective of the game is then to find a mapping between\nthe sets of players in which everyone is happy enough with their match.\n\nIn `matching`, we deal with four types of matching game:\n\n- the stable marriage problem (SM);\n- the hospital-resident assignment problem (HR);\n- the student-allocation problem (SA);\n- the stable roommates problem (SR).\n\n## Installation\n\nMatching requires Python 3.5 or above, and relies only on\n[NumPy](http://www.numpy.org/) for general use.\n\nThe library is most easily installed using `pip`:\n\n```bash\n $ python -m pip install matching\n```\n\nHowever, if you would like to install it from source then go ahead and\nclone the GitHub repository:\n\n```bash\n $ git clone https://github.com/daffidwilde/matching.git\n $ cd matching\n $ python -m pip install .\n```\n\n## Documentation\n\nFull documentation (including tutorials and discussion material) is\navailable here: <https://daffidwilde.github.io/matching/>\n\nAn academic paper on this library has been included in the Journal of\nOpen Source Software (JOSS) and is available here:\n<https://joss.theoj.org/papers/10.21105/joss.02169>\n\n## Playing a simple game\n\nWith all games, Matching uses a `Player` class to represent the members\nof the \"applying\" party, i.e. residents and students. For HR and SA,\nthere are specific classes to represent the roles of `Hospital`,\n`Project` and `Supervisor`.\n\nConsider the following instance of SM which is represented on a\nbipartite graph where the suitors and reviewers are along the left and\nright respectively.\n\n![image](./tex/stable_marriage.png){.align-center width=\"10cm\"}\n\nWe can construct these preferences using dictionaries:\n\n```python\n>>> suitor_preferences = {\n... \"A\": [\"D\", \"E\", \"F\"], \"B\": [\"D\", \"F\", \"E\"], \"C\": [\"F\", \"D\", \"E\"]\n... }\n>>> reviewer_preferences = {\n... \"D\": [\"B\", \"C\", \"A\"], \"E\": [\"A\", \"C\", \"B\"], \"F\": [\"C\", \"B\", \"A\"]\n... }\n\n```\n\nThen to solve this matching game, we make use of the `StableMarriage`\nclass, like so:\n\n\n```python\n>>> from matching.games import StableMarriage\n>>> game = StableMarriage.create_from_dictionaries(\n... suitor_preferences, reviewer_preferences\n... )\n>>> game.solve()\n{A: E, B: D, C: F}\n\n```\n\n## The `Matching` object\n\nThis matching is not a standard Python dictionary, though it does\nlargely look and behave like one. It is in fact an instance of the\n`SingleMatching` class:\n\n```python\n>>> matching = game.matching\n>>> type(matching)\n<class 'matching.matchings.SingleMatching'>\n\n```\n\nThis dictionary-like object is primarily useful as a teaching device\nthat eases the process of manipulating a matching after a solution has\nbeen found.\n\n## `Player` classes\n\nDespite passing dictionaries of strings here, the matching displays\ninstances of `matching.player.Player`:\n\n```python\n>>> matching = game.matching\n>>> for suitor in matching:\n... print(type(suitor))\n<class 'matching.players.player.Player'>\n<class 'matching.players.player.Player'>\n<class 'matching.players.player.Player'>\n\n```\n\nThis is because `create_from_dictionaries` creates instances of the\nappropriate player classes first and passes them to the game class.\nUsing dictionaries like this can be an efficient way of creating large\ngames but it does require the names of the players in each party to be\nunique.\n\nWith all games, Matching uses a `Player` class to represent the members\nof the \"applying\" party, i.e. residents and students. For HR and SA,\nthere are specific classes to represent the roles of `Hospital`,\n`Project` and `Supervisor`.\n\n## A note on performance\n\nOne of the limitations of this library is the time complexities of the\nalgorithm implementations. In practical terms, the running time of any\nof the algorithms in Matching is negligible but the theoretic complexity\nof each has not yet been attained. For example, an instance of HR with\n400 applicants and 20 hospitals is solved in less than one tenth of a\nsecond:\n\n```python\n>>> from matching.games import HospitalResident\n>>> import numpy as np\n>>> prng = np.random.default_rng(0)\n>>> num_residents, num_hospitals = 400, 20\n>>> resident_prefs = {\n... r: np.argsort(prng.random(size=num_hospitals))\n... for r in range(num_residents)\n... }\n>>> hospital_prefs = {\n... h: np.argsort(prng.random(size=num_residents))\n... for h in range(num_hospitals)\n... }\n>>> capacities = {h: num_hospitals for h in hospital_prefs}\n>>> game = HospitalResident.create_from_dictionaries(\n... resident_prefs, hospital_prefs, capacities\n... )\n>>> _ = game.solve() # 48.6 ms \u00b1 963 \u00b5s per loop\n\n```\n\n## Get in contact!\n\nI hope this package is useful, and feel free to contact me here with any\nissues or recommendations. Pull requests are always welcome!\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "A package for solving matching games",
"version": "1.4.3",
"project_urls": {
"changelog": "https://github.com/daffidwilde/matching/blob/main/CHANGES.md",
"documentation": "https://daffidwilde.github.io/matching",
"homepage": "https://github.com/daffidwilde/matching"
},
"split_keywords": [
"game-theory",
"gale-shapley",
"matching-games",
"stable-marriage",
"hospital-resident",
"stable-roommates",
"project-allocation"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b807a405824cf7392682d949bd482695dbff004dfa259b9be0e32780ffb88941",
"md5": "b4e119ce7873153d800fcc13495669dc",
"sha256": "0ce02354174aff8e0dc6fdc155a0f25e113107b7dbf983e51a8288e1726efdd1"
},
"downloads": -1,
"filename": "matching-1.4.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b4e119ce7873153d800fcc13495669dc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.5",
"size": 30065,
"upload_time": "2023-10-04T14:30:48",
"upload_time_iso_8601": "2023-10-04T14:30:48.015918Z",
"url": "https://files.pythonhosted.org/packages/b8/07/a405824cf7392682d949bd482695dbff004dfa259b9be0e32780ffb88941/matching-1.4.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a3d20c0802609e9f3fd7029ece2a1fc7145be669c6713f3be1da353ac4cdb8ce",
"md5": "adc25c2bec90292f32692f4b97fb1599",
"sha256": "130d9e88661f623dc39e84675facee2ce2b192420a2f8c4f274a38393837a5de"
},
"downloads": -1,
"filename": "matching-1.4.3.tar.gz",
"has_sig": false,
"md5_digest": "adc25c2bec90292f32692f4b97fb1599",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5",
"size": 25144,
"upload_time": "2023-10-04T14:30:50",
"upload_time_iso_8601": "2023-10-04T14:30:50.198354Z",
"url": "https://files.pythonhosted.org/packages/a3/d2/0c0802609e9f3fd7029ece2a1fc7145be669c6713f3be1da353ac4cdb8ce/matching-1.4.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-04 14:30:50",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "daffidwilde",
"github_project": "matching",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "matching"
}