def test_party_list(num_cand, add_pref): profile = Profile(num_cand) profile.add_preferences(DichotomousPreferences([1, 3, 5], 3)) profile.add_preferences([0, 4, 6]) profile.add_preferences([0, 4, 6]) profile.add_preferences([2, 7]) profile.add_preferences(DichotomousPreferences([1, 3, 5], 3)) assert profile.party_list() profile.add_preferences(add_pref) assert not profile.party_list()
def test_unitweights(num_cand): profile = Profile(num_cand) profile.add_preferences([]) profile.add_preferences(DichotomousPreferences([0, 4, 5])) profile.add_preferences([0, 4, 5]) p1 = DichotomousPreferences([0, 4, 5]) p2 = DichotomousPreferences([1, 2]) profile.add_preferences([p1, p2]) assert profile.has_unit_weights() profile.add_preferences(DichotomousPreferences([0, 4, 5], 2.4)) assert not profile.has_unit_weights() assert profile.totalweight() == 6.4
def test_iterate(num_cand): profile = Profile(num_cand) profile.add_preferences(DichotomousPreferences([1, 3, 5], 3)) profile.add_preferences([0, 4, 5]) assert len(profile) == 2 for p in profile: assert isinstance(p, DichotomousPreferences)
def test_abcrules_weightsconsidered(rule_instance, verbose): rule_id, algorithm, resolute = rule_instance profile = Profile(3) profile.add_preferences(DichotomousPreferences([0])) profile.add_preferences(DichotomousPreferences([0])) profile.add_preferences(DichotomousPreferences([1], 5)) profile.add_preferences(DichotomousPreferences([0])) committeesize = 1 if (("monroe" in rule_id or rule_id in ["lexmav", "rule-x", "phrag-enestr"])): with pytest.raises(ValueError): abcrules.compute(rule_id, profile, committeesize, algorithm=algorithm, verbose=verbose) elif rule_id == "mav": # Minimax AV ignores weights by definition result = abcrules.compute(rule_id, profile, committeesize, algorithm=algorithm, resolute=resolute, verbose=verbose) if resolute: assert result == [[0]] else: assert result == [[0], [1], [2]] else: result = abcrules.compute(rule_id, profile, committeesize, algorithm=algorithm, resolute=resolute, verbose=verbose) assert len(result) == 1 assert result[0] == [1]
def test_invalidprofiles(num_cand): with pytest.raises(ValueError): Profile(0) with pytest.raises(ValueError): Profile(-8) with pytest.raises(ValueError): Profile(4, ["a", "b", "c"]) Profile(4, ["a", 3, "b", "c"]) profile = Profile(num_cand, "abcdefgh") pref = DichotomousPreferences([num_cand]) with pytest.raises(ValueError): profile.add_preferences(pref) with pytest.raises(TypeError): profile.add_preferences([0, 4, 5, "1"]) with pytest.raises(TypeError): profile.add_preferences(["1", 0, 4, 5]) with pytest.raises(TypeError): profile.add_preferences({1: [1, 2]})
def test_invalidpreferences(): with pytest.raises(ValueError): DichotomousPreferences([-1])
assert committees_pav == [[a, c]] assert committees_seqpav == [[c, d]] assert committees_revseqpav == [[c, d]] print("\n") print(misc.header( "Example from Janson's survey (Example 13.3) / Thiele:", "*")) # Approval profile num_cand = 4 a, b, c, d = list(range(4)) # a = 0, b = 1, c = 2, ... names = "abcd" profile = Profile(num_cand, names=names) profile.add_preferences(DichotomousPreferences([a, c, d], 960)) profile.add_preferences(DichotomousPreferences([b, c, d], 3000)) profile.add_preferences(DichotomousPreferences([b, c], 520)) profile.add_preferences(DichotomousPreferences([a, b], 1620)) profile.add_preferences(DichotomousPreferences([a, d], 1081)) profile.add_preferences(DichotomousPreferences([a, c], 1240)) profile.add_preferences(DichotomousPreferences([b, d], 360)) profile.add_preferences(DichotomousPreferences([d], 360)) profile.add_preferences(DichotomousPreferences([c], 120)) profile.add_preferences(DichotomousPreferences([b], 60)) print(misc.header("Input:")) print(profile.str_compact()) committees_pav = abcrules.compute_pav(profile, 2, verbose=2)