Exemplo n.º 1
0
def test_readfile_and_weights(filename, total_weight, num_voters):
    currdir = os.path.dirname(os.path.abspath(__file__))
    profile = fileio.read_preflib_file(currdir + "/data/" + filename)
    assert len(profile) == total_weight
    for voter in profile:
        assert voter.weight == 1
    profile = fileio.read_preflib_file(currdir + "/data/" + filename,
                                       use_weights=True)
    assert len(profile) == num_voters
    assert sum(voter.weight for voter in profile) == total_weight
Exemplo n.º 2
0
def test_readfile_setsize_with_ties(filename, setsize, expected):
    currdir = os.path.dirname(os.path.abspath(__file__))
    profile = fileio.read_preflib_file(currdir + "/data/" + filename,
                                       setsize=setsize)
    assert [len(voter.approved) for voter in profile] == expected
    for voter in profile:
        assert voter.weight == 1
Exemplo n.º 3
0
def test_readfile_setsize(filename):
    currdir = os.path.dirname(os.path.abspath(__file__))
    profile = \
        fileio.read_preflib_file(currdir + "/data/" + filename,
                                 setsize=2)
    for pref in profile:
        assert len(pref) == 2
Exemplo n.º 4
0
def test_read_and_write_preflib_file():
    currdir = os.path.dirname(os.path.abspath(__file__))
    profile1 = Profile(6)
    profile1.add_voters([[3], [4, 1, 5], [0, 2], [], [0, 1, 2, 3, 4, 5], [5],
                         [1], [1]])
    fileio.write_profile_to_preflib_toi_file(currdir + "/data/test5.toi",
                                             profile1)
    for use_weights in [True, False]:
        profile2 = fileio.read_preflib_file(currdir + "/data/test5.toi",
                                            use_weights=use_weights)
        assert len(profile1) == len(profile2)
        for i, voter in enumerate(profile1):
            assert voter.weight == profile2[i].weight
            assert voter.approved == set(profile2[i].approved)
Exemplo n.º 5
0
committeesize = 2

# directory of preflib.py
currdir = os.path.dirname(os.path.abspath(__file__))

profiles = fileio.load_preflib_files_from_dir(currdir + "/toi-files/",
                                              appr_percent=0.7)

for profile in profiles:
    print("Computing a committee of size", committeesize, end=' ')
    print("with the Proportional Approval Voting (PAV) rule")
    print("given a", profile)
    print("Output:")
    committees = abcrules.compute_pav(profile, committeesize)
    print(str_candsets(committees))
    print("****************************************")


# Example to read a single file (parameter setsize)
profile = fileio.read_preflib_file(
    currdir + "/toi-files/ex_2010.toi", setsize=1)

print("Computing a committee of size", committeesize, end=' ')
print("with Phragmen's sequential rule")
print("given a", profile)
print("Output:")
committees = abcrules.compute_seqphragmen(profile, committeesize)
print(str_candsets(committees))
print("****************************************")
Exemplo n.º 6
0
def test_readfile_corrupt(filename):
    currdir = os.path.dirname(os.path.abspath(__file__))
    with pytest.raises(fileio.MalformattedFileException):
        profile = fileio.read_preflib_file(currdir + "/data-fail/" + filename,
                                           setsize=2)
        print(str(profile))
Exemplo n.º 7
0
def test_readfile_setsize(filename):
    currdir = os.path.dirname(os.path.abspath(__file__))
    profile = fileio.read_preflib_file(currdir + "/data/" + filename,
                                       setsize=2)
    for voter in profile:
        assert len(voter.approved) == 2
Exemplo n.º 8
0
def test_readfile(filename):
    currdir = os.path.dirname(os.path.abspath(__file__))
    profile = fileio.read_preflib_file(currdir + "/data/" + filename,
                                       relative_setsize=0.5)
    assert len(profile) == 5
    assert profile.has_unit_weights()
Exemplo n.º 9
0
def test_readfile_setsize_with_ties(filename, setsize, expected):
    currdir = os.path.dirname(os.path.abspath(__file__))
    profile = \
        fileio.read_preflib_file(currdir + "/data/" + filename,
                                 setsize=setsize)
    assert [len(pref) for pref in profile] == expected