abcvoting.preferences
Preference profiles and voters.
Important
Preference profiles consist of voters.
Voters in a profile are indexed by 0, …, len(profile)-1
Candidates are indexed by 0, …, profile.num_cand-1
The preferences of voters are specified by approval sets, which are sets of candidates.
- class abcvoting.preferences.Profile(num_cand, cand_names=None)
Approval profiles.
Approval profiles are a list of voters, each of which has preferences expressed as approval sets.
- Parameters:
- num_candint
Number of candidates in this profile.
Remark: Not every candidate has to be approved by a voter.
- cand_nameslist of str or str, optional
List of symbolic names for every candidate.
Defaults to [1, 2, …, str(num_cand)].
For example, for num_cand=5 one could have cand_names=”abcde”.
- Attributes:
- candidateslist of int
List of all candidates, i.e., the list containing 0, …, profile.num_cand-1.
- cand_nameslist of str or str
Symbolic names for every candidate.
Defaults to [“0”, “1”, …, str(num_cand-1)].
- add_voter(voter)
Add a set of approved candidates of one voter to the preference profile.
- Parameters:
- voterVoter or iterable of int
The voter to be added.
- Returns:
- None
Expand for references to
abcvoting.preferences.Profile.add_voter
- add_voters(voters)
Add several voters to the preference profile.
Each voter is specified by a set (or list) of approved candidates or by an object of type Voter.
- Parameters:
- votersiterable of Voter or iterable of iterables of int
The voters to be added.
- Returns:
- None
- approved_candidates()
A set of all candidates approved by at least one voter.
- Returns:
- set of int
- convert_to_unit_weights()
Convert all voters with weights into the appropropriate number of unit-weight copies.
Only works if weights are integers.
- Returns:
- None
Examples
>>> profile = Profile(num_cand=3) >>> profile.add_voter(Voter([0, 1], weight=2)) >>> profile.add_voter(Voter([2], weight=1)) >>> print(profile) weighted profile with 2 voters and 3 candidates: voter 0: 2 * {0, 1}, voter 1: 1 * {2} >>> profile.convert_to_unit_weights() >>> print(profile) profile with 3 voters and 3 candidates: voter 0: {0, 1}, voter 1: {0, 1}, voter 2: {2}
Expand for references to
abcvoting.preferences.Profile.convert_to_unit_weights
- convert_to_weighted()
Merge all voters with the same approval set into a single voter with appropropriate weight.
- Returns:
- None
Examples
>>> profile = Profile(num_cand=3) >>> profile.add_voters([[0, 1], [0, 1], [2]]) >>> print(profile) profile with 3 voters and 3 candidates: voter 0: {0, 1}, voter 1: {0, 1}, voter 2: {2} >>> profile.convert_to_weighted() >>> print(profile) weighted profile with 2 voters and 3 candidates: voter 0: 2 * {0, 1}, voter 1: 1 * {2}
Expand for references to
abcvoting.preferences.Profile.convert_to_weighted
- copy()
Return a copy of the profile.
This is a deep copy, i.e., all Voter objects are copied too.
- Returns:
- Profile
- has_unit_weights()
Verify whether all voters in the profile have a weight of 1.
- Returns:
- bool
- is_party_list()
Check whether this profile is a party-list profile.
In a party-list profile all approval sets are either disjoint or equal.
- Returns:
- bool
- property num_cand
Number of candidates.
- str_compact()
Return a string that compactly summarizes the profile and its voters.
- Returns:
- str
- total_weight()
Return the totol weight of all voters, i.e., the sum of weights.
- Returns:
- int or Fraction
Total weight.
- class abcvoting.preferences.Voter(approved, weight=1, num_cand=None)
A set of approved candidates by one voter.
- Parameters:
- approvedCandidateSet
The set of approved candidates.
- weightint or Fraction, default=1
The weight of the voter.
If weight is an integer, the voter is interpreted as if there are weight many voters with the same approval set.
- num_candint, optional
The maximum number of candidates. Used only for checks.
If this num_cand is provided, it is verified that approved does not contain numbers >= num_cand.
Expand for references to
abcvoting.preferences.Voter
- str_with_names(cand_names=None)
Format a Voter, using the names of candidates (instead of indices) if provided.
- Parameters:
- cand_nameslist of str or str, optional
List of symbolic names for every candidate.
- Returns:
- str