mirror of
https://github.com/tgorordo/carousel.git
synced 2026-06-14 21:32:14 -07:00
rename some vars
This commit is contained in:
parent
85f1b71d3f
commit
9d82d0524a
2 changed files with 27 additions and 25 deletions
|
|
@ -6,35 +6,35 @@ import polars.selectors as pls
|
||||||
import itertools as it
|
import itertools as it
|
||||||
|
|
||||||
|
|
||||||
def rank_to_pref(R):
|
def rank_to_pref(ranking):
|
||||||
"""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 = ranking.select(pls.by_index(0)).to_series().name
|
||||||
P = R.select(
|
preferences = ranking.select(
|
||||||
[pl.col(id_col_name).sort_by(c).alias(c) for c in R.columns if c != id_col_name]
|
[pl.col(id_col_name).sort_by(c).alias(c) for c in ranking.columns if c != id_col_name]
|
||||||
)
|
)
|
||||||
return P
|
return preferences
|
||||||
|
|
||||||
|
|
||||||
def pref_to_rank(P):
|
def pref_to_rank(preferences):
|
||||||
"""Converts a preference to a ranking."""
|
"""Converts a preference to a ranking."""
|
||||||
o = P.select(
|
o = preferences.select(
|
||||||
pl.concat_list(P.columns).explode().unique().sort().alias("")
|
pl.concat_list(preferences.columns).explode().unique().sort().alias("")
|
||||||
) # .with_row_index(offset=1)
|
) # .with_row_index(offset=1)
|
||||||
|
|
||||||
r = pl.concat(
|
ranking = pl.concat(
|
||||||
[
|
[
|
||||||
o.join(
|
o.join(
|
||||||
P.with_row_index(offset=1),
|
preferences.with_row_index(offset=1),
|
||||||
how="full",
|
how="full",
|
||||||
left_on="",
|
left_on="",
|
||||||
right_on=c,
|
right_on=c,
|
||||||
maintain_order="left",
|
maintain_order="left",
|
||||||
).select(pl.col("index").alias(c))
|
).select(pl.col("index").alias(c))
|
||||||
for c in P.columns
|
for c in preferences.columns
|
||||||
],
|
],
|
||||||
how="horizontal",
|
how="horizontal",
|
||||||
)
|
)
|
||||||
return pl.concat([o, r], how="horizontal")
|
return pl.concat([o, ranking], how="horizontal")
|
||||||
|
|
||||||
|
|
||||||
""""
|
""""
|
||||||
|
|
@ -50,8 +50,8 @@ def ranking_matrix(A, B):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def check_valid_pref(P):
|
def check_valid_pref(preferences):
|
||||||
repeats = P.select(
|
repeats = preferences.select(
|
||||||
(~pl.all_horizontal((pl.all().is_unique() | pl.all().is_null()).all())).alias(
|
(~pl.all_horizontal((pl.all().is_unique() | pl.all().is_null()).all())).alias(
|
||||||
"repeats"
|
"repeats"
|
||||||
)
|
)
|
||||||
|
|
@ -59,8 +59,8 @@ def check_valid_pref(P):
|
||||||
return not repeats
|
return not repeats
|
||||||
|
|
||||||
|
|
||||||
def check_valid_rank(R):
|
def check_valid_rank(ranking):
|
||||||
ties = R.select(
|
ties = ranking.select(
|
||||||
(~pl.all_horizontal((pl.all().is_unique() | pl.all().is_null()).all())).alias(
|
(~pl.all_horizontal((pl.all().is_unique() | pl.all().is_null()).all())).alias(
|
||||||
"ties"
|
"ties"
|
||||||
)
|
)
|
||||||
|
|
@ -78,8 +78,8 @@ def check_valid_assgn(assgn, applicants, reviewers):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_rank(ranking, ranker, rankee):
|
def get_rank(ranking, ranker, ranked):
|
||||||
idx = ranking.select(pl.arg_where(pl.col("") == rankee)).item()
|
idx = ranking.select(pl.arg_where(pl.col("") == ranked)).item()
|
||||||
return ranking[ranker][idx]
|
return ranking[ranker][idx]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -109,8 +109,10 @@ 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(applicant_ranking, reviewer_ranking):
|
||||||
"""Find the Gale-Shapley deferred-acceptance stable matching for preferences A, R."""
|
"""Find the Gale-Shapley deferred-acceptance stable matching for preferences A, R."""
|
||||||
|
applicants = applicant_ranking.columns[1:]
|
||||||
|
reviewers = reviewer_ranking.columns[1:]
|
||||||
# TODO - the core algorithm!
|
# TODO - the core algorithm!
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,7 @@ def test_prefs_tofrom_ranks(P):
|
||||||
|
|
||||||
|
|
||||||
def test_eg2_unstable():
|
def test_eg2_unstable():
|
||||||
applicant_rankings = pl.DataFrame(
|
ar = pl.DataFrame(
|
||||||
{
|
{
|
||||||
"": ["A", "B", "C", "D"],
|
"": ["A", "B", "C", "D"],
|
||||||
"a": [1, 2, 3, 4],
|
"a": [1, 2, 3, 4],
|
||||||
|
|
@ -91,7 +91,7 @@ def test_eg2_unstable():
|
||||||
"d": [4, 2, 3, 1],
|
"d": [4, 2, 3, 1],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
reviewer_rankings = pl.DataFrame(
|
rr = pl.DataFrame(
|
||||||
{
|
{
|
||||||
"": ["a", "b", "c", "d"],
|
"": ["a", "b", "c", "d"],
|
||||||
"A": [3, 4, 2, 1],
|
"A": [3, 4, 2, 1],
|
||||||
|
|
@ -102,11 +102,11 @@ def test_eg2_unstable():
|
||||||
)
|
)
|
||||||
match = pl.DataFrame({"A": ["a"], "B": ["b"], "C": ["c"], "D": ["d"]})
|
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, ar, rr)
|
||||||
|
|
||||||
|
|
||||||
def test_eg2_isstable():
|
def test_eg2_isstable():
|
||||||
applicant_rankings = pl.DataFrame(
|
ar = pl.DataFrame(
|
||||||
{
|
{
|
||||||
"": ["A", "B", "C", "D"],
|
"": ["A", "B", "C", "D"],
|
||||||
"a": [1, 2, 3, 4],
|
"a": [1, 2, 3, 4],
|
||||||
|
|
@ -115,7 +115,7 @@ def test_eg2_isstable():
|
||||||
"d": [4, 2, 3, 1],
|
"d": [4, 2, 3, 1],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
reviewer_rankings = pl.DataFrame(
|
rr = pl.DataFrame(
|
||||||
{
|
{
|
||||||
"": ["a", "b", "c", "d"],
|
"": ["a", "b", "c", "d"],
|
||||||
"A": [3, 4, 2, 1],
|
"A": [3, 4, 2, 1],
|
||||||
|
|
@ -126,4 +126,4 @@ def test_eg2_isstable():
|
||||||
)
|
)
|
||||||
match = pl.DataFrame({"A": ["c"], "B": ["d"], "C": ["a"], "D": ["b"]})
|
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, ar, rr)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue