[jgogears commit] r42 - in trunk/jgogears/jgogears: . engine

2 views
Skip to first unread message

codesite...@google.com

unread,
Feb 26, 2008, 1:59:55 AM2/26/08
to jgog...@googlegroups.com
Author: syeates
Date: Mon Feb 25 22:58:37 2008
New Revision: 42

Added:
trunk/jgogears/jgogears/TwoGTPRaw.java
trunk/jgogears/jgogears/TwoGTPTest.java
trunk/jgogears/jgogears/engine/SufgoEngine.java
- copied, changed from r31, /trunk/jgogears/jgogears/SufgoEngine.java
trunk/jgogears/jgogears/engine/SufogoEngineTest.java
Removed:
trunk/jgogears/jgogears/SufgoEngine.java
Modified:
trunk/jgogears/jgogears/BoardI.java
trunk/jgogears/jgogears/GTPError.java
trunk/jgogears/jgogears/GTPScore.java
trunk/jgogears/jgogears/GTPState.java
trunk/jgogears/jgogears/GnuGoEngine.java
trunk/jgogears/jgogears/Move.java
trunk/jgogears/jgogears/NoKoRuleSet.java
trunk/jgogears/jgogears/NoKoRuleSetTest.java
trunk/jgogears/jgogears/RuleSet.java
trunk/jgogears/jgogears/TwoGTP.java
trunk/jgogears/jgogears/Vertex.java
trunk/jgogears/jgogears/Zobrist.java
trunk/jgogears/jgogears/engine/Model.java
trunk/jgogears/jgogears/engine/ModelTest.java
trunk/jgogears/jgogears/engine/Node.java

Log:
whole lot of updates

Modified: trunk/jgogears/jgogears/BoardI.java
==============================================================================
--- trunk/jgogears/jgogears/BoardI.java (original)
+++ trunk/jgogears/jgogears/BoardI.java Mon Feb 25 22:58:37 2008
@@ -5,7 +5,7 @@


/**
- * Abstract interface to a cabin. Knows about the size of the board,
which stones are where, and about colours. Knows
+ * Abstract interface to a board. Knows about the size of the board,
which stones are where, and about colours. Knows
* nothing of the history of the board or whose turn it is to play.
*
* @author Stuart
@@ -89,6 +89,18 @@
*
* @return the colour
*/
+ public short getColour(Vertex v){
+ return this.getColour(v.getRow(), v.getColumn());
+ }
+
+ /**
+ * get the colour of a vertex.
+ *
+ * @param row the row
+ * @param column the column
+ *
+ * @return the colour
+ */
public abstract short getColour(int row, int column);

/**
@@ -100,6 +112,13 @@
*
*/
public abstract void setColour(int row, int column, short colour);
+
+ public Collection<Move> getAllLegalMoves(RuleSet rules, short colour){
+ return rules.getAllLegalMoves(null, this, colour);
+ }
+ public Collection<Vertex> getAllLegalVertexes(RuleSet rules, short colour){
+ return rules.getAllLegalVertexes(null, this, colour);
+ }

/**
* get the size of this board.
@@ -154,7 +173,6 @@
return false;
return true;
}
-

/**
* create a new board based on the current board plus a move.

Modified: trunk/jgogears/jgogears/GTPError.java
==============================================================================
--- trunk/jgogears/jgogears/GTPError.java (original)
+++ trunk/jgogears/jgogears/GTPError.java Mon Feb 25 22:58:37 2008
@@ -8,9 +8,6 @@
*/
public class GTPError extends Error {

- /** TODO. */
- private static final long serialVersionUID = 1L;
-
/**
* Instantiates a new gTP error.
*/

Modified: trunk/jgogears/jgogears/GTPScore.java
==============================================================================
--- trunk/jgogears/jgogears/GTPScore.java (original)
+++ trunk/jgogears/jgogears/GTPScore.java Mon Feb 25 22:58:37 2008
@@ -57,14 +57,6 @@
};

/* (non-Javadoc)
- * @see java.lang.Object#clone()
- */
- @Override
- protected Object clone() throws CloneNotSupportedException {
- return new GTPScore(this.toString());
- };
-
- /* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
@@ -173,7 +165,8 @@
String original = s;
if (s.length() == 0) {
System.err.println("cannot initialise a score from a zero length string");
- return false;
+ throw new Error();
+ //return false;
}
try {
this.value = s;

Modified: trunk/jgogears/jgogears/GTPState.java
==============================================================================
--- trunk/jgogears/jgogears/GTPState.java (original)
+++ trunk/jgogears/jgogears/GTPState.java Mon Feb 25 22:58:37 2008
@@ -1,5 +1,7 @@
package jgogears;

+import java.util.*;
+
// TODO: Auto-generated Javadoc
/**
* Class representing the state of a GTP game.
@@ -9,7 +11,7 @@
public class GTPState {

/** The boardsize. */
- protected short boardsize = -1;
+ protected short boardsize = BoardI.DEFAULT_BOARD_SIZE;

/** The board. */
protected BoardI board = null;
@@ -34,6 +36,10 @@

/** The played moves. */
protected java.util.Vector<Move> playedMoves = new java.util.Vector<Move>();
+
+ public GTPState(){
+ this.board = new Board(boardsize);
+ }

/**
* returns true if we're in a playable state or throws an error.
@@ -162,6 +168,23 @@
return;
if (move.getResign())
return;
+ TreeSet<Vertex> captures = RuleSet.DEFAULT.captures(null, board, move);
+ //TODO count the captures
+ Iterator<Vertex> each = captures.iterator();
+ while(each.hasNext()){
+ Vertex vert = each.next();
+ switch (this.board.getColour(vert)){
+ case BoardI.VERTEX_BLACK:
+ this.whiteCapturedCount++;
+ break;
+ case BoardI.VERTEX_WHITE:
+ this.blackCapturedCount++;
+ break;
+ default:
+ throw new Error("capture neither black or white");
+ }
+
+ }
this.board = this.board.newBoard(move);
}

@@ -190,6 +213,7 @@
*/
public void setBoardsize(short boardsize) {
this.boardsize = boardsize;
+ this.board = new Board(boardsize);
}

/**
@@ -215,8 +239,8 @@
*
* @param komi the new komi
*/
- public void setKomi(double komi) {
- this.komi = komi;
+ public void setKomi(double komia) {
+ this.komi = komia;
}

/**
@@ -224,8 +248,8 @@
*
* @param mainTime the new main time
*/
- public void setMainTime(double mainTime) {
- this.mainTime = mainTime;
+ public void setMainTime(double mainTimea) {
+ this.mainTime = mainTimea;
}

/**

Modified: trunk/jgogears/jgogears/GnuGoEngine.java
==============================================================================
--- trunk/jgogears/jgogears/GnuGoEngine.java (original)
+++ trunk/jgogears/jgogears/GnuGoEngine.java Mon Feb 25 22:58:37 2008
@@ -43,6 +43,8 @@

/** The DEBUG. */
public boolean DEBUG = false;
+
+ private short size = BoardI.DEFAULT_BOARD_SIZE;

/**
* Instantiates a new gnu go engine.
@@ -52,6 +54,24 @@
public GnuGoEngine() throws IOException {
this.initialise();
}
+ /**
+ * Instantiates a new gnu go engine.
+ *
+ * @throws IOException Signals that an I/O exception has occurred.
+ */
+ public GnuGoEngine(short size) throws IOException {
+ this.size = size;
+ this.initialise();
+ }
+ /**
+ * Instantiates a new gnu go engine.
+ *
+ * @throws IOException Signals that an I/O exception has occurred.
+ */
+ public GnuGoEngine(int size) throws IOException {
+ this.size = (short)size;
+ this.initialise();
+ }

/**
* Check.
@@ -88,10 +108,11 @@
this.write(GTPConstants.FINALSTATUSLIST + " " + status + "\n\n");
String s = this.read();
TreeSet<Vertex> v = GTPParserUtils.parseVertexList(s);
- if (this.DEBUG)
- System.err.println(s);
- if (this.DEBUG)
- System.err.println(v);
+ if (this.DEBUG){
+ System.err.println("finalStatusList" +status);
+ System.err.println("finalStatusList" +s);
+ System.err.println("finalStatusList" +v);
+ }
return v;
}

@@ -102,10 +123,11 @@
this.write(GTPConstants.FIXEDHANDICAP + " " + handicap + "\n\n");
String s = this.read();
TreeSet<Vertex> v = GTPParserUtils.parseVertexList(s);
- if (this.DEBUG)
- System.err.println(s);
- if (this.DEBUG)
- System.err.println(v);
+ if (this.DEBUG){
+ System.err.println("fixedHandicap" + handicap);
+ System.err.println("fixedHandicap" + s);
+ System.err.println("fixedHandicap" + v);
+ }
return v;
}

@@ -116,8 +138,9 @@
this.write(GTPConstants.GENMOVE + " " + Move.colourString(colour) + "\n\n");
String s = this.read();
// GoMove move = GoMove.createVertex(s.substring(2));
- Vertex v = new Vertex(s);
- Move move = new Move(v.getRow(), v.getColumn(), colour);
+ Move move = new Move(s, colour);
+ if (DEBUG)
+ System.err.println("genMove:" + colour + " " + move);
return move;
}

@@ -249,6 +272,8 @@
this.errreader = new java.io.BufferedReader(new InputStreamReader(this.process.getErrorStream()));
this.writer = new OutputStreamWriter(this.process.getOutputStream());
this.initialised = true;
+
+ this.setBoardSize(this.getSize());

this.check();
Thread.sleep(LARGE_PAUSE);
@@ -273,7 +298,7 @@
if (null == e)
return true;
if (this.DEBUG)
- System.err.println("clearBoard:" + s);
+ System.err.println("loadsgf:" + s);
return false;

}
@@ -317,10 +342,7 @@
Error e = GTPParserUtils.getError(s);
if (null == e)
return true;
- if (this.DEBUG)
- System.err.println("clearBoard:" + s);
return false;
-
}

/* (non-Javadoc)
@@ -566,6 +588,22 @@
t.printStackTrace();
System.err.println(t);
}
+ }
+
+ /**
+ * get the size
+ * @return the size
+ */
+ public final short getSize() {
+ return size;
+ }
+
+ /**
+ * set the size
+ * @param size the size to set
+ */
+ public final void setSize(short size) {
+ this.size = size;
}

}

Modified: trunk/jgogears/jgogears/Move.java
==============================================================================
--- trunk/jgogears/jgogears/Move.java (original)
+++ trunk/jgogears/jgogears/Move.java Mon Feb 25 22:58:37 2008
@@ -139,6 +139,30 @@
this.setColumn(v.getColumn());
}
}
+ /**
+ * create a GoMove from a move string minus the mvoe colour
+ *
+ * @param move the string to parse
+ * @param colour the colour of the move
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ */
+ public Move(String move, short colour) throws
IllegalArgumentException {
+ // System.err.println("GoMove::GoMove(\"" + move + "\"");
+
+ move = move.toLowerCase();
+
+ this.colour = colour;
+ if (move.contains("resign")) {
+ this.resign = true;
+ } else if (move.contains("pass")) {
+ this.pass = true;
+ } else {
+ Vertex v = new Vertex(move);
+ this.setRow(v.getRow());
+ this.setColumn(v.getColumn());
+ }
+ }

/**
* return the colour of this move.

Modified: trunk/jgogears/jgogears/NoKoRuleSet.java
==============================================================================
--- trunk/jgogears/jgogears/NoKoRuleSet.java (original)
+++ trunk/jgogears/jgogears/NoKoRuleSet.java Mon Feb 25 22:58:37 2008
@@ -103,8 +103,8 @@
@Override
public TreeSet<Vertex> getLiberties(short rowb, short columnb, BoardI
board) {
if ((board.getColour(rowb, columnb) == BoardI.VERTEX_EMPTY)
- || (board.getColour(rowb, columnb) == BoardI.VERTEX_EMPTY)
- || (board.getColour(rowb, columnb) == BoardI.VERTEX_EMPTY)) {
+ || (board.getColour(rowb, columnb) == BoardI.VERTEX_KO)
+ || (board.getColour(rowb, columnb) == BoardI.VERTEX_OFF_BOARD)) {
throw new Error("empty sqaures don't have liberties");
}

@@ -283,7 +283,7 @@
short colour = move.getColour();

if (board.getColour(row, column) != BoardI.VERTEX_EMPTY) {
- System.err.println("illegal move, not empty");
+ if (DEBUG)System.err.println("illegal move, not empty");
return false;
}

@@ -297,6 +297,8 @@
liberties.addAll(this.legelsfrompos(row, column - 1, colour, board));

liberties.addAll(this.captures(game, board, move));
+
+ liberties.remove(new Vertex(row,column));

if (liberties.size() > 0)
return true;

Modified: trunk/jgogears/jgogears/NoKoRuleSetTest.java
==============================================================================
--- trunk/jgogears/jgogears/NoKoRuleSetTest.java (original)
+++ trunk/jgogears/jgogears/NoKoRuleSetTest.java Mon Feb 25 22:58:37 2008
@@ -135,6 +135,110 @@

}

+ public void testNoCenterPlay() {
+
+ NoKoRuleSet rule = new NoKoRuleSet();
+ short size = 7;
+ BoardI board = new Board(size);
+
+ Move move = null;
+
+ move = new Move(4, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 2, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+
+ move = new Move(2, 3, BoardI.VERTEX_WHITE);
+ }
+
+ public void testCaptureI() {
+
+ NoKoRuleSet rule = new NoKoRuleSet();
+ short size = 7;
+ BoardI board = new Board(size);
+
+ Move move = null;
+
+ move = new Move(4, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+
+ move = new Move(3, 3, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+
+ if (DEBUG)
+ System.err.println(board);
+ move = new Move(3, 2, BoardI.VERTEX_BLACK);
+ assertTrue(rule.captures(null, board, move).size() == 1);
+ }
+ public void testCaptureII() {
+
+ NoKoRuleSet rule = new NoKoRuleSet();
+ short size = 7;
+ BoardI board = new Board(size);
+
+ Move move = null;
+
+ move = new Move(4, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 2, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 3, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+
+ move = new Move(3, 4, BoardI.VERTEX_BLACK);
+ assertTrue(rule.captures(null, board, move).size() == 1);
+ }
+ public void testCaptureIII() {
+
+ NoKoRuleSet rule = new NoKoRuleSet();
+ short size = 7;
+ BoardI board = new Board(size);
+
+ Move move = null;
+
+ move = new Move(4, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 2, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 3, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+
+ move = new Move(2, 3, BoardI.VERTEX_BLACK);
+ assertTrue(rule.captures(null, board, move).size() == 1);
+ }
+ public void testCaptureIIII() {
+
+ NoKoRuleSet rule = new NoKoRuleSet();
+ short size = 7;
+ BoardI board = new Board(size);
+
+ Move move = null;
+
+ move = new Move(3, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 2, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 3, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+
+ move = new Move(4, 3, BoardI.VERTEX_BLACK);
+ assertTrue(rule.captures(null, board, move).size() == 1);
+ }
+
/**
* Test corner plays.
*/
@@ -534,4 +638,149 @@
assertTrue(string.size() == size * (size - 2));

}
+
+ public void testCaptureComplex() {
+
+ NoKoRuleSet rule = new NoKoRuleSet();
+ short size = 7;
+ BoardI board = new Board(size);
+
+ Move move = null;
+
+ move = new Move(4, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(4, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 2, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(4, 2, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+
+ move = new Move(3, 3, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(3, 2, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+
+ if (DEBUG)
+ System.err.println(board);
+ move = new Move(3, 1, BoardI.VERTEX_BLACK);
+
+ assertTrue(rule.captures(null, board, move).size() == 2);
+ }
+
+ public void testCaptureComplexII() {
+
+ NoKoRuleSet rule = new NoKoRuleSet();
+ short size = 7;
+ BoardI board = new Board(size);
+
+ Move move = null;
+
+ move = new Move(4, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(4, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 2, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 1, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+
+ move = new Move(3, 3, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(3, 2, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(5, 2, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(4, 1, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+
+ if (DEBUG)
+ System.err.println(board);
+ move = new Move(3, 2, BoardI.VERTEX_BLACK);
+
+ assertTrue(rule.captures(null, board, move).size() == 2);
+ }
+ public void testCaptureComplexIII() {
+
+ NoKoRuleSet rule = new NoKoRuleSet();
+ short size = 7;
+ BoardI board = new Board(size);
+
+ Move move = null;
+
+ move = new Move(4, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(4, 4, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(2, 2, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(3, 1, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(4, 0, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(5, 3, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+ move = new Move(6, 2, BoardI.VERTEX_BLACK);
+ board = board.newBoard(move);
+
+ move = new Move(3, 3, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(3, 2, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(5, 2, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(4, 1, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+
+ if (DEBUG)
+ System.err.println(board);
+ move = new Move(4, 2, BoardI.VERTEX_BLACK);
+
+ assertTrue(rule.captures(null, board, move).size() == 2);
+ move = new Move(4, 2, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ if (DEBUG)
+ System.err.println(board);
+ move = new Move(5, 1, BoardI.VERTEX_BLACK);
+ assertTrue(rule.captures(null, board, move).size() + " ",
rule.captures(null, board, move).size() == 5);
+ board = board.newBoard(move);
+ if (DEBUG)
+ System.err.println(board);
+
+ move = new Move(3, 3, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(5, 2, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(4, 1, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ move = new Move(3, 2, BoardI.VERTEX_WHITE);
+ board = board.newBoard(move);
+ if (DEBUG)
+ System.err.println(board);
+
+ move = new Move(4, 2, BoardI.VERTEX_WHITE);
+ assertFalse(rule.moveIsLegal(null, board, move));
+
+
+
+ }
+
}

Modified: trunk/jgogears/jgogears/RuleSet.java
==============================================================================
--- trunk/jgogears/jgogears/RuleSet.java (original)
+++ trunk/jgogears/jgogears/RuleSet.java Mon Feb 25 22:58:37 2008
@@ -3,7 +3,7 @@
*/
package jgogears;

-import java.util.TreeSet;
+import java.util.*;

// TODO: Auto-generated Javadoc
/**
@@ -13,6 +13,8 @@
*/
public abstract class RuleSet {

+ static public final RuleSet DEFAULT = new NoKoRuleSet();
+
/**
* Captures.
*
@@ -133,5 +135,28 @@
* @return true, if move is legal
*/
public abstract boolean moveIsLegal(Game game, BoardI board, Move move);
+
+
+ public Collection<Move> getAllLegalMoves(Game game, BoardI board,
short colour){
+ Stack<Move> moves = new Stack<Move>();
+ for (int i=0;i<board.getSize();i++)
+ for (int j=0;j<board.getSize();j++){
+ Move move = new Move(i,j,colour);
+ if (this.moveIsLegal(null, board, move))
+ moves.push(move);
+ }
+ return moves;
+ }
+
+
+ public Collection<Vertex> getAllLegalVertexes(Game game, BoardI
board, short colour){
+ Stack<Vertex> moves = new Stack<Vertex>();
+ for (int i=0;i<board.getSize();i++)
+ for (int j=0;j<board.getSize();j++)
+ if (this.moveIsLegal(null, board, new Move(i,j,colour)))
+ moves.push(new Vertex(i,j));
+ return moves;
+ }
+

}

Modified: trunk/jgogears/jgogears/TwoGTP.java
==============================================================================
--- trunk/jgogears/jgogears/TwoGTP.java (original)
+++ trunk/jgogears/jgogears/TwoGTP.java Mon Feb 25 22:58:37 2008
@@ -12,27 +12,16 @@

public class TwoGTP {

- /**
- * Play two GTP-compatible players against each other.
- *
- * @param args (ignored)
- *
- * @throws IOException Signals that an I/O exception has occurred.
- */
-
- public static void main(String[] args) throws IOException {
- TwoGTP twoGTP = new TwoGTP();
- twoGTP.black = new GnuGoEngine();
- twoGTP.white = new GnuGoEngine();
- twoGTP.playOutGame();
-
- }

/** The black player. */
- private GTPInterfaceRaw black = null;
+ private GTPInterface black = null;

/** The white player. */
- private GTPInterfaceRaw white = null;
+ private GTPInterface white = null;
+
+ private GTPState state = new GTPState();
+
+ public static final boolean DEBUG = true;

/**
* Run the game. Assumes that the black and white players have
already been set up.
@@ -40,39 +29,88 @@
* @return true, if play out game
*/

- public boolean playOutGame() {
+ public GTPState playOutGame() {
int passes = 0;
boolean blackNext = true;
- //TODO check the black and white players have been set up
+ if (black == null)
+ throw new Error();
+ if (white == null)
+ throw new Error();
+

while (passes < 4) {
+ Move move = null;
if (blackNext) {
- Move move = this.black.genMove(BoardI.VERTEX_BLACK);
+ move = this.black.genMove(BoardI.VERTEX_BLACK, this.state);
assert (move != null);
- this.white.play(move);
if (move.getPass())
passes++;
else
passes = 0;
blackNext = false;
} else {
- Move move = this.white.genMove(BoardI.VERTEX_WHITE);
+ move = this.white.genMove(BoardI.VERTEX_WHITE, this.state);
assert (move != null);
- this.black.play(move);
if (move.getPass())
passes++;
else
passes = 0;
blackNext = true;
}
-
+ if (DEBUG)
+ System.err.println("TwoGTP: played " + move);
+ this.state.playMove(move);
+ if (DEBUG)
+ System.err.println("TwoGTP: played " + move);
+ if (DEBUG)
+ System.err.println(state.getBoard());
}
- System.err.println(this.black.getFinalScore());
- System.err.println(this.white.getFinalScore());
- System.err.println(this.black.showBoard());
- System.err.println(this.white.showBoard());
+ if (DEBUG)
+ System.err.println(this.black.getFinalScore(state));
+ if (DEBUG)
+ System.err.println(this.white.getFinalScore(state));
+
+ return state;
+ }
+
+ /**
+ * get the black
+ * @return the black
+ */
+ public final GTPInterface getBlack() {
+ return black;
+ }
+
+ /**
+ * set the black
+ * @param black the black to set
+ */
+ public final void setBlack(GTPInterface black) {
+ this.black = black;
+ }

- return true;
+ /**
+ * get the white
+ * @return the white
+ */
+ public final GTPInterface getWhite() {
+ return white;
+ }
+
+ /**
+ * set the white
+ * @param white the white to set
+ */
+ public final void setWhite(GTPInterface white) {
+ this.white = white;
+ }
+
+ /**
+ * get the currentBoard
+ * @return the currentBoard
+ */
+ public final BoardI getCurrentBoard() {
+ return this.state.getBoard();
}

}

Added: trunk/jgogears/jgogears/TwoGTPRaw.java
==============================================================================
--- (empty file)
+++ trunk/jgogears/jgogears/TwoGTPRaw.java Mon Feb 25 22:58:37 2008
@@ -0,0 +1,121 @@
+package jgogears;
+
+import java.io.IOException;
+
+/**
+ * An incomplete clone of the TwoGTP program included in the GnuGo
distribution. It runs a go game between a pair of GTP-capiable players
+ *
+ * TODO finish this implementation
+ *
+ * @author syeates
+ */
+
+public class TwoGTPRaw {
+
+ /**
+ * Play two GTP-compatible players against each other.
+ *
+ * @param args (ignored)
+ *
+ * @throws IOException Signals that an I/O exception has occurred.
+ */
+
+ public static void main(String[] args) throws IOException {
+ TwoGTPRaw twoGTP = new TwoGTPRaw();
+ twoGTP.black = new GnuGoEngine();
+ twoGTP.white = new GnuGoEngine();
+ twoGTP.playOutGame();
+
+ }
+
+/** The black player. */
+ private GTPInterfaceRaw black = null;
+
+/** The white player. */
+ private GTPInterfaceRaw white = null;
+
+ private BoardI currentBoard = new Board();
+
+ /**
+ * Run the game. Assumes that the black and white players have
already been set up.
+ *
+ * @return true, if play out game
+ */
+
+ public boolean playOutGame() {
+ int passes = 0;
+ boolean blackNext = true;
+ //TODO check the black and white players have been set up
+
+ while (passes < 4) {
+ Move move = null;
+ if (blackNext) {
+ move = this.black.genMove(BoardI.VERTEX_BLACK);
+ assert (move != null);
+ this.white.play(move);
+ if (move.getPass())
+ passes++;
+ else
+ passes = 0;
+ blackNext = false;
+ } else {
+ move = this.white.genMove(BoardI.VERTEX_WHITE);
+ assert (move != null);
+ this.black.play(move);
+ if (move.getPass())
+ passes++;
+ else
+ passes = 0;
+ blackNext = true;
+ }
+ currentBoard = currentBoard.newBoard(move);
+ }
+ System.err.println(this.black.getFinalScore());
+ System.err.println(this.white.getFinalScore());
+ System.err.println(this.black.showBoard());
+ System.err.println(this.white.showBoard());
+
+ return true;
+ }
+
+ /**
+ * get the black
+ * @return the black
+ */
+ public final GTPInterfaceRaw getBlack() {
+ return black;
+ }
+
+ /**
+ * set the black
+ * @param black the black to set
+ */
+ public final void setBlack(GTPInterfaceRaw black) {
+ this.black = black;
+ }
+
+ /**
+ * get the white
+ * @return the white
+ */
+ public final GTPInterfaceRaw getWhite() {
+ return white;
+ }
+
+ /**
+ * set the white
+ * @param white the white to set
+ */
+ public final void setWhite(GTPInterfaceRaw white) {
+ this.white = white;
+ }
+
+ /**
+ * get the currentBoard
+ * @return the currentBoard
+ */
+ public final BoardI getCurrentBoard() {
+ return currentBoard;
+ }
+
+}

Added: trunk/jgogears/jgogears/TwoGTPTest.java
==============================================================================
--- (empty file)
+++ trunk/jgogears/jgogears/TwoGTPTest.java Mon Feb 25 22:58:37 2008
@@ -0,0 +1,26 @@
+/**
+ *
+ */
+package jgogears;
+
+import junit.framework.TestCase;
+
+/**
+ * test TwoGTP with a pair of GnuGo Players
+ * @author syeates
+ *
+ */
+public class TwoGTPTest extends TestCase {
+
+ public void testRaw() throws Exception {
+ TwoGTPRaw two = new TwoGTPRaw();
+ assertNotNull(two);
+ two.setBlack(new GnuGoEngine(9));
+ two.setWhite(new GnuGoEngine(9));
+ two.playOutGame();
+ GTPScore scoreb = two.getBlack().getFinalScore();
+ GTPScore scorew = two.getWhite().getFinalScore();
+ assertTrue(scoreb + " " + scorew, scoreb.equals(scorew));
+ }
+
+}

Modified: trunk/jgogears/jgogears/Vertex.java
==============================================================================
--- trunk/jgogears/jgogears/Vertex.java (original)
+++ trunk/jgogears/jgogears/Vertex.java Mon Feb 25 22:58:37 2008
@@ -5,13 +5,14 @@
/**
* A class representing a vertex on the board. There is no
representation of colour or the occupancy of the vertex.
*
+ * TODO write some tests for this
+ *
* @author Stuart
*/
public final class Vertex extends Vector<Short> implements
Comparable<Vertex> {

- /** the serial id for serialisation. */
- private static final long serialVersionUID = 1L;
-
+ static final boolean DEBUG = false;
+
/**
* Short cut constructor with ints rather than shorts.
*
@@ -40,7 +41,8 @@
* @param vertexString the vertex string
*/
public Vertex(String vertexString) {
- // System.err.println("parseVertex(\"" + vertexString + "\")");
+ if (DEBUG)
+ System.err.println("parseVertex(\"" + vertexString + "\")");
vertexString = vertexString.toLowerCase();
switch (vertexString.charAt(0)) {
case 'a':
@@ -128,7 +130,8 @@
this.setColumn(((vertexString.charAt(1) - '0') * 10) +
(vertexString.charAt(2) - '1'));
} else
throw new IllegalArgumentException("trying to parse (4) \"" +
vertexString + "\", \"" + vertexString + "\"");
- System.err.println(this);
+ if (DEBUG)
+ System.err.println(this);
}

/**

Modified: trunk/jgogears/jgogears/Zobrist.java
==============================================================================
--- trunk/jgogears/jgogears/Zobrist.java (original)
+++ trunk/jgogears/jgogears/Zobrist.java Mon Feb 25 22:58:37 2008
@@ -4,7 +4,9 @@


/**
- * Class representing a Zobrist hash, a binary hash of the current
board state. TODO add proper references
+ * Class representing a Zobrist hash, a binary hash of the current
board state.
+ *
+ * TODO add proper references
*
* @author Stuart
*/

Modified: trunk/jgogears/jgogears/engine/Model.java
==============================================================================
--- trunk/jgogears/jgogears/engine/Model.java (original)
+++ trunk/jgogears/jgogears/engine/Model.java Mon Feb 25 22:58:37 2008
@@ -13,13 +13,17 @@
public static float DELTA = (float) 0.0001;

/** A value used for == analysis of doubles */
- public static float TINY = (float) 0.0000001;
+ public static float TINY = (float) 0.000000000001;

/** are we being verbose? */
public static boolean DEBUG = false;

/** The random number generator */
private static Random random = new Random();
+
+ private int boardsTrained = 0;
+ private int gamesTrained = 0;
+

/**
* Gets the random.
@@ -42,6 +46,16 @@
}

/**
+ * Gets the random tiny.
+ *
+ * @return the random tiny
+ */
+ static public double getRandomTiny() {
+ double r = random.nextDouble();
+ return r * TINY;
+ }
+
+ /**
* Max of two floats
*
* @param a
@@ -90,6 +104,25 @@
public Node getRoot() {
return this.root;
}
+
+ Vertex getBestScore(BoardI board, boolean white){
+ double[][] result = getScores(board, white);
+ double best = Double.MIN_VALUE;
+ int I = 0,J = 0;
+ int i,j;
+ for (i=0;i<board.getSize();i++)
+ for (j=0;j<board.getSize();j++) {
+ if (result[i][j] > best){
+ if (RuleSet.DEFAULT.moveIsLegal(null, board, new Move(i,j,white?BoardI.VERTEX_WHITE:BoardI.VERTEX_BLACK))){
+ best = result[i][j] + getRandomTiny();
+ I =i;
+ J = j;
+ }
+ }
+ }
+ return new Vertex(I,J);
+ }
+
/**
*
* TODO
@@ -101,10 +134,11 @@
short size = board.getSize();
double[][] result = new double[size][size];
for (short row = 0; row < size; row++) {
- for (short column = 0; column < size; column++) {
+ for (short column = 0; column < size; column++)
+ for (short sym = 0; sym < 8; sym++) {
result[row][column] = TINY;
Node node = this.getRoot();
- VertexLineariser linear = new VertexLineariser(board, row,
column, (short)0, white);
+ VertexLineariser linear = new VertexLineariser(board, row,
column, sym, white);
double estimate = 1;
int depth = 0;
while (linear.hasNext() && node != null) {
@@ -132,7 +166,7 @@
long childCount = 1;
if (child != null)
childCount = child.getCount();
- estimate = estimate * child.getCount() / node.getCount() ;
+ estimate = estimate * childCount / node.getCount() ;

}

@@ -178,11 +212,9 @@
System.out.println("about to train on: " + move);
int colour = move.getColour();
boolean isBlack = colour == BoardI.VERTEX_BLACK;
- float str = (float) (isBlack ? strengthB : strengthW);
+ //float str = (float) (isBlack ? strengthB : strengthW);

- // mark the remaining points as not worth playing
- if ((game != null) || (game.getScore() != null) ||
game.getScore().getScored() || (move != null)
- || move.getPass()) {
+ if ((game != null) || (move != null)|| move.getPass()) {
movecounter++;
for (short i = 0; i < size; i++)
for (short j = 0; j < size; j++)
@@ -193,7 +225,7 @@
if ((!move.getPass()) && (move.getRow() == i) &&
(move.getColumn() == j))
played = true;
// TODO
- root.train(linear, played);
+ root.train(linear, played, played, Integer.MAX_VALUE);
}
}
}

Modified: trunk/jgogears/jgogears/engine/ModelTest.java
==============================================================================
--- trunk/jgogears/jgogears/engine/ModelTest.java (original)
+++ trunk/jgogears/jgogears/engine/ModelTest.java Mon Feb 25 22:58:37 2008
@@ -33,15 +33,14 @@
}

}
- public Model trainNFiles(int count)throws IOException {
+ public static Model trainNFiles(int count, Model model)throws
IOException {
Stack<String> files = new Stack<String>();
files.push("sgf/2004-12");
assertNotNull(files);

int filecount = 0;
int movecount = 0;
- Model model = new Model();
- assertNotNull(model);
+ assertNotNull(model);
Date start = new Date();
assertNotNull(start);

@@ -79,7 +78,10 @@
* Signals that an I/O exception has occurred.
*/
public void testLoadAllSGFFiles() throws IOException {
- Model model = this.trainNFiles(10);
+ Model model = new Model();
+ ModelTest.trainNFiles(20,model);
+ ModelTest.trainNFiles(20,model);
+

BoardI board = new Board(19);
// board = board.newBoard(new Move("white b2"));
@@ -118,7 +120,8 @@
* Signals that an I/O exception has occurred.
*/
public void testTreeWellFormedness() throws IOException {
- Model model = this.trainNFiles(10);
+ Model model = new Model();
+ this.trainNFiles(10,model);
// generate
int nodeCount = 0;
int leafCount = 0;
@@ -174,7 +177,8 @@
* Signals that an I/O exception has occurred.
*/
public void testTreeWellFormednessII() throws IOException {
- Model model = this.trainNFiles(10);
+ Model model = new Model();
+ this.trainNFiles(10,model);
Stack<Node> nodes = new Stack<Node>();
Set<Node> nodeSet = new TreeSet<Node>();
nodes.push(model.getRoot());

Modified: trunk/jgogears/jgogears/engine/Node.java
==============================================================================
--- trunk/jgogears/jgogears/engine/Node.java (original)
+++ trunk/jgogears/jgogears/engine/Node.java Mon Feb 25 22:58:37 2008
@@ -243,13 +243,17 @@
*
* @param linear
* the linear
- * @param strength
- * the strength
+ * @param expand are we expanding?
+ * @param depth the depth to expand to
* @param played
* the played
*/
- public void train(VertexLineariser linear, boolean expand) {
- count++;
+ public void train(VertexLineariser linear, boolean played, boolean
expand, int depth) {
+ if (depth < 0)
+ expand = false;
+ depth--;
+ if (played)
+ count++;
if (!linear.hasNext())
return;
Short colour = linear.next();
@@ -261,7 +265,7 @@
black = new Node();
else
return;
- black.train(linear, expand);
+ black.train(linear, played, expand, depth);
break;
case BoardI.VERTEX_WHITE:
if (white == null)
@@ -269,7 +273,7 @@
white = new Node();
else
return;
- white.train(linear, expand);
+ white.train(linear, played, expand, depth);

break;
case BoardI.VERTEX_OFF_BOARD:
@@ -278,7 +282,7 @@
off = new Node();
else
return;
- off.train(linear, expand);
+ off.train(linear, played, expand, depth);

break;
case BoardI.VERTEX_EMPTY:
@@ -288,7 +292,7 @@
empty = new Node();
else
return;
- empty.train(linear, expand);
+ empty.train(linear, played, expand, depth);
break;
default:
throw new Error();

Copied: trunk/jgogears/jgogears/engine/SufgoEngine.java (from r31, /trunk/jgogears/jgogears/SufgoEngine.java)
==============================================================================
--- /trunk/jgogears/jgogears/SufgoEngine.java (original)
+++ trunk/jgogears/jgogears/engine/SufgoEngine.java Mon Feb 25 22:58:37 2008
@@ -1,99 +1,100 @@
-package jgogears;
+package jgogears.engine;

-import java.util.Iterator;
+import jgogears.*;

-import jgogears.engine.*;

/**
- * TODO.
+ *
*
* @author syeates
*/
public class SufgoEngine extends SkeletonEngine {

/** The model. */
- Model model = null;
+ Model model = new Model();

/**
- * TODO.
+ * Create an engine with an empty model
*/
public SufgoEngine() {
-
+ // do nothing
}

-/**
- * TODO.
- *
- * @param status the status
- * @param state the state
- *
- * @return the move[]
- */
+ /**
+ * TODO.
+ *
+ * @param status
+ * the status
+ * @param state
+ * the state
+ * @return the move[]
+ */
public Move[] finalStatusList(String status, GTPState state) {
// TODO Auto-generated method stub
throw new Error("not implemented");
}

-/**
- * TODO.
- *
- * @return the engine name
- */
+ /**
+ * TODO.
+ *
+ * @return the engine name
+ */
public String getEngineName() {
return "SufgoEngine";
}

-/**
- * TODO.
- *
- * @return the engine version
- */
+ /**
+ * TODO.
+ *
+ * @return the engine version
+ */
public String getEngineVersion() {
return "0.0.0.1";
}

-/**
- * TODO.
- *
- * @param state the state
- *
- * @return the final score
- */
+ /**
+ * TODO.
+ *
+ * @param state
+ * the state
+ * @return the final score
+ */
public GTPScore getFinalScore(GTPState state) {
// TODO Auto-generated method stub
- return null;
+ throw new Error("not implemented");
}

-/**
- * TODO.
- *
- * @param colour the colour
- * @param state the state
- *
- * @return the move
- */
+ /**
+ * Get the best move for this state and colour
+ *
+ * @param colour
+ * the colour
+ * @param state
+ * the state
+ * @return the move
+ */
public Move regGenMove(int colour, GTPState state) {
BoardI board = state.getBoard();
- short size = board.getSize();

- Short[][] values = new Short[size][size];
- boolean invert = colour == BoardI.VERTEX_WHITE;
-
- for (short r = 0; r < size; r++) {
- for (short c = 0; c < size; c++) {
+ Vertex vertex = model.getBestScore(board, colour == BoardI.VERTEX_WHITE);
+
+ return new Move(vertex.getRow(), vertex.getColumn(), colour);
+ }

- for (short j = 0; j < 8; j++) {
- Iterator<Short> linear = new VertexLineariser(board, r, c, j,invert);
- while (linear.hasNext()) {
- Short s = linear.next();
- // System.out.print(" " + s + ", ");
- }
- }
- }
- }
+ /**
+ * get the model
+ * @return the model
+ */
+ public final Model getModel() {
+ return model;
+ }

- // TODO Auto-generated method stub
- return null;
+ /**
+ * set the model
+ * @param model the model to set
+ */
+ public final void setModel(Model model) {
+ this.model = model;
}

}

Added: trunk/jgogears/jgogears/engine/SufogoEngineTest.java
==============================================================================
--- (empty file)
+++ trunk/jgogears/jgogears/engine/SufogoEngineTest.java Mon Feb 25
22:58:37 2008
@@ -0,0 +1,40 @@
+/**
+ *
+ */
+package jgogears.engine;
+
+import junit.framework.TestCase;
+import jgogears.GTPScore;
+import jgogears.*;
+
+/**
+ * test TwoGTP with a pair of GnuGo Players
+ * @author syeates
+ *
+ */
+public class SufogoEngineTest extends TestCase {
+
+ public void testSimple() throws Exception {
+
+ // TODO make sure this terminates
+ GTPState state = new GTPState ();
+ Model model = new Model();
+ ModelTest.trainNFiles( 10, model);
+
+ SufgoEngine black = new SufgoEngine();
+ black.setModel(model);
+
+ SufgoEngine white = new SufgoEngine();
+ white.setModel(model);
+
+ TwoGTP two = new TwoGTP();
+ assertNotNull(two);
+ two.setBlack(new SufgoEngine());
+ two.setWhite(new SufgoEngine());
+ state = two.playOutGame();
+ GTPScore w = two.getWhite().getFinalScore(state);
+ GTPScore b = two.getBlack().getFinalScore(state);
+ assertTrue(w.equals(b));
+ }
+
+}

Reply all
Reply to author
Forward
0 new messages