Skip to content

Commit

Permalink
MCST and AIService preserve GameResult type
Browse files Browse the repository at this point in the history
  • Loading branch information
sandorw committed Nov 14, 2015
1 parent d73cb9d commit 698d9ec
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/github/sandorw/mocabogaso/ai/AIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
*/
public interface AIService<GM extends GameMove> {

<GS extends GameState<GM, ? extends GameResult>> void searchMoves(GS currentGameState, int allottedTimeMs);
<GR extends GameResult, GS extends GameState<GM,GR>> void searchMoves(GS currentGameState, int allottedTimeMs);

GM selectMove();

void applyMove(GM move, GameState<GM, ? extends GameResult> resultingGameState);
<GR extends GameResult, GS extends GameState<GM,GR>> void applyMove(GM move, GS resultingGameState);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class MonteCarloSearchService<GM extends GameMove, NR extends NodeR
private final PlayoutPolicy playoutPolicy;
private final NodeResultsService<NR> nodeResultsService;

public <GS extends GameState<GM, ? extends GameResult>>
public <GR extends GameResult, GS extends GameState<GM,GR>>
MonteCarloSearchService(NodeResultsService<NR> nodeResultsService, PlayoutPolicy policy, GS initialGameState) {
this.nodeResultsService = nodeResultsService;
playoutPolicy = policy;
Expand All @@ -39,7 +39,7 @@ public void setExplorationConstant(float explorationConstant) {
}

@Override
public <GS extends GameState<GM, ? extends GameResult>>
public <GR extends GameResult, GS extends GameState<GM,GR>>
void searchMoves(GS currentGameState, int allottedTimeMs) {
int numSimulations = 0;
long timeout = System.currentTimeMillis() + (long)allottedTimeMs;
Expand All @@ -52,7 +52,7 @@ void searchMoves(GS currentGameState, int allottedTimeMs) {
logMoveChoices(currentGameState);
}

private <GS extends GameState<GM, ? extends GameResult>> void performPlayoutSimulation(GS playoutGameState) {
private <GR extends GameResult, GS extends GameState<GM,GR>> void performPlayoutSimulation(GS playoutGameState) {
SearchTreeIterator<GM,NR> iterator = searchTree.iterator();
while (iterator.hasNext()) {
String currentPlayerName = playoutGameState.getNextPlayerName();
Expand All @@ -74,11 +74,11 @@ public GM selectMove() {
}

@Override
public void applyMove(GM move, GameState<GM, ? extends GameResult> resultingGameState) {
public <GR extends GameResult, GS extends GameState<GM,GR>> void applyMove(GM move, GS resultingGameState) {
searchTree.advanceTree(move, resultingGameState);
}

public void logMoveChoices(GameState<GM, ? extends GameResult> rootGameState) {
public <GR extends GameResult, GS extends GameState<GM,GR>> void logMoveChoices(GS rootGameState) {
String evaluatingPlayerName = rootGameState.getNextPlayerName();
LOGGER.debug("Top level moves considered by the AIService from {}'s perspective:", evaluatingPlayerName);
SearchTreeIterator<GM,NR> iterator = searchTree.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class MonteCarloSearchTree<GM extends GameMove, NR extends NodeResu
private final NodeResultsService<NR> nodeResultsService;
private final Map<Long, SearchTreeNode> transpositionTable;

public <GS extends GameState<GM, ? extends GameResult>>
public <GR extends GameResult, GS extends GameState<GM,GR>>
MonteCarloSearchTree(NodeResultsService<NR> nrService, GS initialGameState) {
rootNode = null;
nodeResultsService = nrService;
Expand All @@ -39,7 +39,8 @@ public GM getMostSimulatedMove() {
return rootNode.getMostSimulatedChildMove();
}

public synchronized <GS extends GameState<GM, ? extends GameResult>> void advanceTree(GM move, GS resultingGameState) {
public synchronized <GR extends GameResult, GS extends GameState<GM,GR>>
void advanceTree(GM move, GS resultingGameState) {
SearchTreeNode newRoot = rootNode.findNodeWithMove(move);
if (newRoot == null) {
long zobristHash = resultingGameState.getZobristHash();
Expand Down Expand Up @@ -76,7 +77,7 @@ private final class SearchTreeNode {
private final List<Pair<GM,SearchTreeNode>> childNodes;
private final NR nodeResults;

private <GS extends GameState<GM, ? extends GameResult>>
private <GR extends GameResult, GS extends GameState<GM,GR>>
SearchTreeNode(GM move, GS resultingGameState) {
parentNodes = Lists.newArrayList();
expanded = false;
Expand Down Expand Up @@ -105,7 +106,7 @@ private int getNumSimulations() {
return nodeResults.getNumSimulations();
}

private synchronized <GS extends GameState<GM, ? extends GameResult>> void expandNode(GS gameState) {
private synchronized <GR extends GameResult, GS extends GameState<GM,GR>> void expandNode(GS gameState) {
if (!expanded && !gameState.isGameOver() &&
((getNumSimulations() >= NODE_EXPAND_THRESHOLD) || (this == rootNode))) {
for (GM move : gameState.getAllValidMoves()) {
Expand Down Expand Up @@ -214,7 +215,7 @@ public GM advanceToNextExplorationNode(String evaluatingPlayerName) {
return pair.getLeft();
}

public <GS extends GameState<GM, ? extends GameResult>> void expandNode(GS gameState) {
public <GR extends GameResult, GS extends GameState<GM,GR>> void expandNode(GS gameState) {
currentNode.expandNode(gameState);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class AMAFMonteCarloSearchService<GM extends GameMove, NR extends A
private final PlayoutPolicy playoutPolicy;
private final AMAFNodeResultsService<NR> nodeResultsService;

public <GS extends GameState<GM, ? extends GameResult>>
public <GR extends GameResult, GS extends GameState<GM,GR>>
AMAFMonteCarloSearchService(AMAFNodeResultsService<NR> nodeResultsService, PlayoutPolicy policy, GS initialGameState) {
this.nodeResultsService = nodeResultsService;
playoutPolicy = policy;
Expand All @@ -43,7 +43,7 @@ public void setExplorationConstant(float explorationConstant) {
}

@Override
public <GS extends GameState<GM, ? extends GameResult>>
public <GR extends GameResult, GS extends GameState<GM,GR>>
void searchMoves(GS currentGameState, int allottedTimeMs) {
int numSimulations = 0;
long timeout = System.currentTimeMillis() + (long)allottedTimeMs;
Expand All @@ -56,7 +56,7 @@ void searchMoves(GS currentGameState, int allottedTimeMs) {
logMoveChoices(currentGameState);
}

private <GS extends GameState<GM, ? extends GameResult>> void performPlayoutSimulation(GS playoutGameState) {
private <GR extends GameResult, GS extends GameState<GM,GR>> void performPlayoutSimulation(GS playoutGameState) {
SearchTreeIterator<GM,NR> iterator = searchTree.iterator();
while (iterator.hasNext()) {
String currentPlayerName = playoutGameState.getNextPlayerName();
Expand All @@ -80,11 +80,11 @@ public GM selectMove() {
}

@Override
public void applyMove(GM move, GameState<GM, ? extends GameResult> resultingGameState) {
public <GR extends GameResult, GS extends GameState<GM,GR>> void applyMove(GM move, GS resultingGameState) {
searchTree.advanceTree(move, resultingGameState);
}

public void logMoveChoices(GameState<GM, ? extends GameResult> rootGameState) {
public <GR extends GameResult, GS extends GameState<GM,GR>> void logMoveChoices(GS rootGameState) {
String evaluatingPlayerName = rootGameState.getNextPlayerName();
LOGGER.debug("Top level moves considered by the AIService from {}'s perspective:", evaluatingPlayerName);
SearchTreeIterator<GM,NR> iterator = searchTree.iterator();
Expand Down

0 comments on commit 698d9ec

Please sign in to comment.