Example #1
0
        public void TopCycleSmithSetTest(string filename, int seats, IEnumerable <string> winners)
        {
            BallotSet             ballots;
            AbstractBallotStorage s = new DavidHillFormat();

            using (MemoryMappedFile file = MemoryMappedFile.CreateFromFile(
                       new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read),
                       null, 0, MemoryMappedFileAccess.CopyOnWrite, HandleInheritability.None, false))
            {
                using (MemoryMappedViewStream vs = file.CreateViewStream(0, 0, MemoryMappedFileAccess.Read))
                    ballots = s.LoadBallots(vs);
            }
            Assert.NotNull(ballots);

            PairwiseGraph g;

            g = new PairwiseGraph(ballots);
            TopCycle t = new TopCycle(g);

            HashSet <string> w = t.GetTopCycle(new Candidate[] { }).Select(x => x.Name).ToHashSet();

            Assert.NotNull(w);
            HashSet <string> unexpectedWinners = w.Except(winners).ToHashSet();
            HashSet <string> unexpectedLosers  = winners.Except(w).ToHashSet();

            Assert.Empty(unexpectedWinners);
            Assert.Empty(unexpectedLosers);
        }
        public void A1Test()
        {
            AbstractBallotStorage s = new DavidHillFormat();
            BallotSet             ballots;

            using (MemoryMappedFile file =
                       MemoryMappedFile.CreateFromFile(
                           new FileStream("./resources/electiondata/historic_elections/BurlingtonVT2009.HIL", FileMode.Open, FileAccess.Read, FileShare.Read),
                           null, 0, MemoryMappedFileAccess.CopyOnWrite, HandleInheritability.None, false))
            {
                using (MemoryMappedViewStream vs = file.CreateViewStream(0, 0, MemoryMappedFileAccess.Read))
                    ballots = s.LoadBallots(vs);
            }

            Assert.NotNull(ballots);

            List <string>            winners = null;
            AbstractTabulatorFactory f;
            AbstractTabulator        t;

            void Monitor_TabulationComplete(object sender, TabulationStateEventArgs e)
            {
                winners = e.CandidateStates
                          .Where(x => x.Value.State == CandidateState.States.elected)
                          .Select(x => x.Key.Name).ToList();

                output.WriteLine("Tabulation completion data:");
                fixture.PrintTabulationState(e);
            }

            void Monitor_RoundComplete(object sender, TabulationStateEventArgs e)
            {
                output.WriteLine("Round completion data:");
                fixture.PrintTabulationState(e);
            }

            f = new TidemansAlternativeTabulatorFactory();

            // Use Last Difference
            f.SetTiebreaker(new LastDifferenceTiebreakerFactory());

            t = f.CreateTabulator();

            Assert.NotNull(t);

            t.Monitor.TabulationComplete += Monitor_TabulationComplete;
            t.Monitor.RoundComplete      += Monitor_RoundComplete;

            t.Tabulate(ballots, null, 1);

            t.Monitor.TabulationComplete -= Monitor_TabulationComplete;
            t.Monitor.RoundComplete      -= Monitor_RoundComplete;
        }
Example #3
0
        public void PairwiseGraphTest(string filename, int seats, IEnumerable <string> winners)
        {
            BallotSet             ballots;
            AbstractBallotStorage s = new DavidHillFormat();

            using (MemoryMappedFile file = MemoryMappedFile.CreateFromFile(
                       new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read),
                       null, 0, MemoryMappedFileAccess.CopyOnWrite, HandleInheritability.None, false))
            {
                using (MemoryMappedViewStream vs = file.CreateViewStream(0, 0, MemoryMappedFileAccess.Read))
                    ballots = s.LoadBallots(vs);
            }
            Assert.NotNull(ballots);

            PairwiseGraph g;

            g = new PairwiseGraph(ballots);
        }
Example #4
0
        protected void TabulatorTest(string filename, int seats, IEnumerable <string> winners)
        {
            List <string>     w = null;
            AbstractTabulator t;

            AbstractBallotStorage s = new DavidHillFormat();

            void Monitor_TabulationBegin(object sender, TabulationDetailsEventArgs e)
            {
                output.WriteLine("Tabulation initial state data:");

                PairwiseGraph    g         = new PairwiseGraph(e.Ballots);
                TopCycle         tC        = new TopCycle(g);
                List <Candidate> withdrawn = e.CandidateStates.Where(
                    x => new[] {
                    CandidateState.States.withdrawn,
                    CandidateState.States.defeated
                }.Contains(x.Value.State)
                    ).Select(x => x.Key).ToList();
                TabulationStateEventArgs e1 = new RankedTabulationStateEventArgs
                {
                    CandidateStates = e.CandidateStates,
                    PairwiseGraph   = g,
                    SmithSet        = tC.GetTopCycle(withdrawn, TopCycle.TopCycleSets.smith),
                    SchwartzSet     = tC.GetTopCycle(withdrawn, TopCycle.TopCycleSets.schwartz)
                };

                fixture.PrintTabulationState(e1);
            }

            void Monitor_TabulationComplete(object sender, TabulationStateEventArgs e)
            {
                w = e.CandidateStates
                    .Where(x => x.Value.State == CandidateState.States.elected)
                    .Select(x => x.Key.Name).ToList();

                output.WriteLine("Tabulation completion data:");
                fixture.PrintTabulationState(e);
            }

            void Monitor_RoundComplete(object sender, TabulationStateEventArgs e)
            {
                output.WriteLine("Round completion data:");
                fixture.PrintTabulationState(e);
            }

            BallotSet ballots;

            using (MemoryMappedFile file = MemoryMappedFile.CreateFromFile(
                       new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read),
                       null, 0, MemoryMappedFileAccess.CopyOnWrite, HandleInheritability.None, false))
            {
                using (MemoryMappedViewStream vs = file.CreateViewStream(0, 0, MemoryMappedFileAccess.Read))
                    ballots = s.LoadBallots(vs);
            }
            Assert.NotNull(ballots);

            // Use Last Difference
            tabulatorFactory.SetTiebreaker(new LastDifferenceTiebreakerFactory());

            t = tabulatorFactory.CreateTabulator();

            Assert.NotNull(t);

            output.WriteLine("Testing file {0}", filename);
            t.Monitor.TabulationComplete += Monitor_TabulationComplete;
            t.Monitor.RoundComplete      += Monitor_RoundComplete;
            t.Monitor.TabulationBegin    += Monitor_TabulationBegin;

            t.Tabulate(ballots, seats: seats);

            t.Monitor.TabulationComplete -= Monitor_TabulationComplete;
            t.Monitor.RoundComplete      -= Monitor_RoundComplete;
            t.Monitor.TabulationBegin    -= Monitor_TabulationBegin;

            Assert.NotNull(w);
            HashSet <string> unexpectedWinners = w.Except(winners).ToHashSet();
            HashSet <string> unexpectedLosers  = winners.Except(w).ToHashSet();

            Assert.Empty(unexpectedWinners);
            Assert.Empty(unexpectedLosers);
        }
Example #5
0
        static void Main(string[] args)
        {
            AbstractBallotStorage s = new DavidHillFormat();
            FileStream            file;

            List <string>       winners    = new List <string>();
            HashSet <Candidate> candidates = new HashSet <Candidate>();
            TopCycle            t;
            BallotSet           bset = null;
            int smithSetCount        = 0;

            for (int j = 0; j < 1000; j++)
            {
                winners.Clear();
                candidates.Clear();
                bset = null;
                List <BallotSet> bsets = new List <BallotSet>();
                bsets.Clear();

                if (bset is null)
                {
                    using (file = new FileStream(args[0], FileMode.Open))
                    {
                        bset = s.LoadBallots(file);
                    }
                }

                for (int i = 0; i < 1000; i++)
                {
                    bsets.Add(bset);
                    //GC.Collect(2, GCCollectionMode.Forced, true, true);
                }

                bset = s.ballotFactory.MergeBallotSets(bsets);
                PairwiseGraph g = new PairwiseGraph(bset);

                t = new TopCycle(g);

                foreach (Ballot b in bset)
                {
                    foreach (Vote v in b.Votes)
                    {
                        candidates.Add(v.Candidate);
                    }
                }

                foreach (Candidate c in t.GetTopCycle(new List <Candidate>(), TopCycle.TopCycleSets.smith))
                {
                    winners.Add(c.Name);
                }

                Console.Write(@"""{0}"" ""smith set"" {1}", args[0], winners.Count());

                foreach (string w in winners)
                {
                    Console.Write(@" ""{0}""", w);
                }
                Console.Write("\n");

                smithSetCount = winners.Count();

                winners.Clear();

                foreach (Candidate c in t.GetTopCycle(new List <Candidate>(), TopCycle.TopCycleSets.schwartz))
                {
                    winners.Add(c.Name);
                }

                if (winners.Count() != smithSetCount)
                {
                    Console.Write(@"""{0}"" ""schwartz set"" {1}", args[0], winners.Count());

                    foreach (string w in winners)
                    {
                        Console.Write(@" ""{0}""", w);
                    }
                    Console.Write("\n");
                }

                foreach (Candidate c in candidates)
                {
                    break;
                    foreach (var v in g.Losses(c))
                    {
                        Console.WriteLine("{0} defeated by {1}\t{2}:{3}", c.Name, v.Name, g.GetVoteCount(c, v).v1, g.GetVoteCount(c, v).v2);
                    }
                    foreach (var v in g.Ties(c))
                    {
                        Console.WriteLine("{0} ties with {1}\t{2}:{3}", c.Name, v.Name, g.GetVoteCount(c, v).v1, g.GetVoteCount(c, v).v2);
                    }
                }
            }
        }