Code cleanup; Initial implementation of root parallelization.
This commit is contained in:
@@ -9,7 +9,6 @@ public class Action {
|
||||
private char column = 'x';
|
||||
private int row = 0;
|
||||
|
||||
//private Player player;
|
||||
private String move;
|
||||
|
||||
public static final Action NONE = new Action("NONE", 'x', 0);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package net.woodyfolsom.msproj;
|
||||
|
||||
public class Command {
|
||||
public enum TYPE { boardsize, clear_board, final_status_list, genmove, INVALID, komi, list_commands, name, play, quit, version };
|
||||
public enum TYPE {
|
||||
boardsize, clear_board, final_status_list, genmove, INVALID, komi, list_commands, name, play, quit, version
|
||||
};
|
||||
|
||||
private String[] cmdFields;
|
||||
private String text;
|
||||
|
||||
@@ -6,8 +6,6 @@ public class CommandParser {
|
||||
private static final Logger LOGGER = Logger.getLogger(CommandParser.class
|
||||
.getName());
|
||||
|
||||
//private static final String INT_PATTERN = "^(\\d+.*|-\\d+.*)";
|
||||
|
||||
/**
|
||||
* This very simple parser converts a small subset of GTP command strings
|
||||
* into Commands.
|
||||
@@ -19,26 +17,31 @@ public class CommandParser {
|
||||
LOGGER.info("Parsing command: " + commandString);
|
||||
// split on whitespace
|
||||
if (commandString == null) {
|
||||
return new Command(Command.TYPE.INVALID,commandString,new String[0]);
|
||||
return new Command(Command.TYPE.INVALID, commandString,
|
||||
new String[0]);
|
||||
}
|
||||
|
||||
String cmdFields[] = commandString.split("\\s+");
|
||||
if (cmdFields.length == 0) {
|
||||
return new Command(Command.TYPE.INVALID,commandString,new String[0]);
|
||||
return new Command(Command.TYPE.INVALID, commandString,
|
||||
new String[0]);
|
||||
}
|
||||
|
||||
if ("boardsize".equals(cmdFields[0])) {
|
||||
return new Command(Command.TYPE.boardsize, commandString, cmdFields);
|
||||
} else if ("clear_board".equals(cmdFields[0])) {
|
||||
return new Command(Command.TYPE.clear_board,commandString,cmdFields);
|
||||
return new Command(Command.TYPE.clear_board, commandString,
|
||||
cmdFields);
|
||||
} else if ("final_status_list".equals(cmdFields[0])) {
|
||||
return new Command(Command.TYPE.final_status_list,commandString,cmdFields);
|
||||
return new Command(Command.TYPE.final_status_list, commandString,
|
||||
cmdFields);
|
||||
} else if ("genmove".equals(cmdFields[0])) {
|
||||
return new Command(Command.TYPE.genmove, commandString, cmdFields);
|
||||
} else if ("komi".equals(cmdFields[0])) {
|
||||
return new Command(Command.TYPE.komi, commandString, cmdFields);
|
||||
} else if ("list_commands".equals(cmdFields[0])) {
|
||||
return new Command(Command.TYPE.list_commands,commandString,cmdFields);
|
||||
return new Command(Command.TYPE.list_commands, commandString,
|
||||
cmdFields);
|
||||
} else if ("name".equals(cmdFields[0])) {
|
||||
return new Command(Command.TYPE.name, commandString, cmdFields);
|
||||
} else if ("play".equals(cmdFields[0])) {
|
||||
|
||||
@@ -253,7 +253,8 @@ public class GameBoard {
|
||||
|
||||
// TODO change boardHashHistory to stack
|
||||
public void popHashHistory() {
|
||||
boardHashHistory.remove(boardHashHistory.get(boardHashHistory.size() - 1));
|
||||
boardHashHistory
|
||||
.remove(boardHashHistory.get(boardHashHistory.size() - 1));
|
||||
}
|
||||
|
||||
public void pushHashHistory() {
|
||||
@@ -270,7 +271,9 @@ public class GameBoard {
|
||||
// TODO marked intersections should really be stored in
|
||||
// a separate array to ensure that the hash code is always in sync with
|
||||
// an actual or transitional board position
|
||||
if (oldSymbol == MARKED_GROUP || newSymbol == MARKED_GROUP || oldSymbol == MARKED_TERRITORY || newSymbol == MARKED_TERRITORY) {
|
||||
if (oldSymbol == MARKED_GROUP || newSymbol == MARKED_GROUP
|
||||
|| oldSymbol == MARKED_TERRITORY
|
||||
|| newSymbol == MARKED_TERRITORY) {
|
||||
board[index] = newSymbol;
|
||||
} else {
|
||||
int hashIndex = boardHashHistory.size() - 1;
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package net.woodyfolsom.msproj;
|
||||
|
||||
public class GameController {
|
||||
|
||||
}
|
||||
@@ -1,21 +1,25 @@
|
||||
package net.woodyfolsom.msproj;
|
||||
|
||||
|
||||
public class GameResult {
|
||||
public static final GameResult BLACK_BY_RESIGNATION = new GameResult(RESULT_TYPE.BLACK_BY_RESIGNATION);
|
||||
public static final GameResult BLACK_BY_RESIGNATION = new GameResult(
|
||||
RESULT_TYPE.BLACK_BY_RESIGNATION);
|
||||
public static final GameResult VOID = new GameResult(RESULT_TYPE.VOID);
|
||||
public static final GameResult WHITE_BY_RESIGNATION = new GameResult(RESULT_TYPE.WHITE_BY_RESIGNATION);
|
||||
public static final GameResult WHITE_BY_RESIGNATION = new GameResult(
|
||||
RESULT_TYPE.WHITE_BY_RESIGNATION);
|
||||
|
||||
private double komi;
|
||||
private int blackScore;
|
||||
private int whiteScore;
|
||||
private int normalizedZeroScore;
|
||||
|
||||
public enum RESULT_TYPE { BLACK_BY_RESIGNATION, WHITE_BY_RESIGNATION, IN_PROGRESS, SCORED, VOID}
|
||||
public enum RESULT_TYPE {
|
||||
BLACK_BY_RESIGNATION, WHITE_BY_RESIGNATION, IN_PROGRESS, SCORED, VOID
|
||||
}
|
||||
|
||||
private RESULT_TYPE resultType;
|
||||
|
||||
public GameResult(int blackScore, int whiteScore, int size, double komi, boolean finished) {
|
||||
public GameResult(int blackScore, int whiteScore, int size, double komi,
|
||||
boolean finished) {
|
||||
this.blackScore = blackScore;
|
||||
this.komi = komi;
|
||||
this.whiteScore = whiteScore;
|
||||
@@ -45,13 +49,16 @@ public class GameResult {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation for the game score as an integer. Lower numbers are better for white.
|
||||
* The minimum value is 0 (Chinese scoring - white owns every intersection on 19x19 and has 9 stone komi).
|
||||
* Likewise, the maximum value if 379x2 (black owns every intersection with zero komi).
|
||||
* Gets a representation for the game score as an integer. Lower numbers are
|
||||
* better for white. The minimum value is 0 (Chinese scoring - white owns
|
||||
* every intersection on 19x19 and has 9 stone komi). Likewise, the maximum
|
||||
* value if 379x2 (black owns every intersection with zero komi).
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getNormalizedScore() {
|
||||
return normalizedZeroScore + 2 * blackScore - ((int)(2 * (whiteScore + komi)));
|
||||
return normalizedZeroScore + 2 * blackScore
|
||||
- ((int) (2 * (whiteScore + komi)));
|
||||
}
|
||||
|
||||
public double getScore(Player color) {
|
||||
|
||||
@@ -55,7 +55,8 @@ public class GameState {
|
||||
|
||||
// This functionality duplicates isTerminal(),
|
||||
// however for the result to be reported, we need to know
|
||||
//how the game was terminated to differentiate between resignation, scored complete
|
||||
// how the game was terminated to differentiate between resignation,
|
||||
// scored complete
|
||||
// and (eventually) time out/forfeit.
|
||||
boolean isFinished = false;
|
||||
|
||||
@@ -75,14 +76,13 @@ public class GameState {
|
||||
}
|
||||
}
|
||||
|
||||
int blackScore = gameBoard.countSymbols(GameBoard.BLACK_STONE,GameBoard.BLACK_TERRITORY);
|
||||
int whiteScore = gameBoard.countSymbols(GameBoard.WHITE_STONE,GameBoard.WHITE_TERRITORY);
|
||||
int blackScore = gameBoard.countSymbols(GameBoard.BLACK_STONE,
|
||||
GameBoard.BLACK_TERRITORY);
|
||||
int whiteScore = gameBoard.countSymbols(GameBoard.WHITE_STONE,
|
||||
GameBoard.WHITE_TERRITORY);
|
||||
|
||||
return new GameResult( blackScore,
|
||||
whiteScore,
|
||||
markedBoard.getSize(),
|
||||
gameConfig.getKomi(),
|
||||
isFinished);
|
||||
return new GameResult(blackScore, whiteScore, markedBoard.getSize(),
|
||||
gameConfig.getKomi(), isFinished);
|
||||
}
|
||||
|
||||
public List<String> getEmptyCoords() {
|
||||
@@ -92,7 +92,8 @@ public class GameState {
|
||||
for (int rowIndex = 0; rowIndex < gameBoard.getSize(); rowIndex++) {
|
||||
if (GameBoard.EMPTY_INTERSECTION == gameBoard.getSymbolAt(
|
||||
colIndex, rowIndex))
|
||||
emptyCoords.add(GameBoard.getCoordinate(colIndex, rowIndex));
|
||||
emptyCoords
|
||||
.add(GameBoard.getCoordinate(colIndex, rowIndex));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +136,8 @@ public class GameState {
|
||||
*/
|
||||
public boolean playStone(Player player, Action action) {
|
||||
if (player != playerToMove) {
|
||||
throw new IllegalArgumentException("Requested " + player + " move, but it is " + playerToMove +"'s turn!");
|
||||
throw new IllegalArgumentException("Requested " + player
|
||||
+ " move, but it is " + playerToMove + "'s turn!");
|
||||
}
|
||||
|
||||
if (player == Player.NONE) {
|
||||
@@ -232,7 +234,8 @@ public class GameState {
|
||||
|
||||
// Moved test for 0 liberties until after attempting to capture
|
||||
// neighboring groups.
|
||||
// This will only happen if no neighboring groups were capture, hence there is nothing to undo.
|
||||
// This will only happen if no neighboring groups were capture, hence
|
||||
// there is nothing to undo.
|
||||
// So return now.
|
||||
if (0 == LibertyCounter.countLiberties(gameBoard, action.getColumn(),
|
||||
action.getRow(), stoneSymbol)) {
|
||||
@@ -251,7 +254,8 @@ public class GameState {
|
||||
gameBoard.setSymbolAt(i, opponentSymbol);
|
||||
}
|
||||
|
||||
//And finally, remove the originally played stone, which was never valid due to ko.
|
||||
// And finally, remove the originally played stone, which was never
|
||||
// valid due to ko.
|
||||
gameBoard.removeStone(action.getColumn(), action.getRow());
|
||||
|
||||
gameBoard.clearCaptureList();
|
||||
@@ -271,10 +275,13 @@ public class GameState {
|
||||
public boolean isTerminal() {
|
||||
int nMoves = moveHistory.size();
|
||||
if (nMoves < 2) {
|
||||
return false; //Impossible for a game to be over in 1 move unless the first player resigns
|
||||
//before the first player has played, the game is considered to be 'not over'
|
||||
return false; // Impossible for a game to be over in 1 move unless
|
||||
// the first player resigns
|
||||
// before the first player has played, the game is considered to be
|
||||
// 'not over'
|
||||
}
|
||||
return moveHistory.get(nMoves-1).isPass() && moveHistory.get(nMoves-2).isPass();
|
||||
return moveHistory.get(nMoves - 1).isPass()
|
||||
&& moveHistory.get(nMoves - 2).isPass();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
||||
@@ -13,8 +13,8 @@ import java.util.Properties;
|
||||
import net.woodyfolsom.msproj.Command.TYPE;
|
||||
import net.woodyfolsom.msproj.policy.AlphaBeta;
|
||||
import net.woodyfolsom.msproj.policy.Minimax;
|
||||
import net.woodyfolsom.msproj.policy.Policy;
|
||||
import net.woodyfolsom.msproj.policy.MonteCarloUCT;
|
||||
import net.woodyfolsom.msproj.policy.Policy;
|
||||
import net.woodyfolsom.msproj.policy.RandomMovePolicy;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package net.woodyfolsom.msproj;
|
||||
|
||||
public class LibertyCounter {
|
||||
public static int countLiberties(GameBoard gameBoard, char colLabel, int rowNum, char groupColor) {
|
||||
return countLiberties(gameBoard, GameBoard.getColumnIndex(colLabel),rowNum-1,groupColor,false);
|
||||
public static int countLiberties(GameBoard gameBoard, char colLabel,
|
||||
int rowNum, char groupColor) {
|
||||
return countLiberties(gameBoard, GameBoard.getColumnIndex(colLabel),
|
||||
rowNum - 1, groupColor, false);
|
||||
}
|
||||
|
||||
public static int countLiberties(GameBoard gameBoard, int col, int row, char groupColor, boolean markGroup) {
|
||||
public static int countLiberties(GameBoard gameBoard, int col, int row,
|
||||
char groupColor, boolean markGroup) {
|
||||
int liberties = markGroup(gameBoard, col, row, groupColor);
|
||||
|
||||
if (!markGroup) {
|
||||
@@ -15,7 +18,8 @@ public class LibertyCounter {
|
||||
return liberties;
|
||||
}
|
||||
|
||||
public static int markGroup(GameBoard gameBoard, int col, int row, char groupColor) {
|
||||
public static int markGroup(GameBoard gameBoard, int col, int row,
|
||||
char groupColor) {
|
||||
char stoneSymbol = gameBoard.getSymbolAt(col, row);
|
||||
if (stoneSymbol == GameBoard.EMPTY_INTERSECTION) {
|
||||
return 1; // one liberty
|
||||
@@ -41,13 +45,16 @@ public class LibertyCounter {
|
||||
return liberties;
|
||||
}
|
||||
|
||||
if (groupColor == GameBoard.WHITE_STONE && stoneSymbol == GameBoard.BLACK_STONE) {
|
||||
if (groupColor == GameBoard.WHITE_STONE
|
||||
&& stoneSymbol == GameBoard.BLACK_STONE) {
|
||||
return 0; // opposing stone - not a liberty
|
||||
}
|
||||
if (groupColor == GameBoard.BLACK_STONE && stoneSymbol == GameBoard.WHITE_STONE) {
|
||||
if (groupColor == GameBoard.BLACK_STONE
|
||||
&& stoneSymbol == GameBoard.WHITE_STONE) {
|
||||
return 0; // opposing stone - not a liberty
|
||||
}
|
||||
|
||||
throw new IllegalStateException("Cannot continue marking group - symbol at current location is not empty intersection, white stone, black stone or group marker");
|
||||
throw new IllegalStateException(
|
||||
"Cannot continue marking group - symbol at current location is not empty intersection, white stone, black stone or group marker");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package net.woodyfolsom.msproj;
|
||||
|
||||
public class Player {
|
||||
public static final Player BLACK = new Player("BLACK", GameBoard.BLACK_STONE);
|
||||
public static final Player NONE = new Player("NONE", GameBoard.EMPTY_INTERSECTION);
|
||||
public static final Player WHITE = new Player("WHITE", GameBoard.WHITE_STONE);
|
||||
public static final Player BLACK = new Player("BLACK",
|
||||
GameBoard.BLACK_STONE);
|
||||
public static final Player NONE = new Player("NONE",
|
||||
GameBoard.EMPTY_INTERSECTION);
|
||||
public static final Player WHITE = new Player("WHITE",
|
||||
GameBoard.WHITE_STONE);
|
||||
|
||||
private char stoneSymbol;
|
||||
private String name;
|
||||
|
||||
@@ -7,22 +7,31 @@ import net.woodyfolsom.msproj.policy.HumanKeyboardInput;
|
||||
import net.woodyfolsom.msproj.policy.MonteCarloUCT;
|
||||
import net.woodyfolsom.msproj.policy.Policy;
|
||||
import net.woodyfolsom.msproj.policy.RandomMovePolicy;
|
||||
import net.woodyfolsom.msproj.policy.RootParallelization;
|
||||
|
||||
public class StandAloneGame {
|
||||
enum PLAYER_TYPE { HUMAN, UCT_FAST, UCT_SLOW };
|
||||
enum PLAYER_TYPE {
|
||||
HUMAN, ROOT_PAR, UCT_FAST, UCT_SLOW
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length != 3) {
|
||||
System.out.println("Incorrect # of arguments: use StandAloneGame <Player1Type> <Player2Type> <# rounds>");
|
||||
System.out.println("For example to play 10 games against MC UCT w/ slow moves: StandAloneGame UCT_SLOW HUMAN 10");
|
||||
System.out
|
||||
.println("Incorrect # of arguments: use StandAloneGame <Player1Type> <Player2Type> <# rounds>");
|
||||
System.out
|
||||
.println("For example to play 10 games against MC UCT w/ slow moves: StandAloneGame UCT_SLOW HUMAN 10");
|
||||
}
|
||||
new StandAloneGame().playGame(parsePlayerType(args[0]), parsePlayerType(args[1]), Integer.valueOf(args[2]));
|
||||
new StandAloneGame().playGame(parsePlayerType(args[0]),
|
||||
parsePlayerType(args[1]), Integer.valueOf(args[2]));
|
||||
}
|
||||
|
||||
private static PLAYER_TYPE parsePlayerType(String playerTypeStr) {
|
||||
if ("UCT_FAST".equalsIgnoreCase(playerTypeStr)) {
|
||||
return PLAYER_TYPE.UCT_FAST;
|
||||
} else if ("UCT_SLOW".equalsIgnoreCase(playerTypeStr) || "UCT".equalsIgnoreCase(playerTypeStr)) {
|
||||
} else if ("ROOT_PAR".equalsIgnoreCase(playerTypeStr)) {
|
||||
return PLAYER_TYPE.ROOT_PAR;
|
||||
} else if ("UCT_SLOW".equalsIgnoreCase(playerTypeStr)
|
||||
|| "UCT".equalsIgnoreCase(playerTypeStr)) {
|
||||
return PLAYER_TYPE.UCT_SLOW;
|
||||
} else if ("HUMAN".equalsIgnoreCase(playerTypeStr)) {
|
||||
return PLAYER_TYPE.HUMAN;
|
||||
@@ -31,7 +40,8 @@ public class StandAloneGame {
|
||||
}
|
||||
}
|
||||
|
||||
public void playGame(PLAYER_TYPE playerType1, PLAYER_TYPE playerType2, int rounds) {
|
||||
public void playGame(PLAYER_TYPE playerType1, PLAYER_TYPE playerType2,
|
||||
int rounds) {
|
||||
|
||||
Policy player1 = getPolicy(playerType1);
|
||||
Policy player2 = getPolicy(playerType2);
|
||||
@@ -42,11 +52,12 @@ public class StandAloneGame {
|
||||
|
||||
List<GameResult> results = new ArrayList<GameResult>();
|
||||
GameConfig gameConfig = new GameConfig(5);
|
||||
for (int round = 0; round < rounds; ) {
|
||||
for (int round = 0; round < rounds; round++) {
|
||||
results.add(referee.play(gameConfig));
|
||||
}
|
||||
|
||||
System.out.println("Cumulative results for 10 games (BLACK=" + playerType1 + ", WHITE=" + playerType2 + ")");
|
||||
System.out.println("Cumulative results for " + rounds + " games (BLACK="
|
||||
+ playerType1 + ", WHITE=" + playerType2 + ")");
|
||||
for (int i = 0; i < rounds; i++) {
|
||||
System.out.println(i + ". " + results.get(i));
|
||||
}
|
||||
@@ -56,12 +67,15 @@ public class StandAloneGame {
|
||||
switch (playerType) {
|
||||
case HUMAN:
|
||||
return new HumanKeyboardInput();
|
||||
case ROOT_PAR:
|
||||
return new RootParallelization(3, 4000L);
|
||||
case UCT_SLOW:
|
||||
return new MonteCarloUCT(new RandomMovePolicy(), 3000L);
|
||||
return new MonteCarloUCT(new RandomMovePolicy(), 4000L);
|
||||
case UCT_FAST:
|
||||
return new MonteCarloUCT(new RandomMovePolicy(), 1000L);
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid PLAYER_TYPE: " + playerType);
|
||||
throw new IllegalArgumentException("Invalid PLAYER_TYPE: "
|
||||
+ playerType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,8 +52,8 @@ public class AlphaBeta implements Policy {
|
||||
|
||||
GameState gameState = new GameState(node.getGameState());
|
||||
|
||||
List<Action> validMoves = validMoveGenerator.getActions(
|
||||
node.getGameState().getGameConfig(), node.getGameState(), player,
|
||||
List<Action> validMoves = validMoveGenerator.getActions(node
|
||||
.getGameState().getGameConfig(), node.getGameState(), player,
|
||||
ActionGenerator.ALL_ACTIONS);
|
||||
|
||||
boolean terminal = isTerminal(validMoves.size(), recursionLevels);
|
||||
@@ -111,8 +111,8 @@ public class AlphaBeta implements Policy {
|
||||
|
||||
GameState gameState = new GameState(node.getGameState());
|
||||
|
||||
List<Action> validMoves = validMoveGenerator.getActions(
|
||||
node.getGameState().getGameConfig(), node.getGameState(), player,
|
||||
List<Action> validMoves = validMoveGenerator.getActions(node
|
||||
.getGameState().getGameConfig(), node.getGameState(), player,
|
||||
ActionGenerator.ALL_ACTIONS);
|
||||
|
||||
boolean terminal = isTerminal(validMoves.size(), recursionLevels);
|
||||
@@ -173,6 +173,7 @@ public class AlphaBeta implements Policy {
|
||||
@Override
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Collection<Action> prohibitedActions, Player player) {
|
||||
throw new UnsupportedOperationException("Prohibited actions not supported by this class.");
|
||||
throw new UnsupportedOperationException(
|
||||
"Prohibited actions not supported by this class.");
|
||||
}
|
||||
}
|
||||
@@ -19,14 +19,18 @@ public class HumanKeyboardInput implements Policy {
|
||||
|
||||
do {
|
||||
try {
|
||||
System.out.println(player + " to move: (enter coord or PASS/RESIGN)");
|
||||
System.out.println(player
|
||||
+ " to move: (enter coord or PASS/RESIGN)");
|
||||
System.in.read(inputBytes);
|
||||
input = new String(inputBytes).trim().toUpperCase();
|
||||
|
||||
action = Action.getInstance(input);
|
||||
|
||||
if (action.isNone()) {
|
||||
System.out.println("'" + input +"' is not a valid move. Please enter a coordinate (A1-T19) or 'PASS'.");
|
||||
System.out
|
||||
.println("'"
|
||||
+ input
|
||||
+ "' is not a valid move. Please enter a coordinate (A1-T19) or 'PASS'.");
|
||||
System.out.println(gameState);
|
||||
continue;
|
||||
}
|
||||
@@ -34,7 +38,8 @@ public class HumanKeyboardInput implements Policy {
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("IOException: " + ioe.getMessage());
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.out.println("IllegalArgumentException - '"+input+"' is not a valid action or coordinate.");
|
||||
System.out.println("IllegalArgumentException - '" + input
|
||||
+ "' is not a valid action or coordinate.");
|
||||
}
|
||||
} while (action == null);
|
||||
|
||||
@@ -51,7 +56,8 @@ public class HumanKeyboardInput implements Policy {
|
||||
}
|
||||
|
||||
do {
|
||||
System.out.println("Sorry, that action is prohibited. Please make another move.");
|
||||
System.out
|
||||
.println("Sorry, that action is prohibited. Please make another move.");
|
||||
action = getAction(gameConfig, gameState, player);
|
||||
} while (prohibitedActions.contains(action));
|
||||
|
||||
|
||||
@@ -51,8 +51,8 @@ public class Minimax implements Policy {
|
||||
|
||||
GameState gameState = new GameState(node.getGameState());
|
||||
|
||||
List<Action> validMoves = validMoveGenerator.getActions(
|
||||
node.getGameState().getGameConfig(), node.getGameState(), player,
|
||||
List<Action> validMoves = validMoveGenerator.getActions(node
|
||||
.getGameState().getGameConfig(), node.getGameState(), player,
|
||||
ActionGenerator.ALL_ACTIONS);
|
||||
|
||||
boolean terminal = isTerminal(validMoves.size(), recursionLevels);
|
||||
@@ -97,8 +97,8 @@ public class Minimax implements Policy {
|
||||
|
||||
GameState gameState = new GameState(node.getGameState());
|
||||
|
||||
List<Action> validMoves = validMoveGenerator.getActions(
|
||||
node.getGameState().getGameConfig(), node.getGameState(), player,
|
||||
List<Action> validMoves = validMoveGenerator.getActions(node
|
||||
.getGameState().getGameConfig(), node.getGameState(), player,
|
||||
ActionGenerator.ALL_ACTIONS);
|
||||
|
||||
boolean terminal = isTerminal(validMoves.size(), recursionLevels);
|
||||
@@ -146,6 +146,7 @@ public class Minimax implements Policy {
|
||||
@Override
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Collection<Action> prohibitedActions, Player player) {
|
||||
throw new UnsupportedOperationException("Prohibited actions not supported by this class.");
|
||||
throw new UnsupportedOperationException(
|
||||
"Prohibited actions not supported by this class.");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package net.woodyfolsom.msproj.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.woodyfolsom.msproj.Action;
|
||||
import net.woodyfolsom.msproj.GameConfig;
|
||||
@@ -25,27 +27,28 @@ public abstract class MonteCarlo implements Policy {
|
||||
}
|
||||
|
||||
/**
|
||||
* Descend the tree from the specified node and return a list of nodes to grow.
|
||||
* Descend the tree from the specified node and return a list of nodes to
|
||||
* grow.
|
||||
*
|
||||
* @param node
|
||||
* @return
|
||||
*/
|
||||
public abstract List<GameTreeNode<MonteCarloProperties>> descend(GameTreeNode<MonteCarloProperties> node);
|
||||
public abstract List<GameTreeNode<MonteCarloProperties>> descend(
|
||||
GameTreeNode<MonteCarloProperties> node);
|
||||
|
||||
@Override
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Player player) {
|
||||
System.out.println(player + " is thinking for up to " + (searchTimeLimit / 1000.0) + " seconds...");
|
||||
private GameTreeNode<MonteCarloProperties> buildTree(GameConfig gameConfig, GameState gameState, Player player) {
|
||||
System.out.println(player + " is thinking for up to "
|
||||
+ (searchTimeLimit / 1000.0) + " seconds...");
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
if (gameState.getPlayerToMove() != player) {
|
||||
throw new RuntimeException("getAction(..." + player +") was requested but GameState.playerToMove was " + gameState.getPlayerToMove());
|
||||
throw new RuntimeException("getAction(..." + player
|
||||
+ ") was requested but GameState.playerToMove was "
|
||||
+ gameState.getPlayerToMove());
|
||||
}
|
||||
//If for some reason no moves are evaluated within the time limit, pass.
|
||||
//Note that this may lose the game by forfeit even when picking any random move could
|
||||
//result in a win.
|
||||
|
||||
GameTreeNode<MonteCarloProperties> rootNode = new GameTreeNode<MonteCarloProperties>(gameState, new MonteCarloProperties());
|
||||
GameTreeNode<MonteCarloProperties> rootNode = new GameTreeNode<MonteCarloProperties>(
|
||||
gameState, new MonteCarloProperties());
|
||||
|
||||
do {
|
||||
|
||||
@@ -54,9 +57,11 @@ public abstract class MonteCarlo implements Policy {
|
||||
List<GameTreeNode<MonteCarloProperties>> newLeaves = new ArrayList<GameTreeNode<MonteCarloProperties>>();
|
||||
|
||||
for (GameTreeNode<MonteCarloProperties> selectedNode : selectedNodes) {
|
||||
Player playerToMove = selectedNode.getGameState().getPlayerToMove();
|
||||
Player playerToMove = selectedNode.getGameState()
|
||||
.getPlayerToMove();
|
||||
|
||||
for (GameTreeNode<MonteCarloProperties> newLeaf : grow(gameConfig, selectedNode, playerToMove)) {
|
||||
for (GameTreeNode<MonteCarloProperties> newLeaf : grow(
|
||||
gameConfig, selectedNode, playerToMove)) {
|
||||
newLeaves.add(newLeaf);
|
||||
}
|
||||
}
|
||||
@@ -68,10 +73,24 @@ public abstract class MonteCarlo implements Policy {
|
||||
|
||||
elapsedTime = System.currentTimeMillis() - startTime;
|
||||
} while (elapsedTime < searchTimeLimit);
|
||||
//TODO: for debugging, temporarily specify the number of state evaluations rather than time limit
|
||||
//} while (numStateEvaluations < searchTimeLimit);
|
||||
|
||||
return getBestAction(rootNode);
|
||||
return rootNode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Player player) {
|
||||
return getBestAction(buildTree(gameConfig,gameState,player));
|
||||
}
|
||||
|
||||
public Map<Action, MonteCarloProperties> getQvalues(GameConfig gameConfig, GameState gameState,
|
||||
Player player) {
|
||||
GameTreeNode<MonteCarloProperties> rootNode = buildTree(gameConfig,gameState,player);
|
||||
Map<Action,MonteCarloProperties> qValues = new HashMap<Action,MonteCarloProperties>();
|
||||
for (Action action : rootNode.getActions()) {
|
||||
qValues.put(action, rootNode.getChild(action).getProperties());
|
||||
}
|
||||
return qValues;
|
||||
}
|
||||
|
||||
public long getElapsedTime() {
|
||||
@@ -80,11 +99,15 @@ public abstract class MonteCarlo implements Policy {
|
||||
|
||||
public abstract Action getBestAction(GameTreeNode<MonteCarloProperties> node);
|
||||
|
||||
public abstract List<GameTreeNode<MonteCarloProperties>> grow(GameConfig gameConfig, GameTreeNode<MonteCarloProperties> node, Player player);
|
||||
public abstract List<GameTreeNode<MonteCarloProperties>> grow(
|
||||
GameConfig gameConfig, GameTreeNode<MonteCarloProperties> node,
|
||||
Player player);
|
||||
|
||||
public abstract int rollout(GameConfig gameConfig, GameTreeNode<MonteCarloProperties> node, Player player);
|
||||
public abstract int rollout(GameConfig gameConfig,
|
||||
GameTreeNode<MonteCarloProperties> node, Player player);
|
||||
|
||||
public abstract void update(GameTreeNode<MonteCarloProperties> node, int reward);
|
||||
public abstract void update(GameTreeNode<MonteCarloProperties> node,
|
||||
int reward);
|
||||
|
||||
public long getSearchTimeLimit() {
|
||||
return searchTimeLimit;
|
||||
|
||||
@@ -22,30 +22,39 @@ public class MonteCarloUCT extends MonteCarlo {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GameTreeNode<MonteCarloProperties>> descend(GameTreeNode<MonteCarloProperties> node) {
|
||||
public List<GameTreeNode<MonteCarloProperties>> descend(
|
||||
GameTreeNode<MonteCarloProperties> node) {
|
||||
double bestScore = Double.NEGATIVE_INFINITY;
|
||||
GameTreeNode<MonteCarloProperties> bestNode = node;
|
||||
|
||||
// What if the optimum leaf node is actually a terminal node?
|
||||
//From Kocsis and Szepesvari, the value of an actual terminal node is 0, so it will never be grown.
|
||||
// From Kocsis and Szepesvari, the value of an actual terminal node is
|
||||
// 0, so it will never be grown.
|
||||
|
||||
double nodeVisits = node.getProperties().getVisits();
|
||||
Set<Action> actionsExplored = node.getActions();
|
||||
GameState gameState = node.getGameState();
|
||||
|
||||
Action randomNewChildAction = new RandomMovePolicy().getAction(node.getGameState().getGameConfig(), gameState, actionsExplored, gameState.getPlayerToMove());
|
||||
Action randomNewChildAction = new RandomMovePolicy().getAction(node
|
||||
.getGameState().getGameConfig(), gameState, actionsExplored,
|
||||
gameState.getPlayerToMove());
|
||||
|
||||
//only descend to the best child if no new actions are available at this node
|
||||
// only descend to the best child if no new actions are available at
|
||||
// this node
|
||||
if (randomNewChildAction == Action.NONE) {
|
||||
for (Action action : actionsExplored) {
|
||||
GameTreeNode<MonteCarloProperties> childNode = node.getChild(action);
|
||||
GameTreeNode<MonteCarloProperties> childNode = node
|
||||
.getChild(action);
|
||||
|
||||
double childScore;
|
||||
if (childNode.getGameState().isTerminal()) {
|
||||
childScore = 0.0;
|
||||
} else {
|
||||
MonteCarloProperties properties = childNode.getProperties();
|
||||
childScore = (double) (properties.getWins() / properties.getVisits()) + (TUNING_CONSTANT * Math.sqrt(Math.log(nodeVisits) / childNode.getProperties().getVisits()));
|
||||
childScore = (double) (properties.getWins() / properties
|
||||
.getVisits())
|
||||
+ (TUNING_CONSTANT * Math.sqrt(Math.log(nodeVisits)
|
||||
/ childNode.getProperties().getVisits()));
|
||||
}
|
||||
// TODO add random tie breaker?
|
||||
// otherwise the child that is selected first will be biased
|
||||
@@ -72,10 +81,12 @@ public class MonteCarloUCT extends MonteCarlo {
|
||||
GameTreeNode<MonteCarloProperties> bestChild = null;
|
||||
|
||||
for (Action action : node.getActions()) {
|
||||
GameTreeNode<MonteCarloProperties> childNode = node.getChild(action);
|
||||
GameTreeNode<MonteCarloProperties> childNode = node
|
||||
.getChild(action);
|
||||
|
||||
MonteCarloProperties properties = childNode.getProperties();
|
||||
double childScore = (double) properties.getWins() / properties.getVisits();
|
||||
double childScore = (double) properties.getWins()
|
||||
/ properties.getVisits();
|
||||
|
||||
if (childScore >= bestScore) {
|
||||
bestScore = childScore;
|
||||
@@ -85,26 +96,40 @@ public class MonteCarloUCT extends MonteCarlo {
|
||||
}
|
||||
|
||||
if (bestAction == Action.NONE) {
|
||||
System.out.println("MonteCarloUCT failed - no actions were found for the current game staet (not even PASS).");
|
||||
System.out
|
||||
.println("MonteCarloUCT failed - no actions were found for the current game staet (not even PASS).");
|
||||
} else {
|
||||
System.out.println("Action " + bestAction + " selected for " + node.getGameState().getPlayerToMove() + " with simulated win ratio of " + (bestScore*100.0 + "%"));
|
||||
System.out.println("It was visited " + bestChild.getProperties().getVisits() + " times out of " + node.getProperties().getVisits() + " rollouts among " + node.getNumChildren() + " valid actions from the current state.");
|
||||
System.out.println("Action " + bestAction + " selected for "
|
||||
+ node.getGameState().getPlayerToMove()
|
||||
+ " with simulated win ratio of "
|
||||
+ (bestScore * 100.0 + "%"));
|
||||
System.out.println("It was visited "
|
||||
+ bestChild.getProperties().getVisits() + " times out of "
|
||||
+ node.getProperties().getVisits() + " rollouts among "
|
||||
+ node.getNumChildren()
|
||||
+ " valid actions from the current state.");
|
||||
}
|
||||
return bestAction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GameTreeNode<MonteCarloProperties>> grow(GameConfig gameConfig, GameTreeNode<MonteCarloProperties> node, Player player) {
|
||||
public List<GameTreeNode<MonteCarloProperties>> grow(GameConfig gameConfig,
|
||||
GameTreeNode<MonteCarloProperties> node, Player player) {
|
||||
Policy randomMovePolicy = new RandomMovePolicy();
|
||||
Set<Action> exploredActions = node.getActions();
|
||||
Action action = randomMovePolicy.getAction(gameConfig, node.getGameState(), exploredActions, player);
|
||||
Action action = randomMovePolicy.getAction(gameConfig,
|
||||
node.getGameState(), exploredActions, player);
|
||||
if (Action.NONE == action) {
|
||||
throw new RuntimeException("Unable to grow node - are all actions already explored? Board state: " + node.getGameState() + "\nExplored actions: " + exploredActions);
|
||||
throw new RuntimeException(
|
||||
"Unable to grow node - are all actions already explored? Board state: "
|
||||
+ node.getGameState() + "\nExplored actions: "
|
||||
+ exploredActions);
|
||||
}
|
||||
GameState nextGameState = new GameState(node.getGameState());
|
||||
nextGameState.playStone(player, action);
|
||||
List<GameTreeNode<MonteCarloProperties>> newChildren = new ArrayList<GameTreeNode<MonteCarloProperties>>();
|
||||
GameTreeNode<MonteCarloProperties> newChild = new GameTreeNode<MonteCarloProperties>(nextGameState,new MonteCarloProperties());
|
||||
GameTreeNode<MonteCarloProperties> newChild = new GameTreeNode<MonteCarloProperties>(
|
||||
nextGameState, new MonteCarloProperties());
|
||||
|
||||
newChildren.add(newChild);
|
||||
node.addChild(action, newChild);
|
||||
@@ -113,12 +138,12 @@ public class MonteCarloUCT extends MonteCarlo {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
/**
|
||||
* Rollout currently depends on the hardcoded ROLLOUT_DEPTH_LIMIT superclass parameter,
|
||||
* Even with super-ko detection, a rollout might take an unrealistically long time due to unlikely playouts.
|
||||
*/
|
||||
public int rollout(GameConfig gameConfig, GameTreeNode<MonteCarloProperties> node, Player player) {
|
||||
public int rollout(GameConfig gameConfig,
|
||||
GameTreeNode<MonteCarloProperties> node, Player player) {
|
||||
Policy randomMovePolicy = new RandomMovePolicy();
|
||||
|
||||
Action action;
|
||||
@@ -127,10 +152,12 @@ public class MonteCarloUCT extends MonteCarlo {
|
||||
Player currentPlayer = rolloutGameState.getPlayerToMove();
|
||||
do {
|
||||
rolloutDepth++;
|
||||
action = randomMovePolicy.getAction(gameConfig, rolloutGameState, currentPlayer);
|
||||
action = randomMovePolicy.getAction(gameConfig, rolloutGameState,
|
||||
currentPlayer);
|
||||
if (action != Action.NONE) {
|
||||
if (!rolloutGameState.playStone(currentPlayer, action)) {
|
||||
throw new RuntimeException("Failed to play move selected by RandomMovePolicy");
|
||||
throw new RuntimeException(
|
||||
"Failed to play move selected by RandomMovePolicy");
|
||||
}
|
||||
currentPlayer = GoGame.getNextPlayer(currentPlayer);
|
||||
}
|
||||
@@ -161,7 +188,8 @@ public class MonteCarloUCT extends MonteCarlo {
|
||||
@Override
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Collection<Action> prohibitedActions, Player player) {
|
||||
throw new UnsupportedOperationException("Prohibited actions not supported by this class.");
|
||||
throw new UnsupportedOperationException(
|
||||
"Prohibited actions not supported by this class.");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,9 +7,12 @@ import net.woodyfolsom.msproj.GameConfig;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.Player;
|
||||
|
||||
|
||||
public interface Policy {
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState, Player player);
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState, Collection<Action> prohibitedActions, Player player);
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Player player);
|
||||
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Collection<Action> prohibitedActions, Player player);
|
||||
|
||||
public int getNumStateEvaluations();
|
||||
}
|
||||
@@ -9,21 +9,42 @@ import net.woodyfolsom.msproj.GameConfig;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.Player;
|
||||
|
||||
|
||||
public class RandomMovePolicy implements Policy, ActionGenerator {
|
||||
private final boolean passWhenLosing = false;
|
||||
|
||||
/**
|
||||
* Does NOT modify the gameState.
|
||||
*/
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Collection<Action> prohibitedMoves, Player player) {
|
||||
return getActions(gameConfig, gameState, prohibitedMoves, player, 1).get(0);
|
||||
|
||||
List<Action> randomActions = getActions(gameConfig, gameState, prohibitedMoves, player, 1);
|
||||
|
||||
//Never randomly generate PASS when losing, because a good opponent will immediately recognize the killer
|
||||
//move of also passing, thereby causing the current player to lose.
|
||||
Action firstAction = randomActions.get(0);
|
||||
|
||||
//Pass when losing enabled? Just return the first random action;
|
||||
if (passWhenLosing) {
|
||||
return firstAction;
|
||||
}
|
||||
|
||||
//But if passing is the only valid move, pass even if losing.
|
||||
if (firstAction.isPass() && !gameState.getResult().isWinner(player)) {
|
||||
if (randomActions.size() > 1) {
|
||||
return randomActions.get(1);
|
||||
} else {
|
||||
return firstAction;
|
||||
}
|
||||
} else {
|
||||
return firstAction;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to generate up to nMoves random moves on behalf of the specified
|
||||
* player. Will return at least one move, which may be 'NONE' if random
|
||||
* search does not succeeed in discovering a valid move. Does NOT modify the
|
||||
* search does not succeed in discovering a valid move. Does NOT modify the
|
||||
* gameState.
|
||||
*
|
||||
* @param gameConfig
|
||||
@@ -35,13 +56,16 @@ public class RandomMovePolicy implements Policy, ActionGenerator {
|
||||
public List<Action> getActions(GameConfig gameConfig, GameState gameState,
|
||||
Collection<Action> prohibitedMoves, Player player, int nMoves) {
|
||||
if (player != gameState.getPlayerToMove()) {
|
||||
throw new IllegalArgumentException("It is not " + player + "'s turn to move!");
|
||||
throw new IllegalArgumentException("It is not " + player
|
||||
+ "'s turn to move!");
|
||||
}
|
||||
|
||||
GameState gameStateCopy = new GameState(gameState);
|
||||
ActionGenerator actionGenerator = new ValidMoveGenerator();
|
||||
|
||||
List<Action> possibleActions = actionGenerator.getActions(gameConfig, gameStateCopy, prohibitedMoves, player, ActionGenerator.ALL_ACTIONS);
|
||||
List<Action> possibleActions = actionGenerator.getActions(gameConfig,
|
||||
gameStateCopy, prohibitedMoves, player,
|
||||
ActionGenerator.ALL_ACTIONS);
|
||||
List<Action> randomActions = new ArrayList<Action>();
|
||||
|
||||
while (possibleActions.size() > 0 && randomActions.size() < nMoves) {
|
||||
@@ -59,8 +83,8 @@ public class RandomMovePolicy implements Policy, ActionGenerator {
|
||||
}
|
||||
|
||||
/**
|
||||
* RandomMoveGenerator does not evaluate any states, but simply returns elements of
|
||||
* a set of uniformly distributed, distinct valid moves.
|
||||
* RandomMoveGenerator does not evaluate any states, but simply returns
|
||||
* elements of a set of uniformly distributed, distinct valid moves.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@@ -71,7 +95,8 @@ public class RandomMovePolicy implements Policy, ActionGenerator {
|
||||
@Override
|
||||
public List<Action> getActions(GameConfig gameConfig, GameState gameState,
|
||||
Player color, int numActions) {
|
||||
return getActions(gameConfig, gameState, new ArrayList<Action>(), color, numActions);
|
||||
return getActions(gameConfig, gameState, new ArrayList<Action>(),
|
||||
color, numActions);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
145
src/net/woodyfolsom/msproj/policy/RootParallelization.java
Normal file
145
src/net/woodyfolsom/msproj/policy/RootParallelization.java
Normal file
@@ -0,0 +1,145 @@
|
||||
package net.woodyfolsom.msproj.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.woodyfolsom.msproj.Action;
|
||||
import net.woodyfolsom.msproj.GameConfig;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.Player;
|
||||
import net.woodyfolsom.msproj.tree.MonteCarloProperties;
|
||||
|
||||
public class RootParallelization implements Policy {
|
||||
private int numTrees = 1;
|
||||
private long timeLimit = 1000L;
|
||||
|
||||
public RootParallelization(int numTrees, long timeLimit) {
|
||||
this.numTrees = numTrees;
|
||||
this.timeLimit = timeLimit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Player player) {
|
||||
Action bestAction = Action.NONE;
|
||||
|
||||
List<PolicyRunner> policyRunners = new ArrayList<PolicyRunner>();
|
||||
List<Thread> simulationThreads = new ArrayList<Thread>();
|
||||
|
||||
for (int i = 0; i < numTrees; i++) {
|
||||
PolicyRunner policyRunner = new PolicyRunner(new MonteCarloUCT(
|
||||
new RandomMovePolicy(), timeLimit), gameConfig, gameState,
|
||||
player);
|
||||
policyRunners.add(policyRunner);
|
||||
|
||||
Thread simThread = new Thread(policyRunner);
|
||||
simulationThreads.add(simThread);
|
||||
}
|
||||
|
||||
for (Thread simThread : simulationThreads) {
|
||||
simThread.start();
|
||||
}
|
||||
|
||||
for (Thread simThread : simulationThreads) {
|
||||
try {
|
||||
simThread.join();
|
||||
} catch (InterruptedException ie) {
|
||||
System.out
|
||||
.println("Interrupted while waiting for Monte Carlo simulations to finish.");
|
||||
}
|
||||
}
|
||||
|
||||
Map<Action,Integer> totalReward = new HashMap<Action,Integer>();
|
||||
Map<Action,Integer> numSims = new HashMap<Action,Integer>();
|
||||
|
||||
for (PolicyRunner policyRunner : policyRunners) {
|
||||
Map<Action, MonteCarloProperties> qValues = policyRunner.getQvalues();
|
||||
for (Action action : qValues.keySet()) {
|
||||
if (totalReward.containsKey(action)) {
|
||||
totalReward.put(action, totalReward.get(action) + qValues.get(action).getWins());
|
||||
} else {
|
||||
totalReward.put(action, qValues.get(action).getWins());
|
||||
}
|
||||
if (numSims.containsKey(action)) {
|
||||
numSims.put(action, numSims.get(action) + qValues.get(action).getVisits());
|
||||
} else {
|
||||
numSims.put(action, qValues.get(action).getVisits());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double bestValue = 0.0;
|
||||
int bestWins = 0;
|
||||
int totalRollouts = 0;
|
||||
|
||||
for (Action action : totalReward.keySet())
|
||||
{
|
||||
int totalWins = totalReward.get(action);
|
||||
int totalSims = numSims.get(action);
|
||||
|
||||
totalRollouts += totalSims;
|
||||
|
||||
double value = ((double)totalWins) / totalSims;
|
||||
|
||||
if (bestAction.isNone() || bestValue < value) {
|
||||
bestAction = action;
|
||||
bestValue = value;
|
||||
bestWins = totalWins;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
System.out.println("Action " + bestAction + " selected for "
|
||||
+ player
|
||||
+ " with simulated win ratio of "
|
||||
+ (bestValue * 100.0 + "% among " + numTrees + " parallel simulations."));
|
||||
System.out.println("It had a value of "
|
||||
+ bestValue + " out of "
|
||||
+ totalRollouts + " rollouts among "
|
||||
+ " valid actions from the current state.");
|
||||
|
||||
return bestAction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action getAction(GameConfig gameConfig, GameState gameState,
|
||||
Collection<Action> prohibitedActions, Player player) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Prohibited actions not supported by this class.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumStateEvaluations() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
class PolicyRunner implements Runnable {
|
||||
Map<Action,MonteCarloProperties> qValues;
|
||||
|
||||
private GameConfig gameConfig;
|
||||
private GameState gameState;
|
||||
private Player player;
|
||||
private MonteCarlo policy;
|
||||
|
||||
public PolicyRunner(MonteCarlo policy, GameConfig gameConfig,
|
||||
GameState gameState, Player player) {
|
||||
this.policy = policy;
|
||||
this.gameConfig = gameConfig;
|
||||
this.gameState = gameState;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public Map<Action,MonteCarloProperties> getQvalues() {
|
||||
return qValues;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
qValues = policy.getQvalues(gameConfig, gameState, player);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ public class SGFGameTree {
|
||||
private List<SGFNode> nodeSequence = new ArrayList<SGFNode>();
|
||||
private List<SGFGameTree> subTrees = new ArrayList<SGFGameTree>();
|
||||
|
||||
|
||||
public int getNodeCount() {
|
||||
return nodeSequence.size();
|
||||
}
|
||||
@@ -87,8 +86,10 @@ public class SGFGameTree {
|
||||
public String toLateX() {
|
||||
StringBuilder latexSB = new StringBuilder();
|
||||
|
||||
//Somewhat convoluted logic here because the grammar does not require all root
|
||||
//properties to be included in the same node in the tree's node sequence, although they should
|
||||
// Somewhat convoluted logic here because the grammar does not require
|
||||
// all root
|
||||
// properties to be included in the same node in the tree's node
|
||||
// sequence, although they should
|
||||
// each be unique among all node sequences in the tree.
|
||||
for (SGFNode node : nodeSequence) {
|
||||
SGFNode.TYPE nodeType = node.getType();
|
||||
@@ -99,7 +100,8 @@ public class SGFGameTree {
|
||||
latexSB.append(node.getFirstValue(SGFIdentifier.SIZE));
|
||||
latexSB.append("}\n");
|
||||
latexSB.append("\\shortstack{\\showfullgoban\\\\");
|
||||
SGFResult result = (SGFResult) node.getFirstValue(SGFIdentifier.RESULT).getValue();
|
||||
SGFResult result = (SGFResult) node.getFirstValue(
|
||||
SGFIdentifier.RESULT).getValue();
|
||||
latexSB.append(result.getFullText());
|
||||
latexSB.append("}\n");
|
||||
break;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SGFNode {
|
||||
public enum TYPE { ROOT, MOVE_BLACK, MOVE_WHITE, EMPTY }
|
||||
public enum TYPE {
|
||||
ROOT, MOVE_BLACK, MOVE_WHITE, EMPTY
|
||||
}
|
||||
|
||||
private List<SGFProperty> properties = new ArrayList<SGFProperty>();
|
||||
private TYPE type = TYPE.EMPTY;
|
||||
@@ -37,7 +39,9 @@ public class SGFNode {
|
||||
return properties.get(i).getValues().get(0);
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("SGFNode does not contain a property with identifier '" + identifier +"'");
|
||||
throw new RuntimeException(
|
||||
"SGFNode does not contain a property with identifier '"
|
||||
+ identifier + "'");
|
||||
}
|
||||
|
||||
public String toSGF() {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -6,7 +6,6 @@ import java.util.Set;
|
||||
|
||||
import net.woodyfolsom.msproj.Action;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.Player;
|
||||
|
||||
public class GameTreeNode<T extends GameTreeNodeProperties> {
|
||||
private GameState gameState;
|
||||
|
||||
@@ -14,8 +14,11 @@ public class AlphaBetaTest {
|
||||
Policy treeSearch = new AlphaBeta();
|
||||
GameConfig gameConfig = new GameConfig(6);
|
||||
GameState gameState = new GameState(gameConfig);
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("PASS"));
|
||||
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("PASS"));
|
||||
gameState.playStone(Player.WHITE, Action.getInstance("B1"));
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("PASS"));
|
||||
gameState.playStone(Player.WHITE, Action.getInstance("C2"));
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("B2"));
|
||||
|
||||
@@ -38,7 +41,9 @@ public class AlphaBetaTest {
|
||||
GameConfig gameConfig = new GameConfig(6);
|
||||
GameState gameState = new GameState(gameConfig);
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("A2"));
|
||||
gameState.playStone(Player.WHITE, Action.getInstance("PASS"));
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("B1"));
|
||||
gameState.playStone(Player.WHITE, Action.getInstance("PASS"));
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("C2"));
|
||||
gameState.playStone(Player.WHITE, Action.getInstance("B2"));
|
||||
|
||||
|
||||
@@ -17,8 +17,11 @@ public class MonteCarloUCTTest {
|
||||
Policy treeSearch = new MonteCarloUCT(new RandomMovePolicy(),10000L);
|
||||
GameConfig gameConfig = new GameConfig(6);
|
||||
GameState gameState = new GameState(gameConfig);
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("PASS"));
|
||||
gameState.playStone(Player.WHITE, Action.getInstance("A2"));
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("PASS"));
|
||||
gameState.playStone(Player.WHITE, Action.getInstance("B1"));
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("PASS"));
|
||||
gameState.playStone(Player.WHITE, Action.getInstance("C2"));
|
||||
gameState.playStone(Player.BLACK, Action.getInstance("B2"));
|
||||
|
||||
|
||||
@@ -3,9 +3,6 @@ package net.woodyfolsom.msproj.sgf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.antlr.runtime.ANTLRInputStream;
|
||||
|
||||
@@ -2,7 +2,6 @@ package net.woodyfolsom.msproj.sgf;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.antlr.runtime.ANTLRInputStream;
|
||||
@@ -15,7 +14,8 @@ public class SGFParserTest {
|
||||
|
||||
@Test
|
||||
public void testParse() throws RecognitionException, IOException {
|
||||
FileInputStream fis = new FileInputStream(new File("data/games/1334-gokifu-20120916-Gu_Li-Lee_Sedol.sgf"));
|
||||
FileInputStream fis = new FileInputStream(new File(
|
||||
"data/games/1334-gokifu-20120916-Gu_Li-Lee_Sedol.sgf"));
|
||||
ANTLRStringStream in = new ANTLRInputStream(fis);
|
||||
SGFLexer lexer = new SGFLexer(in);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
|
||||
Reference in New Issue
Block a user