Updating the JEInterface Matlab code
This commit is contained in:
parent
50dfec343f
commit
b14118bb6f
@ -3,19 +3,18 @@ function int = JEInterface(fhandle, range, varargin)
|
|||||||
% JEInterface(interfaceName, fhandle, range)
|
% JEInterface(interfaceName, fhandle, range)
|
||||||
% JEInterface(interfaceName, fhandle, range, defaultargs)
|
% JEInterface(interfaceName, fhandle, range, defaultargs)
|
||||||
% JEInterface(interfaceName, fhandle, range, defaultargs, options...)
|
% JEInterface(interfaceName, fhandle, range, defaultargs, options...)
|
||||||
|
%
|
||||||
|
% Arguments:
|
||||||
% arguments:
|
|
||||||
% fhandle: a function handle defining the optimization target.
|
% fhandle: a function handle defining the optimization target.
|
||||||
% range: a 2 x dim array defining the solution subspace with lower and
|
% range: a 2 x dim array defining the solution subspace with lower and
|
||||||
% upper bounds - or a scalar defining the bitwidth for binary
|
% upper bounds; or a scalar defining the bitwidth for binary
|
||||||
% problems.
|
% problems.
|
||||||
% defaultArgs: (optional) additional constant argument to the target
|
% defaultArgs: (optional) additional constant argument to the target
|
||||||
% function, empty by default.
|
% function, empty by default.
|
||||||
% options: (optional) options as name-value pairs defining optimization parameters,
|
% options: (optional) options as name-value pairs defining optimization parameters,
|
||||||
% especially tolerance and maximum function calls.
|
% especially tolerance and maximum function calls.
|
||||||
% Check makeOoptions for default settings.
|
% Check makeOoptions for default settings.
|
||||||
|
%
|
||||||
% Further options may be specified using setOptions with a JE options struct.
|
% Further options may be specified using setOptions with a JE options struct.
|
||||||
% Main options are:
|
% Main options are:
|
||||||
% TolX: convergence criterion in the solution space
|
% TolX: convergence criterion in the solution space
|
||||||
@ -85,13 +84,13 @@ if (nargin>2)
|
|||||||
if (rem(nargin,2)==0)
|
if (rem(nargin,2)==0)
|
||||||
error('Invalid number of arguments!');
|
error('Invalid number of arguments!');
|
||||||
end
|
end
|
||||||
disp('Reading options...');
|
disp('Reading options:');
|
||||||
for i=2:2:nargin-2
|
for i=2:2:nargin-2
|
||||||
int=setOpt(int, varargin{i}, varargin{i+1});
|
int=setOpt(int, varargin{i}, varargin{i+1});
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
display(getOptions(int));
|
||||||
% finally create the java object
|
% finally create the java object
|
||||||
int.mp = eva2.server.go.problems.MatlabProblem(int.dim, int.range);
|
int.mp = eva2.server.go.problems.MatlabProblem(int.dim, int.range);
|
||||||
disp('Java object created');
|
disp('Java object created');
|
||||||
|
@ -1,18 +1,31 @@
|
|||||||
function opts = makeOptions(int, varargin)
|
function opts = makeOptions(int, varargin)
|
||||||
% Create a JEInterface options set from scratch. Possible fields are:
|
% Create a JEInterface options set from scratch. Possible fields are:
|
||||||
%'Display';
|
%'Display';
|
||||||
%'MaxFunEvals';'MaxIter';'TolFun';'TolFunEvals';TolX';'TolXEvals', where
|
%'MaxFunEvals';'MaxIter';'TolFun';'TolFunEvals';TolX';'TolXEvals', which,
|
||||||
% all but 'TolFunEvals', 'TolXEvals' are used similar to the optimset.
|
% except for 'TolFunEvals' and 'TolXEvals', are used similar to the optimset.
|
||||||
% The latter two are interpreted as the numbers of evaluations required
|
% The latter two are interpreted as the numbers of evaluations required
|
||||||
% to assume convergence. Default values are TolXEvals=TolFunEvals=200,
|
% to assume convergence. Default values are TolXEvals=TolFunEvals=200,
|
||||||
% TolX=TolFun=1e-4, MaxFunEvals uses a default from EvA2.
|
% TolX=TolFun=1e-4, MaxFunEvals uses a default from EvA2.
|
||||||
|
% Further options are 'CreateStopBox' and 'NiceSleepTime' which are
|
||||||
|
% JE-specific. The 'CreateStopBox' option toggles the GUI box with stop
|
||||||
|
% button provided for graceful interruption of an optimization run.
|
||||||
|
% In cases without X-forwarding, a deactivation by setting it to 0 may be required,
|
||||||
|
% but note that killing optimization may break the threading mechanism. Try stopOptimize with
|
||||||
|
% the 'kill' option.
|
||||||
|
%
|
||||||
|
% 'NiceSleepTime' allows setting a sleep time (in ms) for the concurrent
|
||||||
|
% threading. Technically, either the Matlab
|
||||||
|
% or the Java thread is always waiting for the other one, which may require
|
||||||
|
% full CPU if no sleep time is employed. However, for quick function evaluations,
|
||||||
|
% even minor sleep times substantially reduce allover efficiency, so we advice
|
||||||
|
% to set small sleep times (e.g. 5 ms) only if a single function evaluation
|
||||||
|
% takes considerably longer and leave the sleep time at zero otherwise.
|
||||||
|
%
|
||||||
% Notice that this method creates a parameter set but does not assign it
|
% Notice that this method creates a parameter set but does not assign it
|
||||||
% to the interface instance. Use setOptions to do that.
|
% to the interface instance. Use setOptions to do that.
|
||||||
|
|
||||||
allfields = {'Display'; 'MaxFunEvals';'MaxIter';'TolFun';'TolFunEvals';...
|
allfields = {'Display'; 'MaxFunEvals';'MaxIter';'TolFun';'TolFunEvals';...
|
||||||
'TolX';'TolXEvals'};
|
'TolX'; 'TolXEvals'; 'CreateStopBox'; 'NiceSleepTime'};
|
||||||
|
|
||||||
specialfields={'TolFunEvals', 'TolXEvals'};
|
|
||||||
|
|
||||||
nvararg=nargin-1;
|
nvararg=nargin-1;
|
||||||
if rem(nvararg,2)==1
|
if rem(nvararg,2)==1
|
||||||
@ -36,6 +49,8 @@ opts.('TolX') = 1e-4;
|
|||||||
opts.('TolXEvals') = 200;
|
opts.('TolXEvals') = 200;
|
||||||
opts.('TolFun') = 1e-4;
|
opts.('TolFun') = 1e-4;
|
||||||
opts.('TolFunEvals') = 200;
|
opts.('TolFunEvals') = 200;
|
||||||
|
opts.('CreateStopBox')=1;
|
||||||
|
opts.('NiceSleepTime')=0;
|
||||||
|
|
||||||
for i=1:nvararg/2
|
for i=1:nvararg/2
|
||||||
name=varargin{2*i-1};
|
name=varargin{2*i-1};
|
||||||
@ -48,18 +63,27 @@ for i=1:nvararg/2
|
|||||||
if isempty(optIndex)
|
if isempty(optIndex)
|
||||||
error('Unknown option %s !', name);
|
error('Unknown option %s !', name);
|
||||||
else
|
else
|
||||||
if ~isempty(strmatch(name, specialfields,'exact'))
|
switch name
|
||||||
% test for integer
|
case {'TolFunEvals', 'TolXEvals'} % for special fields, integers > 1 are allowed
|
||||||
if (~isscalar(value) || ~isnumeric(value) || round(value)<1)
|
if ~isscalar(value) || ~isnumeric(value) || round(value)<1
|
||||||
error('invalid value type for %s, expecting numeric scalar > 1!', name);
|
error('Invalid value type for %s, expecting positive numeric scalar!', name);
|
||||||
end
|
end;
|
||||||
value=round(value);
|
value=round(value);
|
||||||
else
|
case 'CreateStopBox'
|
||||||
% test using optimset
|
if ~isscalar(value) || ~isnumeric(value) || round(value)<0 || round(value)>1
|
||||||
optimset(stdSet, name, value);
|
error('Invalid value type for %s, expecting 0 or 1!', name);
|
||||||
|
end;
|
||||||
|
value=round(value);
|
||||||
|
case 'NiceSleepTime'
|
||||||
|
if ~isscalar(value) || ~isnumeric(value) || round(value)<0 ;
|
||||||
|
error('Invalid value type for %s, expecting numeric scalar >= 0!', name);
|
||||||
|
end;
|
||||||
|
value=round(value);
|
||||||
|
otherwise % test using optimset
|
||||||
|
optimset(stdSet, name, value);
|
||||||
end
|
end
|
||||||
% assign to struct
|
% assign to struct
|
||||||
opts.(allfields{optIndex,:}) = value;
|
opts.(allfields{optIndex,:}) = value;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -19,11 +19,15 @@ if ~isempty(int.mediator)
|
|||||||
int.mediator.quit
|
int.mediator.quit
|
||||||
int.mediator='';
|
int.mediator='';
|
||||||
end
|
end
|
||||||
|
% disp(sprintf('creating mediator'));
|
||||||
|
|
||||||
% set up a mediator and inform JE
|
% set up a mediator and inform JE
|
||||||
int.mediator = eva2.server.go.problems.MatlabEvalMediator;
|
int.mediator = eva2.server.go.problems.MatlabEvalMediator(int.opts.NiceSleepTime);
|
||||||
int.mp.setMediator(int.mediator);
|
int.mp.setMediator(int.mediator);
|
||||||
JEMediator=int.mediator;
|
JEMediator=int.mediator;
|
||||||
|
createStopBox=int.opts.CreateStopBox;
|
||||||
|
|
||||||
|
% disp(sprintf('mediator created, calling optimize'));
|
||||||
|
|
||||||
% start the JE thread
|
% start the JE thread
|
||||||
if (optOrPostProc == 1)
|
if (optOrPostProc == 1)
|
||||||
@ -33,6 +37,7 @@ else % post processing
|
|||||||
stopText='Stop EvA2 post processing';
|
stopText='Stop EvA2 post processing';
|
||||||
int.mp.requestPostProcessing(steps, sigmaClust, nBest);
|
int.mp.requestPostProcessing(steps, sigmaClust, nBest);
|
||||||
end
|
end
|
||||||
|
% disp(sprintf('calling optimize done'));
|
||||||
|
|
||||||
% handle the case when the optimization has been called from a users script
|
% handle the case when the optimization has been called from a users script
|
||||||
% and not from the toolboxes parameter estimation function (which has an
|
% and not from the toolboxes parameter estimation function (which has an
|
||||||
@ -47,10 +52,15 @@ if isempty(stopOptimization),
|
|||||||
disp(sprintf('Starting optimization at %s', timeStr));
|
disp(sprintf('Starting optimization at %s', timeStr));
|
||||||
% create a cancel button box (case without SBtoolbox)
|
% create a cancel button box (case without SBtoolbox)
|
||||||
stopText=sprintf('%s (%s)', stopText, timeStr);
|
stopText=sprintf('%s (%s)', stopText, timeStr);
|
||||||
boxHandle=figure('Position',[100 600 250 80], 'MenuBar', 'none', 'Name', 'EvA2 optimization...', 'NumberTitle','off');
|
if (createStopBox == 1)
|
||||||
uicontrol(boxHandle,'Style', 'pushbutton', 'String', 'Cancel', 'Position', [25 25 60 30], 'Callback', 'global stopOptimization; stopOptimization=1;');
|
boxHandle=figure('Position',[100 600 250 80], 'MenuBar', 'none', 'Name', 'EvA2 optimization...', 'NumberTitle','off');
|
||||||
uicontrol(boxHandle,'Style', 'text', 'String', stopText, 'Position', [100 15 130 50]);
|
uicontrol(boxHandle,'Style', 'pushbutton', 'String', 'Cancel', 'Position', [25 25 60 30], 'Callback', 'global stopOptimization; stopOptimization=1;');
|
||||||
drawnow;
|
uicontrol(boxHandle,'Style', 'text', 'String', stopText, 'Position', [100 15 130 50]);
|
||||||
|
drawnow;
|
||||||
|
% else
|
||||||
|
% disp(stopText);
|
||||||
|
end
|
||||||
|
|
||||||
% set flag for non toolbox optimization
|
% set flag for non toolbox optimization
|
||||||
nontoolboxopt = 1;
|
nontoolboxopt = 1;
|
||||||
else
|
else
|
||||||
@ -60,13 +70,19 @@ else
|
|||||||
end
|
end
|
||||||
|
|
||||||
stopOnce=1;
|
stopOnce=1;
|
||||||
|
cnt=1;
|
||||||
|
% disp(sprintf('before eval loop... %d',cnt));
|
||||||
% repeat the mediator thread and eval call until finished
|
% repeat the mediator thread and eval call until finished
|
||||||
try
|
try
|
||||||
while (~int.mediator.isFinished())
|
while (~int.mediator.isFinished())
|
||||||
int.mediator.run;
|
% disp(sprintf('running mediator id %d',cnt));
|
||||||
|
int.mediator.run(cnt);
|
||||||
|
% disp(sprintf('after running mediator id %d',cnt));
|
||||||
|
cnt=cnt+1;
|
||||||
if (~int.mediator.isFinished())
|
if (~int.mediator.isFinished())
|
||||||
|
%disp('getting question');
|
||||||
x = int.mediator.getQuestion();
|
x = int.mediator.getQuestion();
|
||||||
|
%disp('question asked');
|
||||||
if (isempty(int.range))
|
if (isempty(int.range))
|
||||||
%size(x)
|
%size(x)
|
||||||
x=convertUnsignedJE(int, x);
|
x=convertUnsignedJE(int, x);
|
||||||
@ -80,6 +96,7 @@ try
|
|||||||
else
|
else
|
||||||
res = feval(int.f, x, int.args);
|
res = feval(int.f, x, int.args);
|
||||||
end
|
end
|
||||||
|
% disp(sprintf('res is %d',res));
|
||||||
%res
|
%res
|
||||||
catch ME
|
catch ME
|
||||||
disp('function evaluation failed:');
|
disp('function evaluation failed:');
|
||||||
@ -87,6 +104,7 @@ try
|
|||||||
stopOptimization=1;
|
stopOptimization=1;
|
||||||
end
|
end
|
||||||
int.mediator.setAnswer(res);
|
int.mediator.setAnswer(res);
|
||||||
|
%disp('answer provided');
|
||||||
drawnow;
|
drawnow;
|
||||||
if ((stopOptimization==1) && (stopOnce==1))
|
if ((stopOptimization==1) && (stopOnce==1))
|
||||||
disp('User interrupt requested ...');
|
disp('User interrupt requested ...');
|
||||||
@ -108,7 +126,7 @@ catch ME
|
|||||||
% clear global stopOptimization
|
% clear global stopOptimization
|
||||||
%end
|
%end
|
||||||
end
|
end
|
||||||
|
% disp('writing back results');
|
||||||
% write back results
|
% write back results
|
||||||
int=setResultJE(int, int.mediator.getSolution());
|
int=setResultJE(int, int.mediator.getSolution());
|
||||||
int=setResultArrayJE(int, int.mediator.getSolutionSet());
|
int=setResultArrayJE(int, int.mediator.getSolutionSet());
|
||||||
@ -120,6 +138,9 @@ 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,
|
||||||
if (ishandle(boxHandle)) , close(boxHandle); end
|
if createStopBox==1
|
||||||
|
if (ishandle(boxHandle)) , close(boxHandle); end
|
||||||
|
end
|
||||||
clear global stopOptimization
|
clear global stopOptimization
|
||||||
end
|
end
|
||||||
|
% disp('runEvalLoop done');
|
||||||
|
@ -14,8 +14,11 @@ end
|
|||||||
if (nargin > 2)
|
if (nargin > 2)
|
||||||
if ischar(varargin{1})
|
if ischar(varargin{1})
|
||||||
fname=varargin{1};
|
fname=varargin{1};
|
||||||
disp('Writing debug output to ');
|
if (bOn==1)
|
||||||
disp(fname);
|
disp(['Writing debug output to ' fname]);
|
||||||
|
else
|
||||||
|
disp('Debug output deactivated');
|
||||||
|
end
|
||||||
else
|
else
|
||||||
disp('Invalid third argument, expected char. Using default output file name');
|
disp('Invalid third argument, expected char. Using default output file name');
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user