//function used to perform MCTS public MonteCarloPosition StartMCTS(float _curPlayerHealth, float _curEnemyHealth, Node _objNode) { //updates the board to fit the current grid MonteCarloBoard _board = UpdateBoard(_curPlayerHealth, _curEnemyHealth, _objNode); //performs MCTS on the updated board _board = _mcts.findNextMove(_board); MonteCarloPosition _enemyPos = new MonteCarloPosition(); //sets the new enemy pos to the positions returned from MCTS _enemyPos = _board.FindPlayer(MonteCarloBoard._enemyVal); //returns the enemy position return(_enemyPos); }
//function used to perform MCTS and return the best node's board public MonteCarloBoard findNextMove(MonteCarloBoard _board) { //gathers a start and end time, limiting the amount of MCTS cycles for the enemy to a timer float _start = Time.realtimeSinceStartup * 100; float _end = _start + 5; //gets the root of the tree MonteCarloNode _rootNode = _tree.getRoot(); //sets the root node to the current board _rootNode.GetState().SetBoard(_board); //checks whether learning phase has been carried out if (!_tree.learningPhase) { //performs 1000 iterations of MCTS for (int l = 0; l < 1000; l++) { //function used to perform MCTS MCTSCycle(_rootNode); } //sets learning phase as true so that it does not occur again _tree.learningPhase = true; } else { //performs the MCTS for the limited amount of time, terminating on the last cycle once the time is reached. while (Time.realtimeSinceStartup * 100 < _end) { //function used to perform MCTS MCTSCycle(_rootNode); } } //finds the best child node of the root node MonteCarloNode _optimalNode = _rootNode.GetMaxChild(); //sets the root node to the new winner node _tree.setRoot(_optimalNode); //returns the board of the root's best child node return(_optimalNode.GetState().GetBoard()); }
//function used to update a board public MonteCarloBoard UpdateBoard(float _playerHealth, float _enemyHealth, Node _objNode) { MonteCarloBoard _board = new MonteCarloBoard(); //loops through all values of the board for (int z = 0; z < GridSystem._gridSizeZ; z++) { for (int x = 0; x < GridSystem._gridSizeX; x++) { //checks for the enemy node if (GridSystem._grid [x, z] == _objNode) { //sets value of current board x and z to enemy value _board.SetSpecificBoardValues(MonteCarloBoard._enemyVal, x, z); } //checks for the player node else if (GridSystem._grid [x, z]._playerNode == true) { //sets value of current board x and z to player value _board.SetSpecificBoardValues(MonteCarloBoard._playerVal, x, z); } //checks for unwalkable nodes else if (GridSystem._grid [x, z]._walkable == false) { //sets value of current board x and z to 10 setting the value as unwalkable _board.SetSpecificBoardValues(10, x, z); } else { //sets value of current board x and z to 0, making it empty _board.SetSpecificBoardValues(0, x, z); } } } //sets player and enemy health values _board._playerHealth = _playerHealth; _board._enemyHealth = _enemyHealth; _board._originalPlayerHealth = _playerHealth; _board._originalEnemyHealth = _enemyHealth; //returns the updated board return(_board); }
public void TestMoves(int numberOfSimulations, Board board, List <MonteCarloMove> moves, int amountOfTestedMoves) { Random random = new Random(); for (int i = 0; i < numberOfSimulations / 2; i++) { MonteCarloBoard boardForSimulation = new MonteCarloBoard(board); string rand = moves[random.Next(amountOfTestedMoves)].MoveCode; int score = boardForSimulation.StartNewGame(this.Name, rand); foreach (MonteCarloMove findMoveCode in moves) { if (findMoveCode.MoveCode == rand) { findMoveCode.wins += score; findMoveCode.trys++; } } } }
public MonteCarloBoard(MonteCarloBoard _board) { this._boardValues = new int[GridSystem._gridSizeX, GridSystem._gridSizeZ]; int[,] _boardValues = _board.getBoardValues(); this._movedPosition = _board._movedPosition; this._playerHealth = _board._playerHealth; this._enemyHealth = _board._enemyHealth; this._originalPlayerHealth = _board._originalPlayerHealth; this._originalEnemyHealth = _board._originalEnemyHealth; for (int x = 0; x < GridSystem._gridSizeX; x++) { for (int z = 0; z < GridSystem._gridSizeZ; z++) { this._boardValues [x, z] = _boardValues [x, z]; } } }
public void SetBoard(MonteCarloBoard _board) { this._board = _board; }
public MonteCarloState(MonteCarloBoard _board) { this._board = new MonteCarloBoard(_board); }
public MonteCarloState(MonteCarloState _state) { this._board = new MonteCarloBoard(_state.GetBoard()); this._visits = _state.GetVisits(); this._score = _state.GetScore(); }
//class constructors public MonteCarloState() { _board = new MonteCarloBoard(); }