From 41cd0a8cb885f9c0490828631bfc2e33d0d5adb5 Mon Sep 17 00:00:00 2001 From: Marcel Kronfeld Date: Tue, 26 Feb 2008 18:03:55 +0000 Subject: [PATCH] Adding current version of JEInterface (Matlab Interface) to resource folder --- .../@JEInterface/JEInterface.m | 69 +++++++++++++++++++ .../MatlabInterface/@JEInterface/evaluateJE.m | 11 +++ .../MatlabInterface/@JEInterface/getMessage.m | 4 ++ .../MatlabInterface/@JEInterface/getOpt.m | 8 +++ .../MatlabInterface/@JEInterface/getOptions.m | 4 ++ .../MatlabInterface/@JEInterface/getResult.m | 10 +++ .../MatlabInterface/@JEInterface/isFinished.m | 4 ++ .../MatlabInterface/@JEInterface/optimize.m | 54 +++++++++++++++ .../MatlabInterface/@JEInterface/setOpt.m | 9 +++ .../MatlabInterface/@JEInterface/setOptions.m | 7 ++ .../@JEInterface/setResultJE.m | 9 +++ .../@JEInterface/stopOptimize.m | 4 ++ 12 files changed, 193 insertions(+) create mode 100644 resources/MatlabInterface/@JEInterface/JEInterface.m create mode 100644 resources/MatlabInterface/@JEInterface/evaluateJE.m create mode 100644 resources/MatlabInterface/@JEInterface/getMessage.m create mode 100644 resources/MatlabInterface/@JEInterface/getOpt.m create mode 100644 resources/MatlabInterface/@JEInterface/getOptions.m create mode 100644 resources/MatlabInterface/@JEInterface/getResult.m create mode 100644 resources/MatlabInterface/@JEInterface/isFinished.m create mode 100644 resources/MatlabInterface/@JEInterface/optimize.m create mode 100644 resources/MatlabInterface/@JEInterface/setOpt.m create mode 100644 resources/MatlabInterface/@JEInterface/setOptions.m create mode 100644 resources/MatlabInterface/@JEInterface/setResultJE.m create mode 100644 resources/MatlabInterface/@JEInterface/stopOptimize.m diff --git a/resources/MatlabInterface/@JEInterface/JEInterface.m b/resources/MatlabInterface/@JEInterface/JEInterface.m new file mode 100644 index 00000000..bea761dc --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/JEInterface.m @@ -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); + + + + diff --git a/resources/MatlabInterface/@JEInterface/evaluateJE.m b/resources/MatlabInterface/@JEInterface/evaluateJE.m new file mode 100644 index 00000000..0206afa3 --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/evaluateJE.m @@ -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 diff --git a/resources/MatlabInterface/@JEInterface/getMessage.m b/resources/MatlabInterface/@JEInterface/getMessage.m new file mode 100644 index 00000000..8a395228 --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/getMessage.m @@ -0,0 +1,4 @@ +function str = getMessage(int) +% Get information string received from JavaEvA. + +str = int.msg; diff --git a/resources/MatlabInterface/@JEInterface/getOpt.m b/resources/MatlabInterface/@JEInterface/getOpt.m new file mode 100644 index 00000000..fc0b24ae --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/getOpt.m @@ -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); \ No newline at end of file diff --git a/resources/MatlabInterface/@JEInterface/getOptions.m b/resources/MatlabInterface/@JEInterface/getOptions.m new file mode 100644 index 00000000..bbdda818 --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/getOptions.m @@ -0,0 +1,4 @@ +function opts = getOptions(int) +% Return the current optimset structure used by the interface + +opts = int.opts; \ No newline at end of file diff --git a/resources/MatlabInterface/@JEInterface/getResult.m b/resources/MatlabInterface/@JEInterface/getResult.m new file mode 100644 index 00000000..390a96db --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/getResult.m @@ -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 \ No newline at end of file diff --git a/resources/MatlabInterface/@JEInterface/isFinished.m b/resources/MatlabInterface/@JEInterface/isFinished.m new file mode 100644 index 00000000..f6163b82 --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/isFinished.m @@ -0,0 +1,4 @@ +function b = isFinished(int) +% Returns 1 if the optimization was finished, else 0 + +b = int.finished; diff --git a/resources/MatlabInterface/@JEInterface/optimize.m b/resources/MatlabInterface/@JEInterface/optimize.m new file mode 100644 index 00000000..a261546a --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/optimize.m @@ -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; diff --git a/resources/MatlabInterface/@JEInterface/setOpt.m b/resources/MatlabInterface/@JEInterface/setOpt.m new file mode 100644 index 00000000..7330cb0f --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/setOpt.m @@ -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; \ No newline at end of file diff --git a/resources/MatlabInterface/@JEInterface/setOptions.m b/resources/MatlabInterface/@JEInterface/setOptions.m new file mode 100644 index 00000000..85e5bac5 --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/setOptions.m @@ -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; \ No newline at end of file diff --git a/resources/MatlabInterface/@JEInterface/setResultJE.m b/resources/MatlabInterface/@JEInterface/setResultJE.m new file mode 100644 index 00000000..8e8f808e --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/setResultJE.m @@ -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; diff --git a/resources/MatlabInterface/@JEInterface/stopOptimize.m b/resources/MatlabInterface/@JEInterface/stopOptimize.m new file mode 100644 index 00000000..a7d6c540 --- /dev/null +++ b/resources/MatlabInterface/@JEInterface/stopOptimize.m @@ -0,0 +1,4 @@ +function int = stopOptimize(int) +% Stop a running optimization + +int.mp.stopOptimize