diff --git a/resources/MatlabInterface/@JEInterface/evaluateJE.m b/resources/MatlabInterface/@JEInterface/evaluateJE.m deleted file mode 100644 index 0206afa3..00000000 --- a/resources/MatlabInterface/@JEInterface/evaluateJE.m +++ /dev/null @@ -1,11 +0,0 @@ -function v = evaluate(int) -% Interface function for JavaEvA 2. -% Will be called by the MatlabProblem to obtain the target function value -% for a sample individual. - -x = int.mp.getCurrentDoubleArray; -if (isempty(int.args)) - int.mp.setResult(feval(int.f, x)); -else - int.mp.setResult(feval(int.f, x, int.args)); -end diff --git a/resources/MatlabInterface/@JEInterface/isFinished.m b/resources/MatlabInterface/@JEInterface/isFinished.m index f6163b82..2ebb4cdb 100644 --- a/resources/MatlabInterface/@JEInterface/isFinished.m +++ b/resources/MatlabInterface/@JEInterface/isFinished.m @@ -1,4 +1,5 @@ function b = isFinished(int) +% Deprecated: optimization now runs synchronously. % Returns 1 if the optimization was finished, else 0 b = int.finished; diff --git a/resources/MatlabInterface/@JEInterface/runEvalLoopJE.m b/resources/MatlabInterface/@JEInterface/runEvalLoopJE.m index ca4ae9c3..1fae3eaf 100644 --- a/resources/MatlabInterface/@JEInterface/runEvalLoopJE.m +++ b/resources/MatlabInterface/@JEInterface/runEvalLoopJE.m @@ -9,6 +9,7 @@ function int=runEvalLoopJE(int, optOrPostProc, optType, outputFilePrefix, steps, % when optimizing. nBest may be -1 to show all. global stopOptimization +global JEMediator if ~isempty(int.mediator) int.mediator.quit @@ -18,6 +19,7 @@ end % set up a mediator and inform JE int.mediator = javaeva.server.go.problems.MatlabEvalMediator; int.mp.setMediator(int.mediator); +JEMediator=int.mediator; % start the JE thread if (optOrPostProc == 1) @@ -34,7 +36,7 @@ end % stopOptimization is empty. if it is then it is not the toolbox calling % and we create an own button to stop it. if isempty(stopOptimization), - % create a cancel button box (only in the case that + % create a cancel button box (case without SBtoolbox) h=figure('Position',[100 600 250 80], 'MenuBar', 'none', 'Name', 'JavaEvA optimization running...', 'NumberTitle','off'); uicontrol(h,'Style', 'pushbutton', 'String', 'Cancel', 'Position', [25 25 60 30], 'Callback', 'global stopOptimization; stopOptimization=1;'); uicontrol(h,'Style', 'text', 'String', stopText, 'Position', [100 25 120 30]); @@ -51,23 +53,34 @@ end stopOnce=1; % repeat the mediator thread and eval call until finished -while (~int.mediator.isFinished()) - int.mediator.run; - if (~int.mediator.isFinished()) - x = int.mediator.getQuestion(); - if (isempty(int.args)) - res = feval(int.f, x); - else - res = feval(int.f, x, int.args); - end - int.mediator.setAnswer(res); - drawnow; - if ((stopOptimization==1) && (stopOnce==1)) - disp('User interrupt requested ...'); - stopOptimize(int); - stopOnce=0; +try + while (~int.mediator.isFinished()) + int.mediator.run; + if (~int.mediator.isFinished()) + x = int.mediator.getQuestion(); + if (isempty(int.args)) + res = feval(int.f, x); + else + res = feval(int.f, x, int.args); + end + int.mediator.setAnswer(res); + drawnow; + if ((stopOptimization==1) && (stopOnce==1)) + disp('User interrupt requested ...'); + stopOptimize(int); + stopOnce=0; + end end end + clear global JEMediator; +catch + disp('Error in evaluate!'); + %int.mediator.quit; % just in case + %int.mediator=''; + if (nontoolboxopt == 1) + if (ishandle(h)) , close(h); end + clear global stopOptimization + end end % write back results @@ -81,6 +94,6 @@ int.mediator=''; % and not from the toolboxes parameter estimation function (which has an % own stop button). we decide this by checking nontoolboxopt if nontoolboxopt == 1, - close(h); + if (ishandle(h)) , close(h); end clear global stopOptimization -end \ No newline at end of file +end diff --git a/resources/MatlabInterface/@JEInterface/stopOptimize.m b/resources/MatlabInterface/@JEInterface/stopOptimize.m index b6cdcb87..5f99303b 100644 --- a/resources/MatlabInterface/@JEInterface/stopOptimize.m +++ b/resources/MatlabInterface/@JEInterface/stopOptimize.m @@ -1,9 +1,22 @@ -function int = stopOptimize(int) -% Stop a running optimization +function int = stopOptimize(int, varargin) +% Stop a running optimization. +% stopOptimize(JI [,'kill']) +% If 'kill' is given as second argument, the mediator thread is +% stopped, relevant if optimization was stopped using CTRL-C and the +% mediator is a running zombie. +global JEMediator + %disp('in Stop!'); int.mp.stopOptimize; -%if (~isempty(int.mediator)) -% int.mediator.quit; % just in case -% int.mediator=''; -%end \ No newline at end of file + +if (nargin > 1) && (ischar(varargin{1}) && (strcmp(varargin{1},'kill')==1)) + if (~isempty(JEMediator)) + disp('killing mediator...'); + JEMediator.quit; % just in case + JEMediator=''; + clear global JEMediator; + else + disp('no mediator to kill'); + end +end \ No newline at end of file diff --git a/src/javaeva/server/go/problems/MatlabEvalMediator.java b/src/javaeva/server/go/problems/MatlabEvalMediator.java index 00f5f46b..ce67d2d3 100644 --- a/src/javaeva/server/go/problems/MatlabEvalMediator.java +++ b/src/javaeva/server/go/problems/MatlabEvalMediator.java @@ -1,5 +1,7 @@ package javaeva.server.go.problems; +import javaeva.gui.BeanInspector; + /** * This implements a thread acting as a mediator between JavaEvA and Matlab. Thanks to the idea * of Henning Schmidt! @@ -26,7 +28,7 @@ public class MatlabEvalMediator implements Runnable { volatile boolean fin = false; volatile double[] question = null; volatile double[] answer = null; - volatile boolean quit = false; + boolean quit = false; volatile double[] optSolution = null; volatile double[][] optSolSet = null; // MatlabProblem mp = null; @@ -44,17 +46,16 @@ public class MatlabEvalMediator implements Runnable { question = x; requesting = true; // int k=0; -// mp.log("Requesting eval for " + BeanInspector.toString(x) + ", req state is " + requesting + "\n"); +// System.out.println("Requesting eval for " + BeanInspector.toString(x) + ", req state is " + requesting + "\n"); while (requesting && !quit) { // wait for matlab to answer the question if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {}; -// k++; -// if (k>100) { +// if ((k%100)==0) { // System.out.println("waiting for matlab to answer..."); -// k=0; // } +// k++; } -// mp.log("Requesting done \n"); +// System.out.println("Requesting done \n"); // matlab is finished, answer is here return getAnswer(); // return to JE with answer } @@ -67,13 +68,12 @@ public class MatlabEvalMediator implements Runnable { while (!requesting && !isFinished() && !quit) { // wait for JE to pose a question or finish all if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {}; -// k++; -// if (k>100) { +// if ((k%100)==0) { // System.out.println("waiting for JE to ask..."); -// k=0; // } +// k++; } -// mp.log("-- Request arrived in MP thread\n"); +// System.out.println("-- Request arrived in MP thread\n"); // requesting is true, now finish and let Matlab work } @@ -81,6 +81,7 @@ public class MatlabEvalMediator implements Runnable { * Cancel waiting in any case. */ public void quit() { +// System.out.println("IN QUIT!"); quit = true; }