interregnum.methods.preferential.condorcet package¶
Condorcet methods.
- class interregnum.methods.preferential.condorcet.CondorcetAllocator(ranking_f, allow_ties=True, fill_truncated=False)¶
Bases:
MultiWinnerAdapter[AnyName,MultiWinnerResultData,list[Preference[AnyName]],Iterable[Preference[AnyName] |PrefDict[AnyName] |tuple[int|Fraction,Sequence[AnyName|Tuple[AnyName, …] |list[AnyName]] |tuple[AnyName|Tuple[AnyName, …] |list[AnyName], …]]]]An allocator for Condorcet methods.
Create a Condorcet method.
- Parameters:
- calc(preferences, seats=1, candidate_list=None, random_seed=None, filter_f=None)¶
Allocate seats to candidates.
- Parameters:
preferences (Iterable[Preference[AnyName] | PrefDict[AnyName] | tuple[int | Fraction, Sequence[AnyName | Tuple[AnyName, ...] | list[AnyName]] | tuple[AnyName | Tuple[AnyName, ...] | list[AnyName], ...]]]) – list of grouped preference ballots
seats (int) – seats to allocate
candidate_list (Iterable[AnyName] | None) – full list of allowed candidates
random_seed (int | Random | None) – used to resolve candidates ties
filter_f (CandidateFilter[AnyName, Event] | None) – filter candidates that can win a seats
- Return type:
- class interregnum.methods.preferential.condorcet.CondorcetCopelandAllocator(allow_ties=True, fill_truncated=False)¶
Bases:
CondorcetAllocator[AnyName]Copeland’s pairwise aggregation method.
See https://en.wikipedia.org/wiki/Copeland%27s_method
allocatorscollection keys:copeland
condorcet_copeland
Create a Copeland allocator.
- Parameters:
allow_ties (bool) – allow more than one candidate at the same preference position
fill_truncated (bool) – fill a truncated ballot with a tie of the remaining candidates
- class interregnum.methods.preferential.condorcet.CondorcetMinimaxAllocator(margin=False, allow_ties=True, fill_truncated=False)¶
Bases:
CondorcetAllocator[AnyName]Minimax / Simpson-Kramer method.
See https://en.wikipedia.org/wiki/Minimax_Condorcet
allocatorscollection keys:minimax
condorcet_minimax
Create a Minimax allocator.
- Parameters:
margin (bool) – If margin is True, use vote margins instead of difference of votes as the inner score.
allow_ties (bool) – allow more than one candidate at the same preference position
fill_truncated (bool) – fill a truncated ballot with a tie of the remaining candidates
- class interregnum.methods.preferential.condorcet.CondorcetRankedPairsAllocator(allow_ties=True, fill_truncated=False)¶
Bases:
NonDeterministicAllocator[AnyName,RPResultData]Ranked pairs Condorcet allocator.
allocatorscollection keys:ranked_pairs
condorcet_ranked_pairs
Create a ranked pairs allocator.
- Parameters:
allow_ties (bool) – allow more than one candidate at the same preference position
fill_truncated (bool) – fill a truncated ballot with a tie of the remaining candidates
- calc(preferences, seats=1, random_seed=None, candidate_list=None)¶
Allocate seats to candidates.
- Parameters:
preferences (Iterable[Preference[AnyName] | PrefDict[AnyName] | tuple[int | Fraction, Sequence[AnyName | Tuple[AnyName, ...] | list[AnyName]] | tuple[AnyName | Tuple[AnyName, ...] | list[AnyName], ...]]]) – list of grouped preference ballots
seats (int) – seats to allocate
random_seed (int | Random | None) – used to break ties
candidate_list (Iterable[AnyName] | None) – full list of allowed candidates
- Return type:
Submodules¶
interregnum.methods.preferential.condorcet.pairwise module¶
Table for Condorcet methods’ auxiliary operations.
References¶
- class interregnum.methods.preferential.condorcet.pairwise.PairwiseTable(nodes)¶
Bases:
WeightedGraph[AnyName,int|Fraction]Bi-dimensional sparse table for Condorcet.
Create a table with this nodes.
- Parameters:
nodes (ipt.INames[AnyName])
- classmethod from_preferences(preferences, *, fill_truncated=False, all_nodes=None)¶
Create a
PairwiseTablefrom a list of preferential votes.Each preferential vote is preceded by the number of occurrences.
>>> votes = [ (42, ('Memphis', 'Nashville', 'Chattanooga', 'Knoxville')), (26, ('Nashville', 'Chattanooga', 'Knoxville', 'Memphis')), (15, ('Chattanooga', 'Knoxville', 'Nashville', 'Memphis')), (17, ('Knoxville', 'Chattanooga', 'Nashville', 'Memphis')) ] >>> t = PairwiseTable.from_preferences(votes)
- Parameters:
- Return type:
- compare(cand1, cand2)¶
Compare the elements at cand1-cand2 and cand2-cand1.
If the value of (x,y) is greater than the value of (y, x), return (x, y). If both values are equal, return None. Otherwise, return (y, x).
>>> t = PairwiseTable() >>> t['Alice', 'Bob'] = 5 >>> t['Bob', 'Alice'] = 7 >>> t['Bob', 'Charles'] = 3 >>> # ('Bob', 'Alice') >>> t.compare('Alice', 'Bob') >>> # ('Bob', 'Charles') >>> t.compare('Bob', 'Charles') >>> # None >>> t.compare('Alex', 'Zoe')
- margin(cand1, cand2)¶
Return margin value between cand1 and cand2.
Return value at (cand1, cand2) minus value at (cand2, cand1)
>>> t = PairwiseTable() >>> t['Alice', 'Bob'] = 5 >>> t['Bob', 'Alice'] = 7 >>> # -2 >>> t.margin('Alice', 'Bob')
- margins()¶
Return a weighted graph from margin values.
The weight of an edge (x, y) will be margin(x, y).
- Return type:
WeightedGraph[AnyName, int | Fraction | float]
- win(cand1, cand2)¶
Winner votes of (cand1, cand2) against (cand2, cand1).
If cand1 does not win cand2, return 0.
>>> t = PairwiseTable() >>> t['Alice', 'Bob'] = 5 >>> t['Bob', 'Alice'] = 7 >>> # 0 >>> t.win('Alice', 'Bob') >>> # 7 >>> t.win('Bob', 'Alice')
- wins()¶
Return a weighted graph from win values.
The weight of an edege (x, y) will be win(x, y)
- Return type:
WeightedGraph[AnyName, int | Fraction | float]
interregnum.methods.preferential.condorcet.ranked_pairs module¶
Ranked pairs Condorcet allocator.
- class interregnum.methods.preferential.condorcet.ranked_pairs.CondorcetRankedPairsAllocator(allow_ties=True, fill_truncated=False)¶
Bases:
NonDeterministicAllocator[AnyName,RPResultData]Ranked pairs Condorcet allocator.
allocatorscollection keys:ranked_pairs
condorcet_ranked_pairs
Create a ranked pairs allocator.
- Parameters:
allow_ties (bool) – allow more than one candidate at the same preference position
fill_truncated (bool) – fill a truncated ballot with a tie of the remaining candidates
- calc(preferences, seats=1, random_seed=None, candidate_list=None)¶
Allocate seats to candidates.
- Parameters:
preferences (Iterable[Preference[AnyName] | PrefDict[AnyName] | tuple[int | Fraction, Sequence[AnyName | Tuple[AnyName, ...] | list[AnyName]] | tuple[AnyName | Tuple[AnyName, ...] | list[AnyName], ...]]]) – list of grouped preference ballots
seats (int) – seats to allocate
random_seed (int | Random | None) – used to break ties
candidate_list (Iterable[AnyName] | None) – full list of allowed candidates
- Return type:
- class interregnum.methods.preferential.condorcet.ranked_pairs.RPResultData(log=<factory>, threshold=0, remaining_seats=0, rounds=0)¶
Bases:
MultiWinnerResultDataResult data for ranked pairs.
- Parameters:
log (list[Event])
threshold (int | Fraction)
remaining_seats (int)
rounds (int)
- rounds: int = 0¶
- class interregnum.methods.preferential.condorcet.ranked_pairs.RPState(data=<factory>, *, random_seed)¶
Bases:
NonDeterministicStateRanked-pairs allocator state.
- Parameters:
data (RPResultData)
random_seed (dataclasses.InitVar[Union[int, random.Random, NoneType]])
- break_tie(candidates, limit)¶
Break a candidates tie.
- Parameters:
candidates (Iterable[AnotherName]) – list of tied candidates
limit (int) – number of desired winners
- Return type:
tuple[str, list[AnotherName]]
- find_winners(score)¶
Find winners given a ranked pairs score.
- Parameters:
score (RankedPairs[AnyName])
- Return type:
- iter_pairs(score)¶
Iterate ranked pairs.
- Parameters:
score (RankedPairs[AnyName])
- Return type:
- data: RPResultData¶
interregnum.methods.preferential.condorcet.rankings module¶
Ranking list object for Condorcet methods.
- class interregnum.methods.preferential.condorcet.rankings.Copeland(candidates, candidate_list=None)¶
-
Copeland’s pairwise aggregation method.
See https://en.wikipedia.org/wiki/Copeland%27s_method
Create a Copeland ranking.
- Parameters:
- empty()¶
Return True if the score is empty.
- Return type:
bool
- class interregnum.methods.preferential.condorcet.rankings.Minimax(candidates, margin=False, candidate_list=None)¶
-
Minimax / Simpson-Kramer method.
See https://en.wikipedia.org/wiki/Minimax_Condorcet
Create a Minimax ranking.
- Parameters:
- empty()¶
Return True if there is no contender.
- Return type:
bool
- class interregnum.methods.preferential.condorcet.rankings.RankedPairs(candidates, candidate_list=None)¶
Bases:
Ranking[tuple[AnyName,AnyName]]Ranked pairs / Tideman method.
See https://en.wikipedia.org/wiki/Ranked_pairs
Create a ranked pairs ranking.
- Parameters:
- empty()¶
Return True if there is no contender.
- Return type:
bool
- remove_candidate(cand)¶
Remove candidate from calculations.
- remove_name(name)¶
Remove candidate by name from calculations.