コード例 #1
0
        public override VotingSystemResult getResult(Roster roster, List <Ballot> ballotList)
        {
            Dictionary <Candidate, int> candidateScores = new Dictionary <Candidate, int>();

            foreach (Candidate candidate in roster.candidateList)
            {
                candidateScores[candidate] = 0;
            }

            // Count the ballots
            foreach (Ballot ballot in ballotList)
            {
                if (ballot.ballotInstructions != ballotInstructions)
                {
                    throw new Exception("Approval voting system asked to count a ballot that did not use the correct instructions");
                }

                foreach (CandidateScore candidateScore in ballot.candidateScoreList)
                {
                    candidateScores[candidateScore.candidate] += candidateScore.score;
                }
                break;
            }

            // Convert the final tally to a result object
            VotingSystemResult result = new VotingSystemResult(this);

            foreach (Candidate candidate in roster.candidateList)
            {
                result.addCandidate(candidate, candidateScores[candidate]);
            }

            return(result);
        }
コード例 #2
0
ファイル: FPTP.cs プロジェクト: bkelly1984/ElectionSimulator
        public override VotingSystemResult getResult(Roster roster, List <Ballot> ballotList)
        {
            Dictionary <Candidate, int> voteCountDictionary = new Dictionary <Candidate, int>();

            foreach (Candidate candidate in roster.candidateList)
            {
                voteCountDictionary[candidate] = 0;
            }

            foreach (Ballot ballot in ballotList)
            {
                Candidate candidate = null;

                if (ballot.ballotInstructions.ballotType == BallotType.Score && ballot.candidateScoreList.Count > 0)
                {
                    candidate = ballot.candidateScoreList.OrderByDescending(s => s.score).First().candidate;
                }
                else if (ballot.ballotInstructions.ballotType == BallotType.Rank && ballot.preferredCandidateList.Count > 0)
                {
                    candidate = ballot.preferredCandidateList.First();
                }

                voteCountDictionary[candidate]++;

                if (Tweakables.PRINT_FPTP)
                {
                    System.Console.WriteLine(ballot.voter.ToString() + ": " + candidate.ToString());
                }
            }

            // Create the results
            VotingSystemResult result = new VotingSystemResult(this);

            foreach (Candidate candidate in roster.candidateList)
            {
                result.addCandidate(candidate, voteCountDictionary[candidate]);
            }

            if (Tweakables.PRINT_RESULTS)
            {
                System.Console.WriteLine(result.ToString());
            }

            return(result);
        }
コード例 #3
0
        public override VotingSystemResult getResult(Roster roster, List <Ballot> ballotList)
        {
            CondorcetTally condorcetTally = new CondorcetTally(roster, ballotList);

            if (Tweakables.PRINT_CONDORCET)
            {
                System.Console.WriteLine(condorcetTally.ToString());
            }

            List <List <Candidate> > candidateLoopList = findCandidateLoops(roster.candidateList, condorcetTally);

            candidateLoopList = getOrderedCandidateLoopList(candidateLoopList, condorcetTally);

            VotingSystemResult result = new VotingSystemResult(this);

            foreach (List <Candidate> candidateLoop in candidateLoopList)
            {
                int score = 0;

                foreach (Candidate candidate in candidateLoop)
                {
                    score += condorcetTally.getScore(candidate);
                }

                score = score / candidateLoop.Count;

                foreach (Candidate candidate in candidateLoop)
                {
                    result.addCandidate(candidate, score);
                }
            }

            if (Tweakables.PRINT_RESULTS)
            {
                System.Console.WriteLine(result.ToString());
            }

            return(result);
        }
コード例 #4
0
        public override VotingSystemResult getResult(Roster roster, List <Ballot> ballotList)
        {
            Dictionary <Candidate, int> candidateScores = new Dictionary <Candidate, int>();

            foreach (Candidate candidate in roster.candidateList)
            {
                candidateScores[candidate] = 0;
            }

            // Count the ballots
            foreach (Ballot ballot in ballotList)
            {
                if (ballot.ballotInstructions != ballotInstructions)
                {
                    throw new Exception("Borda count voting system asked to count a ballot that did not use the correct instructions");
                }

                int score = roster.candidateList.Count;
                if (ballot.ballotInstructions.ballotType == BallotType.Score && ballot.candidateScoreList.Count > 0)
                {
                    foreach (CandidateScore candidateScore in ballot.candidateScoreList.OrderByDescending(c => c.score))
                    {
                        candidateScores[candidateScore.candidate] += score;
                        score--;
                    }
                }
                else if (ballot.ballotInstructions.ballotType == BallotType.Rank && ballot.preferredCandidateList.Count > 0)
                {
                    foreach (Candidate candidate in ballot.preferredCandidateList)
                    {
                        candidateScores[candidate] += score;
                        score--;
                    }

                    score -= (roster.candidateList.Count - ballot.preferredCandidateList.Count - ballot.dispreferredCandidateList.Count) / 2;

                    foreach (Candidate candidate in roster.candidateList.Except(ballot.preferredCandidateList).Except(ballot.dispreferredCandidateList))
                    {
                        candidateScores[candidate] += score;
                    }

                    score = 1;
                    foreach (Candidate candidate in ballot.dispreferredCandidateList)
                    {
                        candidateScores[candidate] += score;
                        score++;
                    }
                }

                foreach (CandidateScore candidateScore in ballot.candidateScoreList.OrderByDescending(c => c.score))
                {
                    candidateScores[candidateScore.candidate] += score;
                    score--;
                }
                break;
            }

            // Convert the final tally to a result object
            VotingSystemResult result = new VotingSystemResult(this);

            foreach (Candidate candidate in roster.candidateList)
            {
                result.addCandidate(candidate, candidateScores[candidate]);
            }

            return(result);
        }