public MoveOnlyAlphaBetaWithPreplacerMoveMaker(int maxSearchDepth, IUtilityCalculator calculator, PreplacerStrategy preplacer) { _thisThreadPool = _pool.Value; _maxSearchDepth = maxSearchDepth; _calculator = calculator; _preplacer = preplacer; }
public MoveOnlyMonteCarloTreeSearchWithPreplacerMoveMaker(int iterations, IMoveDecisionMaker rolloutMoveMaker, PreplacerStrategy preplacer) : base(iterations, rolloutMoveMaker) { _preplacer = preplacer; }
static void TestFullAi() { var aiToTest = new Func <PlayerDecisionMaker>[] { //() => new PlayerDecisionMaker(new GreedyCardValueUtilityMoveMaker(-1), PlacementMaker.TightDoublerInstance), //() => new PlayerDecisionMaker(new GreedyCardValueUtilityMoveMaker(0), PlacementMaker.TightDoublerInstance), //() => new PlayerDecisionMaker(new GreedyCardValueUtilityMoveMaker(1), PlacementMaker.TightDoublerInstance), //() => new PlayerDecisionMaker(new GreedyCardValueUtilityMoveMaker(2), PlacementMaker.TightDoublerInstance), // //() => new PlayerDecisionMaker(new GreedyCardValueUtilityMoveMaker(-1), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), //() => new PlayerDecisionMaker(new GreedyCardValueUtilityMoveMaker(0), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), //() => new PlayerDecisionMaker(new GreedyCardValueUtilityMoveMaker(1), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), //() => new PlayerDecisionMaker(new GreedyCardValueUtilityMoveMaker(2), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), //() => new PlayerDecisionMaker(new QuickRandomSearchMoveMaker(6, 1000), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), //() => new PlayerDecisionMaker(new QuickRandomSearchMoveMaker(10, 2000), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), //() => new PlayerDecisionMaker(new QuickRandomSearchMoveMaker(20, 5000), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), //() => new PlayerDecisionMaker(new QuickRandomSearchMoveMaker(30, 10000), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), //() => new PlayerDecisionMaker(new MoveOnlyMonteCarloTreeSearchMoveMaker(1000, TuneableUtilityMoveMaker.Tuning1), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), () => new PlayerDecisionMaker(new MoveOnlyMonteCarloTreeSearchMoveMaker(10000, TuneableUtilityMoveMaker.Tuning1), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), () => new PlayerDecisionMaker(new MoveOnlyMonteCarloTreeSearchMoveMaker(10000, TuneableUtilityMoveMaker.Tuning1), new PlacementMaker(new BestEvaluatorStrategy(TuneablePattern2x2BoardEvaluator.Tuning1))), () => { var p = new PreplacerStrategy(new EvaluatorTreeSearchPreplacer(TuneablePattern2x2BoardEvaluator.Tuning1, 4, 4, true)); var m = new MoveOnlyMonteCarloTreeSearchWithPreplacerMoveMaker(10000, TuneableUtilityMoveMaker.Tuning1, p); return(new PlayerDecisionMaker(m, new PlacementMaker(p))); }, () => new PlayerDecisionMaker(new AlphaBetaMoveMaker(15, TuneableByBoardPositionUtilityCalculator.Tuning1), PlacementMaker.ExhaustiveMostFuturePlacementsInstance1_6), () => new PlayerDecisionMaker(new AlphaBetaMoveMaker(15, TuneableByBoardPositionUtilityCalculator.Tuning1), new PlacementMaker(new BestEvaluatorStrategy(TuneablePattern2x2BoardEvaluator.Tuning1))), // //() => //{ // var p = new PreplacerStrategy(new EvaluatorTreeSearchPreplacer(new Pattern2x2BoardEvaluator(), 4, 4, true)); // var m = new MoveOnlyMinimaxWithAlphaBetaPruningWithPreplacerMoveMaker(13, TuneableByBoardPositionUtilityCalculator.Tuning1, p); // return new PlayerDecisionMaker(m, new PlacementMaker(p)); //}, () => { var p = new PreplacerStrategy(new EvaluatorTreeSearchPreplacer(TuneablePattern2x2BoardEvaluator.Tuning1, 4, 4, true)); var m = new MoveOnlyAlphaBetaWithPreplacerMoveMaker(15, TuneableByBoardPositionUtilityCalculator.Tuning1, p); return(new PlayerDecisionMaker(m, new PlacementMaker(p))); }, }; const int TotalRuns = 500; const bool enableConsoleLogging = false; //TODO: Play each AI against each other AI 100 times and print a table of results var totalWins = new int[aiToTest.Length, aiToTest.Length]; var totalTimeTaken = new long[aiToTest.Length]; Console.WriteLine($"Running {aiToTest.Length * (aiToTest.Length - 1) / 2} * {TotalRuns} Games"); int gameNumber = 0; for (var a = 0; a < aiToTest.Length; a++) { for (var b = a; b < aiToTest.Length; b++) { if (a == b) { continue; } long aiATime = 0; long aiBTime = 0; var aiA = aiToTest[a]; var aiB = aiToTest[b]; Console.WriteLine($"{++gameNumber} {aiA().Name} vs {aiB().Name}"); Parallel.For(0, TotalRuns, new ParallelOptions { MaxDegreeOfParallelism = 6 }, (run) => //for (var run = 0; run < TotalRuns; run++) { var state = new SimulationState(SimulationHelpers.GetRandomPieces(run / 2), 0); ConsoleLogger logger = null; if (enableConsoleLogging) { state.Logger = logger = new ConsoleLogger(state); } //state.Fidelity = SimulationFidelity.NoPiecePlacing; //Let each Ai have half of the goes first and half second var runner = new SimulationRunner(state, run % 2 == 0 ? aiA() : aiB(), run % 2 == 1 ? aiA() : aiB()); while (!state.GameHasEnded) { runner.PerformNextStep(); if (logger != null) { logger.PrintBoards(true); } } var aWin = run % 2 == state.WinningPlayer; lock (totalWins) { if (aWin) { totalWins[a, b]++; } else { totalWins[b, a]++; } } Interlocked.Add(ref aiATime, runner.Stopwatches[run % 2].ElapsedMilliseconds); Interlocked.Add(ref aiBTime, runner.Stopwatches[(run + 1) % 2].ElapsedMilliseconds); //Console.WriteLine(aWin); } ); Console.WriteLine(totalWins[a, b]); totalTimeTaken[a] += aiATime; totalTimeTaken[b] += aiBTime; } } //Calculate the total points for each var total = Enumerable.Range(0, aiToTest.Length).Select(ai => Enumerable.Range(0, aiToTest.Length).Sum(opponent => totalWins[ai, opponent])).ToArray(); //Dump a CSV with the results var filename = "result_" + DateTimeOffset.Now.Ticks + ".csv"; var res = new List <string>(); res.Add("," + string.Join(", ", aiToTest.Select(ai => ai().Name)) + ",Win%,Rank,Total Time"); for (var a = 0; a < aiToTest.Length; a++) { var line = aiToTest[a]().Name; for (var b = 0; b < aiToTest.Length; b++) { if (a == b) { line += ","; } else { line += "," + totalWins[a, b]; } } //Win%, rank, time line += "," + (total[a] / (float)(aiToTest.Length - 1) * 100 / TotalRuns).ToString("0.0"); line += "," + (aiToTest.Length - total.Count(c => c < total[a])); line += "," + totalTimeTaken[a]; res.Add(line); } Console.WriteLine("Saving results as " + filename); File.WriteAllLines(filename, res); Process.Start(filename); }