Example #1
0
        public void runSimulation(int trials)
        {
            Roster        roster;
            List <Ballot> ballotList;
            Dictionary <BallotInstructions, List <Ballot> > ballotDictionary = new Dictionary <BallotInstructions, List <Ballot> >();
            VotingSystemResult votingSystemResult;
            List <VoteSummary> voteSummaryList = new List <VoteSummary>();

            for (int i = 0; i < trials; i++)
            {
                roster = new Roster(voterList);
                foreach (Voter voter in voterList)
                {
                    voter.setRoster(roster);
                }

                ballotDictionary.Clear();

                foreach (VotingSystem votingSystem in votingSystemList)
                {
                    // Get or create the ballots
                    if (ballotDictionary.ContainsKey(votingSystem.ballotInstructions))
                    {
                        ballotList = ballotDictionary[votingSystem.ballotInstructions];
                    }
                    else
                    {
                        ballotList = new List <Ballot>();
                        foreach (Voter voter in voterList)
                        {
                            ballotList.Add(voter.createBallot(votingSystem.ballotInstructions));
                        }
                        ballotDictionary.Add(votingSystem.ballotInstructions, ballotList);
                    }

                    // Get the vote summary
                    votingSystemResult = votingSystem.getResult(roster, ballotList);
                    voteSummaryList.Add(getVoteSummary(votingSystemResult));
                }

                ElectionSummary summary = new ElectionSummary(
                    roster.lowestDeviationCandidate.voter.position.deviation,
                    roster.highestDeviationCandidate.voter.position.deviation,
                    voteSummaryList
                    );

                System.IO.File.AppendAllText(@outputPath, summary.ToString() + Environment.NewLine);

                System.Console.WriteLine("Simulation {0} complete", i + 1);
                Console.ReadKey();
                System.Console.WriteLine("");
            }
        }