mirror of
https://github.com/tgorordo/carousel.git
synced 2026-06-14 21:32:14 -07:00
format
This commit is contained in:
parent
8743b04809
commit
8d94b96b85
2 changed files with 72 additions and 17 deletions
|
|
@ -5,6 +5,7 @@ import polars.selectors as pls
|
||||||
|
|
||||||
import itertools as it
|
import itertools as it
|
||||||
|
|
||||||
|
|
||||||
def rank_to_pref(R):
|
def rank_to_pref(R):
|
||||||
"""Converts a ranking to a preference."""
|
"""Converts a ranking to a preference."""
|
||||||
id_col_name = R.select(pls.by_index(0)).to_series().name
|
id_col_name = R.select(pls.by_index(0)).to_series().name
|
||||||
|
|
@ -66,53 +67,74 @@ def check_valid_rank(R):
|
||||||
).get_column("ties")[0]
|
).get_column("ties")[0]
|
||||||
return not ties
|
return not ties
|
||||||
|
|
||||||
|
|
||||||
def check_valid_match(match, applicants, reviewers):
|
def check_valid_match(match, applicants, reviewers):
|
||||||
# TODO
|
# TODO
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def check_valid_assgn(assgn, applicants, reviewers):
|
def check_valid_assgn(assgn, applicants, reviewers):
|
||||||
# TODO
|
# TODO
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_rank(ranking, ranker, rankee):
|
def get_rank(ranking, ranker, rankee):
|
||||||
idx = ranking.select(pl.arg_where(pl.col("") == rankee)).item()
|
idx = ranking.select(pl.arg_where(pl.col("") == rankee)).item()
|
||||||
return ranking[ranker][idx]
|
return ranking[ranker][idx]
|
||||||
|
|
||||||
def check_unstable(match, applicant_ranking, reviewer_ranking):
|
|
||||||
applicants = applicant_ranking.columns[1:] # assume unique applicants
|
|
||||||
for a, b in it.combinations(applicants, 2):
|
|
||||||
A = match.select(c for c in match.iter_columns() if a in c).to_series().name # the reviewer a is matched to
|
|
||||||
B = match.select(c for c in match.iter_columns() if b in c).to_series().name # the reviewer b is matched to
|
|
||||||
|
|
||||||
b_prefers_A = get_rank(applicant_ranking, b, A) < get_rank(applicant_ranking, b, B)
|
def check_unstable(match, applicant_ranking, reviewer_ranking):
|
||||||
A_prefers_b = get_rank(reviewer_ranking, A, b) < get_rank(reviewer_ranking, A, a)
|
applicants = applicant_ranking.columns[1:] # assume unique applicants
|
||||||
|
for a, b in it.combinations(applicants, 2):
|
||||||
|
A = (
|
||||||
|
match.select(c for c in match.iter_columns() if a in c).to_series().name
|
||||||
|
) # the reviewer a is matched to
|
||||||
|
B = (
|
||||||
|
match.select(c for c in match.iter_columns() if b in c).to_series().name
|
||||||
|
) # the reviewer b is matched to
|
||||||
|
|
||||||
|
b_prefers_A = get_rank(applicant_ranking, b, A) < get_rank(
|
||||||
|
applicant_ranking, b, B
|
||||||
|
)
|
||||||
|
A_prefers_b = get_rank(reviewer_ranking, A, b) < get_rank(
|
||||||
|
reviewer_ranking, A, a
|
||||||
|
)
|
||||||
if b_prefers_A and A_prefers_b:
|
if b_prefers_A and A_prefers_b:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# or
|
# or
|
||||||
a_prefers_B = get_rank(applicant_ranking, a, B) < get_rank(applicant_ranking, a, A)
|
a_prefers_B = get_rank(applicant_ranking, a, B) < get_rank(
|
||||||
B_prefers_a = get_rank(reviewer_ranking, B, a) < get_rank(reviewer_ranking, B, b)
|
applicant_ranking, a, A
|
||||||
|
)
|
||||||
|
B_prefers_a = get_rank(reviewer_ranking, B, a) < get_rank(
|
||||||
|
reviewer_ranking, B, b
|
||||||
|
)
|
||||||
if a_prefers_B and B_prefers_a:
|
if a_prefers_B and B_prefers_a:
|
||||||
return True
|
return True
|
||||||
# else
|
# else
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def check_stable(*args, **kwargs):
|
def check_stable(*args, **kwargs):
|
||||||
return not check_unstable(*args, **kwargs)
|
return not check_unstable(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def deferred_acceptance(A, R):
|
def deferred_acceptance(A, R):
|
||||||
"""Find the Gale-Shapley deferred-acceptance stable matching for preferences A, R."""
|
"""Find the Gale-Shapley deferred-acceptance stable matching for preferences A, R."""
|
||||||
# TODO - the core algorithm!
|
# TODO - the core algorithm!
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def assgn_to_match(assgn):
|
def assgn_to_match(assgn):
|
||||||
# TODO
|
# TODO
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def match_to_assgn(match):
|
def match_to_assgn(match):
|
||||||
# TODO
|
# TODO
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
rich.print("Hello from [italic red]carousel[/italic red]!")
|
rich.print("Hello from [italic red]carousel[/italic red]!")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -82,15 +82,48 @@ def test_prefs_tofrom_ranks(P):
|
||||||
|
|
||||||
|
|
||||||
def test_eg3_unstable():
|
def test_eg3_unstable():
|
||||||
applicant_rankings = pl.DataFrame({"": ["A", "B", "C", "D"], "a": [1, 2, 3, 4], "b": [1, 4, 3, 2], "c": [2, 1, 3, 4], "d": [4, 2, 3, 1]})
|
applicant_rankings = pl.DataFrame(
|
||||||
reviewer_rankings = pl.DataFrame({"": ["a", "b", "c", "d"], "A": [3, 4, 2, 1], "B": [3, 1, 4, 2], "C": [2, 3, 4, 1], "D": [3, 2, 1, 4] })
|
{
|
||||||
match = pl.DataFrame({"A" : ["a"], "B": ["b"], "C": ["c"], "D": ["d"]})
|
"": ["A", "B", "C", "D"],
|
||||||
|
"a": [1, 2, 3, 4],
|
||||||
|
"b": [1, 4, 3, 2],
|
||||||
|
"c": [2, 1, 3, 4],
|
||||||
|
"d": [4, 2, 3, 1],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
reviewer_rankings = pl.DataFrame(
|
||||||
|
{
|
||||||
|
"": ["a", "b", "c", "d"],
|
||||||
|
"A": [3, 4, 2, 1],
|
||||||
|
"B": [3, 1, 4, 2],
|
||||||
|
"C": [2, 3, 4, 1],
|
||||||
|
"D": [3, 2, 1, 4],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
match = pl.DataFrame({"A": ["a"], "B": ["b"], "C": ["c"], "D": ["d"]})
|
||||||
|
|
||||||
assert crsl.check_unstable(match, applicant_rankings, reviewer_rankings)
|
assert crsl.check_unstable(match, applicant_rankings, reviewer_rankings)
|
||||||
|
|
||||||
|
|
||||||
def test_eg3_isstable():
|
def test_eg3_isstable():
|
||||||
applicant_rankings = pl.DataFrame({"": ["A", "B", "C", "D"], "a": [1, 2, 3, 4], "b": [1, 4, 3, 2], "c": [2, 1, 3, 4], "d": [4, 2, 3, 1]})
|
applicant_rankings = pl.DataFrame(
|
||||||
reviewer_rankings = pl.DataFrame({"": ["a", "b", "c", "d"], "A": [3, 4, 2, 1], "B": [3, 1, 4, 2], "C": [2, 3, 4, 1], "D": [3, 2, 1, 4] })
|
{
|
||||||
match = pl.DataFrame({"A" : ["c"], "B": ["d"], "C": ["a"], "D": ["b"]})
|
"": ["A", "B", "C", "D"],
|
||||||
|
"a": [1, 2, 3, 4],
|
||||||
|
"b": [1, 4, 3, 2],
|
||||||
|
"c": [2, 1, 3, 4],
|
||||||
|
"d": [4, 2, 3, 1],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
reviewer_rankings = pl.DataFrame(
|
||||||
|
{
|
||||||
|
"": ["a", "b", "c", "d"],
|
||||||
|
"A": [3, 4, 2, 1],
|
||||||
|
"B": [3, 1, 4, 2],
|
||||||
|
"C": [2, 3, 4, 1],
|
||||||
|
"D": [3, 2, 1, 4],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
match = pl.DataFrame({"A": ["c"], "B": ["d"], "C": ["a"], "D": ["b"]})
|
||||||
|
|
||||||
assert crsl.check_stable(match, applicant_rankings, reviewer_rankings)
|
assert crsl.check_stable(match, applicant_rankings, reviewer_rankings)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue