From 698d9ec96d84f9d131ea1772a51fe51a72fb934f Mon Sep 17 00:00:00 2001 From: Sandor Van Wassenhove Date: Fri, 13 Nov 2015 22:35:17 -0800 Subject: [PATCH] MCST and AIService preserve GameResult type --- .../com/github/sandorw/mocabogaso/ai/AIService.java | 4 ++-- .../mocabogaso/ai/mcts/MonteCarloSearchService.java | 10 +++++----- .../mocabogaso/ai/mcts/MonteCarloSearchTree.java | 11 ++++++----- .../ai/mcts/amaf/AMAFMonteCarloSearchService.java | 10 +++++----- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/github/sandorw/mocabogaso/ai/AIService.java b/src/main/java/com/github/sandorw/mocabogaso/ai/AIService.java index 2b7c063..066d474 100644 --- a/src/main/java/com/github/sandorw/mocabogaso/ai/AIService.java +++ b/src/main/java/com/github/sandorw/mocabogaso/ai/AIService.java @@ -12,10 +12,10 @@ */ public interface AIService { - > void searchMoves(GS currentGameState, int allottedTimeMs); + > void searchMoves(GS currentGameState, int allottedTimeMs); GM selectMove(); - void applyMove(GM move, GameState resultingGameState); + > void applyMove(GM move, GS resultingGameState); } diff --git a/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/MonteCarloSearchService.java b/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/MonteCarloSearchService.java index 94adb2c..d3a9b9b 100644 --- a/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/MonteCarloSearchService.java +++ b/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/MonteCarloSearchService.java @@ -23,7 +23,7 @@ public final class MonteCarloSearchService nodeResultsService; - public > + public > MonteCarloSearchService(NodeResultsService nodeResultsService, PlayoutPolicy policy, GS initialGameState) { this.nodeResultsService = nodeResultsService; playoutPolicy = policy; @@ -39,7 +39,7 @@ public void setExplorationConstant(float explorationConstant) { } @Override - public > + public > void searchMoves(GS currentGameState, int allottedTimeMs) { int numSimulations = 0; long timeout = System.currentTimeMillis() + (long)allottedTimeMs; @@ -52,7 +52,7 @@ void searchMoves(GS currentGameState, int allottedTimeMs) { logMoveChoices(currentGameState); } - private > void performPlayoutSimulation(GS playoutGameState) { + private > void performPlayoutSimulation(GS playoutGameState) { SearchTreeIterator iterator = searchTree.iterator(); while (iterator.hasNext()) { String currentPlayerName = playoutGameState.getNextPlayerName(); @@ -74,11 +74,11 @@ public GM selectMove() { } @Override - public void applyMove(GM move, GameState resultingGameState) { + public > void applyMove(GM move, GS resultingGameState) { searchTree.advanceTree(move, resultingGameState); } - public void logMoveChoices(GameState rootGameState) { + public > void logMoveChoices(GS rootGameState) { String evaluatingPlayerName = rootGameState.getNextPlayerName(); LOGGER.debug("Top level moves considered by the AIService from {}'s perspective:", evaluatingPlayerName); SearchTreeIterator iterator = searchTree.iterator(); diff --git a/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/MonteCarloSearchTree.java b/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/MonteCarloSearchTree.java index ec5e9a7..5b9da7c 100644 --- a/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/MonteCarloSearchTree.java +++ b/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/MonteCarloSearchTree.java @@ -25,7 +25,7 @@ public final class MonteCarloSearchTree nodeResultsService; private final Map transpositionTable; - public > + public > MonteCarloSearchTree(NodeResultsService nrService, GS initialGameState) { rootNode = null; nodeResultsService = nrService; @@ -39,7 +39,8 @@ public GM getMostSimulatedMove() { return rootNode.getMostSimulatedChildMove(); } - public synchronized > void advanceTree(GM move, GS resultingGameState) { + public synchronized > + void advanceTree(GM move, GS resultingGameState) { SearchTreeNode newRoot = rootNode.findNodeWithMove(move); if (newRoot == null) { long zobristHash = resultingGameState.getZobristHash(); @@ -76,7 +77,7 @@ private final class SearchTreeNode { private final List> childNodes; private final NR nodeResults; - private > + private > SearchTreeNode(GM move, GS resultingGameState) { parentNodes = Lists.newArrayList(); expanded = false; @@ -105,7 +106,7 @@ private int getNumSimulations() { return nodeResults.getNumSimulations(); } - private synchronized > void expandNode(GS gameState) { + private synchronized > void expandNode(GS gameState) { if (!expanded && !gameState.isGameOver() && ((getNumSimulations() >= NODE_EXPAND_THRESHOLD) || (this == rootNode))) { for (GM move : gameState.getAllValidMoves()) { @@ -214,7 +215,7 @@ public GM advanceToNextExplorationNode(String evaluatingPlayerName) { return pair.getLeft(); } - public > void expandNode(GS gameState) { + public > void expandNode(GS gameState) { currentNode.expandNode(gameState); } diff --git a/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/amaf/AMAFMonteCarloSearchService.java b/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/amaf/AMAFMonteCarloSearchService.java index 9ac2fde..f47161f 100644 --- a/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/amaf/AMAFMonteCarloSearchService.java +++ b/src/main/java/com/github/sandorw/mocabogaso/ai/mcts/amaf/AMAFMonteCarloSearchService.java @@ -27,7 +27,7 @@ public final class AMAFMonteCarloSearchService nodeResultsService; - public > + public > AMAFMonteCarloSearchService(AMAFNodeResultsService nodeResultsService, PlayoutPolicy policy, GS initialGameState) { this.nodeResultsService = nodeResultsService; playoutPolicy = policy; @@ -43,7 +43,7 @@ public void setExplorationConstant(float explorationConstant) { } @Override - public > + public > void searchMoves(GS currentGameState, int allottedTimeMs) { int numSimulations = 0; long timeout = System.currentTimeMillis() + (long)allottedTimeMs; @@ -56,7 +56,7 @@ void searchMoves(GS currentGameState, int allottedTimeMs) { logMoveChoices(currentGameState); } - private > void performPlayoutSimulation(GS playoutGameState) { + private > void performPlayoutSimulation(GS playoutGameState) { SearchTreeIterator iterator = searchTree.iterator(); while (iterator.hasNext()) { String currentPlayerName = playoutGameState.getNextPlayerName(); @@ -80,11 +80,11 @@ public GM selectMove() { } @Override - public void applyMove(GM move, GameState resultingGameState) { + public > void applyMove(GM move, GS resultingGameState) { searchTree.advanceTree(move, resultingGameState); } - public void logMoveChoices(GameState rootGameState) { + public > void logMoveChoices(GS rootGameState) { String evaluatingPlayerName = rootGameState.getNextPlayerName(); LOGGER.debug("Top level moves considered by the AIService from {}'s perspective:", evaluatingPlayerName); SearchTreeIterator iterator = searchTree.iterator();