Skip to content

Commit

Permalink
Using zen to estimate scores (featurecat#558)
Browse files Browse the repository at this point in the history
Support estimate by zen
  • Loading branch information
yzyray authored and zsalch committed Jul 10, 2019
1 parent d52607d commit 2516813
Show file tree
Hide file tree
Showing 18 changed files with 729 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ private void parseLine(String line) {
} else if (isThinking && !isPondering) {
if (Lizzie.frame.isPlayingAgainstLeelaz || isInputCommand) {
Lizzie.board.place(params[1]);
if (Lizzie.frame.isAutoEstimating) {
if (Lizzie.board.getHistory().isBlacksTurn())
Lizzie.frame.zen.sendCommand("play " + "w " + params[1]);
else Lizzie.frame.zen.sendCommand("play " + "b " + params[1]);
Lizzie.frame.zen.countStones();
}
// TODO Do not ponder when playing against Leela Zero
// togglePonder();
if (!isInputCommand) {
Expand Down Expand Up @@ -468,6 +474,12 @@ public void sendCommand(String command) {
}
cmdQueue.addLast(command);
trySendCommandFromQueue();
if (Lizzie.frame.isAutoEstimating) {
if (command.startsWith("play") || command.startsWith("undo")) {
Lizzie.frame.zen.sendCommand(command);
Lizzie.frame.zen.countStones();
}
}
}
}

Expand Down
233 changes: 233 additions & 0 deletions src/main/java/featurecat/lizzie/analysis/YaZenGtp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package featurecat.lizzie.analysis;

import featurecat.lizzie.Lizzie;
import featurecat.lizzie.gui.CountResults;
import featurecat.lizzie.rules.MoveList;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import javax.swing.JOptionPane;

public class YaZenGtp {
private static final ResourceBundle resourceBundle =
ResourceBundle.getBundle("l10n.DisplayStrings");
public Process process;
private BufferedInputStream inputStream;
private BufferedOutputStream outputStream;

public boolean gtpConsole;

private String engineCommand;
// private List<String> commands;
private int cmdNumber;
private ArrayDeque<String> cmdQueue;
private ScheduledExecutorService executor;
ArrayList<Integer> esitmateArray = new ArrayList<Integer>();
public int blackEatCount = 0;
public int whiteEatCount = 0;
public int blackPrisonerCount = 0;
public int whitePrisonerCount = 0;
CountResults results;
boolean firstcount = true;
public int timesOfCounts = 0;
public boolean noRead = false;

public YaZenGtp() throws IOException {

cmdNumber = 1;
cmdQueue = new ArrayDeque<>();
gtpConsole = true;
engineCommand = "YAZenGtp.exe";
startEngine(engineCommand, 0);
}

public void startEngine(String engineCommand, int index) {
ProcessBuilder processBuilder = new ProcessBuilder(engineCommand);
processBuilder.redirectErrorStream(true);
try {
process = processBuilder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, resourceBundle.getString("YaZenGtp.nofile"));
return;
}
initializeStreams();

executor = Executors.newSingleThreadScheduledExecutor();
executor.execute(this::read);
}

private void initializeStreams() {
inputStream = new BufferedInputStream(process.getInputStream());
outputStream = new BufferedOutputStream(process.getOutputStream());
}

private void read() {
try {
int c;
StringBuilder line = new StringBuilder();
while ((c = inputStream.read()) != -1) {
line.append((char) c);

if ((c == '\n')) {

parseLine(line.toString());
line = new StringBuilder();
}
}
System.out.println("YaZenGtp process ended.");

shutdown();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}

private void parseLine(String line) {
synchronized (this) {
Lizzie.gtpConsole.addLineForce(line);
if (line.startsWith("= ")) {
String[] params = line.trim().split(" ");
for (int i = 2; i < params.length; i++) esitmateArray.add(Integer.parseInt(params[i]));
}

if (line.startsWith("Throw")) {
JOptionPane.showMessageDialog(null, resourceBundle.getString("YaZenGtp.nofile"));
shutdown();
}
if (line.startsWith(" ")) {

String[] params = line.trim().split(" ");
if (params.length == Lizzie.board.boardWidth) {
for (int i = 0; i < params.length; i++) esitmateArray.add(Integer.parseInt(params[i]));
}
}
}
if (line.startsWith("= ")) {
String[] params = line.trim().split(" ");
if (params.length == 14) {
if (noRead) {
timesOfCounts = 0;
} else {

blackEatCount = Integer.parseInt(params[3]);
whiteEatCount = Integer.parseInt(params[4]);
blackPrisonerCount = Integer.parseInt(params[5]);
whitePrisonerCount = Integer.parseInt(params[6]);
int blackpoint = Integer.parseInt(params[7]);
int whitepoint = Integer.parseInt(params[8]);
Lizzie.frame.drawEstimateRectZen(esitmateArray);
Lizzie.frame.repaint();
if (firstcount) {
results = Lizzie.frame.countResults;
results.Counts(
blackEatCount,
whiteEatCount,
blackPrisonerCount,
whitePrisonerCount,
blackpoint,
whitepoint);
results.setVisible(true);
firstcount = false;
timesOfCounts = 0;
} else {
results.Counts(
blackEatCount,
whiteEatCount,
blackPrisonerCount,
whitePrisonerCount,
blackpoint,
whitepoint);
results.setVisible(true);
Lizzie.frame.setVisible(true);
timesOfCounts = 0;
}
}
}
}
}

public void shutdown() {
process.destroy();
}

public void sendCommand(String command) {
synchronized (cmdQueue) {
if (!cmdQueue.isEmpty()) {
cmdQueue.removeLast();
}
cmdQueue.addLast(command);
trySendCommandFromQueue();
}
}

private void trySendCommandFromQueue() {
synchronized (cmdQueue) {
if (cmdQueue.isEmpty()) {
return;
}
String command = cmdQueue.removeFirst();
sendCommandToZen(command);
}
}

private void sendCommandToZen(String command) {
// System.out.printf("> %d %s\n", cmdNumber, command);
try {
Lizzie.gtpConsole.addZenCommand(command, cmdNumber);
} catch (Exception ex) {
}
cmdNumber++;
try {
outputStream.write((command + "\n").getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

private void playmove(int x, int y, boolean isblack) {
String coordsname = Lizzie.board.convertCoordinatesToName(x, y);
String color = isblack ? "b" : "w";

sendCommand("play" + " " + color + " " + coordsname);
}

public void syncBoradStat() {
sendCommand("clear_board");
sendCommand("boardsize " + Lizzie.board.boardWidth);
cmdNumber = 1;
ArrayList<MoveList> movelist = Lizzie.board.getMoveList();
int lenth = movelist.size();
for (int i = 0; i < lenth; i++) {
MoveList move = movelist.get(lenth - 1 - i);
if (!move.isPass) {
playmove(move.x, move.y, move.isBlack);
}
}
}

public void countStones() {
if (timesOfCounts > 5) {
noRead = true;
cmdQueue.clear();
Lizzie.frame.noAutoEstimateByZen();
return;
}
timesOfCounts++;
esitmateArray.clear();
blackEatCount = 0;
whiteEatCount = 0;
blackPrisonerCount = 0;
whitePrisonerCount = 0;
sendCommand("territory_statistics territory");
//
sendCommand("score_statistics");
}
}
7 changes: 5 additions & 2 deletions src/main/java/featurecat/lizzie/gui/BasicInfoPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void drawCaptured(Graphics2D g, int posX, int posY, int width, int heigh
int smallDiam = diam / 2;
int bdiam = diam, wdiam = diam;
if (Lizzie.board != null) {
if (Lizzie.board.inScoreMode()) {
if (Lizzie.board.inScoreMode() || Lizzie.frame.isEstimating) {
// do nothing
} else if (Lizzie.board.getHistory().isBlacksTurn()) {
wdiam = smallDiam;
Expand All @@ -125,7 +125,7 @@ private void drawCaptured(Graphics2D g, int posX, int posY, int width, int heigh
posX + width * 3 / 4 - wdiam / 2, posY + height * 3 / 8 + (diam - wdiam) / 2, wdiam, wdiam);

// Draw captures
String bval, wval;
String bval = "", wval = "";
setPanelFont(g, (float) (height * 0.18));
if (Lizzie.board == null) {
return;
Expand All @@ -134,6 +134,9 @@ private void drawCaptured(Graphics2D g, int posX, int posY, int width, int heigh
double score[] = Lizzie.board.getScore(Lizzie.board.scoreStones());
bval = String.format("%.0f", score[0]);
wval = String.format("%.1f", score[1]);
} else if (Lizzie.frame.isEstimating || Lizzie.frame.isAutoEstimating) {
bval = String.format("%d", Lizzie.frame.countResults.allBlackCounts);
wval = String.format("%d", Lizzie.frame.countResults.allWhiteCounts);
} else {
bval = String.format("%d", Lizzie.board.getData().blackCaptures);
wval = String.format("%d", Lizzie.board.getData().whiteCaptures);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/featurecat/lizzie/gui/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,8 @@ public void drawEstimateRectKata(ArrayList<Double> esitmateArray) {
boardRenderer.drawEstimateRectKata(esitmateArray);
}
}

public void drawEstimateRectZen(ArrayList<Integer> esitmateArray) {
boardRenderer.drawEstimateRectZen(esitmateArray);
}
}
33 changes: 32 additions & 1 deletion src/main/java/featurecat/lizzie/gui/BoardRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ private void drawBranch() {
gShadow.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

Optional<MoveData> suggestedMove = (isMainBoard ? mouseOveredMove() : getBestMove());
if (!suggestedMove.isPresent()) {
if (!suggestedMove.isPresent() || (!isMainBoard && Lizzie.frame.isAutoEstimating)) {
return;
}
List<String> variation = suggestedMove.get().variation;
Expand Down Expand Up @@ -1560,6 +1560,9 @@ public void increaseMaxAlpha(int k) {
}

public void removeEstimateRect() {
if (boardWidth <= 0 || boardHeight <= 0) {
return;
}
cachedEsitmateRectImage = new BufferedImage(boardWidth, boardHeight, TYPE_INT_ARGB);
}

Expand Down Expand Up @@ -1608,6 +1611,34 @@ public void drawEstimateRectKata(ArrayList<Double> esitmateArray) {
}
}

public void drawEstimateRectZen(ArrayList<Integer> esitmateArray) {
if (boardWidth <= 0 || boardHeight <= 0) {
return;
}
cachedEsitmateRectImage = new BufferedImage(boardWidth, boardHeight, TYPE_INT_ARGB);
Graphics2D g = cachedEsitmateRectImage.createGraphics();
for (int i = 0; i < esitmateArray.size(); i++) {
if (esitmateArray.get(i) > 0) {
int[] c = Lizzie.board.getCoord(i);
int x = c[1];
int y = c[0];
int stoneX = scaledMarginWidth + squareWidth * x;
int stoneY = scaledMarginHeight + squareHeight * y;
g.setColor(Color.BLACK);
g.fillRect(stoneX - stoneRadius / 2, stoneY - stoneRadius / 2, stoneRadius, stoneRadius);
}
if (esitmateArray.get(i) < 0) {
int[] c = Lizzie.board.getCoord(i);
int x = c[1];
int y = c[0];
int stoneX = scaledMarginWidth + squareWidth * x;
int stoneY = scaledMarginHeight + squareHeight * y;
g.setColor(Color.WHITE);
g.fillRect(stoneX - stoneRadius / 2, stoneY - stoneRadius / 2, stoneRadius, stoneRadius);
}
}
}

private double convertLength(double length) {
double lengthab = Math.abs(length);
if (lengthab > 0.2) {
Expand Down
Loading

0 comments on commit 2516813

Please sign in to comment.