Adding current version of JEInterface (Matlab Interface) to resource folder

This commit is contained in:
Marcel Kronfeld 2008-02-26 18:03:55 +00:00
parent 4ecb9e9841
commit 41cd0a8cb8
12 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,69 @@
function int = JEInterface(interfaceName, fhandle, range, varargin)
% JavaEva Interface for Matlab
% JEInterface(interfaceName, fhandle, range [, optset, defaultargs])
% arguments:
% interfaceName: a JEInterface instance needs to know its own
% name as a String to allow callbacks from Java.
% fhandle: a function handle defining the optimization target.
% range: a 2 x dim array defining the solution subspace with lower and
% upper bounds.
% optset: (optional) an optimset struct defining optimization parameters,
% especially tolerance and maximum function calls. Defaults to the
% JavaEvA default values.
% defaultArgs: (optional) additional constant argument to the target
% function, empty by default.
int.args = [];
int.opts = optimset('MaxFunEvals', javaeva.OptimizerFactory.getDefaultFitCalls, 'TolX', 1e-4, 'TolFun', 1e-4);
int.finished = 1;
int.result = [];
int.callback='';
int.f = '';
int.dim = 0;
int.range = [];
int.mp = [];
int.msg = '';
int.funCalls = 0;
if (isa(interfaceName, 'char'));
int.callback = interfaceName;
else
error('Wrong first argument type, expected char');
end
if (isa(fhandle, 'function_handle'))
int.f = fhandle;
else
error('Wrong second argument type, expected function_handle');
end
if (isa(range, 'double') && (size(range,1) == 2))
int.dim=length(range);
int.range=transpose(range);
else
error('Wrong third argument type, expected double array of 2 x dim');
end
int = class(int,'JEInterface');
switch nargin
case {3}
case {4,5}
if (isa(varargin{1}, 'struct'))
int.opts = varargin{1};
if (isempty(int.opts.TolX)) ; int.opts.TolX = 1e-4; end
if (isempty(int.opts.TolFun)) ; int.opts.TolFun = 1e-4; end
else
error('Wrong fifth argument type, expected optimset struct');
end
if (nargin > 4)
int.args = varargin{2};
end
otherwise
error('Wrong number of arguments!')
end
% finally create the java object
int.mp = javaeva.server.go.problems.MatlabProblem(int.callback, int.dim, int.range);

View File

@ -0,0 +1,11 @@
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

@ -0,0 +1,4 @@
function str = getMessage(int)
% Get information string received from JavaEvA.
str = int.msg;

View File

@ -0,0 +1,8 @@
function val = getOpt(int, optName)
% Set a single optimset value within the JI instance.
% Arguments:
% int: the JEInterface instance
% optName: name of the option to change, e.g. 'MaxFunEvals'
% optVal: new value
val = optimget(int.opts, optName);

View File

@ -0,0 +1,4 @@
function opts = getOptions(int)
% Return the current optimset structure used by the interface
opts = int.opts;

View File

@ -0,0 +1,10 @@
function v = getResult(int)
% Returns the optimization solution if the run has been finished, or an
% intermediate solution if the run has not finished yet or an empty array
% if there is no intermediate solution yet.
if (isFinished(int))
v = int.result;
else
v = int.mp.getIntermediateResult();
end

View File

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

View File

@ -0,0 +1,54 @@
function retInt = optimize(int, optType, varargin)
% Start a JavaEvA optimization run.
% optimize(interface, optType, [, outputFilePrefix ] )
% where
% interface: instance of JEInterface
% optType: integer indicating the type of the optimization strategy
% to use.
% resultFilePrefix: (optional) char prefix for an optional verbose
% output file
if (int.finished == 0)
error('please wait for the current run to finish');
end
if ((nargin == 2) || (nargin == 3))
if (nargin == 3)
outputFilePrefix = varargin{1};
else
outputFilePrefix = 'none';
end
if (~isa(int, 'JEInterface') || ~isscalar(optType) || ~isa(outputFilePrefix, 'char'))
error('Invalid argument!')
end
int.finished = 0;
int.msg = 'running...';
% adapt options possibly changed by the user, concerning
xTol = int.opts.TolX;
maxEvals = int.opts.MaxFunEvals;
fTol = int.opts.TolFun;
% construct Terminators
import javaeva.server.go.operators.terminators.PhenotypeConvergenceTerminator;
import javaeva.server.go.operators.terminators.FitnessConvergenceTerminator;
import javaeva.server.go.operators.terminators.CombinedTerminator;
import javaeva.server.go.operators.terminators.EvaluationTerminator;
% set some default values if theyre not given
if (isempty(int.opts.TolX)) ; xTol = 1e-4; end
if (isempty(int.opts.TolFun)) ; fTol = 1e-4; end
% fminsearch, for example, always uses TolX and TolFun with default
% values of 1e-4 in . Thats what we do as well
convTerm = CombinedTerminator(FitnessConvergenceTerminator(fTol, int32(5), 0, 1), PhenotypeConvergenceTerminator(xTol, 5, 0, 1), 1);
% if MaxFunEvals is defined additionally, combine an
% EvaluationTerminator in disjunction, as Matlab does.
if (~isempty(maxEvals))
javaeva.OptimizerFactory.setTerminator(convTerm);
javaeva.OptimizerFactory.addTerminator(EvaluationTerminator(maxEvals), 0);
end
int.mp.optimize(optType, outputFilePrefix);
else
error('Wrong number of arguments!')
end
retInt=int;

View File

@ -0,0 +1,9 @@
function int = setOpt(int, optName, optVal)
% Set a single optimset value within the JI instance.
% Arguments:
% int: the JEInterface instance
% optName: name of the option to change, e.g. 'MaxFunEvals'
% optVal: new value
opts = optimset(int.opts, optName, optVal);
int.opts = opts;

View File

@ -0,0 +1,7 @@
function int = setOptions(int, options)
% Set the optimization options for the interface.
% parameters:
% int: an interface instance
% options: an optimset instance
int.opts = options;

View File

@ -0,0 +1,9 @@
function int = setJEResult(int, result)
% Interface function to be called by JavaEvA 2.
% Write back the solution and retrieve some additional data.
int.result = result;
int.finished = 1;
int.msg=int.mp.getInfoString;
int.funCalls=int.mp.getFunctionCalls;

View File

@ -0,0 +1,4 @@
function int = stopOptimize(int)
% Stop a running optimization
int.mp.stopOptimize