Skip to content

Commit 919168f

Browse files
This is the real v0.1.0 commit
Move to position search instead of searchmoves
1 parent df43e21 commit 919168f

File tree

5 files changed

+64
-23
lines changed

5 files changed

+64
-23
lines changed

src/main/java/metaengine/GoResult.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class GoResult {
44
private final Score score;
55
private final UCIMove move;
6+
private static final int MATE_CUTOFF = 100;
67
public GoResult(Score score, UCIMove move) {
78
this.score = score;
89
this.move = move;
@@ -41,6 +42,16 @@ public Score getBiasedScore(int bias) {
4142
}
4243
}
4344

45+
public Score flip() {
46+
if (comparisonScore > Integer.MAX_VALUE - MATE_CUTOFF) {
47+
return new Score(comparisonScore - Integer.MAX_VALUE, true);
48+
} else if (comparisonScore < Integer.MIN_VALUE + MATE_CUTOFF) {
49+
return new Score(comparisonScore - Integer.MIN_VALUE, true);
50+
} else {
51+
return new Score(-comparisonScore);
52+
}
53+
}
54+
4455
@Override
4556
public boolean equals(Object other) {
4657
if (other == null || !(other instanceof Score)) {
@@ -62,18 +73,18 @@ public int compareTo(Score other) {
6273

6374
@Override
6475
public String toString() {
65-
if (comparisonScore > Integer.MAX_VALUE - 100) {
76+
if (comparisonScore > Integer.MAX_VALUE - MATE_CUTOFF) {
6677
return "mate " + (Integer.MAX_VALUE - comparisonScore);
67-
} else if (comparisonScore < Integer.MIN_VALUE + 100) {
78+
} else if (comparisonScore < Integer.MIN_VALUE + MATE_CUTOFF) {
6879
return "mate " + (Integer.MIN_VALUE - comparisonScore);
6980
} else {
7081
return "cp " + comparisonScore;
7182
}
7283
}
7384

7485
public boolean isMateScore() {
75-
return comparisonScore > Integer.MAX_VALUE - 100 ||
76-
comparisonScore < Integer.MIN_VALUE + 100;
86+
return comparisonScore > Integer.MAX_VALUE - MATE_CUTOFF ||
87+
comparisonScore < Integer.MIN_VALUE + MATE_CUTOFF;
7788
}
7889
}
7990
}

src/main/java/metaengine/ProcessIO.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,23 @@
88
import java.io.IOException;
99

1010
public class ProcessIO {
11+
private static final boolean DEBUG_WRITES = false;
12+
private static final boolean DEBUG_READS = false;
1113
private final Object readMutex = new Object();
1214
private final BufferedReader fromProc;
1315
private final Object writeMutex = new Object();
1416
private final PrintWriter toProc;
15-
private final boolean debug;
1617
private final String name;
1718

18-
public ProcessIO(Process proc, String name, boolean debug) {
19+
public ProcessIO(Process proc, String name) {
1920
fromProc = new BufferedReader(new InputStreamReader(
2021
proc.getInputStream()));
2122
toProc =
2223
new PrintWriter(new BufferedWriter(
2324
new OutputStreamWriter(proc.getOutputStream())), true);
24-
this.debug = debug;
2525
this.name = name;
2626
}
2727

28-
public ProcessIO(Process proc, String name) {
29-
this(proc, name, false);
30-
}
31-
3228
public ProcessIO(Process proc) {
3329
this(proc, null);
3430
}
@@ -42,7 +38,7 @@ public String readLine() {
4238
} catch (IOException e) {
4339
throw new RuntimeException("Unexpted error reading from process");
4440
}
45-
if (debug) {
41+
if (DEBUG_READS) {
4642
String toPrint = "DEBUG: Read \"" + result + "\"";
4743
if (name != null) {
4844
toPrint += " from \"" + name + "\"";
@@ -56,8 +52,8 @@ public void sendLine(String line) {
5652
synchronized (writeMutex) {
5753
toProc.println(line);
5854
}
59-
if (debug) {
60-
String toPrint = "DEGUG: Sent \"" + line + "\"";
55+
if (DEBUG_WRITES) {
56+
String toPrint = "DEBUG: Sent \"" + line + "\"";
6157
if (name != null) {
6258
toPrint += " to \"" + name + "\"";
6359
}

src/main/java/metaengine/UCIEngine.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111

1212
public class UCIEngine {
13-
private static final boolean DEBUG_IO = false;
14-
1513
private final Process engineProcess;
1614
private final String engineName;
1715
private List<UCIOption> options = null;
@@ -30,7 +28,7 @@ public UCIEngine (List<String> argv)
3028
ProcessBuilder pb = new ProcessBuilder(argv);
3129
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
3230
engineProcess = pb.start();
33-
engineIO = new ProcessIO(engineProcess, engineName, DEBUG_IO);
31+
engineIO = new ProcessIO(engineProcess, engineName);
3432
populateOptions();
3533
}
3634

@@ -159,7 +157,7 @@ public void synchronize() {
159157
}
160158

161159
public void position(UCIPosition pos) {
162-
engineIO.sendLine("position " + pos.toString());
160+
engineIO.sendLine(pos.toString());
163161
}
164162

165163
public void quit() {

src/main/java/metaengine/UCIEnginesManager.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class UCIEnginesManager {
2020
private final List<EngineRecord> enginesList;
2121
private final List<EngineRecord> recomenderRecords = new ArrayList<>();
2222
private final List<EngineRecord> judgeRecords = new ArrayList<>();
23+
private UCIPosition currentPosition = UCIPosition.STARTPOS;
2324

2425
public static UCIEnginesManager create(Configuration conf)
2526
throws InvalidConfigurationException {
@@ -182,12 +183,13 @@ public UCIMove call() {
182183
for (int i = 0; i != judgeRecords.size(); ++i) {
183184
UCIEngine judge = judgeRecords.get(i).getEngine();
184185
String[] goCtorParam = {
185-
"go", "searchmoves",
186-
recResults.get(i).getMove().toString(),
187-
"movetime",
186+
"go", "movetime",
188187
Long.toString(timerTime / 1000000L)
189188
};
189+
UCIPosition judgePosition =
190+
currentPosition.plus(recResults.get(i).getMove());
190191
judgeFutures.add(Main.threadPool.submit(() -> {
192+
judge.position(judgePosition);
191193
return judge.go(new UCIGo(goCtorParam));
192194
}));
193195
}
@@ -201,7 +203,8 @@ public UCIMove call() {
201203
GoResult.Score bestScore =
202204
new GoResult.Score(Integer.MIN_VALUE);
203205
for (int i = 0; i != judgeResults.size(); ++i) {
204-
GoResult.Score origScore = judgeResults.get(i).getScore();
206+
GoResult.Score origScore =
207+
judgeResults.get(i).getScore().flip();
205208
GoResult.Score score = origScore.getBiasedScore(
206209
recomenderRecords.get(i).getConfig().getBias());
207210
if (score.compareTo(bestScore) >= 0) {
@@ -223,11 +226,11 @@ public void setPosition(UCIPosition pos) {
223226
UCIEngine engine = rec.getEngine();
224227
engine.position(pos);
225228
}
229+
currentPosition = pos;
226230
}
227231

228232

229233
public Future<UCIMove> search(UCIGo params) {
230-
SearchInfo result = new SearchInfo();
231234
return Main.threadPool.submit(new Searcher(params));
232235
}
233236

src/main/java/metaengine/UCIPosition.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
public class UCIPosition {
44
// Right now this class is implemented the lazy way. We just store
55
// the string as-is. This may change if further functionality is needed.
6+
public static final UCIPosition STARTPOS = new UCIPosition(new String[] {
7+
"position", "startpos"
8+
});
69
private final String pos;
710

811
UCIPosition(String[] tokens) {
@@ -11,12 +14,42 @@ public class UCIPosition {
1114
for (String token : tokens) {
1215
if (afterFirstToken) {
1316
temp += " " + token;
17+
} else {
18+
temp = token;
1419
}
1520
afterFirstToken = true;
1621
}
1722
pos = temp;
1823
}
1924

25+
UCIPosition plus(UCIMove move) {
26+
String[] tokens = UCIUtils.tokenize(pos);
27+
boolean hasMoves = false;
28+
for (String str : tokens) {
29+
if (str.equals("moves")) {
30+
hasMoves = true;
31+
break;
32+
}
33+
}
34+
35+
if (hasMoves) {
36+
String[] ctorParam = new String[tokens.length + 1];
37+
for (int i = 0; i != tokens.length; ++i) {
38+
ctorParam[i] = tokens[i];
39+
}
40+
ctorParam[tokens.length] = move.toString();
41+
return new UCIPosition(ctorParam);
42+
} else {
43+
String[] ctorParam = new String[tokens.length + 2];
44+
for (int i = 0; i != tokens.length; ++i) {
45+
ctorParam[i] = tokens[i];
46+
}
47+
ctorParam[tokens.length] = "moves";
48+
ctorParam[tokens.length + 1] = move.toString();
49+
return new UCIPosition(ctorParam);
50+
}
51+
}
52+
2053
@Override
2154
public String toString() {
2255
return pos;

0 commit comments

Comments
 (0)