Пример #1
0
    def run(self, input, validWinners=None):
        votes = input

        i = 0
        candidates = []
        for tie in input[0].list:
            for option in tie:
                candidates.append(Candidate(i))
                i += 1

        voteCounts = [1]
        while (len(voteCounts) < len(candidates)):
            voteCounts.append(0)

        for vote in votes:
            curVote = 0
            for tie in vote.getList():
                toSplit = 0
                numOfTie = 0
                for vote in tie:
                    if validWinners == None or vote in validWinners:
                        toSplit += voteCounts[curVote]
                        curVote += 1
                        numOfTie += 1

                if numOfTie > 0:
                    amountGiven = Fraction(toSplit, len(tie))
                    for vote in tie:
                        candidates[vote].addCount(amountGiven)

        output = []
        prev = 100000000000000
        for i in candidates:
            max = -1
            maxPer = 0
            for candidate in candidates:
                if candidate.getCount() > max:
                    max = candidate.getCount()
                    maxPer = candidate.getIdent()

            candidates[maxPer].setCount(-1)
            if max < prev:
                output.append(maxPer)
            else:
                if isinstance(output[len(output) - 1], int):
                    output[len(output) - 1] = [output[len(output) - 1], maxPer]
                else:
                    output[len(output) - 1].append(maxPer)
            prev = max

        return output
Пример #2
0
    def run(self, input):
        votes = input

        i = 0
        output = []
        candidates = []
        for tie in input[0].list:
            for option in tie:
                candidates.append(Candidate(i))
                i += 1

        for time in candidates:
            for voter in votes:
                count = 0
                i = 0
                while (count + len(voter.list[i]) <= time.getIdent()):
                    count += len(voter.getList()[i])
                    i += 1

                amount = 1 / len(voter.list[i])
                for choice in voter.list[i]:
                    candidates[choice].addCount(amount)

            majority = len(votes) / 2
            nextChoice = -1

            for person in candidates:
                if person.getCount() >= majority and person.getAccp() == 1:
                    nextChoice = person.getIdent()
                    majority = person.getCount()

            if nextChoice != -1:
                output.append(nextChoice)
                candidates[nextChoice].setAccp(0)

            while (nextChoice != -1):
                nextChoice = -1
                majority = len(votes) / 2
                for person in candidates:
                    if person.getCount() >= majority and person.getAccp() == 1:
                        nextChoice = person.getIdent()
                        majority = person.getCount()

                if nextChoice != -1:
                    output.append(nextChoice)
                    candidates[nextChoice].setAccp(0)

        return output
Пример #3
0
    def run(self, input):
        Thisvotes = input

        i = 0
        candidates = []
        for tie in input[0].list:
            for option in tie:
                candidates.append(Candidate(i))
                i += 1

        firstVote = len(candidates) - 1

        for vote in Thisvotes:
            curVote = firstVote
            for tie in vote.getList():
                toSplit = 0
                for vote in tie:
                    toSplit += curVote
                    curVote -= 1

                amountGiven = Fraction(toSplit, len(tie))

                for vote in tie:
                    candidates[vote].addCount(amountGiven)

        output = []
        prev = 100000000000000
        for i in candidates:
            max = -1
            maxPer = 0
            for candidate in candidates:
                if candidate.getCount() > max:
                    max = candidate.getCount()
                    maxPer = candidate.getIdent()

            candidates[maxPer].setCount(-1)
            if max < prev:
                output.append(maxPer)
            else:
                if isinstance(output[len(output) - 1], int):
                    output[len(output) - 1] = [output[len(output) - 1], maxPer]
                else:
                    output[len(output) - 1].append(maxPer)
            prev = max

        return output
Пример #4
0
    def run(self, input):
        self.votes = []

        for vote in input:
            self.votes.append(Vote(vote))

        i = 0
        candidates = []
        for tie in input[0].list:
            for option in tie:
                candidates.append(Candidate(i))
                self.key.append([i, i])
                i += 1

        plur = Plurality()
        output = []

        for person in candidates:
            val = 0
            ranking = plur.run(self.votes)

            if isinstance(ranking[len(ranking) - 1], int):
                val = ranking[len(ranking) - 1]
            else:
                val = ranking[len(ranking) - 1][0]

            self.removeLowest(val)

            found = False
            for pair in self.key:
                if val == pair[1] and not found:
                    val = pair[0]
                    found = True

            for pair in self.key:
                if val < pair[0]:
                    pair[1] -= 1
                if val == pair[0]:
                    pair[1] = -1

            output.insert(0, val)

        return output
Пример #5
0
    def run(self, input):
        votes = input

        i = 0
        candidates = []
        for tie in input[0].list:
            for option in tie:
                candidates.append(Candidate(i))
                i += 1

        table = []
        for row in candidates:
            line = []
            for col in candidates:
                line.append(0)
            table.append(line)

        i = 1
        for person in candidates:
            j = i
            while (j < len(candidates)):
                count = 0
                for vote in votes:
                    counted = False
                    k = 0
                    while not counted:
                        for choice in vote.getList()[k]:
                            if choice == person.getIdent():
                                counted = True
                                count += 1
                            if choice == j:
                                counted = True
                                count -= 1
                        k += 1

                table[person.getIdent()][j] = count
                table[j][person.getIdent()] = -1 * count
                j += 1
            i += 1

        return table
Пример #6
0
    def run(self, input, seats):

        self.votes = []
        for ballot in input:
            self.votes.append(Vote(ballot))

        i = 0
        for tie in input[0].list:
            for option in tie:
                self.candidates.append(Candidate(i))
                i += 1

        for rank in self.votes:
            self.distributeNewVotes(rank)

        self.meekSTV(seats)

        return self.output


#       for candidate in self.candidates:
#           print(candidate.getCount())