Пример #1
0
        /// <summary>
        /// Implementation to generate the ranking list for the provided set
        /// of votes for a specific task.
        /// </summary>
        /// <param name="task">The task that the votes are grouped under.</param>
        /// <returns>Returns a ranking list of winning votes.</returns>
        protected override RankResults RankTask(GroupedVotesByTask task)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }

            RankResults winningChoices = new RankResults();

            if (task.Any())
            {
                var voterRankings = GroupRankVotes.GroupByVoterAndRank(task);
                var allChoices    = GroupRankVotes.GetAllChoices(voterRankings);

                for (int i = 1; i <= 9; i++)
                {
                    RankResult winner = GetWinningVote(voterRankings, winningChoices);

                    if (winner == null)
                    {
                        break;
                    }

                    winningChoices.Add(winner);
                    allChoices.Remove(winner.Option);

                    if (!allChoices.Any())
                    {
                        break;
                    }
                }
            }

            return(winningChoices);
        }
Пример #2
0
        /// <summary>
        /// Implementation to generate the ranking list for the provided set
        /// of votes for a specific task.
        /// </summary>
        /// <param name="task">The task that the votes are grouped under.</param>
        /// <returns>Returns a ranking list of winning votes.</returns>
        protected override RankResults RankTask(GroupedVotesByTask task)
        {
            RankResults winningChoices = new RankResults();

            // The groupVotes are used for getting the Wilson score
            var rankedVotes = GroupRankVotes.GroupByVoteAndRank(task);
            // The voterRankings are used for the runoff
            var voterRankings = GroupRankVotes.GroupByVoterAndRank(task);
            // The full choices list is just to keep track of how many we have left.
            var allChoices = GroupRankVotes.GetAllChoices(voterRankings);

            for (int i = 1; i <= 9; i++)
            {
                RankResult winner = GetWinningVote(voterRankings, rankedVotes);

                if (winner == null)
                {
                    break;
                }

                winningChoices.Add(winner);
                allChoices.Remove(winner.Option);

                if (!allChoices.Any())
                {
                    break;
                }

                voterRankings = RemoveChoiceFromVotes(voterRankings, winner.Option);
                rankedVotes   = RemoveChoiceFromRanks(rankedVotes, winner.Option);
            }

            return(winningChoices);
        }
Пример #3
0
        public static IEnumerable <RankGroupedVoters> GroupByVoteAndRank(GroupedVotesByTask task)
        {
            var res = task.GroupBy(vote => VoteString.GetVoteContent(vote.Key), Agnostic.StringComparer)
                      .Select(votes => new RankGroupedVoters {
                VoteContent = votes.Key,
                Ranks       = from v in votes
                              group v by VoteString.GetVoteMarker(v.Key) into vr
                              select new RankedVoters {
                    Rank = int.Parse(vr.Key), Voters = vr.SelectMany(a => a.Value)
                }
            });

            return(res);
        }
Пример #4
0
        /// <summary>
        /// Implementation to generate the ranking list for the provided set
        /// of votes for a specific task.
        /// </summary>
        /// <param name="task">The task that the votes are grouped under.</param>
        /// <returns>Returns a ranking list of winning votes.</returns>
        protected override RankResults RankTask(GroupedVotesByTask task)
        {
            var groupVotes = GroupRankVotes.GroupByVoteAndRank(task);

            var rankedVotes = from vote in groupVotes
                              select new { Vote = vote.VoteContent, Rank = RankVote(vote.Ranks) };

            var orderedVotes = rankedVotes.OrderByDescending(a => a.Rank);

            RankResults results = new RankResults();

            results.AddRange(orderedVotes.Select(a =>
                                                 new RankResult(a.Vote, $"BordaRank: [{a.Rank}]")));

            return(results);
        }
Пример #5
0
        public static IEnumerable <VoterRankings> GroupByVoterAndRank(GroupedVotesByTask task)
        {
            var res = from vote in task
                      from voter in vote.Value
                      group vote by voter into voters
                      select new VoterRankings
            {
                Voter       = voters.Key,
                RankedVotes = (from v in voters
                               select new RankedVote
                {
                    Rank = int.Parse(VoteString.GetVoteMarker(v.Key)),
                    Vote = VoteString.GetVoteContent(v.Key)
                }).ToList()
            };

            return(res);
        }
Пример #6
0
        /// <summary>
        /// Implementation to generate the ranking list for the provided set
        /// of votes for a specific task.
        /// </summary>
        /// <param name="task">The task that the votes are grouped under.</param>
        /// <returns>Returns a ranking list of winning votes.</returns>
        protected override RankResults RankTask(GroupedVotesByTask task)
        {
            // Can calculating the score easily by having all the rankings for
            // each vote grouped together.
            var groupVotes = GroupRankVotes.GroupByVoteAndRank(task);

            var rankedVotes = from vote in groupVotes
                              select new { Vote = vote.VoteContent, Rank = RankScoring.LowerWilsonScore(vote.Ranks) };

            var orderedVotes = rankedVotes.OrderByDescending(a => a.Rank);

            RankResults results = new RankResults();

            results.AddRange(orderedVotes.Select(a =>
                                                 new RankResult(a.Vote, $"Wilson: [{a.Rank:f5}]")));

            return(results);
        }
Пример #7
0
        /// <summary>
        /// Implementation to generate the ranking list for the provided set
        /// of votes for a specific task, based on the Schulze algorithm.
        /// </summary>
        /// <param name="task">The task that the votes are grouped under.</param>
        /// <returns>Returns a ranking list of winning votes.</returns>
        protected override RankResults RankTask(GroupedVotesByTask task)
        {
            if (task == null)
                throw new ArgumentNullException(nameof(task));


            Debug.WriteLine(">>Pairwise Ranking<<");

            List<string> listOfChoices = GroupRankVotes.GetAllChoices(task);

            var voterRankings = GroupRankVotes.GroupByVoterAndRank(task);

            int[,] pairwisePreferences = GetPairwisePreferences(voterRankings, listOfChoices);

            int[,] pairwiseWinners = GetPairwiseWinners(pairwisePreferences, listOfChoices.Count);

            RankResults winningChoices = GetResultsInOrder(pairwiseWinners, listOfChoices);

            return winningChoices;
        }
Пример #8
0
        /// <summary>
        /// Implementation to generate the ranking list for the provided set
        /// of votes for a specific task.
        /// </summary>
        /// <param name="task">The task that the votes are grouped under.</param>
        /// <returns>Returns a ranking list of winning votes.</returns>
        protected override RankResults RankTask(GroupedVotesByTask task)
        {
            Debug.WriteLine(">>Normalized Borda Counting<<");

            //var voterCount = task.SelectMany(t => t.Value).Distinct().Count();

            var groupVotes = GroupRankVotes.GroupByVoteAndRank(task);

            var rankedVotes = from vote in groupVotes
                              select new { Vote = vote.VoteContent, Rank = RankVote(vote.Ranks) };

            var orderedVotes = rankedVotes.OrderBy(a => a.Rank);

            RankResults results = new RankResults();

            results.AddRange(orderedVotes.Select(a =>
                                                 new RankResult(a.Vote, $"BordaNorm: [{a.Rank:f5}]")));

            return(results);
        }
Пример #9
0
        /// <summary>
        /// Implementation to generate the ranking list for the provided set
        /// of votes for a specific task, based on the Schulze algorithm.
        /// </summary>
        /// <param name="task">The task that the votes are grouped under.</param>
        /// <returns>Returns a ranking list of winning votes.</returns>
        protected override RankResults RankTask(GroupedVotesByTask task)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }

            List <string> listOfChoices = GroupRankVotes.GetAllChoices(task);

            var voterRankings = GroupRankVotes.GroupByVoterAndRank(task);

            int[,] pairwisePreferences = GetPairwisePreferences(voterRankings, listOfChoices);

            int[,] strongestPaths = GetStrongestPaths(pairwisePreferences, listOfChoices.Count);

            int[,] winningPaths = GetWinningPaths(strongestPaths, listOfChoices.Count);

            RankResults winningChoices = GetResultsInOrder(winningPaths, listOfChoices);

            return(winningChoices);
        }
Пример #10
0
        /// <summary>
        /// Implementation to generate the ranking list for the provided set
        /// of votes for a specific task, based on the Schulze algorithm.
        /// </summary>
        /// <param name="task">The task that the votes are grouped under.</param>
        /// <returns>Returns a ranking list of winning votes.</returns>
        protected override RankResults RankTask(GroupedVotesByTask task)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }


            Debug.WriteLine(">>Distance U0 Scoring<<");

            List <string> listOfChoices = GroupRankVotes.GetAllChoices(task);

            var voterRankings = GroupRankVotes.GroupByVoterAndRank(task);

            DistanceData pairwiseData = GetPairwiseData(voterRankings, listOfChoices);

            DistanceData strengthData = GetStrongestPaths(pairwiseData, listOfChoices.Count);

            DistanceData winningPaths = GetWinningPaths(strengthData, listOfChoices.Count);

            RankResults winningChoices = GetResultsInOrder(winningPaths, listOfChoices);

            return(winningChoices);
        }
Пример #11
0
        /// <summary>
        /// Gets all choices from all user votes.
        /// </summary>
        /// <param name="task">The collection of user votes.</param>
        /// <returns>Returns a list of all the choices in the task.</returns>
        public static List <string> GetAllChoices(GroupedVotesByTask task)
        {
            var res = task.GroupBy(vote => VoteString.GetVoteContent(vote.Key), Agnostic.StringComparer).Select(vg => vg.Key);

            return(res.ToList());
        }
Пример #12
0
 /// <summary>
 /// Implementation to generate the ranking list for the provided set
 /// of votes for a specific task.
 /// </summary>
 /// <param name="task">The task that the votes are grouped under.</param>
 /// <returns>Returns a ranking list of winning votes.</returns>
 protected abstract RankResults RankTask(GroupedVotesByTask task);