Esempio n. 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();
                }
            }
        }
        public async Task TestSimpleStepRuturnNoCandidates()
        {
            var step = new ElectionDriver.Fakes.StubIElectionStep();
            CandiateRanking[] rankings = new CandiateRanking[0];
            step.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                return rankings;
            };

            var e = new Election();
            e.AddStep(step);
            var result = await e.RunSingleElection();
        }
        public async Task TestSimpleStepNullFail()
        {
            var step = new ElectionDriver.Fakes.StubIElectionStep();
            step.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                return null;
            };

            var e = new Election();
            e.NumberOfCandidates = 15;
            e.NumberOfPeople = 350;
            e.AddStep(step);
            await e.RunSingleElection();
        }
Esempio n. 4
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 async void TestSimpleReturn()
        {
            var step = new ElectionDriver.Fakes.StubIElectionStep();
            int numPeople = 0;
            int numCandidates = 0;
            CandiateRanking[] r = new CandiateRanking[] { new CandiateRanking(0, 1) };
            step.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                numPeople = people.Length;
                numCandidates = people[0].FullRanking().Count();
                return r;
            };

            var e = new Election();
            e.NumberOfCandidates = 15;
            e.NumberOfPeople = 350;
            e.AddStep(step);
            var result = await e.RunSingleElection();

            Assert.AreEqual(350, numPeople, "# of people");
            Assert.AreEqual(15, numCandidates, "# of candidates");

            Assert.AreEqual(r, result, "Candidate ranking that came back isn't right");
        }
        public async Task RunElection20TimesWithDifferentResults()
        {
            var e = new Election() { NumberOfCandidates = 3, NumberOfPeople = 20 };

            var step1 = new ElectionDriver.Fakes.StubIElectionStep();
            step1.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                return new CandiateRanking[] { new CandiateRanking(0, 10), new CandiateRanking(1, 15) };
            };
            e.AddStep(step1);

            var result = await e.RunElectionEnsemble(20);
            Assert.AreEqual(0, result.flips, "Expected # of flips");
            Assert.AreEqual(3, result.candidateResults.Length, "# of different winners");
            Assert.AreEqual(0, result.candidateResults[0].resultTimes[0], "# of times candidate zero won");
            Assert.AreEqual(20, result.candidateResults[0].resultTimes[1], "# of times candidate zero was second");
            Assert.AreEqual(0, result.candidateResults[0].resultTimes[2], "# of times candidate zero was second");
            Assert.AreEqual(20, result.candidateResults[1].resultTimes[0], "# of times candidate one was first");
            Assert.AreEqual(0, result.candidateResults[1].resultTimes[1], "# of times candidate one was second");
            Assert.AreEqual(0, result.candidateResults[1].resultTimes[2], "# of times candidate one was second");
            Assert.AreEqual(0, result.candidateResults[2].resultTimes[0], "# of times candidate one was first");
            Assert.AreEqual(0, result.candidateResults[2].resultTimes[1], "# of times candidate one was second");
            Assert.AreEqual(0, result.candidateResults[2].resultTimes[2], "# of times candidate one was second");
        }
 public void TestCTor()
 {
     var e = new Election();
     var et = new ElectionTrend(e);
 }
        public async void TestElectionReturnOrder()
        {
            var step = new ElectionDriver.Fakes.StubIElectionStep();
            CandiateRanking[] r = new CandiateRanking[] { new CandiateRanking(0, 1), new CandiateRanking(1, 10) };
            step.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                return r;
            };

            var e = new Election();
            e.NumberOfCandidates = 15;
            e.NumberOfPeople = 350;
            e.AddStep(step);
            var result = await e.RunSingleElection();

            Assert.AreEqual(1, result[0].candidate, "Winner was not listed first");
        }
Esempio n. 10
0
 public async Task TestBlankRun()
 {
     var e = new Election();
     await e.RunSingleElection();
 }
Esempio n. 11
0
        public async Task TestFixPeople2Requirement()
        {
            var e = new Election() { NumberOfCandidates = 3, NumberOfPeople = 10 };

            e.AddPeopleConstraint(0.3, p => p.Ranking(0) == 1);
            e.AddPeopleConstraint(0.5, p => p.Ranking(1) == 1);

            var step1 = new ElectionDriver.Fakes.StubIElectionStep();
            int countOfNumber1 = 0;
            int countOfNumber2 = 0;
            step1.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                countOfNumber1 = people.Where(p => p.Ranking(0) == 1).Count();
                countOfNumber2 = people.Where(p => p.Ranking(1) == 1).Count();
                return new CandiateRanking[] { new CandiateRanking(0, 1) };
            };
            e.AddStep(step1);

            var flips = await e.RunSingleElection();
            Assert.AreEqual(3, countOfNumber1, "# of times the zero candidate is 1 shoudl be 3!");
            Assert.AreEqual(5, countOfNumber2, "# of times the zero candidate is 2 shoudl be 5!");
        }
Esempio n. 12
0
        public async Task TestFixPeopleImpossibleRequirement()
        {
            var e = new Election() { NumberOfCandidates = 2, NumberOfPeople = 10 };

            e.AddPeopleConstraint(0.7, p => p.Ranking(0) == 1);
            e.AddPeopleConstraint(0.7, p => p.Ranking(1) == 1);

            var step1 = new ElectionDriver.Fakes.StubIElectionStep();
            int countOfNumber1 = 0;
            int countOfNumber2 = 0;
            step1.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                countOfNumber1 = people.Where(p => p.Ranking(0) == 1).Count();
                countOfNumber2 = people.Where(p => p.Ranking(1) == 0).Count();
                return new CandiateRanking[] { new CandiateRanking(0, 1) };
            };
            e.AddStep(step1);
            var flips = await e.RunSingleElection();

        }
Esempio n. 13
0
        public async void RunSimpleElectionSetWithNoFlip()
        {
            var e = new Election() { NumberOfCandidates = 2, NumberOfPeople = 2 };

            var step1 = new ElectionDriver.Fakes.StubIElectionStep();
            step1.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                return new CandiateRanking[] { new CandiateRanking(0, 1) };
            };
            e.AddStep(step1);

            var flips = await e.RunElection();
            Assert.AreEqual(0, flips.flips, "Expected # of flips");
        }
Esempio n. 14
0
        public async void TestTwoStepElectionSimple()
        {
            var step1 = new ElectionDriver.Fakes.StubIElectionStep();
            CandiateRanking[] ranking1 = new CandiateRanking[] { new CandiateRanking(0, 1), new CandiateRanking(1, 2) };
            step1.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) => ranking1;

            var step2 = new ElectionDriver.Fakes.StubIElectionStep();
            CandiateRanking[] ranking2 = new CandiateRanking[] { new CandiateRanking(1, 1) };
            step2.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) => ranking2;

            var e = new Election();
            e.AddStep(step1);
            e.AddStep(step2);
            var result = await e.RunSingleElection();

            Assert.AreEqual(ranking2, result, "Candidate ranking should be what came out of step 1");
        }
 public ElectionTrend(Election e)
 {
     // TODO: Complete member initialization
     this._election = e;
 }
Esempio n. 16
0
        public async Task RunElection20Times()
        {
            var e = new Election() { NumberOfCandidates = 2, NumberOfPeople = 2 };

            var step1 = new ElectionDriver.Fakes.StubIElectionStep();
            int counterTimesCalledWith2 = 0;
            int counterTimesCalledWith1 = 0;
            step1.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                if (people[0].NumberOfCandidates == 1) {
                    counterTimesCalledWith1++;
                    return new CandiateRanking[] { new CandiateRanking(1, 1) };
                }
                if (people[0].NumberOfCandidates == 2) {
                    counterTimesCalledWith2++;
                    return new CandiateRanking[] { new CandiateRanking(0, 1) };
                }
                Assert.Fail("How do we deal with more than 2 candidates!?");
                return null;
            };
            e.AddStep(step1);

            var result = await e.RunElectionEnsemble(20);
            Assert.AreEqual(20, result.flips, "Expected # of flips");
            Assert.AreEqual(20, counterTimesCalledWith2, "Times called with 2");
            Assert.AreEqual(20, counterTimesCalledWith1, "Times called with 1");
            Assert.AreEqual(2, result.candidateResults.Length, "# of different winners");
            Assert.AreEqual(20, result.candidateResults[0].resultTimes[0], "# of times candidate zero won");
            Assert.AreEqual(0, result.candidateResults[0].resultTimes[1], "# of times candidate zero was second");
            Assert.AreEqual(0, result.candidateResults[1].resultTimes[0], "# of times candidate one was first");
            Assert.AreEqual(0, result.candidateResults[1].resultTimes[1], "# of times candidate one was second");
        }
Esempio n. 17
0
 public void TestSimpleRun()
 {
     var e = new Election();
     e.AddStep(new ESOnlyBestCounts());
     var r = e.RunSingleElection();
 }
Esempio n. 18
0
        public async void RunSimpleElectionSetWithFlip()
        {
            var e = new Election() { NumberOfCandidates = 2, NumberOfPeople = 2 };

            var step1 = new ElectionDriver.Fakes.StubIElectionStep();
            bool firstcall = true;
            step1.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
                {
                    if (firstcall)
                    {
                        firstcall = false;
                        return new CandiateRanking[] { new CandiateRanking(0, 1) };
                    }
                    else
                    {
                        var p1 = people.First();
                        Assert.AreEqual(1, p1.NumberOfCandidates, "# of candidates on second sub-election");
                        Assert.AreEqual(0, p1.FullRanking().First().candidate, "Kept candidate");
                        return new CandiateRanking[] { new CandiateRanking(1, 1) };
                    }
                };
            e.AddStep(step1);

            var flips = await e.RunElection();
            Assert.AreEqual(1, flips.flips, "Expected # of flips");
            Assert.AreEqual(1, flips.candidateOrdering.Length, "# of rnaking candidates");
            Assert.AreEqual(0, flips.candidateOrdering[0], "Candidate order 0");
        }
Esempio n. 19
0
        public async void TestElectionWindowing()
        {
            var step1 = new ElectionDriver.Fakes.StubIElectionStep();
            CandiateRanking[] ranking1 = new CandiateRanking[] { new CandiateRanking(0, 1), new CandiateRanking(1, 2), new CandiateRanking(2, 3) };
            step1.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
                {
                    Assert.IsTrue(people.All(p => p.FullRanking().Count() == 4), "Not always three candidates");
                    return ranking1;
                };

            var step2 = new ElectionDriver.Fakes.StubIElectionStep();
            CandiateRanking[] ranking2 = new CandiateRanking[] { new CandiateRanking(1, 1), new CandiateRanking(0, 1) };
            step2.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) => 
                {
                    Assert.IsTrue(people.All(p => p.FullRanking().Count() == 3), "Not always two candidates");
                    return ranking2;
                };

            var step3 = new ElectionDriver.Fakes.StubIElectionStep();
            CandiateRanking[] ranking3 = new CandiateRanking[] { new CandiateRanking(1, 1)};
            step3.RunStepPersonArrayCandiateRankingArrayArray = (people, prev) =>
            {
                Assert.IsTrue(people.All(p => p.FullRanking().Count() == 2), "Not always two candidates");
                return ranking3;
            };

            var e = new Election();
            e.NumberOfCandidates = 4;
            e.AddStep(step1);
            e.AddStep(step2);
            e.AddStep(step3);
            var result = await e.RunSingleElection();

            Assert.AreEqual(ranking3, result, "Candidate ranking should be what came out of step 1");
        }