Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow lizzie to work with alreadydone's dynamic komi. #329

Merged
merged 5 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/featurecat/lizzie/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Config {
public boolean showRawBoard = false;
public boolean showCaptured = true;
public boolean handicapInsteadOfWinrate = false;
public boolean showDynamicKomi = true;

public boolean showStatus = true;
public boolean showBranch = true;
Expand Down Expand Up @@ -146,6 +147,7 @@ public Config() throws IOException {
largeSubBoard = uiConfig.getBoolean("large-subboard");
handicapInsteadOfWinrate = uiConfig.getBoolean("handicap-instead-of-winrate");
startMaximized = uiConfig.getBoolean("window-maximized");
showDynamicKomi = uiConfig.getBoolean("show-dynamic-komi");
}

// Modifies config by adding in values from default_config that are missing.
Expand Down Expand Up @@ -269,6 +271,7 @@ private JSONObject createDefaultConfig() {
ui.put("board-size", 19);
ui.put("window-size", new JSONArray("[1024, 768]"));
ui.put("window-maximized", false);
ui.put("show-dynamic-komi", true);

config.put("ui", ui);
return config;
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class Leelaz {
private boolean isLoaded = false;
private boolean isCheckingVersion;

// dynamic komi and opponent komi as reported by dynamic-komi version of leelaz
private float dynamicKomi = Float.NaN, dynamicOppKomi = Float.NaN;
/**
* Initializes the leelaz process and starts reading output
*
Expand Down Expand Up @@ -180,7 +182,25 @@ private void parseInfo(String line) {
*/
private void parseLine(String line) {
synchronized (this) {
if (line.equals("\n")) {
if (line.startsWith("komi="))
{
try {
dynamicKomi = Float.parseFloat(line.substring("komi=".length()).trim());
}
catch (NumberFormatException nfe) {
dynamicKomi = Float.NaN;
}
}
else if (line.startsWith("opp_komi="))
{
try {
dynamicOppKomi = Float.parseFloat(line.substring("opp_komi=".length()).trim());
}
catch (NumberFormatException nfe) {
dynamicOppKomi = Float.NaN;
}
}
else if (line.equals("\n")) {
// End of response
} else if (line.startsWith("info")) {
isLoaded = true;
Expand Down Expand Up @@ -425,6 +445,13 @@ public List<MoveData> getBestMoves() {
}
}

public String getDynamicKomi() {
if (Float.isNaN(dynamicKomi) || Float.isNaN(dynamicOppKomi)) {
return null;
}
return String.format("%.1f / %.1f", dynamicKomi, dynamicOppKomi);
}

public boolean isPondering() {
return isPondering;
}
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/featurecat/lizzie/analysis/MoveData.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,30 @@ public MoveData(String line) throws ArrayIndexOutOfBoundsException {
String[] data = line.trim().split(" ");

// Todo: Proper tag parsing in case gtp protocol is extended(?)/changed
coordinate = data[1];
playouts = Integer.parseInt(data[3]);
winrate = Integer.parseInt(data[5])/100.0;
order = Integer.parseInt(data[7]);

variation = new ArrayList<>(Arrays.asList(data));
variation = variation.subList(9, variation.size());
for (int i=0; i<data.length; i++) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change looks good if it's necessary but let's extract it to a new method, I miss the simplicity of the old code that was here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code is simple because it has magical numbers 3,5,7 and 9 assuming constant positions of certain values in leela-zero output and some forks add some extra information that can be read by some tools.
Old output: move C4 visits 274 winrate 4633 order 4 pv C4 Q16 R17
New output: move D17 visits 109 winrate 4630 N 0.881185 order 10 pv D17 Q4 R3 R4 Q3 O3 P3 P4 O2 D4 Q16

Would you elaborate on the kind of a method you'd like to extract here?
Something like this?
HashMap<String, String> ParseLine(line); //key,value pairs
and in the constructor:
(...)playouts = parsed["visits"]

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK thanks for clarifying what went wrong. I guess the code is simple enough since that's all this constructor does! Not really important.

String key = data[i];
if (key.equals("pv")) {
//read variation to the end of line
variation = new ArrayList<>(Arrays.asList(data));
variation = variation.subList(i+1, data.length);
break;
} else {
String value = data[++i];
if (key.equals("move")) {
coordinate = value;
}
if (key.equals("visits")) {
playouts = Integer.parseInt(value);
}
if (key.equals("winrate")) {
winrate = Integer.parseInt(value)/100.0;
}
if (key.equals("order")) {
order = Integer.parseInt(value);
}
}
}
}

public int compareTo(MoveData b) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/featurecat/lizzie/gui/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ private boolean controlIsPressed(KeyEvent e) {
return e.isControlDown() || (mac && e.isMetaDown());
}

private void toggleShowDynamicKomi() {
Lizzie.config.showDynamicKomi = !Lizzie.config.showDynamicKomi;
}

@Override
public void keyPressed(KeyEvent e) {

Expand Down Expand Up @@ -358,6 +362,10 @@ public void keyPressed(KeyEvent e) {
}
break;

case VK_D:
toggleShowDynamicKomi();
break;

default:
shouldDisableAnalysis = false;
}
Expand Down
34 changes: 28 additions & 6 deletions src/main/java/featurecat/lizzie/gui/LizzieFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -322,6 +323,15 @@ public void paint(Graphics g0) {
int ponderingY = boardY + (int) (maxSize*0.93);
double ponderingSize = .02;

// dynamic komi
int dynamicKomiLabelX = this.getInsets().left;
int dynamicKomiLabelY = boardY + (int) (maxSize*0.86);

int dynamicKomiX = this.getInsets().left;
int dynamicKomiY = boardY + (int) (maxSize*0.89);
double dynamicKomiSize = .02;


// loading message
int loadingX = ponderingX;
int loadingY = ponderingY;
Expand Down Expand Up @@ -390,6 +400,11 @@ public void paint(Graphics g0) {
ponderingX, ponderingY, ponderingSize);
}

if (Lizzie.config.showDynamicKomi && Lizzie.leelaz.getDynamicKomi() != null) {
drawPonderingState(g, resourceBundle.getString("LizzieFrame.display.dynamic-komi"), dynamicKomiLabelX, dynamicKomiLabelY, dynamicKomiSize);
drawPonderingState(g, Lizzie.leelaz.getDynamicKomi(), dynamicKomiX, dynamicKomiY, dynamicKomiSize);
}

// Todo: Make board move over when there is no space beside the board
if (Lizzie.config.showWinrate) {
drawWinrateGraphContainer(backgroundG, contx, conty, contw, conth);
Expand Down Expand Up @@ -517,17 +532,22 @@ void drawControls() {
// redraw background
createBackground();

List<String> commandsToShow = new ArrayList<>(Arrays.asList(commands));
if (Lizzie.leelaz.getDynamicKomi() != null) {
commandsToShow.add(resourceBundle.getString("LizzieFrame.commands.keyD"));
}

Graphics2D g = cachedImage.createGraphics();

int maxSize = Math.min(getWidth(), getHeight());
Font font = new Font(systemDefaultFontName, Font.PLAIN, (int) (maxSize * 0.034));
g.setFont(font);
FontMetrics metrics = g.getFontMetrics(font);
int maxCommandWidth = Arrays.asList(commands).stream().reduce(0, (Integer i, String command) -> Math.max(i, metrics.stringWidth(command)), (Integer a, Integer b) -> Math.max(a, b));
int maxCommandWidth = commandsToShow.stream().reduce(0, (Integer i, String command) -> Math.max(i, metrics.stringWidth(command)), (Integer a, Integer b) -> Math.max(a, b));
int lineHeight = (int) (font.getSize() * 1.15);

int boxWidth = Util.clamp((int) (maxCommandWidth * 1.4), 0, getWidth());
int boxHeight = Util.clamp(commands.length * lineHeight, 0, getHeight());
int boxHeight = Util.clamp(commandsToShow.size() * lineHeight, 0, getHeight());

int commandsX = Util.clamp(getWidth() / 2 - boxWidth / 2, 0, getWidth());
int commandsY = Util.clamp(getHeight() / 2 - boxHeight / 2, 0, getHeight());
Expand All @@ -553,10 +573,12 @@ void drawControls() {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(Color.WHITE);
for (int i = 0; i < commands.length; i++) {
String[] split = commands[i].split("\\|");
g.drawString(split[0], verticalLineX - metrics.stringWidth(split[0]) - strokeRadius * 4, font.getSize() + (commandsY + i * lineHeight));
g.drawString(split[1], verticalLineX + strokeRadius * 4, font.getSize() + (commandsY + i * lineHeight));
int lineOffset = commandsY;
for (String command : commandsToShow) {
String[] split = command.split("\\|");
g.drawString(split[0], verticalLineX - metrics.stringWidth(split[0]) - strokeRadius * 4, font.getSize() + lineOffset);
g.drawString(split[1], verticalLineX + strokeRadius * 4, font.getSize() + lineOffset);
lineOffset += lineHeight;
}

refreshBackground();
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/l10n/DisplayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LizzieFrame.commands.keyAltC=ctrl-c|copy SGF to clipboard
LizzieFrame.commands.keyAltV=ctrl-v|paste SGF from clipboard
LizzieFrame.commands.keyC=c|toggle coordinates
LizzieFrame.commands.keyControl=ctrl|undo/redo 10 moves
LizzieFrame.commands.keyD=d|show/hide dynamic komi
LizzieFrame.commands.keyDownArrow=down arrow|redo
LizzieFrame.commands.keyEnd=end|go to end
LizzieFrame.commands.keyEnter=enter|force Leela Zero move
Expand All @@ -44,10 +45,11 @@ LizzieFrame.prompt.failedToSaveFile=Failed to save file.
LizzieFrame.prompt.sgfExists=The SGF file already exists, do you want to replace it?
LizzieFrame.prompt.showControlsHint=hold x = view controls
LizzieFrame.display.lastMove=Last move
LizzieFrame.display.pondering=Pondering
LizzieFrame.display.pondering=Pondering
LizzieFrame.display.on=on
LizzieFrame.display.off=off
LizzieFrame.display.loading=Leela Zero is loading...
LizzieFrame.display.download-latest-network-prompt=Download the latest network file? This may take some time.
LizzieFrame.display.leelaz-missing=Did not find Leela Zero, update config.txt or download from Leela Zero homepage
LizzieFrame.display.network-missing=Did not find network/weights.\nUpdate config.txt (network-file) or download from Leela Zero homepage
LizzieFrame.display.dynamic-komi=dyn. komi:
2 changes: 2 additions & 0 deletions src/main/resources/l10n/DisplayStrings_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
LizzieFrame.commands.keyAltC=ctrl-c|\u62F7\u8D1DSGF\u5230\u526A\u8D34\u677F
LizzieFrame.commands.keyAltV=ctrl-v|\u4ECE\u526A\u8D34\u677F\u7C98\u8D34SGF\u5C40\u9762
LizzieFrame.commands.keyC=c|\u663E\u793A/\u9690\u85CF\u5750\u6807
LizzieFrame.commands.keyD=d|show/hide dynamic komi
LizzieFrame.commands.keyControl=ctrl|\u5411\u524D/\u5411\u540E10\u6B65
LizzieFrame.commands.keyDownArrow=\u4E0B\u65B9\u5411\u952E|\u5411\u540E\u4E00\u6B65
LizzieFrame.commands.keyEnd=end|\u8DF3\u5230\u68CB\u8C31\u672B\u5C3E
Expand Down Expand Up @@ -39,3 +40,4 @@ LizzieFrame.display.loading=Leela Zero\u6B63\u5728\u8F7D\u5165\u4E2D...
LizzieFrame.display.download-latest-network-prompt=Download the latest network file? This may take some time.
LizzieFrame.display.leelaz-missing=Did not find Leela Zero, update config.txt or download from Leela Zero homepage
LizzieFrame.display.network-missing=Did not find network/weights.\nUpdate config.txt (network-file) or download from Leela Zero homepage
LizzieFrame.display.dynamic-komi=dyn. komi: