Пример #1
0
        /// <summary>
        /// Try to emulate some fairly complex election algorithms that I've seen around
        /// the web.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            uint nElections = 4000;
            uint nCandidates = 4;
            Console.WriteLine("Running the Run-off Spokes election with {0} candidates {1} times.", nCandidates, nElections);

            var e = new Election()
            {
                NumberOfPeople = 400,
                NumberOfCandidates = (int) nCandidates
            };

            // Election:
            // 1. Everyone has a single vote
            // 2. If someone is > 50%, then that person is the winner.
            // 3. Otherwise the top two are kept, and we have a run-off with everyone has single vote
            e.AddStep(new ESOnlyBestCounts());
            e.AddStep(new ESKeepThoseBetterThan(0.50) { DoNothingIfNoOnePasses = true });
            e.AddStep(new ESKeepBestN(2));
            e.AddStep(new ESOnlyBestCounts());

            var flips = e.RunElectionEnsemble(nElections).Result;
            Console.WriteLine("Saw {0} flips in {1} elections.", flips.flips, nElections);
            for (int icand = 0; icand < flips.candidateResults.Length; icand++)
            {
                Console.Write("Candidate {0}: ", icand);
                for (int irank = 0; irank < flips.candidateResults.Length; irank++)
                {
                    Console.Write("{0}={1}, ", irank, flips.candidateResults[icand].resultTimes[irank]);
                }
                Console.WriteLine();
            }

            // Do a trend as a function of candidate 0...

            double pointDelta = 0.02;

            var eTrend = new ElectionTrend(e);
            var results = eTrend.RunTrend(
                (point, numPoints) => Tuple.Create<double, Func<Person, bool>>(pointDelta * point, p => p.Ranking(0) == nCandidates - 1),
                points: 15,
                numberPerPoint: (int) nElections
                );

            for (int i = 0; i < results.Length; i++)
            {
                var r = results[i].Result;
                Console.WriteLine("Election with candidate 0 having {0}% of the vote ({1} flips):", pointDelta * 100.0 * i, r.flips);
                for (int icand = 0; icand < flips.candidateResults.Length; icand++)
                {
                    Console.Write("  Candidate {0}: ", icand);
                    for (int irank = 0; irank < r.candidateResults.Length; irank++)
                    {
                        Console.Write("{0}={1}, ", irank, r.candidateResults[icand].resultTimes[irank]);
                    }
                    Console.WriteLine();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Run a very simple majority election
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            uint nElections = 4000;
            uint nCandidates = 4;
            Console.WriteLine("Running the Majority election with {0} candidates {1} times.", nCandidates, nElections);

            var e = new Election()
            {
                NumberOfCandidates = (int) nCandidates,
                NumberOfPeople = 4000
            };
            e.AddStep(new ESOnlyBestCounts());

            var flips = e.RunElectionEnsemble(nElections).Result;

            Console.WriteLine("Saw {0} flips in {1} elections.", flips.flips, nElections);
            for (int icand = 0; icand < flips.candidateResults.Length; icand++)
            {
                Console.Write("Candidate {0}: ", icand);
                for (int irank = 0; irank < flips.candidateResults.Length; irank++)
                {
                    Console.Write("{0}={1}, ", irank, flips.candidateResults[icand].resultTimes[irank]);
                }
                Console.WriteLine();
            }

            // Do a trend as a function of candidate 0...

            var eTrend = new ElectionTrend(e);
            var results = eTrend.RunTrend(
                (point, numPoints) => Tuple.Create<double, Func<Person, bool>>(0.1*point, p => p.Ranking(0) == nCandidates-1),
                points: 10,
                numberPerPoint: (int) nElections
                );

            for (int i = 0; i < results.Length; i++)
            {
                var r = results[i].Result;
                Console.WriteLine("Election with candidate 0 having {0}% of the vote ({1} flips):", 10.0 * i, r.flips);
                for (int icand = 0; icand < flips.candidateResults.Length; icand++)
                {
                    Console.Write("  Candidate {0}: ", icand);
                    for (int irank = 0; irank < r.candidateResults.Length; irank++)
                    {
                        Console.Write("{0}={1}, ", irank, r.candidateResults[icand].resultTimes[irank]);
                    }
                    Console.WriteLine();
                }
            }
        }
        public async Task TestSimpleTrend()
        {
            // Simple, constant election
            var e = new Election() { NumberOfCandidates = 2, NumberOfPeople = 10 };
            var step = new ElectionDriver.Fakes.StubIElectionStep();
            step.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                var mypeople = people.Where(p => p.NumberOfCandidates == 2);
                return new CandiateRanking[] { 
                    new CandiateRanking(0, mypeople.Where(p => p.Ranking(0) == 1).Count()),
                    new CandiateRanking(1, mypeople.Where(p => p.Ranking(1) == 1).Count())
                };
            };
            e.AddStep(step);

            var et = new ElectionTrend(e);
            var r = et.RunTrend(
                (point, totPoint) => Tuple.Create<double, Func<Person, bool>>(0.1*point, p => p.Ranking(0) == 1),
                points: 10
                );

            Assert.AreEqual(10, r.Length, "# of ensemble results that came back");
            for (int i = 0; i < 10; i++)
            {
                var real = await r[i];
                var frac = 0.1 * i;

                Console.WriteLine("Election Point {0}: ", i);

                for (int icand = 0; icand < 2; icand++)
                {
                    Console.Write("  Candidate {0}: ", icand);
                    Console.WriteLine("r1={0}, r2={1}", real.candidateResults[icand].resultTimes[0],
                        real.candidateResults[icand].resultTimes[1]);

                    if (i < 5)
                    {
                        Assert.AreEqual(0, real.candidateResults[0].resultTimes[0], "Expected for iteration " + i + "!");
                    }
                    else
                    {
                        Assert.AreEqual(50, real.candidateResults[0].resultTimes[0], "Expected for iteration " + i + "!");
                    }
                }

            }
        }
 public void TestCTor()
 {
     var e = new Election();
     var et = new ElectionTrend(e);
 }