Vector3 PlayerMove() { Vector3 action_ = Vector3.zero; if (select_algorithm == 1) { Minimax action = new Minimax(this, depth); action_ = action.GetAction(); } else if (select_algorithm == 2) { AlphaBeta action = new AlphaBeta(this, depth); action_ = action.GetAction(); } else if (select_algorithm == 3) { Expectimax action = new Expectimax(this, depth); action_ = action.GetAction(); } return(action_); }
// Runs a number of games with the given AI agents private static void RunMultipleTests(TYPE AItype) { if (AItype == TYPE.MINIMAX || AItype == TYPE.MINIMAX_PARALLEL_ITERATIVE_DEEPENING || AItype == TYPE.MINIMAX_ITERATIVE_DEEPENING) { int runs = GetRuns(); int depth = 0; int timeLimit = 0; if (AItype == TYPE.MINIMAX) { depth = GetDepth(); } else { timeLimit = GetChoice("Time limit?"); } StreamWriter writer = new StreamWriter(MINIMAX_LOG_FILE_NAME, true); Dictionary<int, int> highCardCount = new Dictionary<int, int>() { { 192, 0 }, { 384, 0 }, { 768, 0 }, { 1536, 0 }, { 3072, 0 }, { 6144, 0 } }; for (int i = 0; i < runs; i++) { GameEngine game = new GameEngine(); Minimax minimax = new Minimax(game, depth); var watch = Stopwatch.StartNew(); State endState = null; if (AItype == TYPE.MINIMAX) endState = minimax.Run(false); else if (AItype == TYPE.MINIMAX_PARALLEL_ITERATIVE_DEEPENING) endState = minimax.RunParallelIterativeDeepening(false, timeLimit); else if (AItype == TYPE.MINIMAX_ITERATIVE_DEEPENING) endState = minimax.RunIterativeDeepening(false, timeLimit); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; int highestTile = BoardHelper.GetHighestCard(endState.Grid); int score = game.CalculateFinalScore(); String stats = i + "\t" + depth + "\t" + highestTile + "\t" + score + "\t" + elapsedMs; Console.WriteLine(stats); writer.WriteLine(stats); List<int> keys = new List<int>(highCardCount.Keys); for (int j = 0; j < keys.Count; j++) { if (highestTile >= keys[j]) highCardCount[keys[j]]++; } } writer.Close(); Console.WriteLine(GetStatistics(highCardCount, runs)); } else if (AItype == TYPE.EXPECTIMAX_CLASSIC || AItype == TYPE.EXPECTIMAX_PARALLEL || AItype == TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING || AItype == TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { int runs = GetRuns(); int depth = 0; int timeLimit = 0; if (AItype != TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING && AItype != TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { depth = GetDepth(); } else { timeLimit = GetChoice("Time limit?"); } StreamWriter writer = new StreamWriter(EXPECTIMAX_LOG_FILE_NAME, true); Dictionary<int, int> highCardCount = new Dictionary<int, int>() { { 192, 0 }, { 384, 0 }, { 768, 0 }, { 1536, 0 }, { 3072, 0 }, { 6144, 0 } }; for (int i = 0; i < runs; i++) { GameEngine game = new GameEngine(); Expectimax expectimax = new Expectimax(game, depth); var watch = Stopwatch.StartNew(); State endState = null; if (AItype == TYPE.EXPECTIMAX_CLASSIC) { endState = expectimax.Run(false); } else if (AItype == TYPE.EXPECTIMAX_PARALLEL) { endState = expectimax.RunParallelExpectimax(false); } else if (AItype == TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING) { endState = expectimax.RunParallelIterativeDeepeningExpectimax(false, timeLimit); } else if (AItype == TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { endState = expectimax.RunIterativeDeepening(false, timeLimit); } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; int highestTile = BoardHelper.GetHighestCard(endState.Grid); int score = game.CalculateFinalScore(); String stats = i + "\t" + depth + "\t" + highestTile + "\t" + score + "\t" + elapsedMs; Console.WriteLine(stats); writer.WriteLine(stats); List<int> keys = new List<int>(highCardCount.Keys); for (int j = 0; j < keys.Count; j++) { if (highestTile >= keys[j]) highCardCount[keys[j]]++; } } writer.Close(); Console.WriteLine(GetStatistics(highCardCount, runs)); } else if (AItype == TYPE.MCTS_CLASSIC || AItype == TYPE.MCTS_PARALLEL) { int runs = GetRuns(); int timeLimit = GetChoice("Time limit?"); StreamWriter writer = new StreamWriter(MCTS_LOG_FILE_NAME, true); Dictionary<int, int> highCardCount = new Dictionary<int, int>() { { 192, 0 }, { 384, 0 }, { 768, 0 }, { 1536, 0 }, { 3072, 0 }, { 6144, 0 } }; for (int i = 0; i < runs; i++) { GameEngine game = new GameEngine(); MCTS mcts = new MCTS(game); var watch = Stopwatch.StartNew(); State endState = null; if (AItype == TYPE.MCTS_CLASSIC) { endState = mcts.RunTimeLimitedMCTS(false, timeLimit); } else if (AItype == TYPE.MCTS_PARALLEL) { endState = mcts.RunParallelTimeLimitedMCTS(false, timeLimit); } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; int highestTile = BoardHelper.GetHighestCard(endState.Grid); int score = game.CalculateFinalScore(); String stats = i + "\t" + highestTile + "\t" + score + "\t" + elapsedMs; Console.WriteLine(stats); writer.WriteLine(stats); List<int> keys = new List<int>(highCardCount.Keys); for (int j = 0; j < keys.Count; j++) { if (highestTile >= keys[j]) highCardCount[keys[j]]++; } } writer.Close(); Console.WriteLine(GetStatistics(highCardCount, runs)); } }
// Runs expectimax searches to test the weights of the chromosome private double TestWeights() { double total = 0; ConcurrentBag<double> subTotals = new ConcurrentBag<double>(); Parallel.For(0, NUM_THREADS, j => { double subtotal = 0; for (int i = 0; i < NUM_TESTS / NUM_THREADS; i++) { GameEngine gameEngine = new GameEngine(); Expectimax expectimax = new Expectimax(gameEngine, 2); State end = expectimax.RunStar1WithUnlikelyPruning(false, chromosome); double points = end.Points; subtotal += points; } subTotals.Add(subtotal); }); foreach (double sub in subTotals) { total += sub; } num_tests += NUM_TESTS; return total; }
// Runs a game by given AI showing it in the console private static void RunGraphicAIGame(TYPE AItype) { if (AItype == TYPE.MINIMAX || AItype == TYPE.MINIMAX_PARALLEL_ITERATIVE_DEEPENING || AItype == TYPE.MINIMAX_ITERATIVE_DEEPENING) { int depth = 0; int timeLimit = 0; GameEngine game = new GameEngine(); Minimax minimax = new Minimax(game, depth); if (AItype == TYPE.MINIMAX_PARALLEL_ITERATIVE_DEEPENING) { timeLimit = GetChoice("Time limit?"); minimax.RunParallelIterativeDeepening(true, timeLimit); } else if (AItype == TYPE.MINIMAX_ITERATIVE_DEEPENING) { timeLimit = GetChoice("Time limit?"); minimax.RunIterativeDeepening(true, timeLimit); } else { depth = GetDepth(); minimax.Run(true); } } else if (AItype == TYPE.EXPECTIMAX_CLASSIC || AItype == TYPE.EXPECTIMAX_PARALLEL || AItype == TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING || AItype == TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { int depth = 0; if (AItype != TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING) depth = GetDepth(); GameEngine game = new GameEngine(); Expectimax expectimax = new Expectimax(game, depth); if (AItype == TYPE.EXPECTIMAX_CLASSIC) { expectimax.Run(true); } else if (AItype == TYPE.EXPECTIMAX_PARALLEL) { int timeLimit = GetChoice("Time limit?"); expectimax.RunParallelExpectimax(true); } else if (AItype == TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING) { int timeLimit = GetChoice("Time limit?"); expectimax.RunParallelIterativeDeepeningExpectimax(true, timeLimit); } else if (AItype == TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { int timeLimit = GetChoice("Time limit?"); expectimax.RunIterativeDeepening(true, timeLimit); } } else if (AItype == TYPE.MCTS_CLASSIC || AItype == TYPE.MCTS_PARALLEL) { int timeLimit = GetChoice("Time limit?"); GameEngine game = new GameEngine(); MCTS mcts = new MCTS(game); if (AItype == TYPE.MCTS_CLASSIC) mcts.RunTimeLimitedMCTS(true, timeLimit); else if (AItype == TYPE.MCTS_PARALLEL) mcts.RunParallelTimeLimitedMCTS(true, timeLimit); } }