Example #1
0
def write_profile_to_preflib_toi_file(profile, filename):
    """Writes profile in a preflib toi file

    Parameters:

        profile: Profile
            Profile to be written in preflib file.

        filename: str
            File name of the preflib file to be written.

    Returns:
        None
    """
    with open(filename, "w") as f:
        # write: number of candidates
        f.write(str(profile.num_cand) + "\n")
        # write: names of candidates
        for cand in profile.candidates:
            f.write(f"{cand + 1}, {profile.cand_names[cand]}\n")
        # write: info about number of voters and total weight
        total_weight = sum(voter.weight for voter in profile)
        f.write(f"{total_weight}, {total_weight}, {len(profile)}\n")
        # write: approval sets and weights
        for voter in profile:
            str_approval_set = misc.str_set_of_candidates(
                voter.approved, cand_names=list(range(1, profile.num_cand + 1))
            )
            f.write(f"{voter.weight}, {str_approval_set}\n")
Example #2
0
 def __str__(self):
     if self.has_unit_weights():
         output = f"profile with {len(self._voters)} votes and {self.num_cand} candidates:\n"
         for voter in self._voters:
             output += " " + str_set_of_candidates(voter.approved,
                                                   self.cand_names) + ",\n"
     else:
         output = (f"weighted profile with {len(self._voters)} votes"
                   f" and {self.num_cand} candidates:\n")
         for voter in self._voters:
             output += f" {voter.weight} * "
             output += f"{str_set_of_candidates(voter.approved, self.cand_names)} ,\n"
     return output[:-2]
Example #3
0
    def str_compact(self):
        compact = OrderedDict()
        for voter in self._voters:
            if tuple(voter.approved) in compact:
                compact[tuple(voter.approved)] += voter.weight
            else:
                compact[tuple(voter.approved)] = voter.weight
        if self.has_unit_weights():
            output = ""
        else:
            output = "weighted "
        output += "profile with %d votes and %d candidates:\n" % (len(
            self._voters), self.num_cand)
        for approval_set in compact:
            output += (" " + str(compact[approval_set]) + " x " +
                       str_set_of_candidates(approval_set, self.cand_names) +
                       ",\n")
        output = output[:-2]
        if not self.has_unit_weights():
            output += "\ntotal weight: " + str(self.totalweight())
        output += "\n"

        return output
Example #4
0
    profile.add_voters(inst.approval_sets)
    truepref = profile[0].approved
    print(profile.str_compact())

    parameters = {}
    if inst.rule_id == "leximaxphragmen":
        parameters["lexicographic_tiebreaking"] = True

    committees = abcrules.compute(inst.rule_id,
                                  profile,
                                  inst.committeesize,
                                  resolute=True,
                                  **parameters)
    committee1 = committees[0]
    print("original winning committee:\n " +
          misc.str_set_of_candidates(committee1, cand_names))

    # verify correctness
    assert (
        committee1 in inst.committees_first
    ), f"({inst.rule_id}) {committees[0]} not in {inst.committees_first}"

    print("\nManipulation by voter 0: " +
          misc.str_set_of_candidates(inst.approval_sets[0], cand_names) +
          " --> " +
          misc.str_set_of_candidates(inst.manipulated_vote, cand_names))
    if not all(cand in truepref for cand in inst.manipulated_vote):
        print(" (not a subset!)")

    inst.approval_sets[0] = inst.manipulated_vote
    profile = Profile(num_cand, cand_names=cand_names)
Example #5
0
num_cand = 3
a, b, c = (0, 1, 2)
approval_sets = [{a}] * 2 + [{a, c}] * 3 + [{b, c}] * 3 + [{b}] * 2
cand_names = "abcde"
profile = Profile(num_cand, cand_names=cand_names)
profile.add_voters(approval_sets)

print(misc.header("1st profile:"))
print(profile.str_compact())

print("winning committees for k=1 and k=2:")
for rule_id in ["pav", "cc", "monroe", "minimaxphragmen", "minimaxav"]:
    comm1 = abcrules.compute(rule_id, profile, 1, resolute=True)[0]
    comm2 = abcrules.compute(rule_id, profile, 2, resolute=True)[0]
    print(" " + abcrules.Rule(rule_id).shortname + ": " +
          misc.str_set_of_candidates(comm1, cand_names) + " vs " +
          misc.str_set_of_candidates(comm2, cand_names))
    assert not all(cand in comm1 for cand in comm2)

###

num_cand = 4
a, b, c, d = 0, 1, 2, 3
approval_sets = ([{a}] * 6 + [{a, c}] * 4 + [{a, b, c}] * 2 + [{a}] * 2 +
                 [{a, d}] * 1 + [{b, d}] * 3)
cand_names = "abcde"
profile = Profile(num_cand, cand_names=cand_names)
profile.add_voters(approval_sets)

print()
print(misc.header("2nd profile:"))
Example #6
0
    profile.add_voters(approval_sets)
    truepref = profile[0].approved
    print(profile.str_compact())

    committees = abcrules.compute(rule_id,
                                  profile,
                                  committeesize,
                                  resolute=resolute)
    print("original winning committees:\n" +
          misc.str_sets_of_candidates(committees, cand_names))

    # verify correctness
    assert committees == commsfirst

    print("Manipulation by voter 0: " +
          misc.str_set_of_candidates(approval_sets[0], cand_names) + " --> " +
          misc.str_set_of_candidates(modvote, cand_names))
    if not all(cand in truepref for cand in modvote):
        print(" (not a subset!)")

    approval_sets[0] = modvote
    profile = Profile(num_cand, cand_names=cand_names)
    profile.add_voters(approval_sets)

    committees = abcrules.compute(rule_id,
                                  profile,
                                  committeesize,
                                  resolute=resolute)
    print("\nwinning committees after manipulation:\n" +
          misc.str_sets_of_candidates(committees, cand_names))
Example #7
0
    all_variant = all(
        all(cand in committee for cand in inst.mod_approval_set)
        for committee in inst.committees_first)
    assert some_variant or all_variant
    if all_variant:
        assert not all(
            all(cand in committee for cand in inst.mod_approval_set)
            for committee in inst.committees_after)
    else:
        assert not any(
            all(cand in committee for cand in inst.mod_approval_set)
            for committee in inst.committees_after)

    if inst.with_additional_voter:
        print("additional voter: " +
              misc.str_set_of_candidates(inst.mod_approval_set, cand_names))
        new_approval_set = inst.mod_approval_set
        profile.add_voter(new_approval_set)
    else:
        new_approval_set = list(
            set(inst.mod_approval_set) | set(inst.approval_sets[0]))
        print("change of voter 0: " + misc.str_set_of_candidates(
            list(original_approval_set), cand_names) + " --> " +
              misc.str_set_of_candidates(new_approval_set, cand_names))
        profile[0] = new_approval_set

    committees = abcrules.compute(inst.rule_id, profile, inst.committeesize)
    print("\nwinning committees after the modification:\n" +
          misc.str_sets_of_candidates(committees, cand_names))

    # verify correctness