private static void TimeOneMove(IAlgorithmInterface uct, Board board) { var stopwatch = Stopwatch.StartNew(); var move = uct.SelectMove(board); stopwatch.Stop(); board.PutToken(move); Console.WriteLine($"Selected move {move}"); Console.WriteLine($"Time UCT {stopwatch.ElapsedMilliseconds / (double)1000}"); }
private static void PlayGame(Dictionary <string, int> ResultDict, Dictionary <string, int> UsedMovesSum, IAlgorithmInterface firstPlayer, IAlgorithmInterface secondPlayer, int k) { if (firstPlayer is AlphaBeta st) { st.IsFirstPlayer = true; } if (secondPlayer is AlphaBeta nd) { nd.IsFirstPlayer = false; } firstPlayer.SetSeed(k); secondPlayer.SetSeed(k); var board = new Board(); var activePlayer = firstPlayer; while (board.Result == Result.None) { board.PutToken(activePlayer.SelectMove(board)); if (activePlayer == firstPlayer) { activePlayer = secondPlayer; } else { activePlayer = firstPlayer; } } if (board.Result == Result.Draw) { } else { if (board.Result == Result.FirstWon) { ResultDict[firstPlayer.ToString() + " vs " + secondPlayer.ToString()]++; UsedMovesSum[firstPlayer.ToString() + " vs " + secondPlayer.ToString()] += board.MovesByWinner(); } else { ResultDict[secondPlayer.ToString() + " vs " + firstPlayer.ToString()]++; UsedMovesSum[secondPlayer.ToString() + " vs " + firstPlayer.ToString()] += board.MovesByWinner(); } } }
private void StartButton_Click(object sender, EventArgs e) { ab = new AlphaBeta(); uct = new UCT(Math.Sqrt(2), 123, 100000); puct = new PUCT(Math.Sqrt(2), 123, 100000); ucb1tuned = new UCB1TUNED(1, 123, 100000); if (playerRB.Checked) { vs = VS.Player; } else if (abRB.Checked) { vs = VS.AlphaBeta; } else { vs = VS.MCTS; } if (puctRB.Checked) { algorithm = puct; } if (uctRB.Checked) { algorithm = uct; } if (tunedRB.Checked) { algorithm = ucb1tuned; } yourTurn = youStartBox.Checked; board = new Board(); firstPlayer = play = start = true; MainPanel.Invalidate(); if (!yourTurn) { PlayMove(); CheckResult(); firstPlayer = !firstPlayer; MainPanel.Invalidate(); } }
static void TestAlgorithms() { var rollouts = 16000; var gameCount = 100; var algorithms = new List <IAlgorithmInterface>() { new PUCT(1, 0, rollouts), new PUCT(2, 0, rollouts), new PUCT(3, 0, rollouts), new PUCT(4, 0, rollouts), new PUCT(5, 0, rollouts), new PUCT(6, 0, rollouts), //new PUCT(Math.Sqrt(2), 0, rollouts), new UCB1TUNED(1, 0, rollouts), //new AlphaBeta() }; var ResultDictA = new Dictionary <string, int>(); var ResultDictB = new Dictionary <string, int>(); var UsedMovesSumA = new Dictionary <string, int>(); var UsedMovesSumB = new Dictionary <string, int>(); for (int i = 0; i < algorithms.Count - 1; i++) { for (int j = algorithms.Count - 1; j < algorithms.Count; j++) { //A vs B, A is first player ResultDictA[algorithms[i].ToString() + " vs " + algorithms[j].ToString()] = 0; ResultDictA[algorithms[j].ToString() + " vs " + algorithms[i].ToString()] = 0; UsedMovesSumA[algorithms[i].ToString() + " vs " + algorithms[j].ToString()] = 0; UsedMovesSumA[algorithms[j].ToString() + " vs " + algorithms[i].ToString()] = 0; ResultDictB[algorithms[i].ToString() + " vs " + algorithms[j].ToString()] = 0; ResultDictB[algorithms[j].ToString() + " vs " + algorithms[i].ToString()] = 0; UsedMovesSumB[algorithms[i].ToString() + " vs " + algorithms[j].ToString()] = 0; UsedMovesSumB[algorithms[j].ToString() + " vs " + algorithms[i].ToString()] = 0; IAlgorithmInterface firstPlayer = algorithms[i]; IAlgorithmInterface secondPlayer = algorithms[j]; for (int k = 0; k < gameCount; k++) { if (k < gameCount / 2) { PlayGame(ResultDictA, UsedMovesSumA, firstPlayer, secondPlayer, k); } else { PlayGame(ResultDictB, UsedMovesSumB, secondPlayer, firstPlayer, k); } Console.Write("."); } Console.WriteLine(); Console.WriteLine($"Progess: {i} vs {j} done"); } } var csvA = string.Join( Environment.NewLine, ResultDictA.Select(d => $"{d.Key};{d.Value};{UsedMovesSumA[d.Key]};") ); System.IO.File.WriteAllText("./resultA.csv", csvA); var csvB = string.Join( Environment.NewLine, ResultDictB.Select(d => $"{d.Key};{d.Value};{UsedMovesSumB[d.Key]};") ); System.IO.File.WriteAllText("./resultB.csv", csvB); }