Exemplo n.º 1
0
        private void UpdateCountersAtEndOfSim(MonteCarloSimResult bwo)
        {
            MonteCarloSimState update = new MonteCarloSimState();
            update.nRuns = bwo.nRuns;
            update.iThread = -1;
            update.nTotalThrows = Sum(bwo.nThrowsList);
            update.nInfinateLoops = bwo.nInfiniteLoops;
            update.nTotalWars = Sum(bwo.nTotalWarsList);
            update.nTotalTies = Sum(bwo.nTiesList);
            update.nTotalSingleWars = Sum(bwo.nSingleWarsList);
            update.nTotalDoubleWars = Sum(bwo.nDoubleWarsList);
            update.nTotalTripleWars = Sum(bwo.nTripleWarsList);
            update.nTotalQuadWars = Sum(bwo.nQuadWarsList);
            update.nTotalFiveWars = Sum(bwo.nFiveWarsList);
            update.nTotalSixWars = Sum(bwo.nSixWarsList);
            update.nTotalSevenWars = Sum(bwo.nSevenWarsList);

            int iProgress = 100;
            MonteCarloSim_ProgressChanged(this, new ProgressChangedEventArgs(iProgress, update));
        }
Exemplo n.º 2
0
        private void SaveOutputFiles(MonteCarloSimResult bwo)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "CSV File|*.csv|Text File|*.txt|All Files|*.*";
            if (sfd.ShowDialog() != DialogResult.OK) return;

            StreamWriter sw = new StreamWriter(sfd.FileName);

            int nMinThrows, nMaxThrows, nMinTotalWars, nMaxTotalWars, nMinTies, nMaxTies, nMinSingleWars, nMaxSingleWars, nMinDouble, nMaxDouble;
            int nMinTriple, nMaxTriple, nMinQuad, nMaxQuad, nMinFive, nMaxFive, nMinSix, nMaxSix, nMinSeven, nMaxSeven;
            Int64 nCountThrows, nCountTotalWars, nCountTies, nCountSingleWars, nCountDouble, nCountTriple, nCountQuad, nCountFive, nCountSix, nCountSeven;
            double dAvgThrows, dAvgTotalWars, dAvgTies, dAvgSingleWars, dAvgDouble, dAvgTriple, dAvgQuad, dAvgFive, dAvgSix, dAvgSeven;

            GetMaxMinAvgCount(bwo.nThrowsList, out nMaxThrows, out nMinThrows, out dAvgThrows, out nCountThrows, bwo.nInfiniteLoops);
            GetMaxMinAvgCount(bwo.nTotalWarsList, out nMaxTotalWars, out nMinTotalWars, out dAvgTotalWars, out nCountTotalWars);
            GetMaxMinAvgCount(bwo.nTiesList, out nMaxTies, out nMinTies, out dAvgTies, out nCountTies);
            GetMaxMinAvgCount(bwo.nSingleWarsList, out nMaxSingleWars, out nMinSingleWars, out dAvgSingleWars, out nCountSingleWars);
            GetMaxMinAvgCount(bwo.nDoubleWarsList, out nMaxDouble, out nMinDouble, out dAvgDouble, out nCountDouble);
            GetMaxMinAvgCount(bwo.nTripleWarsList, out nMaxTriple, out nMinTriple, out dAvgTriple, out nCountTriple);
            GetMaxMinAvgCount(bwo.nQuadWarsList, out nMaxQuad, out nMinQuad, out dAvgQuad, out nCountQuad);
            GetMaxMinAvgCount(bwo.nFiveWarsList, out nMaxFive, out nMinFive, out dAvgFive, out nCountFive);
            GetMaxMinAvgCount(bwo.nSixWarsList, out nMaxSix, out nMinSix, out dAvgSix, out nCountSix);
            GetMaxMinAvgCount(bwo.nSevenWarsList, out nMaxSeven, out nMinSeven, out dAvgSeven, out nCountSeven);

            sw.WriteLine("Number of Games, " + bwo.nRuns);
            int[] PlayerWins = GetPlayerWins(bwo);
            for (int i = 0; i < bwo.nPlayers; i++)
            {
                sw.WriteLine("Player " + (i+1) + " Wins, " + PlayerWins[i]);
            }
            sw.WriteLine("Number of infinite loops, " + bwo.nInfiniteLoops);

            sw.WriteLine("Max Throws in a game, " + nMaxThrows);
            sw.WriteLine("Min Throws in a game, " + nMinThrows);
            sw.WriteLine("Avg Throws in a game, " + dAvgThrows);
            sw.WriteLine("Total Throws in all games, " + nCountThrows);

            sw.WriteLine("Max Ties in a game, " + nMaxTies);
            sw.WriteLine("Avg Ties in a game, " + dAvgTies);
            sw.WriteLine("Total Ties in all games, " + nCountTies);

            sw.WriteLine("Max total wars in a game, " + nMaxTotalWars);
            sw.WriteLine("Avg total wars in a game, " + dAvgTotalWars);
            sw.WriteLine("Total total wars in all games, " + nCountTotalWars);

            sw.WriteLine("Max single wars in a game, " + nMaxSingleWars);
            sw.WriteLine("Avg single wars in a game, " + dAvgSingleWars);
            sw.WriteLine("Total single wars in all games, " + nCountSingleWars);

            sw.WriteLine("Max double wars in a game, " + nMaxDouble);
            sw.WriteLine("Avg double wars in a game, " + dAvgDouble);
            sw.WriteLine("Total double wars in all games, " + nCountDouble);

            sw.WriteLine("Max triple wars in a game, " + nMaxTriple);
            sw.WriteLine("Avg triple wars in a game, " + dAvgTriple);
            sw.WriteLine("Total triple wars in all games, " + nCountTriple);

            sw.WriteLine("Max quad wars in a game, " + nMaxQuad);
            sw.WriteLine("Avg quad wars in a game, " + dAvgQuad);
            sw.WriteLine("Total quad wars in all games, " + nCountQuad);

            sw.WriteLine("Max quintuple wars in a game, " + nMaxFive);
            sw.WriteLine("Avg quintuple wars in a game, " + dAvgFive);
            sw.WriteLine("Total quintuple wars in all games, " + nCountFive);

            sw.WriteLine("Max sextuple wars in a game, " + nMaxSix);
            sw.WriteLine("Avg sextuple wars in a game, " + dAvgSix);
            sw.WriteLine("Total sextuple wars in all games, " + nCountSix);

            sw.WriteLine("Max septuble wars in a game, " + nMaxSeven);
            sw.WriteLine("Avg septuble wars in a game, " + dAvgSeven);
            sw.WriteLine("Total septuble wars in all games, " + nCountSeven);

            sw.WriteLine();
            sw.WriteLine();

            if( checkBoxVerbose.Checked)
            {
                sw.WriteLine("Game Number, Winner, Number of Throws, Total Wars, Single Wars, Double Wars, Triple Wars, Quadruple Wars, Quintuple Wars, Sextuple Wars, Septuble Wars");

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < bwo.nThrowsList.Count; i++)
                {
                    sb.Clear();
                    sb.Append((i+1).ToString());
                    sb.Append(", " + (bwo.iWinnerList[i]+1));
                    sb.Append(", " + bwo.nThrowsList[i]);
                    sb.Append(", " + bwo.nTotalWarsList[i]);
                    sb.Append(", " + bwo.nSingleWarsList[i]);
                    sb.Append(", " + bwo.nDoubleWarsList[i]);
                    sb.Append(", " + bwo.nTripleWarsList[i]);
                    sb.Append(", " + bwo.nQuadWarsList[i]);
                    sb.Append(", " + bwo.nFiveWarsList[i]);
                    sb.Append(", " + bwo.nSixWarsList[i]);
                    sb.Append(", " + bwo.nSevenWarsList[i]);
                    sw.WriteLine(sb.ToString());
                }
                sw.WriteLine();
                sw.WriteLine();
            }

            sw.Close();
        }
Exemplo n.º 3
0
        private void StartMonteCarloSimulationClicked(object sender, EventArgs e)
        {
            if (!workerAutoThrow.IsBusy || m_Workers != null)
            {
                EnableUI(false);
                int nThreads = (int)numericUpDownNThreads.Value;
                m_Workers = new BackgroundWorker[nThreads];

                var rand = new Random();
                m_WorkersOutput = new MonteCarloSimResult();
                m_WorkersOutput.iThread = 0;

                for (int iThread = 0; iThread < nThreads; iThread++)
                {
                    m_Workers[iThread] = new BackgroundWorker();
                    m_Workers[iThread].WorkerReportsProgress = true;
                    m_Workers[iThread].WorkerSupportsCancellation = true;
                    m_Workers[iThread].DoWork += new DoWorkEventHandler(MonteCarloSim_DoWork);
                    m_Workers[iThread].ProgressChanged += new ProgressChangedEventHandler(MonteCarloSim_ProgressChanged);
                    m_Workers[iThread].RunWorkerCompleted += new RunWorkerCompletedEventHandler(MonteCarloSim_RunWorkerCompleted);

                    MonteCarloSimArgs args = new MonteCarloSimArgs();

                    args.nGames = (int)numericUpDownNGames.Value / nThreads;
                    args.nPlayers = (int)numericUpDownNPlayers.Value;
                    args.iSeed = rand.Next();
                    args.bShuffleWinnings = checkBoxShuffleResult.Checked;
                    args.nThreads = nThreads;
                    args.iThread = iThread;

                    m_Workers[iThread].RunWorkerAsync(args);
                }
            }
            else MessageBox.Show("Simulation is already running!"); // should be an assert... because we should never be here.
        }
Exemplo n.º 4
0
        private void MonteCarloSim_DoWork(object sender, DoWorkEventArgs e)
        {
            MonteCarloSimArgs args = e.Argument as MonteCarloSimArgs;
            BackgroundWorker bw = sender as BackgroundWorker;
            MonteCarloSimResult bwo = new MonteCarloSimResult();
            bwo.iThread = args.iThread;
            bwo.nPlayers = args.nPlayers;

            kbCardGameWar game = new kbCardGameWar(args.nPlayers, new Random(args.iSeed));
            game.ShuffleRecentlyWonCards = args.bShuffleWinnings;

            for (int i = 0; i < args.nGames; i++)
            {
                if (bw.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }

                game.Restart(args.nPlayers);
                game.ShuffleDeck();
                game.Deal();
                game.PlayTillFinished();

                if (game.State == kbCardGameWar.GameState.eInfiniteLoop) bwo.nThrowsList.Add(0);
                else bwo.nThrowsList.Add(game.Counters.nTurns);

                if (game.State == kbCardGameWar.GameState.eInfiniteLoop) bwo.nInfiniteLoops++;
                bwo.iWinnerList.Add(game.Winner);
                bwo.nTiesList.Add(game.Counters.nTies);
                bwo.nTotalWarsList.Add(game.Counters.nTotalWars);
                bwo.nSingleWarsList.Add(game.Counters.nSingleWars);
                bwo.nDoubleWarsList.Add(game.Counters.nDoubleWars);
                bwo.nTripleWarsList.Add(game.Counters.nTripleWars);
                bwo.nQuadWarsList.Add(game.Counters.nQuadrupleWars);
                bwo.nFiveWarsList.Add(game.Counters.nQuintupleWars);
                bwo.nSixWarsList.Add(game.Counters.nSextupleWars);
                bwo.nSevenWarsList.Add(game.Counters.nSeptupleWars);
                bwo.nRuns++;
                if (i % 500 == 0)
                {
                    MonteCarloSimUpdateProgress(bw, bwo, i, args);
                }
            }
            MonteCarloSimUpdateProgress(bw, bwo, args.nGames, args);

            e.Result = bwo;
        }
Exemplo n.º 5
0
        private void MonteCarloSimUpdateProgress(BackgroundWorker bw, MonteCarloSimResult bwo, int iRun, MonteCarloSimArgs args)
        {
            MonteCarloSimState update = new MonteCarloSimState();
            update.nRuns = bwo.nRuns;
            update.iThread = args.iThread;
            update.nTotalThrows = Sum(bwo.nThrowsList);
            update.nInfinateLoops = bwo.nInfiniteLoops;
            update.nTotalWars = Sum(bwo.nTotalWarsList);
            update.nTotalTies = Sum(bwo.nTiesList);
            update.nTotalSingleWars = Sum(bwo.nSingleWarsList);
            update.nTotalDoubleWars = Sum(bwo.nDoubleWarsList);
            update.nTotalTripleWars = Sum(bwo.nTripleWarsList);
            update.nTotalQuadWars = Sum(bwo.nQuadWarsList);
            update.nTotalFiveWars = Sum(bwo.nFiveWarsList);
            update.nTotalSixWars = Sum(bwo.nSixWarsList);
            update.nTotalSevenWars = Sum(bwo.nSevenWarsList);

            bw.ReportProgress((int)(100.0 * iRun / (double)args.nGames), update);
        }
Exemplo n.º 6
0
 private int[] GetPlayerWins(MonteCarloSimResult bwo)
 {
     int[] playerwins = new int[bwo.nPlayers + 1];
     foreach (int winner in bwo.iWinnerList)
     {
         if (winner < 0) playerwins[bwo.nPlayers]++;    // this is for when there was no winner.
         else playerwins[winner]++;
     }
     return playerwins;
 }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the output text
        /// </summary>
        private string GetOutputText(MonteCarloSimResult bwo)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("****************************************");
            sb.AppendLine("Number of games played: " + bwo.nRuns.ToString("N0"));
            if (checkBoxShuffleResult.Checked) sb.AppendLine("Shuffle the winning cards every turn");
            else sb.AppendLine("Perfect play, no shuffling of cards between turns, infinate loops possible!");

            int[] PlayerWins = GetPlayerWins(bwo);
            for (int i = 0; i < bwo.nPlayers; i++)
            {
                sb.AppendLine("Player " + (i+1) + " Wins: " + PlayerWins[i].ToString("N0"));
            }
            sb.AppendLine("Number of infinite loops: " + bwo.nInfiniteLoops.ToString("N0"));
            sb.AppendLine("");

            int nMinThrows, nMaxThrows;
            Int64 nCountThrows;
            double dAvgThrows;
            GetMaxMinAvgCount(bwo.nThrowsList, out nMaxThrows, out nMinThrows, out dAvgThrows, out nCountThrows, bwo.nInfiniteLoops);
            sb.AppendLine("Max throws in a game: " + nMaxThrows.ToString("N0"));
            sb.AppendLine("Min throws in a game: " + nMinThrows.ToString("N0"));
            sb.AppendLine("Avg throws in a game: " + dAvgThrows.ToString());
            sb.AppendLine("Total throws in all games: " + nCountThrows.ToString("N0"));
            sb.AppendLine("");

            int nMaxTies, nMinTies;
            Int64 nCountTies;
            double dAvgTies;
            GetMaxMinAvgCount(bwo.nTiesList, out nMaxTies, out nMinTies, out dAvgTies, out nCountTies);
            sb.AppendLine("Max ties in a game: " + nMaxTies.ToString("N0"));
            sb.AppendLine("Avg ties in a game: " + dAvgTies.ToString());
            sb.AppendLine("Total ties in all games: " + nCountTies.ToString("N0"));
            sb.AppendLine("");

            int nMinTotalWars, nMaxTotalWars;
            Int64 nCountTotalWars;
            double dAvgTotalWars;
            GetMaxMinAvgCount(bwo.nTotalWarsList, out nMaxTotalWars, out nMinTotalWars, out dAvgTotalWars, out nCountTotalWars);
            sb.AppendLine("Max total wars in a game: " + nMaxTotalWars.ToString("N0"));
            sb.AppendLine("Min total wars in a game: " + nMinTotalWars.ToString("N0"));
            sb.AppendLine("Avg total wars in a game: " + dAvgTotalWars.ToString());
            sb.AppendLine("Total total wars in all games: " + nCountTotalWars.ToString("N0"));
            sb.AppendLine("");

            int nMinSingleWars, nMaxSingleWars;
            Int64 nCountSingleWars;
            double dAvgSingleWars;
            GetMaxMinAvgCount(bwo.nSingleWarsList, out nMaxSingleWars, out nMinSingleWars, out dAvgSingleWars, out nCountSingleWars);
            sb.AppendLine("Max single wars in a game: " + nMaxSingleWars.ToString("N0"));
            sb.AppendLine("Min single wars in a game: " + nMinSingleWars.ToString("N0"));
            sb.AppendLine("Avg single wars in a game: " + dAvgSingleWars.ToString());
            sb.AppendLine("Total single wars in all games: " + nCountSingleWars.ToString("N0"));
            sb.AppendLine("");

            int nMinDouble, nMaxDouble;
            Int64 nCountDouble;
            double dAvgDouble;
            GetMaxMinAvgCount(bwo.nDoubleWarsList, out nMaxDouble, out nMinDouble, out dAvgDouble, out nCountDouble);
            sb.AppendLine("Max double wars in a game: " + nMaxDouble.ToString("N0"));
            sb.AppendLine("Avg double wars in a game: " + dAvgDouble.ToString());
            sb.AppendLine("Total double wars in all games: " + nCountDouble.ToString("N0"));
            sb.AppendLine("");

            int nMinTriple, nMaxTriple;
            Int64 nCountTriple;
            double dAvgTriple;
            GetMaxMinAvgCount(bwo.nTripleWarsList, out nMaxTriple, out nMinTriple, out dAvgTriple, out nCountTriple);
            sb.AppendLine("Max triple wars in a game: " + nMaxTriple.ToString("N0"));
            sb.AppendLine("Avg triple wars in a game: " + dAvgTriple.ToString());
            sb.AppendLine("Total triple wars in all games: " + nCountTriple.ToString("N0"));
            sb.AppendLine("");

            int nMinQuad, nMaxQuad;
            Int64 nCountQuad;
            double dAvgQuad;
            GetMaxMinAvgCount(bwo.nQuadWarsList, out nMaxQuad, out nMinQuad, out dAvgQuad, out nCountQuad);
            sb.AppendLine("Max quad wars in a game: " + nMaxQuad.ToString("N0"));
            sb.AppendLine("Avg quad wars in a game: " + dAvgQuad.ToString());
            sb.AppendLine("Total quad wars in all games: " + nCountQuad.ToString("N0"));
            sb.AppendLine("");

            int nMinFive, nMaxFive;
            Int64 nCountFive;
            double dAvgFive;
            GetMaxMinAvgCount(bwo.nFiveWarsList, out nMaxFive, out nMinFive, out dAvgFive, out nCountFive);
            sb.AppendLine("Max quintuple wars in a game: " + nMaxFive.ToString("N0"));
            sb.AppendLine("Avg quintuple wars in a game: " + dAvgFive.ToString());
            sb.AppendLine("Total quintuple wars in all games: " + nCountFive.ToString("N0"));
            sb.AppendLine("");

            int nMinSix, nMaxSix;
            Int64 nCountSix;
            double dAvgSix;
            GetMaxMinAvgCount(bwo.nSixWarsList, out nMaxSix, out nMinSix, out dAvgSix, out nCountSix);
            sb.AppendLine("Max sextuple wars in a game: " + nMaxSix.ToString("N0"));
            sb.AppendLine("Avg sextuple wars in a game: " + dAvgSix.ToString());
            sb.AppendLine("Total sextuple wars in all games: " + nCountSix.ToString("N0"));
            sb.AppendLine("");

            int nMinSeven, nMaxSeven;
            Int64 nCountSeven;
            double dAvgSeven;
            GetMaxMinAvgCount(bwo.nSevenWarsList, out nMaxSeven, out nMinSeven, out dAvgSeven, out nCountSeven);
            sb.AppendLine("Max septuble wars in a game: " + nMaxSeven.ToString("N0"));
            sb.AppendLine("Avg septuble wars in a game: " + dAvgSeven.ToString());
            sb.AppendLine("Total septuble wars in all games: " + nCountSeven.ToString("N0"));
            sb.AppendLine("");

            return sb.ToString();
        }
Exemplo n.º 8
0
 public void CombineResults(MonteCarloSimResult bwo)
 {
     this.iThread++;  // use this as a counter to know how many times we've combined outputs.
     this.nPlayers = bwo.nPlayers;
     this.nInfiniteLoops += bwo.nInfiniteLoops;
     this.nRuns += bwo.nRuns;
     this.iWinnerList.AddRange(bwo.iWinnerList);
     this.nThrowsList.AddRange(bwo.nThrowsList);
     this.nTiesList.AddRange(bwo.nTiesList);
     this.nTotalWarsList.AddRange(bwo.nTotalWarsList);
     this.nSingleWarsList.AddRange(bwo.nSingleWarsList);
     this.nDoubleWarsList.AddRange(bwo.nDoubleWarsList);
     this.nTripleWarsList.AddRange(bwo.nTripleWarsList);
     this.nQuadWarsList.AddRange(bwo.nQuadWarsList);
     this.nFiveWarsList.AddRange(bwo.nFiveWarsList);
     this.nSixWarsList.AddRange(bwo.nSixWarsList);
     this.nSevenWarsList.AddRange(bwo.nSevenWarsList);
 }