Skip to content

Commit

Permalink
2018/08/29 Multiple Weights Switching
Browse files Browse the repository at this point in the history
  • Loading branch information
zsal authored and zsal committed Aug 29, 2018
1 parent 9d3bfd2 commit bd81db6
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 14 deletions.
35 changes: 35 additions & 0 deletions src/main/java/featurecat/lizzie/Lizzie.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package featurecat.lizzie;

import org.json.JSONArray;
import org.json.JSONException;
import featurecat.lizzie.analysis.Leelaz;
import featurecat.lizzie.plugin.PluginManager;
Expand Down Expand Up @@ -94,5 +95,39 @@ public static void shutdown() {
leelaz.shutdown();
System.exit(0);
}

public static void switchEngine(int index) {

String commandLine = null;
if (index == 0) {
commandLine = Lizzie.config.leelazConfig.getString("engine-command");
} else {
JSONArray commandList = Lizzie.config.leelazConfig.getJSONArray("engine-command-list");
if (commandList != null && commandList.length() >= index) {
commandLine = commandList.getString(index - 1);
}
}

final int moveNumber = board.getData().moveNumber;

// Workaround for leelaz cannot exit when restarting
if (leelaz.isThinking) {
if (Lizzie.frame.isPlayingAgainstLeelaz) {
Lizzie.frame.isPlayingAgainstLeelaz = false;
Lizzie.leelaz.togglePonder(); // we must toggle twice for it to restart pondering
Lizzie.leelaz.isThinking = false;
}
Lizzie.leelaz.togglePonder();
}

board.goToMoveNumber(0);
try {
leelaz.restartEngine(commandLine);
board.goToMoveNumber(moveNumber);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
92 changes: 80 additions & 12 deletions src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public class Leelaz {

private boolean isLoaded = false;
private boolean isCheckingVersion;

// for Multiple Engine
private String engineCommand = null;
private List<String> commands = null;
private JSONObject config = null;
private String currentWeight = null;
private String isSwitching = "";

// dynamic komi and opponent komi as reported by dynamic-komi version of leelaz
private float dynamicKomi = Float.NaN, dynamicOppKomi = Float.NaN;
Expand All @@ -78,7 +85,7 @@ public Leelaz() throws IOException, JSONException {
currentCmdNum = 0;
cmdQueue = new ArrayDeque<>();

JSONObject config = Lizzie.config.config.getJSONObject("leelaz");
config = Lizzie.config.config.getJSONObject("leelaz");

printCommunication = config.getBoolean("print-comms");
maxAnalyzeTimeMillis = MINUTE * config.getInt("max-analyze-time-minutes");
Expand All @@ -87,23 +94,64 @@ public Leelaz() throws IOException, JSONException {
updateToLatestNetwork();
}

String startfolder = new File(Config.getBestDefaultLeelazPath()).getParent(); // todo make this a little more obvious/less bug-prone

// Check if network file is present
File wf = new File(startfolder + '/' + config.getString("network-file"));
if (!wf.exists()) {
JOptionPane.showMessageDialog(null, resourceBundle.getString("LizzieFrame.display.network-missing"));
}
// String startfolder = new File(Config.getBestDefaultLeelazPath()).getParent(); // todo make this a little more obvious/less bug-prone
//
// // Check if network file is present
// File wf = new File(startfolder + '/' + config.getString("network-file"));
// if (!wf.exists()) {
// JOptionPane.showMessageDialog(null, resourceBundle.getString("LizzieFrame.display.network-missing"));
// }


// command string for starting the engine
String engineCommand = config.getString("engine-command");
engineCommand = config.getString("engine-command");
// substitute in the weights file
engineCommand = engineCommand.replaceAll("%network-file", config.getString("network-file"));
// create this as a list which gets passed into the processbuilder
List<String> commands = Arrays.asList(engineCommand.split(" "));
// commands = Arrays.asList(engineCommand.split(" "));

// run leelaz
// ProcessBuilder processBuilder = new ProcessBuilder(commands);
// processBuilder.directory(new File(config.optString("engine-start-location", ".")));
// processBuilder.redirectErrorStream(true);
// process = processBuilder.start();
//
// initializeStreams();
//
// // Send a version request to check that we have a supported version
// // Response handled in parseLine
// isCheckingVersion = true;
// sendCommand("version");
//
// // start a thread to continuously read Leelaz output
// new Thread(this::read).start();
startEngine(engineCommand);
Lizzie.frame.refreshBackground();
}

public void startEngine(String engineCommand) throws IOException {
if (engineCommand == null || "".equals(engineCommand.trim())) {
return;
}
String startfolder = new File(Config.getBestDefaultLeelazPath()).getParent(); // todo make this a little more obvious/less bug-prone

// Check if network file is present
File wf = new File(startfolder + '/' + config.getString("network-file"));
if (!wf.exists()) {
JOptionPane.showMessageDialog(null, resourceBundle.getString("LizzieFrame.display.network-missing"));
}
commands = Arrays.asList(engineCommand.split(" "));
if (commands != null) {
int weightIndex = commands.indexOf("--weights");
if (weightIndex > -1) {
currentWeight = commands.get(weightIndex+1);
} else {
weightIndex = commands.indexOf("-w");
if (weightIndex > -1) {
currentWeight = commands.get(weightIndex+1);
}
}
}
ProcessBuilder processBuilder = new ProcessBuilder(commands);
processBuilder.directory(new File(startfolder));
processBuilder.redirectErrorStream(true);
Expand All @@ -118,9 +166,20 @@ public Leelaz() throws IOException, JSONException {

// start a thread to continuously read Leelaz output
new Thread(this::read).start();
Lizzie.frame.refreshBackground();
}


public void restartEngine(String engineCommand) throws IOException {
if (engineCommand == null || "".equals(engineCommand.trim())) {
return;
}
isSwitching = " Switching";
this.engineCommand = engineCommand;
shutdown();
startEngine(engineCommand);
//togglePonder();
}

private void updateToLatestNetwork() {
try {
if (needToDownloadLatestNetwork()) {
Expand Down Expand Up @@ -204,6 +263,7 @@ else if (line.equals("\n")) {
// End of response
} else if (line.startsWith("info")) {
isLoaded = true;
isSwitching = "";
if (isResponseUpToDate()) {
// This should not be stale data when the command number match
parseInfo(line.substring(5));
Expand Down Expand Up @@ -294,7 +354,7 @@ private void read() {
System.out.println("Leelaz process ended.");

shutdown();
System.exit(-1);
//System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
Expand Down Expand Up @@ -561,4 +621,12 @@ private synchronized void notifyBestMoveListeners() {
public boolean isLoaded() {
return isLoaded;
}

public String currentWeight() {
return currentWeight;
}

public String isSwitching() {
return isSwitching;
}
}
7 changes: 6 additions & 1 deletion src/main/java/featurecat/lizzie/gui/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,12 @@ public void keyPressed(KeyEvent e) {
break;

default:
shouldDisableAnalysis = false;
if (((e.getModifiers() & KeyEvent.CTRL_MASK) != 0 || (e.getModifiers() & KeyEvent.META_MASK) != 0) && KeyEvent.VK_0 <= e.getKeyCode() && e.getKeyCode() <= KeyEvent.VK_9) {
int index = e.getKeyCode() - KeyEvent.VK_0;
Lizzie.switchEngine(index);
} else {
shouldDisableAnalysis = false;
}
}

if (shouldDisableAnalysis && Lizzie.board.inAnalysisMode())
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/featurecat/lizzie/gui/LizzieFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public void paint(Graphics g0) {
if (Lizzie.leelaz != null && Lizzie.leelaz.isLoaded()) {
if (Lizzie.config.showStatus) {
drawPonderingState(g, resourceBundle.getString("LizzieFrame.display.pondering") +
(Lizzie.leelaz.isPondering()?resourceBundle.getString("LizzieFrame.display.on"):resourceBundle.getString("LizzieFrame.display.off")),
(Lizzie.leelaz.isPondering()?resourceBundle.getString("LizzieFrame.display.on"):resourceBundle.getString("LizzieFrame.display.off")) + " " + Lizzie.leelaz.currentWeight() + Lizzie.leelaz.isSwitching(),
ponderingX, ponderingY, ponderingSize);
}

Expand Down

0 comments on commit bd81db6

Please sign in to comment.