Skip to content

Commit

Permalink
Quick hack to support Leela 0.11.0 again (cf. #172 #182 #247)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiraoka committed Nov 16, 2020
1 parent ebeb446 commit f8c4371
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
59 changes: 58 additions & 1 deletion src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -98,6 +100,11 @@ public class Leelaz {
public double scoreStdev = 0;
public static int engineIndex = 0;

public boolean isLeela0110 = false;
private Timer leela0110PonderingTimer;
private BoardData leela0110PonderingBoardData;
private static final int LEELA0110_PONDERING_INTERVAL_MILLIS = 1000;

/**
* Initializes the leelaz process and starts reading output
*
Expand Down Expand Up @@ -384,6 +391,7 @@ private void parseLine(String line) {
}
isLoaded = true;
if (isResponseUpToDate()
|| isLeela0110 && isPondering
|| isThinking
&& (!isPondering && Lizzie.frame.isPlayingAgainstLeelaz || isInputCommand)) {
// TODO Do not update the best moves when playing against Leela Zero
Expand All @@ -392,6 +400,7 @@ private void parseLine(String line) {
&& (Lizzie.config.limitBestMoveNum == 0
|| bestMoves.size() < Lizzie.config.limitBestMoveNum)) {
bestMoves.add(MoveData.fromSummary(line));
if (isLeela0110) return;
notifyBestMoveListeners();
Lizzie.frame.refresh(1);
}
Expand All @@ -403,6 +412,12 @@ private void parseLine(String line) {
}
isThinking = false;

} else if (isLeela0110 && line.startsWith("=====")) {
if (isLeela0110PonderingValid()) Lizzie.board.getData().tryToSetBestMoves(bestMoves);
Lizzie.frame.refresh(1);
Lizzie.frame.updateTitle();
leela0110UpdatePonder();
return;
} else if (line.startsWith("=") || line.startsWith("?")) {
if (printCommunication || gtpConsole) {
System.out.print(line);
Expand Down Expand Up @@ -446,9 +461,12 @@ private void parseLine(String line) {
if (params[1].startsWith("KataGo")) {
this.isKataGo = true;
Lizzie.initializeAfterVersionCheck(this);
} else if (params[1].equals("Leela")) {
this.isLeela0110 = true;
Lizzie.initializeAfterVersionCheck(this);
}
isCheckingName = false;
} else if (isCheckingVersion && !isKataGo) {
} else if (isCheckingVersion && !isKataGo && !isLeela0110) {
String[] ver = params[1].split("\\.");
int minor = Integer.parseInt(ver[1]);
// Gtp support added in version 15
Expand Down Expand Up @@ -726,6 +744,10 @@ public void analyzeAvoid(String parameters) {
public void ponder() {
isPondering = true;
startPonderTime = System.currentTimeMillis();
if (isLeela0110) {
leela0110Ponder();
return;
}
if (Lizzie.board.isAvoding && Lizzie.board.isKeepingAvoid && !isKataGo)
analyzeAvoid(
"avoid b "
Expand Down Expand Up @@ -758,8 +780,43 @@ public void togglePonder() {
Lizzie.frame.updateBasicInfo();
}

private void leela0110Ponder() {
synchronized (this) {
if (leela0110PonderingBoardData != null) return;
leela0110PonderingBoardData = Lizzie.board.getData();
bestMoves = new ArrayList<>();
sendCommand("time_left b 0 0");
leela0110PonderingTimer = new Timer();
leela0110PonderingTimer.schedule(
new TimerTask() {
public void run() {
sendCommand("name");
}
},
LEELA0110_PONDERING_INTERVAL_MILLIS);
}
}

private void leela0110StopPonder() {
if (leela0110PonderingTimer != null) {
leela0110PonderingTimer.cancel();
leela0110PonderingTimer = null;
}
leela0110PonderingBoardData = null;
}

private void leela0110UpdatePonder() {
leela0110StopPonder();
if (isPondering) leela0110Ponder();
}

private boolean isLeela0110PonderingValid() {
return leela0110PonderingBoardData == Lizzie.board.getData();
}

/** End the process */
public void shutdown() {
leela0110StopPonder();
if (process != null) process.destroy();
}

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/featurecat/lizzie/analysis/MoveData.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,19 @@ public static MoveData fromInfo(String line) throws ArrayIndexOutOfBoundsExcepti
*
* <p>Q4 -> 4348 (V: 43.88%) (LCB: 43.81%) (N: 18.67%) PV: Q4 D16 D4 Q16 R14 R6 C1
*
* <p>Leela 0.11.0
*
* <p>R16 -> 788 (W: 48.73%) (U: 48.02%) (V: 49.09%: 141) (N: 3.8%) PV: R16 D17 Q3
*
* @param summary line of summary output
*/
public static MoveData fromSummary(String summary) {
Matcher match = summaryPatternLcb.matcher(summary.trim());
if (!match.matches()) {
// support 0.16 0.15
Matcher matchold = summaryPatternWinrate.matcher(summary.trim());
Pattern oldPattern =
Lizzie.leelaz.isLeela0110 ? summaryPatternLeela0110 : summaryPatternWinrate;
Matcher matchold = oldPattern.matcher(summary.trim());
if (!matchold.matches()) {
throw new IllegalArgumentException("Unexpected summary format: " + summary);
} else {
Expand Down Expand Up @@ -178,6 +184,8 @@ public static MoveData fromSummary(String summary) {
private static Pattern summaryPatternWinrate =
Pattern.compile("^ *(\\w\\d*) -> *(\\d+) \\(V: ([^%)]+)%\\) \\([^\\)]+\\) PV: (.+).*$");
// support 0.16 0.15
private static Pattern summaryPatternLeela0110 =
Pattern.compile("^ *(\\w\\d*) -> *(\\d+) \\(W: ([^%)]+)%\\).*PV: (.+).*$");

public static int getPlayouts(List<MoveData> moves) {
int playouts = 0;
Expand Down

0 comments on commit f8c4371

Please sign in to comment.