Exemple #1
0
        static void Main(string[] args)
        {
            do
            {
                GridGameParameters ggp = GridGameParameters.DefaultParameters("ascii");

                Console.WriteLine("t = Tic-Tac-Toe");
                Console.WriteLine("c = Connect4");
                Console.WriteLine("r = Reversi");
                string gameName = getOption("Game? ", "t", "c", "r");

                switch (gameName)
                {
                    case "t": ggp.Game = "tictactoe"; break;
                    case "c": ggp.Game = "connect4"; break;
                    case "r": ggp.Game = "reversi"; break;
                    default:
                        break;
                }

                bool player1 = getOption("Do you want to be player 1 or 2? ", "1", "2") == "1";

                Console.WriteLine("h = Human");
                Console.WriteLine("m = Minimax");
                Console.WriteLine("r = Random");
                Console.WriteLine("t = Monte Carlo Tree Search");
                string agentType = getOption("Opponent type? ", "h", "m", "r", "t");

                IAgent opp;
                switch (agentType)
                {
                    case "h": opp = new AsciiAgent(1); break;
                    case "m":
                        int depth;
                        string depthStr = getOption("Max tree search depth? ");
                        while (!int.TryParse(depthStr, out depth))
                            depthStr = getOption("Invalid depth value. Try again: ");
                        ggp.MinimaxDepth = depth;
                        opp = ggp.CreateMinimaxAgent(1); break;
                    case "r": opp = new RandomAgent(1); break;
                    case "t": 
                        int trials;
                        string trialStr = getOption("Max trials per move? ");
                        while (!int.TryParse(trialStr, out trials))
                            trialStr = getOption("Invalid depth value. Try again: ");
                        ggp.MonteCarloTrials = trials;
                        opp = ggp.CreateMctsAgent(1, false);
                        break;
                    default:
                        throw new Exception("Unknown agent type");
                }

                GridGame game = CreateGame(gameName, player1, opp);

                game.PlayToEnd();

                game.Board.PrintBoard();

                AnnounceWinner(game, player1);

            } while (getOption("Play again (y/n)? ", "y", "n") == "y");
        }
Exemple #2
0
        static void Main(string[] args)
        {
            _params = GridGameParameters.GetParameters(args);
            if (_params == null)
                return;

            Debug.Assert(_params.AgentPath != null);
            Debug.Assert(File.Exists(_params.AgentPath));

            using (TextWriter writer = new StreamWriter(_params.ConfigPath))
            {
                XmlSerializer ser = new XmlSerializer(typeof(GridGameParameters));
                ser.Serialize(writer, _params);
            }

            _experiment = new GridGameExperiment(_params.ConfigPath);

            IAgent agent = null;
            IAgent benchmark = null;

            // Load the agent to benchmark
            var modelGenome = _experiment.LoadPopulation(XmlReader.Create(_params.AgentPath))[0];
            var brain = _experiment.CreateGenomeDecoder().Decode(modelGenome);
            if (_params.MctsNeat)
            {
                Console.WriteLine("Creating MCTS-NEAT agent");
                agent = _params.CreateMctsNeatAgent(1, brain);
            }
            else
            {
                Console.WriteLine("Creating MCTS agent");
                agent = _params.CreateMctsAgent(1, false);
            }

            // Create the benchmark MCTS agent
            if (_params.Evaluator == "mcts")
            {
                Console.WriteLine("Creating MCTS benchmark agent");
                benchmark = _params.CreateMctsAgent(-1, true);
            }
            else
            {
                Console.WriteLine("Creating Random benchmark agent");
                benchmark = new RandomAgent(-1);
            }

            Outcome[] outcomes = new Outcome[_params.MatchesPerOpponent * 2];

            Console.WriteLine("Starting games as player 1..");
            using (_moveLog = new StreamWriter(_params.BenchmarkGameLogPath))
            {
                for (int i = 0; i < _params.MatchesPerOpponent; i++)
                {
                    Console.Write(i + "...");
                    if (i > 0 && i % 10 == 0)
                        Console.WriteLine();
                    outcomes[i] = RunTrial(agent, benchmark, 1);
                }
                Console.WriteLine();

                Console.WriteLine("Starting games as player 2..");
                for (int i = 0; i < _params.MatchesPerOpponent; i++)
                {
                    Console.Write(i + "...");
                    if (i > 0 && i % 10 == 0)
                        Console.WriteLine();
                    outcomes[i + _params.MatchesPerOpponent] = RunTrial(benchmark, agent, -1);
                }
                Console.WriteLine();
                Console.WriteLine("Saving log file...");
            }

            using (TextWriter writer = new StreamWriter(_params.BenchmarkResultsPath))
            {
                // games
                // wins, ties, losses
                // win %, tie %, loss %
                // p1 wins, p1 ties, p1 losses, 
                // p1 win %, p1 tie %, p1 loss %, 
                // p2 wins, p2 ties, p2 losses, 
                // p2 win %, p2 tie %, p2 loss %,
                // time per move, turns per game
                writer.WriteLine("games,wins,win%,ties,tie%,losses,loss%,p1 wins,p1 win%,p1 ties,p1 tie%,p1 losses,p1 loss%,p2 wins,p2 win%,p2 ties,p2 tie%,p2 losses,p2 loss%,avg time per move, avg total moves per game");

                writer.WriteLine(
                    outcomes.Length + "," + // games
                    
                    outcomes.Count(o => o.Winner == o.AgentId) + "," + // wins
                    Pct(outcomes.Count(o => o.Winner == o.AgentId), outcomes.Length) + "," + // win %
                    outcomes.Count(o => o.Winner == 0) + "," + // ties
                    Pct(outcomes.Count(o => o.Winner == 0), outcomes.Length) + "," + // tie %
                    outcomes.Count(o => o.Winner == o.BenchmarkId) + "," + // losses
                    Pct(outcomes.Count(o => o.Winner == o.BenchmarkId), outcomes.Length) + "," + // loss %
                    
                    outcomes.Count(o => o.Winner == o.AgentId && o.AgentId == 1) + "," + // p1 wins
                    Pct(outcomes.Count(o => o.Winner == o.AgentId && o.AgentId == 1), outcomes.Count(o => o.AgentId == 1)) + "," + // p1 win %
                    outcomes.Count(o => o.Winner == 0 && o.AgentId == 1) + "," + // p1 ties
                    Pct(outcomes.Count(o => o.Winner == 0 && o.AgentId == 1), outcomes.Count(o => o.AgentId == 1)) + "," + // p1 tie %
                    outcomes.Count(o => o.Winner == o.BenchmarkId && o.AgentId == 1) + "," + // p1 losses
                    Pct(outcomes.Count(o => o.Winner == o.BenchmarkId && o.AgentId == 1), outcomes.Count(o => o.AgentId == 1)) + "," + // p1 loss %

                    outcomes.Count(o => o.Winner == o.AgentId && o.AgentId == -1) + "," + // p2 wins
                    Pct(outcomes.Count(o => o.Winner == o.AgentId && o.AgentId == -1), outcomes.Count(o => o.AgentId == -1)) + "," + // p2 win %
                    outcomes.Count(o => o.Winner == 0 && o.AgentId == -1) + "," + // p2 ties
                    Pct(outcomes.Count(o => o.Winner == 0 && o.AgentId == -1), outcomes.Count(o => o.AgentId == -1)) + "," + // p2 tie %
                    outcomes.Count(o => o.Winner == o.BenchmarkId && o.AgentId == -1) + "," + // p2 losses
                    Pct(outcomes.Count(o => o.Winner == o.BenchmarkId && o.AgentId == -1), outcomes.Count(o => o.AgentId == -1)) + "," + // p2 loss %

                    string.Format("{0:N2},", outcomes.Average(o => o.AverageTurnTime)) +
                    string.Format("{0:N2}", outcomes.Average(o => o.TotalMoves))
                    );
            }
            Console.WriteLine("Done!");
        }