static double Evaluate(IAgent model, IAgent benchmark, GridGameParameters _params) { double score = 0; Console.WriteLine("Starting games as player 1.."); for (int i = 0; i < _params.MatchesPerOpponent; i++) { var game = _params.GameFunction(model, benchmark); game.PlayToEnd(); if (game.Winner == 1) score += _params.WinReward; else if (game.Winner == 0) score += _params.TieReward; else score += _params.LossReward; } Console.WriteLine("Starting games as player 2.."); for (int i = 0; i < _params.MatchesPerOpponent; i++) { var game = _params.GameFunction(benchmark, model); game.PlayToEnd(); if (game.Winner == -1) score += _params.WinReward; else if (game.Winner == 0) score += _params.TieReward; else score += _params.LossReward; } Console.WriteLine("Done!"); return score; }
public double Evaluate(int[,] board, GridGameParameters ggp, int player) { // ANN should map from board squares to a single value Debug.Assert(Brain.InputCount == board.Length); Debug.Assert(Brain.OutputCount == 1); // Clear the network Brain.ResetState(); // Console.WriteLine("player: {0}", player); // Set the board state as the inputs for (int i = 0; i < board.GetLength(0); i++) for (int j = 0; j < board.GetLength(1); j++) { //Console.WriteLine("[{0},{1}]={2}", pieceX, pieceY, board[pieceX, pieceY].toBoardSensor(player)); Brain.InputSignalArray[i * board.GetLength(0) + j] = board[i, j].toBoardSensor(player); } // Activate the network Brain.Activate(); //Console.WriteLine("{0}: {1}", AgentId, Brain.OutputSignalArray[0]); // Return the value of the board return Brain.OutputSignalArray[0] * _params.WinReward; }
public MctsNeatAgent(int id, CheckGameOver check, GetValidNextMoves valid, IBlackBox brain, ApplyMove applyMove, GridGameParameters parameters) : base(id, check, valid, applyMove, parameters) { Brain = brain; }
public BlondieAgent(int id, MinimaxAgent.CheckGameOver check, MinimaxAgent.GetValidNextMoves valid, IBlackBox brain, MinimaxAgent.ApplyMove apply, GridGameParameters parameters) : base(id) { Brain = brain; _params = parameters; _minimax = new MinimaxAgent(id, check, valid, Evaluate, apply, parameters); }
public MinimaxAgent(int id, CheckGameOver check, GetValidNextMoves valid, BoardEval eval, ApplyMove apply, GridGameParameters parameters) : base(id) { Debug.Assert(parameters != null); _checkGameOver = check; _validNextMoves = valid; _boardEval = eval; _applyMove = apply; _params = parameters; }
public MctsAgent(int id, CheckGameOver check, GetValidNextMoves valid, ApplyMove applyMove, GridGameParameters parameters, bool benchmarkAgent = false) : base(id) { Debug.Assert(parameters != null); _checkGameOver = check; _validNextMoves = valid; _applyMove = applyMove; _params = parameters; _random = new Random(); BenchmarkAgent = benchmarkAgent; mcTrials = benchmarkAgent ? _params.EvaluatorMonteCarloTrials : _params.MonteCarloTrials; minTrialsPerMove = benchmarkAgent ? _params.EvaluatorMinMcTrialsPerMove : _params.MinMcTrialsPerMove; uctConst = benchmarkAgent ? _params.EvaluatorUctConst : _params.UctConst; }
public GridGameExperiment(GridGameParameters parameters) { Parameters = parameters; initialize(); }
public static GridGameParameters DefaultParameters(string name) { GridGameParameters gg = new GridGameParameters() { Name = name, Description = "", Game = "tictactoe", Inputs = 9, Outputs = 9, Evaluator = "random", WinReward = 2, TieReward = 1, LossReward = 0, Generations = 500, PopulationSize = 100, Species = 10, SocialAgents = false, LamarckianEvolution = false, Subcultures = 10, GenerationsPerMemoryIncrement = 20, MaxMemorySize = 0, ExperimentPath = "../../../experiments/tictactoe/random/", BlondieAgents = false, MinimaxDepth = 9, OpponentPath = null, MatchesPerOpponent = 1, MonteCarloTrials = 1000, MinMcTrialsPerMove = 1, UctConst = 0.5, HyperNeat = false, MctsNeat = false, RoundRobinOpponents = 100 }; gg.ResultsPath = gg.ExperimentPath + gg.Name + "_results.csv"; gg.ConfigPath = gg.ExperimentPath + gg.Name + "_config.xml"; gg.ChampionPath = gg.ExperimentPath + gg.Name + "_gen{0}_champion.xml"; gg.BenchmarkResultsPath = gg.ExperimentPath + gg.Name + "_benchmark.csv"; gg.BenchmarkGameLogPath = gg.ExperimentPath + gg.Name + "_games.txt"; return gg; }
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!"); }