示例#1
0
def test_old_plurality_unweighted_winner():
    assert RulePlurality(["A", "A", "B", "A", "C"]).winner_ == "A"
    assert RulePlurality(["A", "A", "B", "C", "", "", ""]).winner_ == "A"
    assert RulePlurality(["A", "A", "B", "A", "C"],
                         voters=['e1', 'e2', 'e3', 'e4', 'e5']).winner_ == 'A'
    # If we use linear orders as input, it should also work as expected
    assert RulePlurality([["A", "B", "C"], ["A", "B", "C"], ["B", "A", "C"],
                          ["C", "B", "A"], ["A", "B", "C"]]).winner_ == "A"
    assert RulePlurality([["A", "B", "C"], ["A", "B", "C"], ["B", "A", "C"],
                          ["C", "B", "A"], ["A", "B", "C"]],
                         voters=['e1', 'e2', 'e3', 'e4', 'e5']).winner_ == "A"
 def __init__(self,
              *args,
              rule1: Rule = None,
              rule2: Rule = None,
              elimination: Elimination = None,
              **kwargs):
     if rule1 is None:
         rule1 = RulePlurality()
     if rule2 is None:
         rule2 = RulePlurality()
     if elimination is None:
         elimination = EliminationLast(k=-2)
     super().__init__(*args,
                      rules=[rule1, rule2],
                      eliminations=[elimination],
                      **kwargs)
示例#3
0
def test_order_and_trailers():
    plurality = RulePlurality(ballots=['a', 'b', 'c', 'd', 'e'],
                              weights=[2, 3, 1, 3, 1],
                              tie_break=Priority.ASCENDING)
    assert plurality.order_ == [{'b', 'd'}, {'a'}, {'c', 'e'}]
    assert plurality.strict_order_ == ['b', 'd', 'a', 'c', 'e']
    assert plurality.cotrailers_ == {'c', 'e'}
    assert plurality.trailer_ == 'e'
def test():
    irv = RuleIteratedElimination(base_rule=RulePlurality())
    irv([
        'a > b > c',
        'b > a > c',
        'c > a > b'
    ], weights=[2, 3, 4])
    assert irv.order_ == [{'b'}, {'c'}, {'a'}]
    assert irv.winner_ == 'b'

    irv = RuleIteratedElimination(base_rule=RulePlurality(), tie_break=Priority.ASCENDING)
    irv([
        'a > b > c > d',
        'd > b > c > a',
        'b > a > c > d',
        'c > a > b > d'
    ], weights=[1, 1, 3, 4])
    assert irv.order_ == [{'b'}, {'c'}, {'a'}, {'d'}]
    assert irv.winner_ == 'b'
 def __init__(self,
              *args,
              base_rule: Rule = None,
              elimination: Elimination = None,
              **kwargs):
     if base_rule is None:
         base_rule = RulePlurality()
     if elimination is None:
         elimination = EliminationLast(k=1)
     super().__init__(*args,
                      base_rule=base_rule,
                      elimination=elimination,
                      **kwargs)
示例#6
0
def test_old_plurality_unweighted_cowinners():
    """Test the unweighted Plurality rule (co-winners)"""
    assert "A" in RulePlurality(["A", "A", "B", "A", "C", "B", "B"]).cowinners_
    assert "B" in RulePlurality(["A", "A", "B", "A", "C", "B", "B"]).cowinners_
    assert "A" in RulePlurality(["A", "A", "B", "C", "C", "", ""]).cowinners_
    assert "C" in RulePlurality(["A", "A", "B", "C", "C", "", ""]).cowinners_
    assert "A" in RulePlurality(["A", "A", "B", "A", "C"],
                                voters=['e1', 'e2', 'e3', 'e4',
                                        'e5']).cowinners_
    # If we use linear orders as input, it should also work as expected
    assert "A" in RulePlurality([["A", "B", "C"], ["A", "B", "C"],
                                 ["B", "A", "C"], ["B", "C", "A"],
                                 ["A", "B", "C"]]).cowinners_
    assert "A" in RulePlurality(
        [["A", "B", "C"], ["A", "B", "C"], ["B", "A", "C"], ["B", "C", "A"],
         ["A", "B", "C"]],
        voters=['e1', 'e2', 'e3', 'e4', 'e5']).cowinners_
示例#7
0
def test():
    plurality = RulePlurality()

    profile = [["A", "B", "C"], ["A", "B", "C"], ["B", "A", "C"],
               ["C", "B", "A"], ["A", "B", "C"], {
                   'A': 10,
                   'B': 7,
                   'C': 0
               }, 'B > A ~ C > D']
    assert plurality(profile).gross_scores_ == {'A': 4, 'B': 2, 'C': 1, 'D': 0}
    assert plurality(profile, candidates={'A', 'B', 'D'}).gross_scores_ == {
        'A': 4,
        'B': 3,
        'D': 0
    }

    profile = [None, None, 'b', 42]
    assert plurality(profile).gross_scores_ == {'b': 1, 42: 1}

    profile = ['a', 'a > b ~ c', 'b', ['c', 'b'], 42]
    assert plurality(profile).gross_scores_ == {'a': 2, 'b': 1, 'c': 1, 42: 1}

    profile = [
        'a > b > c',
        'c > b > a',
    ]
    assert plurality(profile, candidates={'b', 'c'}).gross_scores_ == {
        'b': 1,
        'c': 1
    }
    assert plurality(profile, candidates={'a', 'b', 'c',
                                          'd'}).gross_scores_ == {
                                              'a': 1,
                                              'b': 0,
                                              'c': 1,
                                              'd': 0
                                          }
    assert plurality(profile, candidates={'b', 'c', 'd'}).gross_scores_ == {
        'b': 1,
        'c': 1,
        'd': 0
    }
示例#8
0
def test_old_plurality_weighted_winner():
    assert RulePlurality(["A", "A", "B", "A", "C"], weights=[1, 1, 10, 1,
                                                             2]).winner_ == "B"
    assert RulePlurality(["A", "A", "B", "A", "C", "", "", ""],
                         weights=[1, 1, 10, 1, 2, 1, 42, 1]).winner_ == "B"
    assert RulePlurality(["A", "A", "B", "A", "C"],
                         weights=[1, 1, 10, 1, 2],
                         voters=['e1', 'e2', 'e3', 'e4', 'e5']).winner_ == "B"
    # If we use linear orders as input, it should also work as expected
    assert RulePlurality([["A", "B", "C"], ["A", "B", "C"], ["B", "A", "C"],
                          ["C", "B", "A"], ["A", "B", "C"]],
                         weights=[1, 1, 10, 1, 2]).winner_ == "B"
    assert RulePlurality([["A", "B", "C"], ["A", "B", "C"], ["B", "A", "C"],
                          ["C", "B", "A"], ["A", "B", "C"]],
                         weights=[1, 1, 10, 1, 2],
                         voters=['e1', 'e2', 'e3', 'e4', 'e5']).winner_ == "B"
    assert RulePlurality([
        BallotOneName("A"),
        BallotOneName("A"),
        BallotOneName("B"),
        BallotOneName("A"),
        BallotOneName("C")
    ],
                         weights=[1, 1, 10, 1, 2]).winner_ == "B"
示例#9
0
def test_random_tie_break():
    for i in range(5):
        rule = RulePlurality(['a', 'b'], tie_break=Priority.RANDOM)
        assert rule.winner_ == rule.strict_order_[0]
示例#10
0
def test_exact_precision():
    plurality = RulePlurality(ballots=['a', 'b', 'c', 'd'],
                              weights=[35, 30, 25, 10])
    assert sum(plurality.scores_.values()) == 1