KemenyYoungVoting crashes

Bug #1738569 reported by Mate
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
ballotbox
New
Undecided
Unassigned

Bug Description

KemenyYoungVoting only works if for every pair of candidates A and B there is at least one voter who prefers A to B.
This is the case for the example under
https://pythonhosted.org/ballotbox/docs/methods/singlewinner/preferential.html

You can trigger the problem by having a single vote only:

>>> from ballotbox.singlewinner.preferential import KemenyYoungVoting
>>> bb = BallotBox(method=KemenyYoungVoting)
>>> preference = {"Memphis": 1, "Nashville": 2, "Chattanooga": 3, "Knoxville": 4}
>>> bb.add_votes(preference, 1)
>>> bb.get_winner()

The fix is to change this:

    def get_ranks(self):
        ranks = []
        for possibility in itertools.permutations(self.preference_options):
            rank = 0
            for index, option1 in enumerate(possibility[:-1]):
                for option2 in possibility[index + 1:]:
                    key = "%s > %s" % (option1, option2)
                    rank += self.lookup[key] ############### CHANGE THIS LINE
            ranks.append((rank, possibility))
        return sorted(ranks, reverse=True)

to this:

    def get_ranks(self):
        ranks = []
        for possibility in itertools.permutations(self.preference_options):
            rank = 0
            for index, option1 in enumerate(possibility[:-1]):
                for option2 in possibility[index + 1:]:
                    key = "%s > %s" % (option1, option2)
                    rank += self.lookup.get(key, 0) ############### TO THIS
            ranks.append((rank, possibility))
        return sorted(ranks, reverse=True)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.