Beispiel #1
0
        public override ElectionResults GetElectionResults(CandidateComparerCollection <ScoreBallot> ballots)
        {
            // Choose the top two scorers
            // NOTE: No tiebreaking
            var sortedCandidates = ScoreBallot.GetElectionResults(ballots).Ranking;

            int Pop()
            {
                var value = sortedCandidates[0][0];

                sortedCandidates[0].RemoveAt(0);
                if (sortedCandidates[0].Count == 0)
                {
                    sortedCandidates.RemoveAt(0);
                }

                return(value);
            }

            var first  = Pop();
            var second = Pop();

            // Then compare those two head-to-head
            var compare = ballots.Compare(first, second);

            if (compare == 0)
            {
                sortedCandidates.Insert(0, new List <int> {
                    first, second
                });
            }
            else if (compare < 0)
            {
                sortedCandidates.Insert(0, new List <int> {
                    first
                });
                sortedCandidates.Insert(0, new List <int> {
                    second
                });
            }
            else
            {
                sortedCandidates.Insert(0, new List <int> {
                    second
                });
                sortedCandidates.Insert(0, new List <int> {
                    first
                });
            }

            return(new ElectionResults(sortedCandidates));
        }
Beispiel #2
0
        public override ElectionResults GetElectionResults(CandidateComparerCollection <BucketBallot <Bucket> > ballots)
        {
            var firstChoices         = new int[ballots.CandidateCount];
            var maximumApprovalCount = new int[ballots.CandidateCount];

            foreach (var(ballot, count) in ballots.Comparers)
            {
                foreach (var c in ballot.Buckets.IndexesWhere(a => a == Bucket.Best))
                {
                    firstChoices[c]         += count;
                    maximumApprovalCount[c] += count;
                }

                foreach (var c in ballot.Buckets.IndexesWhere(a => a == Bucket.Good))
                {
                    maximumApprovalCount[c] += count;
                }
            }

            var firstChoiceWinners     = firstChoices.MaxIndexes().ToList();
            var maximumApprovalWinners = maximumApprovalCount.MaxIndexes().ToList();

            var approvalCount = firstChoices.SelectToArray(c => c);

            var winners = ballots.Compare(firstChoiceWinners[0], maximumApprovalWinners[0]) > 0 ? firstChoiceWinners : maximumApprovalWinners;

            var results = new ElectionResults(new List <List <int> > {
                winners
            });

            results.AddHeading("Votes");
            results.AddTable(
                approvalCount.IndexOrderByDescending()
                .Select(c => new ElectionResults.Value[] {
                (ElectionResults.Candidate)c,
                approvalCount[c],
                firstChoices[c],
                approvalCount[c] - firstChoices[c],
            }),
                "Votes",
                "First",
                "Comp.");

            return(results);
        }
Beispiel #3
0
        public override ElectionResults GetElectionResults(CandidateComparerCollection <BucketBallot <V321.Result> > ballots)
        {
            // Find 3 Semifinalists: the candidates with the most “good” ratings.
            // Find 2 Finalists: the semifinalists with the fewest "bad" ratings".
            // Find 1 winner: the finalist who is rated above the other on more ballots.
            var bucketCounts = BucketBallot <V321.Result> .GetBucketCounts(ballots);

            var finalists = bucketCounts[Result.Good]
                            .IndexOrderByDescending()
                            .Take(3)
                            .OrderBy(c => bucketCounts[Result.Bad][c])
                            .ToList();

            var first   = finalists[0];
            var second  = finalists[1];
            var compare = ballots.Compare(first, second);

            return(new ElectionResults((compare == 0 ? new List <List <int> > {
                new List <int> {
                    first, second
                }
            }
                : compare < 0 ? new List <List <int> > {
                new List <int> {
                    second
                }, new List <int> {
                    first
                }
            }
                : new List <List <int> > {
                new List <int> {
                    first
                }, new List <int> {
                    second
                }
            })
                                       .Concat(bucketCounts[Result.Good]
                                               .IndexOrderByDescending()
                                               .Skip(3)
                                               .Select(c => new List <int> {
                c
            }))
                                       .ToList()));
        }