Another JEInterface update

This commit is contained in:
Marcel Kronfeld 2008-04-02 14:36:41 +00:00
parent 17411d6775
commit c490cf1e29
5 changed files with 62 additions and 45 deletions

View File

@ -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

View File

@ -1,4 +1,5 @@
function b = isFinished(int) function b = isFinished(int)
% Deprecated: optimization now runs synchronously.
% Returns 1 if the optimization was finished, else 0 % Returns 1 if the optimization was finished, else 0
b = int.finished; b = int.finished;

View File

@ -9,6 +9,7 @@ function int=runEvalLoopJE(int, optOrPostProc, optType, outputFilePrefix, steps,
% when optimizing. nBest may be -1 to show all. % when optimizing. nBest may be -1 to show all.
global stopOptimization global stopOptimization
global JEMediator
if ~isempty(int.mediator) if ~isempty(int.mediator)
int.mediator.quit int.mediator.quit
@ -18,6 +19,7 @@ end
% set up a mediator and inform JE % set up a mediator and inform JE
int.mediator = javaeva.server.go.problems.MatlabEvalMediator; int.mediator = javaeva.server.go.problems.MatlabEvalMediator;
int.mp.setMediator(int.mediator); int.mp.setMediator(int.mediator);
JEMediator=int.mediator;
% start the JE thread % start the JE thread
if (optOrPostProc == 1) if (optOrPostProc == 1)
@ -34,7 +36,7 @@ end
% stopOptimization is empty. if it is then it is not the toolbox calling % stopOptimization is empty. if it is then it is not the toolbox calling
% and we create an own button to stop it. % and we create an own button to stop it.
if isempty(stopOptimization), 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'); 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', 'pushbutton', 'String', 'Cancel', 'Position', [25 25 60 30], 'Callback', 'global stopOptimization; stopOptimization=1;');
uicontrol(h,'Style', 'text', 'String', stopText, 'Position', [100 25 120 30]); uicontrol(h,'Style', 'text', 'String', stopText, 'Position', [100 25 120 30]);
@ -51,6 +53,7 @@ end
stopOnce=1; stopOnce=1;
% repeat the mediator thread and eval call until finished % repeat the mediator thread and eval call until finished
try
while (~int.mediator.isFinished()) while (~int.mediator.isFinished())
int.mediator.run; int.mediator.run;
if (~int.mediator.isFinished()) if (~int.mediator.isFinished())
@ -69,6 +72,16 @@ while (~int.mediator.isFinished())
end end
end 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 % write back results
int=setResultJE(int, int.mediator.getSolution()); int=setResultJE(int, int.mediator.getSolution());
@ -81,6 +94,6 @@ int.mediator='';
% and not from the toolboxes parameter estimation function (which has an % and not from the toolboxes parameter estimation function (which has an
% own stop button). we decide this by checking nontoolboxopt % own stop button). we decide this by checking nontoolboxopt
if nontoolboxopt == 1, if nontoolboxopt == 1,
close(h); if (ishandle(h)) , close(h); end
clear global stopOptimization clear global stopOptimization
end end

View File

@ -1,9 +1,22 @@
function int = stopOptimize(int) function int = stopOptimize(int, varargin)
% Stop a running optimization % 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!'); %disp('in Stop!');
int.mp.stopOptimize; int.mp.stopOptimize;
%if (~isempty(int.mediator))
% int.mediator.quit; % just in case if (nargin > 1) && (ischar(varargin{1}) && (strcmp(varargin{1},'kill')==1))
% int.mediator=''; if (~isempty(JEMediator))
%end disp('killing mediator...');
JEMediator.quit; % just in case
JEMediator='';
clear global JEMediator;
else
disp('no mediator to kill');
end
end

View File

@ -1,5 +1,7 @@
package javaeva.server.go.problems; 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 * This implements a thread acting as a mediator between JavaEvA and Matlab. Thanks to the idea
* of Henning Schmidt! * of Henning Schmidt!
@ -26,7 +28,7 @@ public class MatlabEvalMediator implements Runnable {
volatile boolean fin = false; volatile boolean fin = false;
volatile double[] question = null; volatile double[] question = null;
volatile double[] answer = null; volatile double[] answer = null;
volatile boolean quit = false; boolean quit = false;
volatile double[] optSolution = null; volatile double[] optSolution = null;
volatile double[][] optSolSet = null; volatile double[][] optSolSet = null;
// MatlabProblem mp = null; // MatlabProblem mp = null;
@ -44,17 +46,16 @@ public class MatlabEvalMediator implements Runnable {
question = x; question = x;
requesting = true; requesting = true;
// int k=0; // 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) { while (requesting && !quit) {
// wait for matlab to answer the question // wait for matlab to answer the question
if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {}; if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {};
// k++; // if ((k%100)==0) {
// if (k>100) {
// System.out.println("waiting for matlab to answer..."); // 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 // matlab is finished, answer is here
return getAnswer(); // return to JE with answer return getAnswer(); // return to JE with answer
} }
@ -67,13 +68,12 @@ public class MatlabEvalMediator implements Runnable {
while (!requesting && !isFinished() && !quit) { while (!requesting && !isFinished() && !quit) {
// wait for JE to pose a question or finish all // wait for JE to pose a question or finish all
if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {}; if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {};
// k++; // if ((k%100)==0) {
// if (k>100) {
// System.out.println("waiting for JE to ask..."); // 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 // requesting is true, now finish and let Matlab work
} }
@ -81,6 +81,7 @@ public class MatlabEvalMediator implements Runnable {
* Cancel waiting in any case. * Cancel waiting in any case.
*/ */
public void quit() { public void quit() {
// System.out.println("IN QUIT!");
quit = true; quit = true;
} }