コード例 #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();
                }
            }
        }
コード例 #3
0
        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");
        }
コード例 #4
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");
        }