Exemple #1
0
 def ExecuteProblem2(self,inputText):
     GlobalVariables.initalizeConstants()
     for sentence in inputText.split('\n'):
         if 'Input:' not in sentence:
             continue
         splitSentence = sentence.split()
         contestants = splitSentence[1:]
     ballot = Ballot([kingdom for kingdom in GlobalVariables.kingdoms if kingdom.Name in contestants])
     ballot.VotingProcess()
     ballot.GetBallotResult()
Exemple #2
0
def run_election(pop: CombinedPopulation) -> List[ElectionResult]:
    voters = pop.generate_voters(1000)
    candidates = set(gen_candidates(pop.republicans) + gen_candidates(pop.democrats))
    ballots = [Ballot(v, candidates, default_election_config) for v in voters]
    plurality = PartyPrimaryElection(ballots, candidates, pop, default_election_config)
    irv = InstantRunoffElection(ballots, candidates)
    return [plurality.result(), irv.result()]
def run_sample_election(model: CandidateModel, process: ElectionConstructor,
                        population: NDPopulation, train: bool):
    candidates = []
    model_entries = set(np.random.choice(range(6), 3, replace=False))
    r_candidates = gen_non_model_candidates(model, population)
    for i in range(6):
        if i in model_entries and model.ready():
            ideology = Ideology(model.choose_ideology(candidates))
            c = Candidate("m-" + str(i), Independents, ideology, 0)
        else:
            if train:
                c = r_candidates.pop()
            else:
                ideology = population.unit_sample_voter().ideology
                c = Candidate("r-" + str(i), Independents, ideology, 0)

        candidates += [c]

    voters = population.generate_unit_voters(1000)
    ballots = [Ballot(v, candidates, unit_election_config) for v in voters]
    result = process.run(ballots, set(candidates))
    winner = result.winner()
    balance = 0

    return winner, candidates, balance
def run_election() -> float:
    pop = combined_population
    voters = pop.generate_voters(1000)
    candidates = gen_candidates(pop.republicans) + gen_candidates(pop.democrats)
    ballots = list(map(lambda v: Ballot(v, candidates, default_election_config), voters))
    election = PartyPrimaryElection(ballots, set(candidates), pop, default_election_config)
    return election.result().winner().ideology.vec[0]
 def find_winner(self):
     winning_votes = len(self.ballots)/2 +1
     num_rounds = 0
     while True:
         num_rounds+=1
         votes = self.count_votes()
         best_candidate = self.find_max(votes)
         if best_candidate is None:
             break
         if votes[best_candidate] >=winning_votes:
             self.number_of_rounds = num_rounds
             return best_candidate
         else:
             worst_candidate = self.find_min(votes)
             '''
             there may be more than one candidates with minimum votes but
             worst returns only one
             delete all those candidates
             '''
             worst_votes = votes[worst_candidate]
             for candidate in votes:
                 if votes[candidate]==worst_votes:
                     Ballot.eliminate_candidate(candidate)
Exemple #6
0
    def parse_ballot_file(self, file_name: str) -> NoReturn:
        '''Receives ballots file and extracts and stores ballots in list of sets

        Each line of the ballots file will contain only 1 ballot number followed by its fotes. After
        interpreting the ballots, each ballot is stored in a list of sets. Each ballot in a set in the list
        belongs to the candidate that has rank #1 within that ballot.

        Args:
           file_name(str): Name of the CSV file where the ballot number and votes are stored

        Note:
           The format is as follows:
              - ballot_number_1,candidate:1,candidate:2,...,candidate:k
              - ballot_number_2,candidate:1,candidate:2,...,candidate:k
              - ...
        '''

        with open(file_name, "r") as fp:
            # Use list comprehension to interpret line by line of the ballots file with help from
            # the Ballot class. Initially store all ballots in monolithic list
            ballots = DoublyLinkedList(
                [Ballot(line) for line in fp.readlines()])

            # Initialize some ballots statistics
            self.ballots_received = ballots.size()
            self.invalid_ballots = 0
            self.blank_ballots = 0

            # Generate enough sets to later reorganize the ballots based on candidate that has rank #1
            self.ballots = DoublyLinkedList(
                [Set() for i in range(self.candidates.size())])

            for ballot in ballots:
                if ballot.isBlank():
                    self.blank_ballots += 1

                # Ignore invalid ballots
                if ballot.isInvalid():
                    self.invalid_ballots += 1
                    continue

                # Otherwise, we can process ballot and store it accordingly
                candidate = ballot.getCandidateByRank(1)
                if 0 < candidate and candidate < self.candidates.size():
                    self.ballots[candidate].add(ballot)
Exemple #7
0
def run_election(processes: List[ElectionConstructor]) -> List[ElectionResult]:
    global all_voters, all_candidates
    pop = NDPopulation(np.array([0, 0]), np.array([40, 40]))
    voters = pop.generate_unit_voters(1000)
    candidates = gen_candidates(6, pop)
    candidates.append(
        Candidate("V",
                  Independents,
                  Ideology(np.random.normal(scale=[1.0, 1.0])),
                  quality=0))

    vv = [v.ideology.distance_from_o() for v in voters]
    all_voters = np.append(all_voters, vv)
    cc = [c.ideology.distance_from_o() for c in candidates]
    all_candidates = np.append(all_candidates, cc)
    ballots = [Ballot(v, candidates, unit_election_config) for v in voters]
    results = [p.run(ballots, set(candidates)) for p in processes]
    return results