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

0 views
Skip to first unread message

codesite...@google.com

unread,
Mar 18, 2008, 5:51:31 AM3/18/08
to jgog...@googlegroups.com
Author: syeates
Date: Tue Mar 18 02:50:45 2008
New Revision: 63

Added:
trunk/jgogears/jgogears/Random.java
trunk/jgogears/jgogears/engine/StraightVertexLineariser.java
trunk/jgogears/jgogears/engine/StraightVertexLineariserTest.java
Modified:
trunk/jgogears/jgogears/BoardI.java
trunk/jgogears/jgogears/GlobalTests.java
trunk/jgogears/jgogears/Move.java
trunk/jgogears/jgogears/NoKoRuleSet.java
trunk/jgogears/jgogears/RandomEngine.java
trunk/jgogears/jgogears/TwoGTP.java
trunk/jgogears/jgogears/Zobrist.java
trunk/jgogears/jgogears/engine/Engine.java
trunk/jgogears/jgogears/engine/Model.java
trunk/jgogears/jgogears/engine/ModelTest.java
trunk/jgogears/jgogears/engine/Scorer.java
trunk/jgogears/jgogears/engine/SufgoEngine.java
trunk/jgogears/jgogears/engine/Trainer.java
trunk/jgogears/jgogears/engine/VertexLineariser.java
trunk/jgogears/jgogears/engine/VertexLineariserTest.java

Log:
new linearisation method

Modified: trunk/jgogears/jgogears/BoardI.java
==============================================================================
--- trunk/jgogears/jgogears/BoardI.java (original)
+++ trunk/jgogears/jgogears/BoardI.java Tue Mar 18 02:50:45 2008
@@ -403,7 +403,7 @@
* the colour to set this to
*/
protected void setColour(int row, int column, int colour) {
- throw new Error();
+ throw new Error("concrete classes need to override this method " +
row + column + colour);
}

/**
@@ -424,6 +424,29 @@
@Override
public String toString() {
return BoardToASCII.Transform(this);
+ }
+
+ public boolean isOffBoard(int row, int column){
+ if (row < 0)
+ return true;
+ if (column < 0)
+ return true;
+ if (row >= this.getSize())
+ return true;
+ if (column < this.getSize())
+ return true;
+ return false;
+ }
+ public boolean isWayOffBoard(int row, int column){
+ if (row < -1)
+ return true;
+ if (column < -1)
+ return true;
+ if (row >= this.getSize()+1)
+ return true;
+ if (column >= this.getSize()+1)
+ return true;
+ return false;
}

}

Modified: trunk/jgogears/jgogears/GlobalTests.java
==============================================================================
--- trunk/jgogears/jgogears/GlobalTests.java (original)
+++ trunk/jgogears/jgogears/GlobalTests.java Tue Mar 18 02:50:45 2008
@@ -26,7 +26,7 @@
suite.addTestSuite(TreeIteratorTest.class);
suite.addTestSuite(NodeTest.class);
suite.addTestSuite(ModelTest.class);
- suite.addTestSuite(VertexLineariserTest.class);
+ suite.addTestSuite(StraightVertexLineariserTest.class);
suite.addTestSuite(MoveTest.class);
suite.addTestSuite(SGFPropertyTest.class);
suite.addTestSuite(SGFNodeTest.class);

Modified: trunk/jgogears/jgogears/Move.java
==============================================================================
--- trunk/jgogears/jgogears/Move.java (original)
+++ trunk/jgogears/jgogears/Move.java Tue Mar 18 02:50:45 2008
@@ -305,7 +305,7 @@
rowS = "z";
break;
default:
- throw new java.lang.InternalError();
+ throw new java.lang.InternalError("bad row="+s);
}
return rowS;
}

Modified: trunk/jgogears/jgogears/NoKoRuleSet.java
==============================================================================
--- trunk/jgogears/jgogears/NoKoRuleSet.java (original)
+++ trunk/jgogears/jgogears/NoKoRuleSet.java Tue Mar 18 02:50:45 2008
@@ -295,7 +295,7 @@
short row = move.getRow();
short column = move.getColumn();
short colour = move.getColour();
-
+
if (board.getColour(row, column) != BoardI.VERTEX_EMPTY) {
if (DEBUG)
System.err.println("illegal move, not empty");
@@ -303,7 +303,7 @@
}

if (colour != BoardI.VERTEX_BLACK && colour != BoardI.VERTEX_WHITE)
- throw new Error();
+ throw new Error("Internal error, bad colour");
TreeSet<Vertex> liberties = new TreeSet<Vertex>();

liberties.addAll(this.legelsfrompos(row + 1, column, colour, board));

Added: trunk/jgogears/jgogears/Random.java
==============================================================================
--- (empty file)
+++ trunk/jgogears/jgogears/Random.java Tue Mar 18 02:50:45 2008
@@ -0,0 +1,55 @@
+/**
+ *
+ */
+package jgogears;
+
+
+/**
+ * A random number generator
+ * @author syeates
+ *
+ */
+public class Random {
+ /** The random number generator */
+ private static final java.util.Random random = new java.util.Random();
+ /** A small value used as a small increment to probabilities */
+ public static final double DELTA = (double) 0.01;
+
+ /**
+ * Gets the random.
+ *
+ * @return the random
+ */
+ static public final double nextDouble() {
+ double r = random.nextDouble();
+ return r;
+ }
+ static public final boolean nextBoolean() {
+ boolean r = random.nextBoolean();
+ return r;
+ }
+ static public final int nextInt(int max) {
+ int r = random.nextInt(max);
+ return r;
+ }
+
+ /**
+ * Gets the random delta.
+ *
+ * @return the random delta
+ */
+ static public final double getRandomDelta() {
+ double r = random.nextDouble();
+ return r * DELTA;
+ }
+
+ static public final double getRandomBest(double first,double second){
+ if (first + getRandomDelta() >= second + getRandomDelta())
+ return first;
+ else
+ return second;
+ }
+
+
+
+}

Modified: trunk/jgogears/jgogears/RandomEngine.java
==============================================================================
--- trunk/jgogears/jgogears/RandomEngine.java (original)
+++ trunk/jgogears/jgogears/RandomEngine.java Tue Mar 18 02:50:45 2008
@@ -8,8 +8,6 @@
*/
public class RandomEngine extends SkeletonEngine implements
GTPInterface {

- /** The random. */
- public Random random = new Random(0);

/*
* (non-Javadoc)
@@ -60,8 +58,8 @@
short row;
short column;
do {
- row = (short) this.random.nextInt(state.boardsize);
- column = (short) this.random.nextInt(state.boardsize);
+ row = (short) Random.nextInt(state.boardsize);
+ column = (short) Random.nextInt(state.boardsize);
} while (state.board.getColour(row, column) == BoardI.VERTEX_EMPTY);
return new Move(row, column, colour);
}

Modified: trunk/jgogears/jgogears/TwoGTP.java
==============================================================================
--- trunk/jgogears/jgogears/TwoGTP.java (original)
+++ trunk/jgogears/jgogears/TwoGTP.java Tue Mar 18 02:50:45 2008
@@ -61,13 +61,14 @@
if (this.white == null)
throw new Error();
Move move = null;
+ BoardI oldBoard = this.state.getBoard();
if (this.blackNext) {
move = this.black.genMove(BoardI.VERTEX_BLACK, this.state);
} else {
move = this.white.genMove(BoardI.VERTEX_WHITE, this.state);
}
- if (!RuleSet.DEFAULT.moveIsLegal(null, this.state.getBoard(), move))
- throw new Error();
+ if (!RuleSet.DEFAULT.moveIsLegal(null, oldBoard, move))
+ throw new Error(move + "\n" + oldBoard);
if (move.getPass())
this.passes++;
else

Modified: trunk/jgogears/jgogears/Zobrist.java
==============================================================================
--- trunk/jgogears/jgogears/Zobrist.java (original)
+++ trunk/jgogears/jgogears/Zobrist.java Tue Mar 18 02:50:45 2008
@@ -29,14 +29,14 @@
static private void init() {
if (grid == null) {
grid = new BitSet[MAX_BOARD_SIZE][MAX_BOARD_SIZE][MAX_COLOUR];
- Random random = new Random(new Date().getTime());
+
for (int i = 0; i < MAX_BOARD_SIZE; i++)
for (int j = 0; j < MAX_BOARD_SIZE; j++)
for (int k = 0; k < MAX_COLOUR; k++) {
grid[i][j][k] = new BitSet();
// System.err.println("" + i + " " + j + " " + k);
for (int l = 0; l < ZOBRIST_SIZE; l++) {
- grid[i][j][k].set(l, random.nextBoolean());
+ grid[i][j][k].set(l, Random.nextBoolean());
}
}
}

Modified: trunk/jgogears/jgogears/engine/Engine.java
==============================================================================
--- trunk/jgogears/jgogears/engine/Engine.java (original)
+++ trunk/jgogears/jgogears/engine/Engine.java Tue Mar 18 02:50:45 2008
@@ -22,14 +22,16 @@
*/
public static void main(String[] args) throws IOException {

- GTPState state = new GTPState();
Model model = new Model();
System.out.println("about to train model");

Trainer trainer = new Trainer();
trainer.setModel(model);
- trainer.train(20);
+
+ trainer.train(200);
System.out.println("model trained");
+ System.out.println(model.getRoot());
+ System.out.println(model.getRoot().size());

SufgoEngine black = new SufgoEngine();
black.setModel(model);
@@ -37,15 +39,16 @@
SufgoEngine white = new SufgoEngine();
white.setModel(model);

- TwoGTP two = new TwoGTP();
+ for (int j = 0; j < 200; j++) {
+ TwoGTP two = new TwoGTP();
+ two.setBlack(black);
+ two.setWhite(white);

- two.setBlack(black);
- two.setWhite(white);
-
-
- for (int i = 0; i < 200; i++) {
- state = two.move();
- System.out.println(state.getBoard());
+ for (int i = 0; i < 200; i++) {
+ GTPState state = new GTPState();
+ state = two.move();
+ System.out.println(state.getBoard());
+ }
}

}

Modified: trunk/jgogears/jgogears/engine/Model.java
==============================================================================
--- trunk/jgogears/jgogears/engine/Model.java (original)
+++ trunk/jgogears/jgogears/engine/Model.java Tue Mar 18 02:50:45 2008
@@ -8,8 +8,6 @@
*/
public final class Model {

- /** A small value used as a small increment to probabilities */
- public static float DELTA = (float) 0.0001;

/** are we being verbose? */
public static boolean DEBUG = false;
@@ -19,26 +17,6 @@

/** The random number generator */
private static Random random = new Random();
-
- /**
- * Gets the random.
- *
- * @return the random
- */
- static public double getRandom() {
- double r = random.nextDouble();
- return r;
- }
-
- /**
- * Gets the random delta.
- *
- * @return the random delta
- */
- static public double getRandomDelta() {
- double r = random.nextDouble();
- return r * DELTA;
- }

/**
* Max of two doubles, biased towards the first in the case of equality

Modified: trunk/jgogears/jgogears/engine/ModelTest.java
==============================================================================
--- trunk/jgogears/jgogears/engine/ModelTest.java (original)
+++ trunk/jgogears/jgogears/engine/ModelTest.java Tue Mar 18 02:50:45 2008
@@ -195,23 +195,6 @@
}

/**
- * Test random.
- */
- public void testRandom() {
- double a = Model.getRandom();
- if (DEBUG)
- System.err.println("Random data = " + a);
- }
-
- /**
- * Test random delta.
- */
- public void testRandomDelta() {
- double a = Model.getRandomDelta();
- System.err.println("Random delta = " + a);
- }
-
- /**
* check some stats on some files.
*
* @throws IOException

Modified: trunk/jgogears/jgogears/engine/Scorer.java
==============================================================================
--- trunk/jgogears/jgogears/engine/Scorer.java (original)
+++ trunk/jgogears/jgogears/engine/Scorer.java Tue Mar 18 02:50:45 2008
@@ -13,6 +13,8 @@
public class Scorer {
/** are we being verbose? */
public static boolean DEBUG = false;
+ /** are we being verbose? */
+ public static boolean DEBUG_BRANCH = false;

/**
* Which vertex is the best to play?
@@ -25,22 +27,28 @@
* @return the vertex to play
*/
Vertex getBestScore(Model model, BoardI board, boolean white) {
+ if (DEBUG)
+ System.err.println("In getBestScore");
double[][] result = this.getScores(model, board, white);
double best = Double.MAX_VALUE;
- int I = 0, J = 0;
+ int I = -1, J = -1;
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]) {
+ && best > result[i][j]) {
best = result[i][j];
I = i;
J = j;
}
}
+ if (DEBUG)
+ System.err.print("{" + i + ","+ j + "},");
}
+ if (DEBUG)
+ System.err.println("exiting getBestScore");
return new Vertex(I, J);
}

@@ -61,7 +69,7 @@
result[row][column] = 0.0;
Node node = model.getRoot();
int maxdepth = 0;
- VertexLineariser linear = new VertexLineariser(board, row,
column, sym, white);
+ StraightVertexLineariser linear = new
StraightVertexLineariser(board, row, column, sym, white);
double estimate = 0.0;
int depth = 0;
while (linear.hasNext() && node != null) {
@@ -93,11 +101,10 @@
if (child != null) {
childP = child.getPlayed();
childNP = child.getNotPlayed();
- node = child;
estimate = estimate * 0.5 + childP / (childP + childNP) * 0.5;
}
-
- if (DEBUG)
+ node = child;
+ if (DEBUG_BRANCH)
System.err.println("Model::getScores following a " + BoardI.colourString(colour)
+ " branch, estimate = " + estimate + ", childP = " + childP
+ ", childNP = "
+ childNP + ", combination = " + childP / (childP + childNP)
* 0.5);
@@ -105,16 +112,19 @@

// estimate = (1 + previous.getPlayed()) / (previous.getPlayed()
+ previous.getNotPlayed()) * (1 -
// (1 /depth));
- if (result[row][column] <= estimate) {
- result[row][column] = estimate;
- if (DEBUG)
- System.err.println("Model::getScores " + estimate);
- }
- if (DEBUG)
- System.err.println("Model::getScores " + maxdepth);
+ result[row][column] = Random.getRandomBest(estimate,result[row][column]);
+ //if (DEBUG)
+ // System.err.println("Model::getScores " + maxdepth);
}
}
}
+ if (DEBUG)
+ for (int i = 0; i < result.length; i++) {
+ for (int j = 0; j < result[i].length; j++)
+ System.err.print(" " + result[i][j]);
+ System.err.println();
+ }
+
return result;
}


Added: trunk/jgogears/jgogears/engine/StraightVertexLineariser.java
==============================================================================
--- (empty file)
+++ trunk/jgogears/jgogears/engine/StraightVertexLineariser.java Tue
Mar 18 02:50:45 2008
@@ -0,0 +1,269 @@
+package jgogears.engine;
+
+import java.util.*;
+
+import jgogears.BoardI;
+
+/**
+ * The Class Straight Vertex Lineariser.
+ */
+final public class StraightVertexLineariser implements Iterator<Short> {
+
+ /** the SIZE of the board we've cached */
+ static final short BOARD_SIZE = 19;
+
+ /** the SIZE of the board plus the surrounding edge */
+ static final short SIZE = BOARD_SIZE + 2;
+
+ /** the maximum length of the sequence */
+ static final short SEQUENCE_SIZE = SIZE * SIZE * 2 * 2;
+
+ /** the static master sequence */
+ static short[][][] master = null;
+
+ /** The offset. */
+ protected int offset = 0;
+
+ /** The board. */
+ protected BoardI board = null;
+
+ /** The row. */
+ short row = -2;
+
+ /** The column. */
+ short column = -2;
+
+ /** The sym. */
+ short sym = -2;
+
+ /** Have the colours been inverted? */
+ boolean invert = false;
+
+ /** are we being verbose */
+ final static boolean PROGRESS = true;
+
+ /**
+ * static initialiser. inits the master array.
+ *
+ * @return true, if successful
+ */
+ static boolean init() {
+ if (master == null) {
+ master = new short[8][2][SEQUENCE_SIZE];
+ fillMasterL((short) 0, 0, true);
+ fillMasterL((short) 1, 1, true);
+ fillMasterL((short) 2, 2, true);
+ fillMasterL((short) 3, 3, true);
+ fillMasterR((short) 4, 0, false);
+ fillMasterR((short) 5, 1, false);
+ fillMasterR((short) 6, 2, false);
+ fillMasterR((short) 7, 3, false);
+ }
+ return true;
+ }
+
+ static final boolean isAreadyThere(int cnt, int row, int column) {
+ for (int l = 0; l < SEQUENCE_SIZE; l++)
+ if (master[cnt][0][l] == row && master[cnt][1][l] == column)
+ return true;
+ return false;
+ }
+
+ static final void fillMasterL(short cnt, int state, boolean reverse) {
+ for (int n = 0; n < 2; n++)
+ for (int l = 0; l < SEQUENCE_SIZE; l++)
+ master[cnt][n][l] = -5;
+
+ short row = 0;
+ short column = 0;
+ short counter = 0;
+ while (counter < SEQUENCE_SIZE) {
+ master[cnt][0][counter] = row;
+ master[cnt][1][counter] = column;
+ switch (state) {
+ case 0:
+ if (isAreadyThere(cnt, row, column - 1)) {
+ row++;
+ } else {
+ column--;
+ state = 1;
+ }
+ break;
+ case 1:
+ if (isAreadyThere(cnt, row - 1, column)) {
+ column--;
+ } else {
+ row--;
+ state = 2;
+ }
+ break;
+ case 2:
+ if (isAreadyThere(cnt, row, column + 1)) {
+ row--;
+ } else {
+ column++;
+ state = 3;
+ }
+ break;
+ case 3:
+ if (isAreadyThere(cnt, row + 1, column)) {
+ column++;
+ } else {
+ row++;
+ state = 0;
+ }
+ break;
+ default:
+ throw new Error("bad state " + state);
+ }
+ counter++;
+
+ }
+ }
+
+ static final void fillMasterR(short cnt, int state, boolean reverse) {
+ for (int n = 0; n < 2; n++)
+ for (int l = 0; l < SEQUENCE_SIZE; l++)
+ master[cnt][n][l] = -5;
+
+ short row = 0;
+ short column = 0;
+ short counter = 0;
+ while (counter < SEQUENCE_SIZE) {
+ master[cnt][0][counter] = row;
+ master[cnt][1][counter] = column;
+ switch (state) {
+ case 0:
+ if (isAreadyThere(cnt, row, column - 1)) {
+ row--;
+ } else {
+ column--;
+ state = 1;
+ }
+ break;
+ case 1:
+ if (isAreadyThere(cnt, row + 1, column)) {
+ column--;
+ } else {
+ row++;
+ state = 2;
+ }
+ break;
+ case 2:
+ if (isAreadyThere(cnt, row, column + 1)) {
+ row++;
+ } else {
+ column++;
+ state = 3;
+ }
+ break;
+ case 3:
+ if (isAreadyThere(cnt, row - 1, column)) {
+ column++;
+ } else {
+ row--;
+ state = 0;
+ }
+ break;
+ default:
+ throw new Error("bad state " + state);
+ }
+ counter++;
+
+ }
+ }
+
+ /**
+ * Instantiates a new vertex lineariser.
+ *
+ * @param board
+ * the board
+ * @param row
+ * the row
+ * @param column
+ * the column
+ * @param sym
+ * the sym
+ * @param invert
+ * are we inverting the colour?
+ */
+ public StraightVertexLineariser(BoardI board, short row, short
column, short sym, boolean invert) {
+ this.board = board;
+ this.row = row;
+ this.column = column;
+ this.sym = sym;
+ this.invert = invert;
+
+ init();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.util.Iterator#hasNext()
+ */
+ public boolean hasNext() {
+ return this.offset < SEQUENCE_SIZE - 1;
+ }
+
+ /**
+ * Invert a colour. Used when white is to play
+ *
+ * @param colour
+ * @return the inverted colour
+ */
+ public Short invert(Short colour) {
+ switch (colour.shortValue()) {
+ case BoardI.VERTEX_BLACK:
+ return BoardI.VERTEX_WHITE;
+ case BoardI.VERTEX_WHITE:
+ return BoardI.VERTEX_BLACK;
+ default:
+ return colour;
+
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.util.Iterator#next()
+ */
+ public Short next() {
+ if (!this.hasNext())
+ throw new NoSuchElementException();
+ // System.err.println("next() " + sym + " " + row + " " + column + " "
+ // + offset);
+ while (board.isWayOffBoard(master[this.sym][0][this.offset] +
this.row, master[this.sym][1][this.offset]
+ + this.column)
+ && hasNext())
+ offset++;
+
+ short c = this.board.getColour(master[this.sym][0][this.offset] +
this.row, master[this.sym][1][this.offset]
+ + this.column);
+ this.offset++;
+ if (this.invert)
+ return c;
+ else
+ return this.invert(c);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.util.Iterator#remove()
+ */
+ public void remove() {
+ throw new java.lang.UnsupportedOperationException();
+ }
+
+ @Override
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ while (this.hasNext())
+ buf.append(this.next()).append(' ');
+ ;
+ return buf.toString();
+ }
+
+}

Added: trunk/jgogears/jgogears/engine/StraightVertexLineariserTest.java
==============================================================================
--- (empty file)
+++ trunk/jgogears/jgogears/engine/StraightVertexLineariserTest.java
Tue Mar 18 02:50:45 2008
@@ -0,0 +1,734 @@
+package jgogears.engine;
+
+import java.io.StringReader;
+import java.util.Iterator;
+
+import jgogears.*;
+import junit.framework.TestCase;
+
+// TODO: Auto-generated Javadoc
+/**
+ * The Class VertexLineariserTest.
+ */
+public class StraightVertexLineariserTest extends TestCase {
+
+ /** Are we using verbose debugging?. */
+ public static final boolean DEBUG = false;
+
+ /**
+ * Identical linearisation.
+ *
+ * @param board
+ * the board
+ * @param rowa
+ * the rowa
+ * @param columna
+ * the columna
+ * @param rowb
+ * the rowb
+ * @param columnb
+ * the columnb
+ * @return true, if successful
+ */
+ boolean identicalLinearisation(BoardI board, short rowa, short
columna, short rowb, short columnb) {
+ for (short j = 0; j < 8; j++) {
+ boolean matchFound = false;
+ for (short i = 0; i < 8; i++) {
+ if (i != j) {
+ boolean differenceFound = false;
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
rowa, columna, j, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
rowb, columnb, i, false);
+ assertTrue(lineara != null);
+ assertTrue(linearb != null);
+ while (lineara.hasNext() && linearb.hasNext()
&& !differenceFound) {
+ Short a = lineara.next();
+ Short b = linearb.next();
+ assertTrue(a != null);
+ assertTrue(b != null);
+ if (a == null || b == null || !a.equals(b))
+ differenceFound = true;
+ }
+ if (!differenceFound)
+ matchFound = true;
+ }
+ }
+ if (!matchFound) {
+ if (DEBUG)
+ System.err.println(rowa + " " + columna + " " + rowb + " " +
columnb + " " + j);
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testAllDifferent() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white b2"));
+ board = board.newBoard(new Move("black k4"));
+ board = board.newBoard(new Move("white c3"));
+ board = board.newBoard(new Move("black g4"));
+ board = board.newBoard(new Move("white d4"));
+ board = board.newBoard(new Move("black h4"));
+ board = board.newBoard(new Move("white n4"));
+ board = board.newBoard(new Move("black b4"));
+ board = board.newBoard(new Move("white c4"));
+ board = board.newBoard(new Move("white n5"));
+ board = board.newBoard(new Move("black b5"));
+ board = board.newBoard(new Move("white c5"));
+
+ assertNotNull(board);
+ for (short row = 2; row < board.getSize() - 2; row++)
+ for (short column = 2; column < board.getSize() - 2; column++)
+ for (short j = 0; j < 8; j++)
+ for (short i = 0; i < 8; i++) {
+ if (i != j) {
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
row, column, j, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
row, column, i, false);
+ assertTrue(lineara != null);
+ assertTrue(linearb != null);
+ boolean differenceFound = false;
+ while (lineara.hasNext() && linearb.hasNext()) {
+ Short a = lineara.next();
+ Short b = linearb.next();
+ assertTrue(a != null);
+ assertTrue(b != null);
+ if (a == null || b == null)
+ throw new Error();
+ if (!a.equals(b))
+ differenceFound = true;
+ }
+ assertTrue(i + " " + j + " " + row + " " + column + "\n"
+ + new StraightVertexLineariser(board, row, column, j, false) + "\n"
+ + new StraightVertexLineariser(board, row, column, i, false), differenceFound);
+ }
+ }
+
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testAllDifferentII() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white b1"));
+
+ assertNotNull(board);
+ for (short j = 0; j < 8; j++)
+ for (short i = 0; i < 8; i++) {
+ if (i != j) {
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 2, (short) 2, j, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
(short) 2, (short) 2, i, false);
+ assertTrue(lineara != null);
+ assertTrue(linearb != null);
+ boolean differenceFound = false;
+ while (lineara.hasNext() && linearb.hasNext()) {
+ Short a = lineara.next();
+ Short b = linearb.next();
+ assertTrue(a != null);
+ assertTrue(b != null);
+ if (a == null || b == null)
+ throw new Error();
+ if (!a.equals(b))
+ differenceFound = true;
+ }
+ assertTrue(i + " " + j, differenceFound);
+ }
+ }
+
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testBoardFalseSymmetryI() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white l10"));
+ board = board.newBoard(new Move("black a1"));
+ board = board.newBoard(new Move("white k10"));
+ board = board.newBoard(new Move("black g18"));
+ if (DEBUG)
+ System.err.println(board);
+
+ assertNotNull(board);
+ for (short row = 0; row < board.getSize(); row++)
+ for (short column = 0; column < board.getSize(); column++) {
+ if (DEBUG)
+ for (short j = 0; j < 8; j++) {
+ Iterator<Short> linear = new StraightVertexLineariser(board,
row, column, j, false);
+ assertTrue(linear != null);
+ while (linear.hasNext()) {
+ Short s = linear.next();
+ assertTrue(s != null);
+ System.err.print(" " + s + ", ");
+ }
+ System.err.println();
+ }
+ if (DEBUG)
+ for (short j = 0; j < 8; j++) {
+ Iterator<Short> linear = new StraightVertexLineariser(board,
column, row, j, false);
+ assertTrue(linear != null);
+ while (linear.hasNext()) {
+ Short s = linear.next();
+ assertTrue(s != null);
+ System.err.print(" " + s + ", ");
+ }
+ System.err.println();
+ }
+
+ boolean result;
+ result = this.identicalLinearisation(board, row, column, column, row);
+ assertFalse(result);
+ }
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testBoardFalseSymmetryII() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white l10"));
+ board = board.newBoard(new Move("black a1"));
+ board = board.newBoard(new Move("white k10"));
+ board = board.newBoard(new Move("black g18"));
+ System.err.println(board);
+
+ assertNotNull(board);
+ for (short row = 0; row < board.getSize(); row++)
+ for (short column = 0; column < board.getSize(); column++) {
+
+ boolean result;
+ result = this.identicalLinearisation(board, row, column, (short)
(board.getSize() - row - 1),
+ (short) (board.getSize() - column - 1));
+ assertFalse(result);
+ }
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testBoardSymmetryI() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white k10"));
+ if (DEBUG)
+ System.err.println(board);
+
+ assertNotNull(board);
+ for (short row = 0; row < board.getSize(); row++)
+ for (short column = 0; column < board.getSize(); column++) {
+ boolean result;
+ result = this.identicalLinearisation(board, row, column, column, row);
+ assertTrue(result);
+ }
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testBoardSymmetryII() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white k10"));
+ if (DEBUG)
+ System.err.println(board);
+
+ assertNotNull(board);
+ for (short row = 0; row < board.getSize(); row++)
+ for (short column = 0; column < board.getSize(); column++) {
+ boolean result;
+ result = this.identicalLinearisation(board, row, column, (short)
(board.getSize() - row - 1),
+ (short) (board.getSize() - column - 1));
+ assertTrue(result);
+ }
+ }
+
+ /**
+ * test what happens when linearising boards of different sizes.
+ *
+ * @throws Exception
+ * the exception
+ */
+ public void testDifferenetSizes13() throws Exception {
+ try {
+ BoardI board = BoardI.newBoard();
+ StraightVertexLineariser lineariser = new
StraightVertexLineariser(board, (short) 2, (short) 2, (short) 0, false);
+ assertNotNull(lineariser);
+ board = BoardI.newBoard(13);
+ lineariser = new StraightVertexLineariser(board, (short) 2, (short)
2, (short) 0, false);
+ fail("shouldn't be able to linearise different sizes of board");
+ } catch (IllegalArgumentException e) {
+ assertNotNull(e);
+ }
+ }
+
+
+ /**
+ * test what happens when linearising boards of different sizes.
+ *
+ * @throws Exception
+ * the exception
+ */
+ public void testDifferenetSizes5() throws Exception {
+ try {
+ BoardI board = BoardI.newBoard(19);
+ StraightVertexLineariser lineariser = new
StraightVertexLineariser(board, (short) 2, (short) 2, (short) 0, false);
+ assertNotNull(lineariser);
+ board = BoardI.newBoard(9);
+ lineariser = new StraightVertexLineariser(board, (short) 2, (short)
2, (short) 0, false);
+ fail("shouldn't be able to linearise different sizes of board");
+ } catch (IllegalArgumentException e) {
+ assertNotNull(e);
+ }
+ }
+
+ /**
+ * test what happens when linearising boards of different sizes.
+ *
+ * @throws Exception
+ * the exception
+ */
+ public void testDifferenetSizes9() throws Exception {
+ try {
+ BoardI board = BoardI.newBoard(19);
+ StraightVertexLineariser lineariser = new
StraightVertexLineariser(board, (short) 2, (short) 2, (short) 0, false);
+ assertNotNull(lineariser);
+ board = BoardI.newBoard(9);
+ lineariser = new StraightVertexLineariser(board, (short) 2, (short)
2, (short) 0, false);
+ fail("shouldn't be able to linearise different sizes of board");
+ } catch (IllegalArgumentException e) {
+ assertNotNull(e);
+ }
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testEmptyBoardSymmetryI() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+
+ for (short row = 0; row < board.getSize(); row++)
+ for (short column = 0; column < board.getSize(); column++) {
+ for (short j = 0; j < 8; j++) {
+ Iterator<Short> linear = new StraightVertexLineariser(board, row,
column, j, false);
+ while (linear.hasNext()) {
+ Short s = linear.next();
+ if (DEBUG)
+ System.err.print(" " + s + ", ");
+ }
+ if (DEBUG)
+ System.err.println();
+ }
+ if (DEBUG)
+ System.err.println();
+ for (short j = 0; j < 8; j++) {
+ Iterator<Short> linear = new StraightVertexLineariser(board,
(short) (board.getSize() - row - 1),
+ (short) (board.getSize() - column - 1), j, false);
+ while (linear.hasNext()) {
+ Short s = linear.next();
+ if (DEBUG)
+ System.err.print(" " + s + ", ");
+ }
+ if (DEBUG)
+ System.err.println();
+ }
+ if (DEBUG)
+ System.err.println();
+ if (DEBUG)
+ System.err.println();
+
+ boolean result;
+ result = this.identicalLinearisation(board, row, column, (short)
(board.getSize() - row - 1),
+ (short) (board.getSize() - column - 1));
+ assertTrue(result);
+ }
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testEmptyBoardSymmetryII() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+
+ for (short row = 0; row < board.getSize(); row++)
+ for (short column = 0; column < board.getSize(); column++) {
+ boolean result;
+ result = this.identicalLinearisation(board, row, column, column, row);
+ assertTrue(result);
+ }
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testEmptyBoardSymmetryReimplematation() {
+ BoardI board = BoardI.newBoard();
+ short size = board.getSize();
+ boolean matches[][][] = new boolean[3][8][8];
+
+ for (short sym1 = 0; sym1 < 8; sym1++) {
+
+ for (short sym2 = 0; sym2 < 8; sym2++) {
+ matches[0][sym1][sym2] = true;
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 0, (short) 0, sym1, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
(short) (size - 1), (short) (size - 1), sym2,
+ false);
+ while (lineara.hasNext()) {
+ assertTrue(lineara.hasNext());
+ assertTrue(linearb.hasNext());
+ Short a = lineara.next();
+ Short b = linearb.next();
+ if (!a.equals(b))
+ matches[0][sym1][sym2] = false;
+ }
+ assertFalse(linearb.hasNext());
+ }
+
+ for (short sym2 = 0; sym2 < 8; sym2++) {
+ matches[1][sym1][sym2] = true;
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 0, (short) 0, sym1, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
(short) 0, (short) (size - 1), sym2, false);
+ while (lineara.hasNext()) {
+ assertTrue(lineara.hasNext());
+ assertTrue(linearb.hasNext());
+ Short a = lineara.next();
+ Short b = linearb.next();
+ if (!a.equals(b))
+ matches[1][sym1][sym2] = false;
+ }
+ assertFalse(linearb.hasNext());
+ }
+
+ for (short sym2 = 0; sym2 < 8; sym2++) {
+ matches[2][sym1][sym2] = true;
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 0, (short) 0, sym1, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
(short) (size - 1), (short) 0, sym2, false);
+ while (lineara.hasNext()) {
+ assertTrue(lineara.hasNext());
+ assertTrue(linearb.hasNext());
+ Short a = lineara.next();
+ Short b = linearb.next();
+ if (!a.equals(b))
+ matches[2][sym1][sym2] = false;
+ }
+ assertFalse(linearb.hasNext());
+ }
+ }
+ for (short corn = 0; corn < 3; corn++) {
+ for (short sym1 = 0; sym1 < 8; sym1++) {
+ boolean tfound= false;
+ boolean ffound= false;
+ for (short sym2 = 0; sym2 < 8; sym2++) {
+ if(DEBUG)
+ System.err.print(matches[corn][sym1][sym2] + " ");
+ if (matches[corn][sym1][sym2])
+ tfound = true;
+ else
+ ffound = true;
+ }
+ assertTrue(tfound);
+ assertTrue(ffound);
+ if(DEBUG)
+ System.err.println();
+ }
+ if(DEBUG)
+ System.err.println();
+ }
+ }
+
+ /**
+ * Test everything.
+ *
+ * @throws Exception
+ * the exception
+ */
+ public void testEverything() throws Exception {
+
+ String examples[] = {
+ // SGFParser.EXAMPLEONE,
+ SGFParser.EXAMPLETWO, SGFParser.EXAMPLETHREE,
SGFParser.EXAMPLEFOUR, SGFParser.EXAMPLEFIVE };
+
+ for (int i = 0; i < examples.length; i++) {
+ String example = examples[i];
+
+ StringReader reader = new StringReader(example);
+ jgogears.SGF.SGF parser = new jgogears.SGF.SGF(reader);
+ SGFGameTree tree = parser.gameTree();
+
+ assertTrue(parser != null);
+ assertTrue(parser.toString() != null);
+ assertTrue(parser.toString().length() > 0);
+
+ assertTrue(tree != null);
+ if (tree == null)
+ throw new Error();
+ assertTrue(tree.toString() != null);
+ assertTrue(tree.toString().length() > 0);
+
+ Game game = new Game(tree);
+ assertTrue(game != null);
+
+ Iterator<BoardI> iterator = game.getBoards();
+ assertTrue(iterator != null);
+ if (iterator == null)
+ throw new Error();
+ BoardI board = null;
+ while (iterator.hasNext()) {
+ board = iterator.next();
+ assertTrue(board != null);
+ for (short j = 0; j < 8; j++) {
+ Iterator<Short> linear = new StraightVertexLineariser(board,
(short) 2, (short) 2, j, false);
+ assertTrue(linear != null);
+ while (linear.hasNext()) {
+ Short s = linear.next();
+ assertTrue(s != null);
+ // System.out.print(" " + s + ", ");
+ }
+ }
+ // System.out.println();
+ }
+
+ }
+
+ }
+
+ /**
+ * make sure that all the linearisations are different.
+ */
+ public void testFirst() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white b2"));
+
+ assertNotNull(board);
+
+ if (DEBUG)
+ System.err.print("VertexLineariserTest::testFirst() Black = " + BoardI.parseColour("black"));
+ if (DEBUG)
+ System.err.print(" White = " + BoardI.parseColour("white"));
+ if (DEBUG)
+ System.err.print(" Empty = " + BoardI.VERTEX_EMPTY);
+ if (DEBUG)
+ System.err.print(" Off = " + BoardI.VERTEX_OFF_BOARD);
+ if (DEBUG)
+ System.err.println("");
+ for (short j = 0; j < 8; j++) {
+ Iterator<Short> linear = new StraightVertexLineariser(board,
(short) 1, (short) 1, j, false);
+ assertTrue(linear != null);
+ while (linear.hasNext()) {
+ Short s = linear.next();
+ assertTrue(s != null);
+ if (DEBUG)
+ System.err.print(" " + s + ", ");
+ }
+ if (DEBUG)
+ System.err.println();
+ }
+
+ for (short j = 0; j < 8; j++) {
+ if (DEBUG)
+ System.err.println("StraightVertexLineariserTest::testFirst sym="
+ j);
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 1, (short) 1, j, true);
+ assertTrue(lineara != null);
+ Short a = lineara.next();
+ assertTrue(a != null);
+ if (a == null)
+ throw new Error();
+ assertTrue(a + " " + BoardI.VERTEX_WHITE + "\n" + board,
a.equals(new Short(BoardI.VERTEX_WHITE)));
+ }
+ }
+
+ /**
+ * Test first all.
+ */
+ public void testFirstAll() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white b2"));
+
+ assertNotNull(board);
+
+ if (DEBUG)
+ System.err.print("VertexLineariserTest::testFirst() Black = " + BoardI.parseColour("black"));
+ if (DEBUG)
+ System.err.print(" White = " + BoardI.parseColour("white"));
+ if (DEBUG)
+ System.err.print(" Empty = " + BoardI.VERTEX_EMPTY);
+ if (DEBUG)
+ System.err.print(" Off = " + BoardI.VERTEX_OFF_BOARD);
+ if (DEBUG)
+ System.err.println("");
+ for (short row = 0; row < board.getSize(); row++)
+ for (short column = 0; column < board.getSize(); column++)
+ for (short j = 0; j < 8; j++) {
+ if (DEBUG)
+ System.err.println("StraightVertexLineariserTest::testFirst
sym=" + j);
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
row, column, j, true);
+ assertTrue(lineara != null);
+ Short a = lineara.next();
+ assertTrue(a != null);
+ if (a == null)
+ throw new Error();
+ assertTrue(a + " " + board.getColour(row, column) + "\n" + board,
a.equals(new Short(board
+ .getColour(row, column))));
+ }
+ }
+
+ /**
+ * Test trained model.
+ */
+ public void testSize() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ int count = 0;
+ Iterator<Short> linear = new StraightVertexLineariser(board, (short)
2, (short) 2, (short) 0, false);
+ while (linear.hasNext()) {
+ Short s = linear.next();
+ assertNotNull(s);
+ count++;
+ }
+ int plussize = StraightVertexLineariser.SIZE *StraightVertexLineariser.SIZE;
+
+ assertTrue(count + "/" + plussize+1, count == plussize+1);
+ }
+
+ /**
+ * Test trained model.
+ */
+ public void testVerbose() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white b2"));
+ board = board.newBoard(new Move("black k4"));
+ board = board.newBoard(new Move("white c3"));
+ board = board.newBoard(new Move("black g4"));
+ board = board.newBoard(new Move("white d4"));
+ board = board.newBoard(new Move("black h4"));
+ board = board.newBoard(new Move("white n4"));
+
+ assertNotNull(board);
+ if (DEBUG)
+
System.err.print("StraightVertexLineariserTest::testVertexLineariser()
Black = " + BoardI.parseColour("black"));
+ if (DEBUG)
+ System.err.print(" White = " + BoardI.parseColour("white"));
+ if (DEBUG)
+ System.err.print(" Empty = " + BoardI.VERTEX_EMPTY);
+ if (DEBUG)
+ System.err.print(" Off = " + BoardI.VERTEX_OFF_BOARD);
+ if (DEBUG)
+ System.err.println("");
+ for (short j = 0; j < 8; j++) {
+ Iterator<Short> linear = new StraightVertexLineariser(board,
(short) 2, (short) 2, j, false);
+ assertTrue(linear != null);
+ while (linear.hasNext()) {
+ Short s = linear.next();
+ assertTrue(s != null);
+ if (DEBUG)
+ System.err.print(" " + s + ", ");
+ }
+ if (DEBUG)
+ System.err.println();
+ }
+
+ }
+
+ /**
+ * Test trained model.
+ */
+ public void testVerboseII() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ board = board.newBoard(new Move("white b2"));
+ board = board.newBoard(new Move("black c3"));
+ board = board.newBoard(new Move("white a3"));
+ board = board.newBoard(new Move("black g4"));
+ board = board.newBoard(new Move("white d4"));
+ board = board.newBoard(new Move("black h4"));
+ board = board.newBoard(new Move("white n4"));
+
+ for (short j = 0; j < 8; j++) {
+ Iterator<Short> linear = new StraightVertexLineariser(board,
(short) 2, (short) 2, j, false);
+ assertTrue(linear != null);
+ if (DEBUG)
+ System.err.println(linear);
+ linear = new StraightVertexLineariser(board, (short) 2, (short) 2,
j, false);
+ }
+
+ }
+ /**
+ * Test initialised master
+ */
+ public void testMaster() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+
+ Iterator<Short> linear = new StraightVertexLineariser(board, (short)
2, (short) 2,(short) 0, false);
+ assertTrue(linear != null);
+ for (short j = 0; j < 8; j++) {
+ int totalboardsizea = board.getSize() *board.getSize();
+ int totalboardsizeb = board.getSize() *board.getSize();
+ int totalboardsizec = board.getSize() *board.getSize();
+ int totalboardsized = board.getSize() *board.getSize();
+ assertTrue(StraightVertexLineariser.master[j][0].length == StraightVertexLineariser.SEQUENCE_SIZE);
+ for (short seq = 0; seq <
StraightVertexLineariser.master[j][0].length; seq++) {
+ if (DEBUG && seq < 9)
+ System.err.print("{" + StraightVertexLineariser.master[j][0][seq]
+ "," + StraightVertexLineariser.master[j][1][seq] + "},");
+ if (board.getColour(StraightVertexLineariser.master[j][0][seq],
StraightVertexLineariser.master[j][1][seq]) == Board.VERTEX_EMPTY)
+ totalboardsizea --;
+ if (board.getColour(StraightVertexLineariser.master[j][0][seq]+18,
StraightVertexLineariser.master[j][1][seq]) == Board.VERTEX_EMPTY)
+ totalboardsizeb --;
+ if (board.getColour(StraightVertexLineariser.master[j][0][seq],
StraightVertexLineariser.master[j][1][seq]+18) == Board.VERTEX_EMPTY)
+ totalboardsizec --;
+ if (board.getColour(StraightVertexLineariser.master[j][0][seq]+18,
StraightVertexLineariser.master[j][1][seq]+18) == Board.VERTEX_EMPTY)
+ totalboardsized --;
+ }
+ if (DEBUG)
+ System.err.println();
+ assertTrue(totalboardsizea == 0);
+ assertTrue(totalboardsizeb == 0);
+ assertTrue(totalboardsizec == 0);
+ assertTrue(totalboardsized == 0);
+ }
+ }
+ /**
+ * Test initialised master
+ */
+ public void testMasterII() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ // this call makes sure that the master is initialised
+ Iterator<Short> linear = new StraightVertexLineariser(board, (short)
2, (short) 2,(short) 0, false);
+ assertTrue(linear != null);
+
+ for (short row= 0; row < board.getSize();row++)
+ for (short column= 0; column < board.getSize();column++)
+ for (short j = 0; j < 8; j++) {
+ boolean found = false;
+ for (short seq = 0; seq <
StraightVertexLineariser.master[j][0].length&& found == false; seq++)
+ if (StraightVertexLineariser.master[j][0][seq] == row &&
StraightVertexLineariser.master[j][1][seq] == column && found == false )
+ found = true;
+ assertTrue("" + row + " " + column + " " + j, found);
+ }
+ }
+ /**
+ * Test initialised master
+ */
+ public void testContants() {
+ BoardI board = BoardI.newBoard();
+ assertNotNull(board);
+ // this call makes sure that the master is initialised
+ Iterator<Short> linear = new StraightVertexLineariser(board, (short)
2, (short) 2,(short) 0, false);
+ assertTrue(linear != null);
+
+System.err.println("BOARD_SIZE=" + StraightVertexLineariser.BOARD_SIZE
+ " SIZE=" + StraightVertexLineariser.SIZE + " SEQUENCE_SIZE=" + StraightVertexLineariser.SEQUENCE_SIZE);
+System.err.println("master[this.sym][0].length=" + StraightVertexLineariser.master[0][0].length);
+ }
+}

Modified: trunk/jgogears/jgogears/engine/SufgoEngine.java
==============================================================================
--- trunk/jgogears/jgogears/engine/SufgoEngine.java (original)
+++ trunk/jgogears/jgogears/engine/SufgoEngine.java Tue Mar 18 02:50:45 2008
@@ -81,8 +81,10 @@
*/
public Move regGenMove(int colour, GTPState state) {
BoardI board = state.getBoard();
- Vertex vertex = new Scorer().getBestScore(this.model, board, colour
== BoardI.VERTEX_WHITE);
- return new Move(vertex.getRow(), vertex.getColumn(), colour);
+ Scorer scorer = new Scorer();
+ Vertex vertex = scorer.getBestScore(this.model, board, colour == BoardI.VERTEX_WHITE);
+ Move move = new Move(vertex.getRow(), vertex.getColumn(), colour);
+ return move;
}

/**

Modified: trunk/jgogears/jgogears/engine/Trainer.java
==============================================================================
--- trunk/jgogears/jgogears/engine/Trainer.java (original)
+++ trunk/jgogears/jgogears/engine/Trainer.java Tue Mar 18 02:50:45 2008
@@ -9,13 +9,13 @@
import jgogears.*;

/**
- *
* @author syeates
*/
public class Trainer {
static final private boolean DEBUG = false;
static final private boolean PROGRESS = true;
- private boolean onlyOneNewNodePerSymmetry = true;
+ static final private boolean freeRideForEmpty = true;
+ static final private boolean onlyOneNewNodePerSymmetry = true;
public final int DEFAULT_NUMBER_OF_FILES = Integer.MAX_VALUE;
private Model model = null;
final public static String LIBRARY = "sgf/2004-12";
@@ -79,7 +79,7 @@
*
* @return the model
*/
- public Model train() {
+ public Model train() {
return train(DEFAULT_NUMBER_OF_FILES);
}

@@ -90,39 +90,50 @@
* the count
* @return the model
*/
- public Model train(int count) {
+ public Model train(int count) {
try {
- Collection<String> files = loadAllSGFfiles();
- int filecount = 0;
- int examined = 0;
- Iterator<String> iterator = files.iterator();
- while (iterator.hasNext() && filecount < count) {
- String filename = iterator.next();
-
- Game game = Game.loadFromFile(new File(filename));
- examined++;
- if (game.getSize() == 19) {
- filecount++;
- train( game);
- } else {
- if (DEBUG)
- System.err.print("!");
+ Collection<String> files = loadAllSGFfiles();
+ int filecount = 0;
+ int examined = 0;
+ Iterator<String> iterator = files.iterator();
+ while (iterator.hasNext() && filecount < count) {
+ String filename = iterator.next();
+
+ Game game = Game.loadFromFile(new File(filename));
+ examined++;
+ if (game.getSize() == 19) {
+ filecount++;
+ train(game);
+ } else {
+ if (DEBUG)
+ System.err.print("!");

- }
- // if (PROGRESS || DEBUG)
- // System.err.print(".");
- if (PROGRESS)
- System.err.println(filecount + "/" + examined + "/" + files.size()
+ "/" + count + " "
- + model.getBoardsTrained() + "b " + model.size() + "n "
- + ((Runtime.getRuntime().totalMemory() / 1000) -
(Runtime.getRuntime().freeMemory() / 1000))
- + "K " + Runtime.getRuntime().totalMemory() / 1000 + "K " + Runtime.getRuntime().maxMemory()
- / 1000 + "K");
+ }
+ // if (PROGRESS || DEBUG)
+ // System.err.print(".");
+ if (PROGRESS)
+ System.err
+ .println(filecount
+ + "/"
+ + examined
+ + "/"
+ + files.size()
+ + "/"
+ + count
+ + " "
+ + model.getBoardsTrained()
+ + "b "
+ + model.size()
+ + "n "
+ + ((Runtime.getRuntime().totalMemory() / 1000) -
(Runtime.getRuntime().freeMemory() / 1000))
+ + "K " + Runtime.getRuntime().totalMemory() / 1000 + "K "
+ + Runtime.getRuntime().maxMemory() / 1000 + "K");

- }
+ }

- if (DEBUG)
- System.err.println("\nTrainer::trainNFiles loaded " + filecount + " files ");
- }catch (IOException e) {
+ if (DEBUG)
+ System.err.println("\nTrainer::trainNFiles loaded " + filecount
+ " files ");
+ } catch (IOException e) {
System.err.println(e);
return null;
}
@@ -160,26 +171,21 @@
int colour = move.getColour();
boolean isBlack = colour == BoardI.VERTEX_BLACK;
// float str = (float) (isBlack ? strengthB : strengthW);
-
- if (move != null)
- if ( move.getPlay()){
- for (short i = 0; i < size; i++)
- for (short j = 0; j < size; j++)
- for (short sym = 0; sym < 8; sym++) {
- VertexLineariser linear = new VertexLineariser(board, i, j, sym, !isBlack);
- if (move.getRow() != i && move.getColumn() != j)
- train( linear, true, true, 100);//TODO
- else
- train( linear, false, true, 100);//TODO
- }
- } else if ( move.getPass()) {
+ if (move.getResign())
+ return;
+ else
for (short i = 0; i < size; i++)
for (short j = 0; j < size; j++)
for (short sym = 0; sym < 8; sym++) {
- VertexLineariser linear = new VertexLineariser(board, i, j, sym, !isBlack);
- train(linear, false, true, 100);//TODO
+ StraightVertexLineariser linear = new
StraightVertexLineariser(board, i, j, sym, !isBlack);
+ if (move.getPlay())
+ if (move.getRow() != i && move.getColumn() != j)
+ train(linear, true, true, 100);// TODO
+ else
+ train(linear, false, true, 100);// TODO
+ else if (move.getPass())
+ train(linear, false, true, 100);// TODO
}
- }
}
}

@@ -192,75 +198,76 @@
* are we expanding?
* @param depth
* the depth to expand to
- * @param root
- * the root of the model
* @param playeda
* the played
*/
- public void train(VertexLineariser linear, boolean playeda, boolean
expand, int depth) {
+ public void train(StraightVertexLineariser linear, boolean playeda,
boolean expand, int depth) {
Node root = model.getRoot();
- while (root !=null && linear.hasNext()){
- if (depth <= 0)
- expand = false;
- if (root.getNotPlayed() + root.getPlayed() < this.getMinBranchSize())
- expand = false;
- depth--;
- if (playeda)
- root.setPlayed(root.getPlayed() + 1);
- else
- root.setNotPlayed(root.getNotPlayed() + 1);
- Short colour = linear.next();
- boolean expandMore = !onlyOneNewNodePerSymmetry;
-
- switch (colour) {
- case BoardI.VERTEX_BLACK:
- if (root.getBlack() == null)
- if (expand) {
- root.setBlack(new Node());
- root = root.getBlack();
- expand = expandMore;
- } else
- return;
+ boolean freeRideUsed = false;
+ while (root != null && linear.hasNext()) {
+ if (depth <= 0)
+ expand = false;
+ if (root.getNotPlayed() + root.getPlayed() < this.getMinBranchSize())
+ expand = false;
+ depth--;
+ if (playeda)
+ root.setPlayed(root.getPlayed() + 1);
else
- root = root.getBlack();
- break;
- case BoardI.VERTEX_WHITE:
- if (root.getWhite() == null)
- if (expand) {
- root.setWhite(new Node());
+ root.setNotPlayed(root.getNotPlayed() + 1);
+ Short colour = linear.next();
+ boolean expandMore = !onlyOneNewNodePerSymmetry;
+
+ switch (colour) {
+ case BoardI.VERTEX_BLACK:
+ freeRideUsed = true;
+ if (root.getBlack() == null)
+ if (expand) {
+ root.setBlack(new Node());
+ root = root.getBlack();
+ expand = expandMore;
+ } else
+ return;
+ else
+ root = root.getBlack();
+ break;
+ case BoardI.VERTEX_WHITE:
+ freeRideUsed = true;
+ if (root.getWhite() == null)
+ if (expand) {
+ root.setWhite(new Node());
+ root = root.getWhite();
+ expand = expandMore;
+ } else
+ return;
+ else
root = root.getWhite();
- expand = expandMore;
- } else
- return;
- else
- root = root.getWhite();
- break;
- case BoardI.VERTEX_KO:
- case BoardI.VERTEX_EMPTY:
- if (root.getEmpty() == null)
- if (expand) {
- root.setEmpty(new Node());
- expand = expandMore;
+ break;
+ case BoardI.VERTEX_KO:
+ case BoardI.VERTEX_EMPTY:
+ if (root.getEmpty() == null)
+ if (expand || (!freeRideUsed && freeRideForEmpty)) {
+ root.setEmpty(new Node());
+ expand = expandMore;
+ root = root.getEmpty();
+ } else
+ return;
+ else
root = root.getEmpty();
- } else
- return;
- else
- root = root.getEmpty();
- break;
- case BoardI.VERTEX_OFF_BOARD:
- if (root.getOff() == null)
- if (expand) {
- root.setOff(new Node());
- expand = expandMore;
+ break;
+ case BoardI.VERTEX_OFF_BOARD:
+ if (root.getOff() == null)
+ if (expand|| (!freeRideUsed && freeRideForEmpty)) {
+ root.setOff(new Node());
+ expand = expandMore;
+ root = root.getOff();
+ } else
+ return;
+ else
root = root.getOff();
- } else
- return;
- else
- root = root.getOff();
- break;
- default:
- throw new Error();
- }
+ break;
+ default:
+ throw new Error();
+ }
}
}

@@ -274,27 +281,17 @@
}

/**
- * set the onlyOneNewNodePerSymmetry
- *
- * @param onlyOneNewNodePerSymmetry
- * the onlyOneNewNodePerSymmetry to set
- */
- public final void setOnlyOneNewNodePerSymmetry(boolean
onlyOneNewNodePerSymmetry) {
- this.onlyOneNewNodePerSymmetry = onlyOneNewNodePerSymmetry;
- }
-
- /**
* get the minBranchSize
*
* @return the minBranchSize
*/
public final double getMinBranchSize() {
- return model.getGamesTrained()*2 + 10;
+ return model.getGamesTrained() * 3 + 20;
}

-
/**
* get the model
+ *
* @return the model
*/
public final Model getModel() {
@@ -303,7 +300,9 @@

/**
* set the model
- * @param model the model to set
+ *
+ * @param model
+ * the model to set
*/
public final void setModel(Model model) {
this.model = model;

Modified: trunk/jgogears/jgogears/engine/VertexLineariser.java
==============================================================================
--- trunk/jgogears/jgogears/engine/VertexLineariser.java (original)
+++ trunk/jgogears/jgogears/engine/VertexLineariser.java Tue Mar 18
02:50:45 2008
@@ -166,7 +166,7 @@
}
double d = Math.pow(row - i + row_offset, 2) + Math.pow(column
- j + column_offset, 2);

- ArrayList<Short> array = new ArrayList<Short>();
+ ArrayList<Short> array = new ArrayList<Short>(2);
array.add(new Short(i));
array.add(new Short(j));

@@ -181,8 +181,9 @@
for (int i = 0; i < SIZE * SIZE; i++) {
if (values.isEmpty())
throw new Error();
- Short[] thisone = values.get(values.firstKey()).toArray(array);
- values.remove(values.firstKey());
+ Double key = values.firstKey();
+ Short[] thisone = values.get(key).toArray(array);
+ values.remove(key);
// rowsequence[i] = thisone[0];
// columnsequence[i] = thisone[1];
cache[0][sym][row][column][i] = thisone[0];

Modified: trunk/jgogears/jgogears/engine/VertexLineariserTest.java
==============================================================================
--- trunk/jgogears/jgogears/engine/VertexLineariserTest.java (original)
+++ trunk/jgogears/jgogears/engine/VertexLineariserTest.java Tue Mar 18
02:50:45 2008
@@ -36,8 +36,8 @@
for (short i = 0; i < 8; i++) {
if (i != j) {
boolean differenceFound = false;
- Iterator<Short> lineara = new VertexLineariser(board, rowa,
columna, j, false);
- Iterator<Short> linearb = new VertexLineariser(board, rowb,
columnb, i, false);
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
rowa, columna, j, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
rowb, columnb, i, false);
assertTrue(lineara != null);
assertTrue(linearb != null);
while (lineara.hasNext() && linearb.hasNext()
&& !differenceFound) {
@@ -83,8 +83,8 @@
for (short j = 0; j < 8; j++)
for (short i = 0; i < 8; i++) {
if (i != j) {
- Iterator<Short> lineara = new VertexLineariser(board, row,
column, j, false);
- Iterator<Short> linearb = new VertexLineariser(board, row,
column, i, false);
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
row, column, j, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
row, column, i, false);
assertTrue(lineara != null);
assertTrue(linearb != null);
boolean differenceFound = false;
@@ -99,8 +99,8 @@
differenceFound = true;
}
assertTrue(i + " " + j + " " + row + " " + column + "\n"
- + new VertexLineariser(board, row, column, j, false) + "\n"
- + new VertexLineariser(board, row, column, i, false), differenceFound);
+ + new StraightVertexLineariser(board, row, column, j, false) + "\n"
+ + new StraightVertexLineariser(board, row, column, i, false), differenceFound);
}
}

@@ -118,8 +118,8 @@
for (short j = 0; j < 8; j++)
for (short i = 0; i < 8; i++) {
if (i != j) {
- Iterator<Short> lineara = new VertexLineariser(board, (short) 2,
(short) 2, j, false);
- Iterator<Short> linearb = new VertexLineariser(board, (short) 2,
(short) 2, i, false);
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 2, (short) 2, j, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
(short) 2, (short) 2, i, false);
assertTrue(lineara != null);
assertTrue(linearb != null);
boolean differenceFound = false;
@@ -157,7 +157,7 @@
for (short column = 0; column < board.getSize(); column++) {
if (DEBUG)
for (short j = 0; j < 8; j++) {
- Iterator<Short> linear = new VertexLineariser(board, row,
column, j, false);
+ Iterator<Short> linear = new StraightVertexLineariser(board,
row, column, j, false);
assertTrue(linear != null);
while (linear.hasNext()) {
Short s = linear.next();
@@ -168,7 +168,7 @@
}
if (DEBUG)
for (short j = 0; j < 8; j++) {
- Iterator<Short> linear = new VertexLineariser(board, column,
row, j, false);
+ Iterator<Short> linear = new StraightVertexLineariser(board,
column, row, j, false);
assertTrue(linear != null);
while (linear.hasNext()) {
Short s = linear.next();
@@ -255,10 +255,10 @@
public void testDifferenetSizes13() throws Exception {
try {
BoardI board = BoardI.newBoard();
- VertexLineariser lineariser = new VertexLineariser(board, (short)
2, (short) 2, (short) 0, false);
+ StraightVertexLineariser lineariser = new
StraightVertexLineariser(board, (short) 2, (short) 2, (short) 0, false);
assertNotNull(lineariser);
board = BoardI.newBoard(13);
- lineariser = new VertexLineariser(board, (short) 2, (short) 2,
(short) 0, false);
+ lineariser = new StraightVertexLineariser(board, (short) 2, (short)
2, (short) 0, false);
fail("shouldn't be able to linearise different sizes of board");
} catch (IllegalArgumentException e) {
assertNotNull(e);
@@ -274,10 +274,10 @@
public void testDifferenetSizes25() throws Exception {
try {
BoardI board = BoardI.newBoard();
- VertexLineariser lineariser = new VertexLineariser(board, (short)
2, (short) 2, (short) 0, false);
+ StraightVertexLineariser lineariser = new
StraightVertexLineariser(board, (short) 2, (short) 2, (short) 0, false);
assertNotNull(lineariser);
board = BoardI.newBoard(25);
- lineariser = new VertexLineariser(board, (short) 2, (short) 2,
(short) 0, false);
+ lineariser = new StraightVertexLineariser(board, (short) 2, (short)
2, (short) 0, false);
fail("shouldn't be able to linearise different sizes of board");
} catch (IllegalArgumentException e) {
assertNotNull(e);
@@ -293,10 +293,10 @@
public void testDifferenetSizes5() throws Exception {
try {
BoardI board = BoardI.newBoard(19);
- VertexLineariser lineariser = new VertexLineariser(board, (short)
2, (short) 2, (short) 0, false);
+ StraightVertexLineariser lineariser = new
StraightVertexLineariser(board, (short) 2, (short) 2, (short) 0, false);
assertNotNull(lineariser);
board = BoardI.newBoard(9);
- lineariser = new VertexLineariser(board, (short) 2, (short) 2,
(short) 0, false);
+ lineariser = new StraightVertexLineariser(board, (short) 2, (short)
2, (short) 0, false);
fail("shouldn't be able to linearise different sizes of board");
} catch (IllegalArgumentException e) {
assertNotNull(e);
@@ -312,10 +312,10 @@
public void testDifferenetSizes9() throws Exception {
try {
BoardI board = BoardI.newBoard(19);
- VertexLineariser lineariser = new VertexLineariser(board, (short)
2, (short) 2, (short) 0, false);
+ StraightVertexLineariser lineariser = new
StraightVertexLineariser(board, (short) 2, (short) 2, (short) 0, false);
assertNotNull(lineariser);
board = BoardI.newBoard(9);
- lineariser = new VertexLineariser(board, (short) 2, (short) 2,
(short) 0, false);
+ lineariser = new StraightVertexLineariser(board, (short) 2, (short)
2, (short) 0, false);
fail("shouldn't be able to linearise different sizes of board");
} catch (IllegalArgumentException e) {
assertNotNull(e);
@@ -332,7 +332,7 @@
for (short row = 0; row < board.getSize(); row++)
for (short column = 0; column < board.getSize(); column++) {
for (short j = 0; j < 8; j++) {
- Iterator<Short> linear = new VertexLineariser(board, row, column,
j, false);
+ Iterator<Short> linear = new StraightVertexLineariser(board, row,
column, j, false);
while (linear.hasNext()) {
Short s = linear.next();
if (DEBUG)
@@ -344,7 +344,7 @@
if (DEBUG)
System.err.println();
for (short j = 0; j < 8; j++) {
- Iterator<Short> linear = new VertexLineariser(board, (short)
(board.getSize() - row - 1),
+ Iterator<Short> linear = new StraightVertexLineariser(board,
(short) (board.getSize() - row - 1),
(short) (board.getSize() - column - 1), j, false);
while (linear.hasNext()) {
Short s = linear.next();
@@ -393,8 +393,8 @@

for (short sym2 = 0; sym2 < 8; sym2++) {
matches[0][sym1][sym2] = true;
- Iterator<Short> lineara = new VertexLineariser(board, (short) 0,
(short) 0, sym1, false);
- Iterator<Short> linearb = new VertexLineariser(board, (short)
(size - 1), (short) (size - 1), sym2,
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 0, (short) 0, sym1, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
(short) (size - 1), (short) (size - 1), sym2,
false);
while (lineara.hasNext()) {
assertTrue(lineara.hasNext());
@@ -409,8 +409,8 @@

for (short sym2 = 0; sym2 < 8; sym2++) {
matches[1][sym1][sym2] = true;
- Iterator<Short> lineara = new VertexLineariser(board, (short) 0,
(short) 0, sym1, false);
- Iterator<Short> linearb = new VertexLineariser(board, (short) 0,
(short) (size - 1), sym2, false);
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 0, (short) 0, sym1, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
(short) 0, (short) (size - 1), sym2, false);
while (lineara.hasNext()) {
assertTrue(lineara.hasNext());
assertTrue(linearb.hasNext());
@@ -424,8 +424,8 @@

for (short sym2 = 0; sym2 < 8; sym2++) {
matches[2][sym1][sym2] = true;
- Iterator<Short> lineara = new VertexLineariser(board, (short) 0,
(short) 0, sym1, false);
- Iterator<Short> linearb = new VertexLineariser(board, (short)
(size - 1), (short) 0, sym2, false);
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 0, (short) 0, sym1, false);
+ Iterator<Short> linearb = new StraightVertexLineariser(board,
(short) (size - 1), (short) 0, sym2, false);
while (lineara.hasNext()) {
assertTrue(lineara.hasNext());
assertTrue(linearb.hasNext());
@@ -500,7 +500,7 @@
board = iterator.next();
assertTrue(board != null);
for (short j = 0; j < 8; j++) {
- Iterator<Short> linear = new VertexLineariser(board, (short) 2,
(short) 2, j, false);
+ Iterator<Short> linear = new StraightVertexLineariser(board,
(short) 2, (short) 2, j, false);
assertTrue(linear != null);
while (linear.hasNext()) {
Short s = linear.next();
@@ -526,7 +526,7 @@
assertNotNull(board);

if (DEBUG)
- System.err.print("VertexLineariserTest::testFirst() Black = " + BoardI.parseColour("black"));
+ System.err.print("StraightVertexLineariserTest::testFirst() Black
= " + BoardI.parseColour("black"));
if (DEBUG)
System.err.print(" White = " + BoardI.parseColour("white"));
if (DEBUG)
@@ -536,7 +536,7 @@
if (DEBUG)
System.err.println("");
for (short j = 0; j < 8; j++) {
- Iterator<Short> linear = new VertexLineariser(board, (short) 1,
(short) 1, j, false);
+ Iterator<Short> linear = new StraightVertexLineariser(board,
(short) 1, (short) 1, j, false);
assertTrue(linear != null);
while (linear.hasNext()) {
Short s = linear.next();
@@ -550,8 +550,8 @@

for (short j = 0; j < 8; j++) {
if (DEBUG)
- System.err.println("VertexLineariserTest::testFirst sym=" + j);
- Iterator<Short> lineara = new VertexLineariser(board, (short) 1,
(short) 1, j, true);
+ System.err.println("StraightVertexLineariserTest::testFirst sym="
+ j);
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
(short) 1, (short) 1, j, true);
assertTrue(lineara != null);
Short a = lineara.next();
assertTrue(a != null);
@@ -572,7 +572,7 @@
assertNotNull(board);

if (DEBUG)
- System.err.print("VertexLineariserTest::testFirst() Black = " + BoardI.parseColour("black"));
+ System.err.print("StraightVertexLineariserTest::testFirst() Black
= " + BoardI.parseColour("black"));
if (DEBUG)
System.err.print(" White = " + BoardI.parseColour("white"));
if (DEBUG)
@@ -585,8 +585,8 @@
for (short column = 0; column < board.getSize(); column++)
for (short j = 0; j < 8; j++) {
if (DEBUG)
- System.err.println("VertexLineariserTest::testFirst sym=" + j);
- Iterator<Short> lineara = new VertexLineariser(board, row,
column, j, true);
+ System.err.println("StraightVertexLineariserTest::testFirst
sym=" + j);
+ Iterator<Short> lineara = new StraightVertexLineariser(board,
row, column, j, true);
assertTrue(lineara != null);
Short a = lineara.next();
assertTrue(a != null);
@@ -604,7 +604,7 @@
BoardI board = BoardI.newBoard();
assertNotNull(board);
int count = 0;
- Iterator<Short> linear = new VertexLineariser(board, (short) 2,
(short) 2, (short) 0, false);
+ Iterator<Short> linear = new StraightVertexLineariser(board, (short)
2, (short) 2, (short) 0, false);
while (linear.hasNext()) {
Short s = linear.next();
assertNotNull(s);
@@ -631,7 +631,7 @@

assertNotNull(board);
if (DEBUG)
- System.err.print("VertexLineariserTest::testVertexLineariser()
Black = " + BoardI.parseColour("black"));
+
System.err.print("StraightVertexLineariserTest::testStraightVertexLineariser()
Black = " + BoardI.parseColour("black"));
if (DEBUG)
System.err.print(" White = " + BoardI.parseColour("white"));
if (DEBUG)
@@ -641,7 +641,7 @@
if (DEBUG)
System.err.println("");
for (short j = 0; j < 8; j++) {
- Iterator<Short> linear = new VertexLineariser(board, (short) 2,
(short) 2, j, false);
+ Iterator<Short> linear = new StraightVertexLineariser(board,
(short) 2, (short) 2, j, false);
assertTrue(linear != null);
while (linear.hasNext()) {
Short s = linear.next();

Reply all
Reply to author
Forward
0 new messages