Merging mk rev. 211, 212: Matlab interface now works with binary data using uint32 (GAIndividualIntegerData).
This commit is contained in:
@@ -6,7 +6,7 @@ function int = JEInterface(interfaceName, fhandle, range, varargin)
|
||||
% 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.
|
||||
% upper bounds - or a scalar defining the bitwidth for binary problems.
|
||||
% optset: (optional) an optimset struct defining optimization parameters,
|
||||
% especially tolerance and maximum function calls. Defaults to the
|
||||
% EvA2 default values.
|
||||
@@ -24,6 +24,11 @@ function int = JEInterface(interfaceName, fhandle, range, varargin)
|
||||
% and TolFun for a certain time, e.g. 100 evaluations.
|
||||
% To ignore a criterion, set it to 0. E.g. to perform 10^5 evaluations in
|
||||
% any case, set TolX=TolFun=0 and MaxFunEvals=10^5.
|
||||
%
|
||||
% You may define a 2xdim range with a double valued function handle or for
|
||||
% binary problems set a scalar as range defining the number of bits to be
|
||||
% used. The values passed to the function handle will then be arrays of
|
||||
% uint32, each of them representing 32 bits.
|
||||
|
||||
int.args = [];
|
||||
int.opts = optimset('MaxFunEvals', eva2.OptimizerFactory.getDefaultFitCalls, 'TolX', 1e-4, 'TolFun', 1e-4);
|
||||
@@ -40,6 +45,7 @@ int.funCalls = 0;
|
||||
int.mediator = '';
|
||||
int.optParams = [];
|
||||
int.optParamValues = [];
|
||||
int.hexMask=hex2dec('ffffffff');
|
||||
|
||||
if (isa(interfaceName, 'char'));
|
||||
int.callback = interfaceName;
|
||||
@@ -56,7 +62,11 @@ 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');
|
||||
%error('Wrong third argument type, expected double array of 2 x dim');
|
||||
if (size(range,1)==size(range,2)==1)
|
||||
int.range=[];
|
||||
int.dim=range;
|
||||
end
|
||||
end
|
||||
|
||||
int = class(int,'JEInterface');
|
||||
@@ -80,7 +90,7 @@ switch nargin
|
||||
end
|
||||
|
||||
% finally create the java object
|
||||
int.mp = eva2.server.go.problems.MatlabProblem(int.callback, int.dim, int.range);
|
||||
int.mp = eva2.server.go.problems.MatlabProblem(int.dim, int.range);
|
||||
|
||||
|
||||
|
||||
|
14
resources/MatlabInterface/@JEInterface/convertUnsignedJE.m
Normal file
14
resources/MatlabInterface/@JEInterface/convertUnsignedJE.m
Normal file
@@ -0,0 +1,14 @@
|
||||
function [ z ] = convertUnsignedJE( int, x )
|
||||
%CONVERTUNSIGNEDJE Convert signed 32-bit integer to unsigned.
|
||||
% Detailed explanation goes here
|
||||
|
||||
z=zeros(size(x,1),size(x,2), 'uint32');
|
||||
for j=1 : size(x,1)
|
||||
for i=1 : size(x,2)
|
||||
if (x(j,i) < 0)
|
||||
z(j,i) = 1+bitxor(uint32(-x(j,i)), int.hexMask);
|
||||
else
|
||||
z(j,i) = x(j,i);
|
||||
end
|
||||
end
|
||||
end
|
@@ -13,6 +13,10 @@ end
|
||||
if (isempty(sol))
|
||||
fit = NaN;
|
||||
else
|
||||
if (isempty(int.range))
|
||||
sol=convertUnsignedJE(int, sol);
|
||||
end;
|
||||
|
||||
if (isempty(int.args))
|
||||
fit = feval(int.f, sol);
|
||||
else
|
||||
|
@@ -64,10 +64,24 @@ try
|
||||
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);
|
||||
if (isempty(int.range))
|
||||
%size(x)
|
||||
x=convertUnsignedJE(int, x);
|
||||
%disp('here B');
|
||||
%x
|
||||
end
|
||||
% size(x)
|
||||
try
|
||||
if (isempty(int.args))
|
||||
res = feval(int.f, x);
|
||||
else
|
||||
res = feval(int.f, x, int.args);
|
||||
end
|
||||
%res
|
||||
catch ME
|
||||
disp('function evaluation failed:');
|
||||
disp(ME.message);
|
||||
stopOptimization=1;
|
||||
end
|
||||
int.mediator.setAnswer(res);
|
||||
drawnow;
|
||||
@@ -79,8 +93,9 @@ try
|
||||
end
|
||||
end
|
||||
clear global JEMediator;
|
||||
catch
|
||||
catch ME
|
||||
disp('Error in evaluate!');
|
||||
disp(ME.message);
|
||||
%int.mediator.quit; % just in case
|
||||
%int.mediator='';
|
||||
|
||||
|
@@ -2,7 +2,6 @@ function int = setJEResult(int, result)
|
||||
% Interface function to be called by EvA2.
|
||||
|
||||
% Write back the solution and retrieve some additional data.
|
||||
|
||||
int.result = result;
|
||||
int.finished = 1;
|
||||
int.msg=int.mp.getInfoString;
|
||||
|
Reference in New Issue
Block a user