public void AddResults(StrategyComparisonResults results, Func<string, string> GetOutputFilename)
        {
            System.Threading.Interlocked.Increment(ref outstandingTasks);
            // write out HTML report summary
            var thread = new System.Threading.Thread(delegate()
            {
                var generator = new HtmlReportGenerator(results);

                CreateHtmlReport(generator, GetOutputFilename(results.comparison.playerActions[0].PlayerName + " VS " + results.comparison.playerActions[1].PlayerName + ".html"));
                System.Threading.Interlocked.Decrement(ref outstandingTasks);
            });
            thread.Start();
        }
Esempio n. 2
0
        protected object GetLineGraphData(
            StrategyComparisonResults comparisonResults,
            string title,
            ForwardAndReversePerTurnPlayerCounters counters)
        {
            if (!counters.forwardTotal.HasNonZeroData)
            {
                return null;
            }

            int maxTurn = comparisonResults.gameEndOnTurnHistogramData.GetXAxisValueCoveringUpTo(97);

            var options = GoogleChartsHelper.GetLineGraphOptions(
               "title",
               comparisonResults.comparison.playerActions[0].PlayerName,
               comparisonResults.comparison.playerActions[1].PlayerName,
               counters,
               comparisonResults.statGatherer.turnCounters,
               maxTurn);

            return options;
        }
Esempio n. 3
0
        public StrategyComparisonResults ComparePlayers(
            GetLogForGame getHumanReadableLogWriter = null,
            GetLogForGame getDebugLogWriter         = null,
            bool shouldParallel         = true,
            bool gatherStats            = true,
            CreateGameLog createGameLog = null,
            int randomSeed = 0)
        {
            PlayerAction player1 = playerActions[0];
            PlayerAction player2 = playerActions[1];

            var result = new StrategyComparisonResults(this, gatherStats);

            Action <int> loopBody = delegate(int gameCount)
            {
                System.Threading.Interlocked.Increment(ref totalGameCount);
                using (IndentedTextWriter textWriter = getHumanReadableLogWriter != null ? getHumanReadableLogWriter(gameCount) :  null)
                    using (IndentedTextWriter debugWriter = getDebugLogWriter != null ? getDebugLogWriter(gameCount) : null)
                    {
                        var gameLogs = new List <IGameLog>();
                        if (gatherStats)
                        {
                            gameLogs.Add(result.statGatherer);
                        }
                        if (createGameLog != null)
                        {
                            gameLogs.Add(createGameLog());
                        }
                        if (textWriter != null)
                        {
                            var humanReadableGameLog = new HumanReadableGameLog(textWriter);
                            gameLogs.Add(humanReadableGameLog);
                            var gainSequenceGameLog = new GainSequenceGameLog(textWriter);
                            gameLogs.Add(gainSequenceGameLog);
                        }
                        if (debugWriter != null)
                        {
                            var debugLog = new DebugGameLog(debugWriter);
                            gameLogs.Add(debugLog);
                            var gainSequenceGameLog = new GainSequenceGameLog(debugWriter);
                            gameLogs.Add(gainSequenceGameLog);
                        }

                        var gameLogMultiplexer = new GameLogMultiplexer(gameLogs.ToArray());

                        // swap order every game if needed
                        int[] playedPositions = this.GetPlayerOrderForGameNumber(gameCount);

                        Random random = new Random(gameCount + randomSeed);
                        using (Game game = new Game(random, gameConfig, gameLogMultiplexer))
                        {
                            GameState gameState = new GameState(
                                playerActions,
                                playedPositions,
                                game);

                            gameState.PlayGameToEnd();
                            PlayerState[] winners = gameState.WinningPlayers;

                            int player1Score    = gameState.players.OriginalPlayerOrder[playedPositions[0]].TotalScore();
                            int player2Score    = gameState.players.OriginalPlayerOrder[playedPositions[1]].TotalScore();
                            int scoreDifference = player2Score - player1Score;

                            lock (result)
                            {
                                result.pointSpreadHistogramData.AddOneToBucket(scoreDifference);
                                result.gameEndOnTurnHistogramData.AddOneToBucket(gameState.players.CurrentPlayer.TurnNumber);
                                result.maxTurnNumber = Math.Max(gameState.players.CurrentPlayer.TurnNumber, result.maxTurnNumber);
                                if (winners.Length == 1)
                                {
                                    int winningPlayerIndex = winners[0].Actions == player1 ? 0 : 1;
                                    result.winnerCount[winningPlayerIndex]++;
                                }
                                else
                                {
                                    result.tieCount++;
                                }
                            }
                        }
                    }
            };

            if (shouldParallel)
            {
                Parallel.ForEach(Enumerable.Range(0, numberOfGames), loopBody);
            }
            else
            {
                for (int gameCount = 0; gameCount < numberOfGames; ++gameCount)
                {
                    loopBody(gameCount);
                }
            }

            result.gameEndOnTurnHistogramData.InitializeAllBucketsUpTo(result.maxTurnNumber);

            return(result);
        }
        public StrategyComparisonResults ComparePlayers(
            GetLogForGame getHumanReadableLogWriter = null,
            GetLogForGame getDebugLogWriter = null,
            bool shouldParallel = true,
            bool gatherStats = true,
            CreateGameLog createGameLog = null,
            int randomSeed = 0)
        {
            PlayerAction player1 = playerActions[0];
            PlayerAction player2 = playerActions[1];

            var result = new StrategyComparisonResults(this, gatherStats);

            Action<int> loopBody = delegate(int gameCount)
            {
                System.Threading.Interlocked.Increment(ref totalGameCount);
                using (IndentedTextWriter textWriter = getHumanReadableLogWriter != null ? getHumanReadableLogWriter(gameCount) :  null)
                using (IndentedTextWriter debugWriter = getDebugLogWriter != null ? getDebugLogWriter(gameCount) : null)
                {
                    var gameLogs = new List<IGameLog>();
                    if (gatherStats)
                    {
                        gameLogs.Add(result.statGatherer);
                    }
                    if (createGameLog != null)
                    {
                        gameLogs.Add(createGameLog());
                    }
                    if (textWriter != null)
                    {
                        var humanReadableGameLog = new HumanReadableGameLog(textWriter);
                        gameLogs.Add(humanReadableGameLog);
                        var gainSequenceGameLog = new GainSequenceGameLog(textWriter);
                        gameLogs.Add(gainSequenceGameLog);
                    }
                    if (debugWriter != null)
                    {
                        var debugLog = new DebugGameLog(debugWriter);
                        gameLogs.Add(debugLog);
                        var gainSequenceGameLog = new GainSequenceGameLog(debugWriter);
                        gameLogs.Add(gainSequenceGameLog);
                    }

                    var gameLogMultiplexer = new GameLogMultiplexer(gameLogs.ToArray());

                    // swap order every game if needed
                    int[] playedPositions = this.GetPlayerOrderForGameNumber(gameCount);

                    Random random = new Random(gameCount + randomSeed);
                    using (Game game = new Game(random, gameConfig, gameLogMultiplexer))
                    {

                        GameState gameState = new GameState(
                            playerActions,
                            playedPositions,
                            game);

                        gameState.PlayGameToEnd();
                        PlayerState[] winners = gameState.WinningPlayers;

                        int player1Score = gameState.players.OriginalPlayerOrder[playedPositions[0]].TotalScore();
                        int player2Score = gameState.players.OriginalPlayerOrder[playedPositions[1]].TotalScore();
                        int scoreDifference = player2Score - player1Score;

                        lock (result)
                        {
                            result.pointSpreadHistogramData.AddOneToBucket(scoreDifference);
                            result.gameEndOnTurnHistogramData.AddOneToBucket(gameState.players.CurrentPlayer.TurnNumber);
                            result.maxTurnNumber = Math.Max(gameState.players.CurrentPlayer.TurnNumber, result.maxTurnNumber);
                            if (winners.Length == 1)
                            {
                                int winningPlayerIndex = winners[0].Actions == player1 ? 0 : 1;
                                result.winnerCount[winningPlayerIndex]++;
                            }
                            else
                            {
                                result.tieCount++;
                            }
                        }
                    }
                }
            };

            if (shouldParallel)
            {
                Parallel.ForEach(Enumerable.Range(0, numberOfGames), loopBody);
            }
            else
            {
                for (int gameCount = 0; gameCount < numberOfGames; ++gameCount)
                    loopBody(gameCount);
            }

            result.gameEndOnTurnHistogramData.InitializeAllBucketsUpTo(result.maxTurnNumber);

            return result;
        }
        public static string GetHtmlReport(StrategyComparisonResults comparisonResults)
        {
            var htmlGenerator = new HtmlReportGenerator(comparisonResults);

            return htmlGenerator.CreateHtmlReport();
        }
 public HtmlReportGenerator(StrategyComparisonResults comparisonResults)
 {
     this.comparisonResults = comparisonResults;
 }