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)
% Deprecated: optimization now runs synchronously.
% Returns 1 if the optimization was finished, else 0
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.
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
end

View File

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

View File

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