From bd81db6df2951f6f97ef17e12237b8a9ffef287c Mon Sep 17 00:00:00 2001 From: zsal Date: Wed, 29 Aug 2018 23:15:51 +0800 Subject: [PATCH] 2018/08/29 Multiple Weights Switching --- src/main/java/featurecat/lizzie/Lizzie.java | 35 +++++++ .../featurecat/lizzie/analysis/Leelaz.java | 92 ++++++++++++++++--- .../java/featurecat/lizzie/gui/Input.java | 7 +- .../featurecat/lizzie/gui/LizzieFrame.java | 2 +- 4 files changed, 122 insertions(+), 14 deletions(-) diff --git a/src/main/java/featurecat/lizzie/Lizzie.java b/src/main/java/featurecat/lizzie/Lizzie.java index 71f0c968c..b5c9f40ee 100644 --- a/src/main/java/featurecat/lizzie/Lizzie.java +++ b/src/main/java/featurecat/lizzie/Lizzie.java @@ -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; @@ -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(); + } + + } } diff --git a/src/main/java/featurecat/lizzie/analysis/Leelaz.java b/src/main/java/featurecat/lizzie/analysis/Leelaz.java index 0d08328e4..91b78611e 100644 --- a/src/main/java/featurecat/lizzie/analysis/Leelaz.java +++ b/src/main/java/featurecat/lizzie/analysis/Leelaz.java @@ -59,6 +59,13 @@ public class Leelaz { private boolean isLoaded = false; private boolean isCheckingVersion; + + // for Multiple Engine + private String engineCommand = null; + private List 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; @@ -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"); @@ -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 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); @@ -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()) { @@ -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)); @@ -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); @@ -561,4 +621,12 @@ private synchronized void notifyBestMoveListeners() { public boolean isLoaded() { return isLoaded; } + + public String currentWeight() { + return currentWeight; + } + + public String isSwitching() { + return isSwitching; + } } diff --git a/src/main/java/featurecat/lizzie/gui/Input.java b/src/main/java/featurecat/lizzie/gui/Input.java index ee32f376a..706eab6a3 100644 --- a/src/main/java/featurecat/lizzie/gui/Input.java +++ b/src/main/java/featurecat/lizzie/gui/Input.java @@ -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()) diff --git a/src/main/java/featurecat/lizzie/gui/LizzieFrame.java b/src/main/java/featurecat/lizzie/gui/LizzieFrame.java index 3bb067a5d..811ea4d63 100644 --- a/src/main/java/featurecat/lizzie/gui/LizzieFrame.java +++ b/src/main/java/featurecat/lizzie/gui/LizzieFrame.java @@ -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); }