Beispiel #1
0
def test_abc_yaml_instances_use_only_main_rule_ids(filename):
    profile, committeesize, compute_instances, _ = fileio.read_abcvoting_yaml_file(filename)
    for compute_instance in compute_instances:
        # If this assertation fails, _load_abc_yaml_compute_instances() has to be adapted
        # so that it actually loads the .abc.yaml files and extracts the rule_id's.
        # However, this would be rather slow.
        assert compute_instance["rule_id"] in abcrules.MAIN_RULE_IDS
def test_output_JR_with_rules(abc_yaml_instance):
    # read the instance from the file
    profile, _, compute_instances, _ = fileio.read_abcvoting_yaml_file(
        abc_yaml_instance)

    for rule_id in ["monroe", "seqcc", "greedy-monroe", "cc"]:
        # get output computed by rule `rule_id` for this instance
        for computed_output in compute_instances:
            if computed_output["rule_id"] == rule_id:
                input_committee = computed_output["result"][0]
                break
        # winning committee should satisfy JR
        assert properties.check_JR(profile, input_committee)
def test_output_PJR_seqPhragmen(abc_yaml_instance):
    # read the instance from the file
    profile, _, compute_instances, _ = fileio.read_abcvoting_yaml_file(
        abc_yaml_instance)

    # get output computed by rule seqPhragmen for this instance
    for computed_output in compute_instances:
        if computed_output["rule_id"] == "seqphragmen":
            input_committee = computed_output["result"][0]
            break

    # winning committee for this profile computed by seqPhragmen should satisfy PJR
    assert properties.check_PJR(profile, input_committee, algorithm="gurobi")
Beispiel #4
0
def test_read_special_abc_yaml_file2():
    currdir = os.path.dirname(os.path.abspath(__file__))
    filename = currdir + "/data/test8.abc.yaml"

    profile1 = Profile(6)
    profile1.add_voters([{3}, {1, 4, 5}, {0, 2}, {}, {0, 1, 2, 3, 4, 5},
                         {1, 3}, {1}, {1}])

    profile2, committeesize, compute_instances, data = fileio.read_abcvoting_yaml_file(
        filename)
    assert str(profile1) == str(profile2)
    assert committeesize == 2
    assert len(compute_instances) == 1
    assert abcrules.compute(**compute_instances[0]) == [{1, 3}]
Beispiel #5
0
def test_read_special_abc_yaml_file1():
    currdir = os.path.dirname(os.path.abspath(__file__))
    filename = currdir + "/data/test7.abc.yaml"

    profile1 = Profile(6)
    profile1.add_voters([[3], [4, 1, 5], [0, 2], [], [0, 1, 2, 3, 4, 5], [5],
                         [1], [1]])
    fileio.write_abcvoting_instance_to_yaml_file(filename,
                                                 profile1,
                                                 description="just a profile")

    profile2, committeesize, compute_instances2, data2 = fileio.read_abcvoting_yaml_file(
        filename)
    assert str(profile1) == str(profile2)
    assert committeesize is None
    assert compute_instances2 == []
Beispiel #6
0
def test_read_and_write_abc_yaml_file():
    currdir = os.path.dirname(os.path.abspath(__file__))
    filename = currdir + "/data/test6.abc.yaml"
    profile1 = Profile(6)
    profile1.add_voters([[3], [4, 1, 5], [0, 2], [], [0, 1, 2, 3, 4, 5], [5],
                         [1], [1]])
    committeesize1 = 3
    compute_instances1 = [
        {
            "rule_id": "pav",
            "resolute": True
        },
        {
            "rule_id": "seqphragmen",
            "algorithm": "float-fractions"
        },
        {
            "rule_id": "equal-shares",
            "algorithm": "standard-fractions",
            "skip_phragmen_phase": True,
        },
    ]

    fileio.write_abcvoting_instance_to_yaml_file(
        filename,
        profile1,
        committeesize=committeesize1,
        compute_instances=compute_instances1)

    profile2, committeesize2, compute_instances2, _ = fileio.read_abcvoting_yaml_file(
        filename)
    assert committeesize1 == committeesize2
    assert len(profile1) == len(profile2)
    for i, voter in enumerate(profile1):
        assert voter.weight == profile2[i].weight
        assert voter.approved == set(profile2[i].approved)
    for i in range(len(compute_instances1)):
        assert abcrules.compute(profile=profile1,
                                committeesize=committeesize1,
                                **compute_instances1[i]) == abcrules.compute(
                                    **compute_instances2[i])
    for compute_instance in compute_instances2:
        compute_instance.pop("profile")
        compute_instance.pop("committeesize")
    assert compute_instances1 == compute_instances2
def test_matching_output_different_approaches(abc_yaml_instance):
    # read the instance from the file
    profile, _, compute_instances, _ = fileio.read_abcvoting_yaml_file(
        abc_yaml_instance)

    # get one sample committee as input for the functions
    input_committee = compute_instances[0]["result"][0]

    assert properties.check_pareto_optimality(
        profile, input_committee,
        algorithm="brute-force") == properties.check_pareto_optimality(
            profile, input_committee, algorithm="gurobi")
    assert properties.check_EJR(
        profile, input_committee,
        algorithm="brute-force") == properties.check_EJR(profile,
                                                         input_committee,
                                                         algorithm="gurobi")
    assert properties.check_PJR(
        profile, input_committee,
        algorithm="brute-force") == properties.check_PJR(profile,
                                                         input_committee,
                                                         algorithm="gurobi")
Beispiel #8
0
def test_read_corrupt_abc_yaml_file(filename):
    currdir = os.path.dirname(os.path.abspath(__file__))
    with pytest.raises(fileio.MalformattedFileException):
        profile = fileio.read_abcvoting_yaml_file(currdir + "/data-fail/" +
                                                  filename)
        print(str(profile))
Beispiel #9
0
def test_selection_of_abc_yaml_instances(filename, rule_id, algorithm):
    profile, committeesize, compute_instances, _ = fileio.read_abcvoting_yaml_file(filename)
    for compute_instance in compute_instances:
        if compute_instance["rule_id"] == rule_id:
            abcrules.compute(**compute_instance, algorithm=algorithm)