-
Notifications
You must be signed in to change notification settings - Fork 20
3.3.0 bg prefp opt #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
3.3.0 bg prefp opt #297
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3296e6c
Reorganize slate inner functions to optimize
peterrrock2 14bb0aa
Redo __add__ for preference profiles to use df
peterrrock2 0c274e2
Optmize cumulative (because I needed to check adding large score prof…
peterrrock2 418e76b
Improve memory estimates for slate and name bt
peterrrock2 a5c8341
Fix lint
peterrrock2 f35cbe4
Chris comments
peterrrock2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
87 changes: 87 additions & 0 deletions
87
src/votekit/ballot_generator/bloc_slate_generator/slate_utils.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import numpy as np | ||
| from typing import Sequence | ||
|
|
||
| from votekit.ballot_generator.bloc_slate_generator.model import BlocSlateConfig | ||
| from votekit.pref_interval import PreferenceInterval | ||
|
|
||
|
|
||
| def _make_cand_ordering_by_slate( | ||
| config: BlocSlateConfig, pref_intervals_by_slate_dict: dict[str, PreferenceInterval] | ||
| ) -> dict[str, list[str]]: | ||
| """ | ||
| Create a candidate ordering within each slate based on the preference intervals. | ||
|
|
||
| The candidate oridering is determined by sampling without replacement according to | ||
| the preference intervals using the Plackett-Luce model (i.e. throwing a dart and removing | ||
| that part of the interval). | ||
|
|
||
| Args: | ||
| config (BlocSlateConfig): Configuration object containing all necessary parameters for | ||
| working with a bloc-slate ballot generator. | ||
| pref_intervals_by_slate_dict (dict[str, PreferenceInterval]): A dictionary mapping | ||
| slate names to their corresponding PreferenceInterval objects. | ||
|
|
||
| Returns: | ||
| dict[str, list[str]]: A dictionary mapping slate names to a list of candidate names | ||
| ordered according to the sampled preference intervals. | ||
| """ | ||
| cand_ordering_by_slate = {} | ||
|
|
||
| for slate in config.slates: | ||
| preference_interval = pref_intervals_by_slate_dict[slate].interval | ||
| cands = pref_intervals_by_slate_dict[slate].non_zero_cands | ||
|
|
||
| if len(cands) == 0: | ||
| continue | ||
|
|
||
| distribution = [preference_interval[c] for c in cands] | ||
|
|
||
| # sample by Plackett-Luce within the bloc | ||
| cand_ordering = np.random.choice( | ||
| a=list(cands), size=len(cands), p=distribution, replace=False | ||
| ) | ||
|
|
||
| cand_ordering_by_slate[slate] = list(cand_ordering) | ||
| return cand_ordering_by_slate | ||
|
|
||
|
|
||
| def _convert_ballot_type_to_ranking( | ||
| ballot_type: Sequence[str], | ||
| cand_ordering_by_slate: dict[str, list[str]], | ||
| ) -> list[frozenset[str]]: | ||
| """ | ||
| Given a ballot type and a candidate ordering by slate, convert the ballot type to a ranking. | ||
|
|
||
| Example: | ||
|
|
||
| Given a ballot type "AABBA" and candidate ordering by slate | ||
| {"A": ["a3", "a1", "a2"], "B": ["b2", "b1"]}, the function will return | ||
| [frozenset({"a3"}), frozenset({"a1"}), frozenset({"b2"}), frozenset({"b1"}), frozenset({"a2"})]. | ||
|
|
||
| Args: | ||
| ballot_type (Sequence[str]): A sequence of slate names representing the ballot type. | ||
| cand_ordering_by_slate (dict[str, list[str]]): A dictionary mapping slate names to a list | ||
| of candidate names ordered according to the sampled preference intervals. | ||
|
|
||
| Returns: | ||
| list[frozenset[str]]: A list of frozensets, where each frozenset contains a single | ||
| candidate name, representing the ranking derived from the ballot type and candidate | ||
| ordering | ||
| """ | ||
| positions = {s: 0 for s in cand_ordering_by_slate} | ||
| ranking: list[frozenset[str]] = [frozenset("~")] * len(ballot_type) | ||
|
|
||
| fset_cache: dict[str, frozenset[str]] = {} | ||
|
|
||
| for i, slate in enumerate(ballot_type): | ||
| pos = positions[slate] | ||
| cand = cand_ordering_by_slate[slate][pos] | ||
| positions[slate] = pos + 1 | ||
|
|
||
| fset = fset_cache.get(cand) | ||
| if fset is None: | ||
| fset = frozenset((cand,)) | ||
| fset_cache[cand] = fset | ||
| ranking[i] = fset | ||
|
|
||
| return ranking | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.