/// <summary> /// Wykonujemy symulacje /// </summary> /// <returns></returns> private double Rollout(MctsNode node, int numberOfLastKingMovesWithoutBeat) { int result = 0; CheckersBoard board = node.Board; PieceColor color = node.Color; while (true) { var moves = board.GetAllPossibleMoves(color); if (moves.Count > 0) { var move = moves[RandomGenerator.Next(0, moves.Count - 1)]; board = board.GetBoardAfterMove(move); if (move.OldPiece.IsKing && (move.BeatedPieces == null || move.BeatedPieces.Count == 0)) { numberOfLastKingMovesWithoutBeat++; } else { numberOfLastKingMovesWithoutBeat = 0; } if (numberOfLastKingMovesWithoutBeat > 49) { return(0.5); } } else { result = color == PieceColor.Black ? 1 : -1; break; } color = color == PieceColor.Black ? PieceColor.White : PieceColor.Black; } return(result); }
public MctsTree(int numberOfIterations, double uctParameter, Random generator, CheckersBoard board, PieceColor color) { NumberOfTotalSimulations = 0; NumberOfIterations = numberOfIterations; UctParameter = uctParameter; RandomGenerator = generator; Root = new MctsNode(color, board, null); }
public MctsNode(PieceColor color, CheckersBoard board, MctsNode parent, Move move) { Move = move; Color = color; Board = board; Parent = parent; NumberOfSimulations = 0; NumberOfWins = 0; }
/// <summary> /// Dodajemy do liścia nowe gałęzie odpowiadające kolejnym ruchom z danego stanu /// </summary> /// <param name="node"></param> private void Expand(MctsNode node) { var moves = node.Board.GetAllPossibleMoves(node.Color); if (moves.Count > 0) { var children = new List <MctsNode>(); foreach (var move in moves) { var board = node.Board.GetBoardAfterMove(move); var color = node.Color == PieceColor.White ? PieceColor.Black : PieceColor.White; children.Add(new MctsNode(color, board, node, move)); } node.Children = children; } }
/// <summary> /// Wrzucamy wyniki wyżej /// </summary> private void Backpropagate(double result, MctsNode node) { while (node != null) { node.NumberOfSimulations++; if (result == 0.5) { node.NumberOfWins += 0.5; } if (result == -1 && node.Color == PieceColor.White) { node.NumberOfWins++; } if (result == 1 && node.Color == PieceColor.Black) { node.NumberOfWins++; } node = node.Parent; } }