Last active
August 27, 2017 05:11
-
-
Save danhammer/bdd89d56c42d12934b5bd939bf5d2631 to your computer and use it in GitHub Desktop.
rough example of the gale/shapely algorithm to find a stable marriage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy | |
import tierion | |
# GLOBALS | |
MEN = ['a', 'b', 'c', 'd', 'e'] | |
WOMEN = ['A', 'B', 'C', 'D', 'E'] | |
MEN_PREF = {m:numpy.random.permutation(WOMEN) for m in MEN} | |
WOMEN_PREF = {w:numpy.random.permutation(MEN) for w in WOMEN} | |
# SUPPORTING FUNCTIONS | |
def ranking_dict(woman): | |
# Returns a dictionary where each key is a man and each value is his ranking, | |
# as seen by the given woman; based on the global preferences. | |
rank_list = WOMEN_PREF[woman] | |
return dict(zip(rank_list, range(len(rank_list)))) | |
def accepted_proposals(proposals): | |
# Accepts a set of proposals and returns those that were accepted by the | |
# women. | |
approached_women = set([w for (_, w) in proposals]) | |
accepted = [] | |
for app_w in approached_women: | |
suitors = [m for (m, w) in proposals if w==app_w] | |
suitor_ranks = {k:v for k, v in ranking_dict(app_w).items() if k in suitors} | |
top_suitor = min(suitor_ranks, key=suitor_ranks.get) | |
accepted += [(top_suitor, app_w)] | |
return accepted | |
iteration = 0 | |
matched = {} | |
proposals = [] | |
while len(matched.items()) < len(MEN): | |
matched_men = [m for m, w in matched.items()] | |
new_proposals = [(x, MEN_PREF[x][iteration]) for x in MEN if x not in matched_men] | |
proposals += new_proposals | |
matched = dict(accepted_proposals(proposals)) | |
iteration += 1 | |
if (iteration == len(MEN) and (len(matched.items()) < len(MEN))): | |
# match the final pair, if no matches occur | |
matched_women = [w for m, w in matched.items()] | |
matched_men = [m for m, w in matched.items()] | |
[unmatched_man] = list(set(MEN) - set(matched_men)) | |
[unmatched_woman] = list(set(WOMEN) - set(matched_women)) | |
matched.update({unmatched_man: unmatched_woman}) | |
record = { | |
"iteration": iteration, | |
"new_proposals": dict(new_proposals), | |
"matched": dict(matched) | |
} | |
tierion.post_record(record) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def post_record(record): | |
base_path = 'https://api.tierion.com/v1/records' | |
headers = { | |
'X-Username': '[email protected]', | |
'X-Api-Key': 'bTv8fsPJmXXYMVnG9Vr2WUyJDyjjcNRVWR4ULI7NfF0=' | |
} | |
payload = { | |
"datastoreId": 3086, | |
"data": record | |
} | |
res = requests.post(base_path, json=payload, headers=headers) | |
return res.json() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment