Modified:
trunk/jgogears/.classpath
trunk/jgogears/jgogears/TwoGTP.java
trunk/jgogears/jgogears/engine/Engine.java
trunk/jgogears/jgogears/engine/Model.java
trunk/jgogears/jgogears/engine/ModelTest.java
trunk/jgogears/jgogears/engine/SufogoEngineTest.java
trunk/jgogears/jgogears/engine/Trainer.java
Log:
more trainer tests, Engine updates
Modified: trunk/jgogears/.classpath
==============================================================================
--- trunk/jgogears/.classpath (original)
+++ trunk/jgogears/.classpath Mon Mar 10 12:09:55 2008
@@ -1,7 +1,7 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="src" path=""/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3.8.1"/>
- <classpathentry kind="output" path=""/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path=""/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3.8.1"/>
+ <classpathentry kind="output" path=""/>
+</classpath>
Modified: trunk/jgogears/jgogears/TwoGTP.java
==============================================================================
--- trunk/jgogears/jgogears/TwoGTP.java (original)
+++ trunk/jgogears/jgogears/TwoGTP.java Mon Mar 10 12:09:55 2008
@@ -9,7 +9,7 @@
public class TwoGTP {
/** are we spewing debugging information? */
- public static final boolean DEBUG = false;
+ public static final boolean DEBUG = true;
/** The black player. */
private GTPInterface black = null;
@@ -22,6 +22,8 @@
private int passes = 0;
private boolean blackNext = true;
+
+ private RuleSet rules = new NoKoRuleSet();
/**
* get the black
@@ -78,9 +80,6 @@
this.passes = 0;
this.blackNext = true;
}
- if (DEBUG)
- System.err.println("TwoGTP: playing " + move);
- this.state.playMove(move);
if (DEBUG)
System.err.println("TwoGTP: played " + move);
if (DEBUG)
Modified: trunk/jgogears/jgogears/engine/Engine.java
==============================================================================
--- trunk/jgogears/jgogears/engine/Engine.java (original)
+++ trunk/jgogears/jgogears/engine/Engine.java Mon Mar 10 12:09:55 2008
@@ -26,7 +26,7 @@
Model model = new Model();
System.out.println("about to train model");
- new Trainer().trainFiles(100, model);
+ new Trainer().train(20,model);
System.out.println("model trained");
SufgoEngine black = new SufgoEngine();
Modified: trunk/jgogears/jgogears/engine/Model.java
==============================================================================
--- trunk/jgogears/jgogears/engine/Model.java (original)
+++ trunk/jgogears/jgogears/engine/Model.java Mon Mar 10 12:09:55 2008
@@ -73,9 +73,9 @@
return b;
}
- private final int boardsTrained = 0;
+ private int boardsTrained = 0;
- private final int gamesTrained = 0;
+ private int gamesTrained = 0;
/** The root of the model tree */
private final Node root = new Node();
@@ -95,4 +95,53 @@
return this.root;
}
+ /**
+ * get the boardsTrained
+ *
+ * @return the boardsTrained
+ */
+ public final int getBoardsTrained() {
+ if (DEBUG)
+ System.err.print(":");
+ return boardsTrained;
+ }
+
+ /**
+ * get the gamesTrained
+ *
+ * @return the gamesTrained
+ */
+ public final int getGamesTrained() {
+ return gamesTrained;
+ }
+
+ /**
+ * set the boardsTrained
+ *
+ * @param boardsTrained
+ * the boardsTrained to set
+ */
+ public final void setBoardsTrained(int boardsTrained) {
+ if (DEBUG)
+ System.err.print(";");
+ this.boardsTrained = boardsTrained;
+ }
+
+ /**
+ * set the gamesTrained
+ *
+ * @param gamesTrained
+ * the gamesTrained to set
+ */
+ public final void setGamesTrained(int gamesTrained) {
+ this.gamesTrained = gamesTrained;
+ }
+ /**
+ * get the size of the model
+ *
+ * @return the size
+ */
+ public int size() {
+ return root.size();
+ }
}
Modified: trunk/jgogears/jgogears/engine/ModelTest.java
==============================================================================
--- trunk/jgogears/jgogears/engine/ModelTest.java (original)
+++ trunk/jgogears/jgogears/engine/ModelTest.java Mon Mar 10 12:09:55 2008
@@ -27,7 +27,7 @@
public ModelTest() throws IOException {
if (model == null) {
model = new Model();
- new Trainer().trainFiles(10, model);
+ new Trainer().train(10, model);
}
}
Modified: trunk/jgogears/jgogears/engine/SufogoEngineTest.java
==============================================================================
--- trunk/jgogears/jgogears/engine/SufogoEngineTest.java (original)
+++ trunk/jgogears/jgogears/engine/SufogoEngineTest.java Mon Mar 10
12:09:55 2008
@@ -27,7 +27,7 @@
GTPState state = new GTPState();
Model model = new Model();
- new Trainer().trainFiles(10, model);
+ new Trainer().train(10, model);
SufgoEngine black = new SufgoEngine();
black.setModel(model);
Modified: trunk/jgogears/jgogears/engine/Trainer.java
==============================================================================
--- trunk/jgogears/jgogears/engine/Trainer.java (original)
+++ trunk/jgogears/jgogears/engine/Trainer.java Mon Mar 10 12:09:55 2008
@@ -15,8 +15,67 @@
*/
public class Trainer {
static final private boolean DEBUG = false;
+ static final private boolean PROGRESS = true;
+ private boolean onlyOneNewNodePerSymmetry = true;
private int minBranchSize = 20;
- private int defaultNumberOfFiles = 100;
+ private int defaultNumberOfFiles = Integer.MAX_VALUE;
+ final public static String LIBRARY = "sgf/2004-12";
+
+ /**
+ * Loads all the default SGF files
+ *
+ * @param directory
+ * the directory to load
+ * @throws IOException
+ * Signals that an I/O exception has occurred.
+ * @return a Collection of Strings
+ */
+ public static Collection<String> loadAllSGFfiles() throws IOException {
+ return loadAllSGFfiles(LIBRARY);
+ }
+
+ /**
+ * Loads all SGF files in a directory
+ *
+ * @param directory
+ * the directory to load
+ * @throws IOException
+ * Signals that an I/O exception has occurred.
+ * @return a Collection of Strings
+ */
+ public static Collection<String> loadAllSGFfiles(String directory)
throws IOException {
+ Stack<String> files = new Stack<String>();
+ files.push(directory);
+ Stack<String> result = new Stack<String>();
+
+ while (files.size() > 0) {
+ String filename = files.pop();
+ File file = new File(filename);
+ if (DEBUG)
+ System.err.println("examining \"" + filename + "\"");
+ if (file.exists()) {
+ if (!file.isDirectory()) {
+ // System.err.println("\"" + filename + "\" is not a
+ // directory, parsing as an SGF file");
+ if (file.getName().endsWith(".sgf"))
+ result.push(file.getCanonicalPath());
+
+ } else {
+ if (DEBUG)
+ System.err.println("\"" + filename + "\" is a directory");
+ String[] children = file.list();
+ if (!file.getName().contains(".svn"))
+ for (int i = 0; i < children.length; i++) {
+ // System.err.println("pushing \"" + children[i] +
+ // "\"");
+ files.push(filename + "/" + children[i]);
+ }
+ }
+ }
+ }
+ return result;
+
+ }
/**
* Train on files.
@@ -27,8 +86,8 @@
* @throws IOException
* Signals that an I/O exception has occurred.
*/
- public Model trainFiles(Model model) throws IOException {
- return trainFiles(defaultNumberOfFiles, model);
+ public Model train(Model model) throws IOException {
+ return train(defaultNumberOfFiles, model);
}
/**
@@ -42,33 +101,37 @@
* @throws IOException
* Signals that an I/O exception has occurred.
*/
- public Model trainFiles(int count, Model model) throws IOException {
- Stack<String> files = new Stack<String>();
- files.push("sgf/2004-12");
-
+ public Model train(int count, Model model) throws IOException {
+ 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(model, game);
+ } else {
+ if (DEBUG)
+ System.err.print("!");
- while (files.size() > 0 && filecount < count) {
- String filename = files.pop();
- File file = new File(filename);
- if (file.exists()) {
- if (!file.isDirectory()) {
-
- Game game = Game.loadFromFile(file);
- if (game.getSize() == 19) {
- filecount++;
- train(model, game);
- }
- } else {
- String[] children = file.list();
- for (int i = 0; i < children.length; i++) {
- files.push(filename + "/" + children[i]);
- }
- }
}
+ // 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("Trainer::trainNFiles loaded " + filecount + " files ");
+ System.err.println("\nTrainer::trainNFiles loaded " + filecount + " files ");
return model;
}
@@ -87,9 +150,11 @@
if (moves == null)
throw new Error();
int movecounter = 1;
+ model.setGamesTrained(model.getGamesTrained() + 1);
while (boards.hasNext() && moves.hasNext()) {
movecounter++;
+ model.setBoardsTrained(model.getBoardsTrained() + 1);
BoardI board = boards.next();
if (board == null)
throw new Error();
@@ -104,7 +169,7 @@
boolean isBlack = colour == BoardI.VERTEX_BLACK;
// float str = (float) (isBlack ? strengthB : strengthW);
- if (game != null || move.getPass()) {
+ if (game != null && !move.getPass()) {
movecounter++;
for (short i = 0; i < size; i++)
for (short j = 0; j < size; j++)
@@ -136,6 +201,7 @@
* the played
*/
public void train(Node root, VertexLineariser linear, boolean
playeda, boolean expand, int depth) {
+ while (root !=null && linear.hasNext()){
if (depth <= 0)
expand = false;
if (root.getNotPlayed() + root.getPlayed() < minBranchSize)
@@ -145,55 +211,116 @@
root.setPlayed(root.getPlayed() + 1);
else
root.setNotPlayed(root.getNotPlayed() + 1);
- if (!linear.hasNext())
- return;
Short colour = linear.next();
+ boolean expandMore = !onlyOneNewNodePerSymmetry;
switch (colour) {
case BoardI.VERTEX_BLACK:
if (root.getBlack() == null)
if (expand) {
root.setBlack(new Node());
- train(root.getBlack(), linear, playeda, false, depth);
+ root = root.getBlack();
+ expand = expandMore;
} else
return;
else
- train(root.getBlack(), linear, playeda, expand, depth);
+ root = root.getBlack();
break;
case BoardI.VERTEX_WHITE:
if (root.getWhite() == null)
if (expand) {
root.setWhite(new Node());
- train(root.getWhite(), linear, playeda, false, depth);
+ root = root.getWhite();
+ expand = expandMore;
} else
return;
else
- train(root.getWhite(), linear, playeda, expand, depth);
+ root = root.getWhite();
break;
case BoardI.VERTEX_KO:
case BoardI.VERTEX_EMPTY:
if (root.getEmpty() == null)
if (expand) {
root.setEmpty(new Node());
- train(root.getEmpty(), linear, playeda, false, depth);
+ expand = expandMore;
+ root = root.getEmpty();
} else
return;
else
- train(root.getEmpty(), linear, playeda, expand, depth);
+ root = root.getEmpty();
break;
case BoardI.VERTEX_OFF_BOARD:
if (root.getOff() == null)
if (expand) {
root.setOff(new Node());
- train(root.getOff(), linear, playeda, false, depth);
+ expand = expandMore;
+ root = root.getOff();
} else
return;
else
- train(root.getOff(), linear, playeda, expand, depth);
+ root = root.getOff();
break;
default:
throw new Error();
}
+ }
+ }
+
+ /**
+ * get the onlyOneNewNodePerSymmetry
+ *
+ * @return the onlyOneNewNodePerSymmetry
+ */
+ public final boolean isOnlyOneNewNodePerSymmetry() {
+ return onlyOneNewNodePerSymmetry;
+ }
+
+ /**
+ * 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 int getMinBranchSize() {
+ return minBranchSize;
+ }
+
+ /**
+ * set the minBranchSize
+ *
+ * @param minBranchSize
+ * the minBranchSize to set
+ */
+ public final void setMinBranchSize(int minBranchSize) {
+ this.minBranchSize = minBranchSize;
+ }
+
+ /**
+ * get the defaultNumberOfFiles
+ *
+ * @return the defaultNumberOfFiles
+ */
+ public final int getDefaultNumberOfFiles() {
+ return defaultNumberOfFiles;
+ }
+
+ /**
+ * set the defaultNumberOfFiles
+ *
+ * @param defaultNumberOfFiles
+ * the defaultNumberOfFiles to set
+ */
+ public final void setDefaultNumberOfFiles(int defaultNumberOfFiles) {
+ this.defaultNumberOfFiles = defaultNumberOfFiles;
}
}