Matlab interface now uses true binary data type for binary problems
This commit is contained in:
parent
e8c14b0aa8
commit
a207014610
@ -2,13 +2,24 @@ function [ z ] = convertUnsignedJE( int, x )
|
|||||||
%CONVERTUNSIGNEDJE Convert signed 32-bit integer to unsigned.
|
%CONVERTUNSIGNEDJE Convert signed 32-bit integer to unsigned.
|
||||||
% Detailed explanation goes here
|
% Detailed explanation goes here
|
||||||
|
|
||||||
z=zeros(size(x,1),size(x,2), 'uint32');
|
if strcmp(class(x),'java.util.BitSet')
|
||||||
for j=1 : size(x,1)
|
z=num2str(10^(int.dim-1));
|
||||||
for i=1 : size(x,2)
|
for i=1:int.dim
|
||||||
if (x(j,i) < 0)
|
if (x.get(i-1)) % Java indices start at zero!
|
||||||
z(j,i) = 1+bitxor(uint32(-x(j,i)), int.hexMask);
|
z(i)='1';
|
||||||
else
|
else
|
||||||
z(j,i) = x(j,i);
|
z(i)='0';
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
z=zeros(size(x,1),size(x,2), 'int32');
|
||||||
|
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
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -78,7 +78,7 @@ try
|
|||||||
% disp(sprintf('running mediator id %d',cnt));
|
% disp(sprintf('running mediator id %d',cnt));
|
||||||
int.mediator.run(cnt);
|
int.mediator.run(cnt);
|
||||||
% disp(sprintf('after running mediator id %d',cnt));
|
% disp(sprintf('after running mediator id %d',cnt));
|
||||||
cnt=cnt+1;
|
cnt=cnt+1;
|
||||||
if (~int.mediator.isFinished())
|
if (~int.mediator.isFinished())
|
||||||
% disp('getting question');
|
% disp('getting question');
|
||||||
x = int.mediator.getQuestion();
|
x = int.mediator.getQuestion();
|
||||||
|
@ -6,11 +6,13 @@ if (isempty(int.range))
|
|||||||
% binary problem
|
% binary problem
|
||||||
s=sprintf('Binary problem of bitwidth %d', int.dim);
|
s=sprintf('Binary problem of bitwidth %d', int.dim);
|
||||||
disp(s);
|
disp(s);
|
||||||
numInts=ceil(int.dim/wordwidth);
|
%numInts=ceil(int.dim/wordwidth);
|
||||||
% generate trial vector
|
% generate trial vector
|
||||||
x=ceil(rand(1,numInts).*(2^wordwidth));
|
%x=ceil(rand(1,numInts).*(2^wordwidth));
|
||||||
overheadBits=numInts*wordwidth-int.dim;
|
%overheadBits=numInts*wordwidth-int.dim;
|
||||||
x(numInts)=bitshift(x(numInts),-overheadBits); % shift right by overhead
|
%x(numInts)=bitshift(x(numInts),-overheadBits); % shift right by overhead
|
||||||
|
bs=eva2.tools.math.RNG.randomBitSet(0.5, int.dim);
|
||||||
|
x=convertUnsignedJE(int, bs);
|
||||||
else
|
else
|
||||||
% double problem
|
% double problem
|
||||||
x=rand(1, int.dim);
|
x=rand(1, int.dim);
|
||||||
@ -35,9 +37,10 @@ try
|
|||||||
res = feval(int.f, x, int.args);
|
res = feval(int.f, x, int.args);
|
||||||
end
|
end
|
||||||
catch ME
|
catch ME
|
||||||
disp('Function evaluation failed:');
|
disp('JEInterface: Test function evaluation failed:');
|
||||||
disp(ME.message);
|
disp(ME.message);
|
||||||
error(['Test failed! ' ME.message]);
|
rethrow(ME);
|
||||||
|
%error(['Test failed! ' ME.message]);
|
||||||
end
|
end
|
||||||
|
|
||||||
disp('Function returned: ');
|
disp('Function returned: ');
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package eva2.server.go.problems;
|
package eva2.server.go.problems;
|
||||||
|
|
||||||
|
import java.util.BitSet;
|
||||||
|
|
||||||
import eva2.gui.BeanInspector;
|
import eva2.gui.BeanInspector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -240,7 +242,7 @@ public class MatlabEvalMediator {
|
|||||||
optSolSet = solSet;
|
optSolSet = solSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSolutionSet(int[][] solSet) {
|
void setSolutionSet(BitSet[] solSet) {
|
||||||
// System.err.println("setting SolSet " + ((solSet != null) ? solSet.length : 0));
|
// System.err.println("setting SolSet " + ((solSet != null) ? solSet.length : 0));
|
||||||
optSolSet = solSet;
|
optSolSet = solSet;
|
||||||
}
|
}
|
||||||
|
@ -5,15 +5,17 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.BitSet;
|
||||||
|
|
||||||
import eva2.OptimizerFactory;
|
import eva2.OptimizerFactory;
|
||||||
import eva2.OptimizerRunnable;
|
import eva2.OptimizerRunnable;
|
||||||
import eva2.gui.BeanInspector;
|
import eva2.gui.BeanInspector;
|
||||||
|
import eva2.server.go.PopulationInterface;
|
||||||
import eva2.server.go.individuals.AbstractEAIndividual;
|
import eva2.server.go.individuals.AbstractEAIndividual;
|
||||||
import eva2.server.go.individuals.ESIndividualDoubleData;
|
import eva2.server.go.individuals.ESIndividualDoubleData;
|
||||||
import eva2.server.go.individuals.GAIndividualIntegerData;
|
import eva2.server.go.individuals.GAIndividualBinaryData;
|
||||||
|
import eva2.server.go.individuals.InterfaceDataTypeBinary;
|
||||||
import eva2.server.go.individuals.InterfaceDataTypeDouble;
|
import eva2.server.go.individuals.InterfaceDataTypeDouble;
|
||||||
import eva2.server.go.individuals.InterfaceDataTypeInteger;
|
|
||||||
import eva2.server.go.operators.postprocess.InterfacePostProcessParams;
|
import eva2.server.go.operators.postprocess.InterfacePostProcessParams;
|
||||||
import eva2.server.go.operators.postprocess.PostProcess;
|
import eva2.server.go.operators.postprocess.PostProcess;
|
||||||
import eva2.server.go.operators.postprocess.PostProcessParams;
|
import eva2.server.go.operators.postprocess.PostProcessParams;
|
||||||
@ -25,6 +27,7 @@ import eva2.server.go.operators.terminators.PopulationMeasureTerminator.Stagnati
|
|||||||
import eva2.server.go.populations.Population;
|
import eva2.server.go.populations.Population;
|
||||||
import eva2.server.go.strategies.InterfaceOptimizer;
|
import eva2.server.go.strategies.InterfaceOptimizer;
|
||||||
import eva2.server.stat.InterfaceTextListener;
|
import eva2.server.stat.InterfaceTextListener;
|
||||||
|
import eva2.tools.ToolBox;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface problem class for Matlab(TM). Towards EvA2 this behaves like any other double valued
|
* Interface problem class for Matlab(TM). Towards EvA2 this behaves like any other double valued
|
||||||
@ -105,13 +108,18 @@ public class MatlabProblem extends AbstractOptimizationProblem implements Interf
|
|||||||
((InterfaceDataTypeDouble)this.m_Template).SetDoubleRange(range);
|
((InterfaceDataTypeDouble)this.m_Template).SetDoubleRange(range);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (m_Template == null) m_Template = new GAIndividualIntegerData();
|
///// binary alternative
|
||||||
|
if (m_Template == null) m_Template = new GAIndividualBinaryData(getProblemDimension());
|
||||||
|
|
||||||
|
///// Integer alternative
|
||||||
|
/*if (m_Template == null) m_Template = new GAIndividualIntegerData();
|
||||||
int intLen = 1+((getProblemDimension()-1)/32);
|
int intLen = 1+((getProblemDimension()-1)/32);
|
||||||
int lastIntCodingBits = getProblemDimension()-((intLen-1)*32);
|
int lastIntCodingBits = getProblemDimension()-((intLen-1)*32);
|
||||||
if (lastIntCodingBits > 32) System.err.println("ERROR in MatlabProblem:initTemplate");
|
if (lastIntCodingBits > 32) System.err.println("ERROR in MatlabProblem:initTemplate");
|
||||||
((GAIndividualIntegerData)m_Template).setIntegerDataLength(intLen);
|
((GAIndividualIntegerData)m_Template).setIntegerDataLength(intLen);
|
||||||
((GAIndividualIntegerData)m_Template).SetIntRange(Integer.MIN_VALUE, Integer.MAX_VALUE);
|
((GAIndividualIntegerData)m_Template).SetIntRange(Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||||
if (lastIntCodingBits < 32) ((GAIndividualIntegerData)m_Template).SetIntRange(intLen-1, 0, (int)Math.pow(2, lastIntCodingBits)-1);
|
if (lastIntCodingBits < 32) ((GAIndividualIntegerData)m_Template).SetIntRange(intLen-1, 0, (int)Math.pow(2, lastIntCodingBits)-1);
|
||||||
|
*/ /////
|
||||||
// System.err.println("integer length is "+((GAIndividualIntegerData)m_Template).getIntegerData().length);
|
// System.err.println("integer length is "+((GAIndividualIntegerData)m_Template).getIntegerData().length);
|
||||||
// System.err.println("Range is " + BeanInspector.toString(((GAIndividualIntegerData)m_Template).getIntRange()));
|
// System.err.println("Range is " + BeanInspector.toString(((GAIndividualIntegerData)m_Template).getIntRange()));
|
||||||
// m_Template = new GAIndividualBinaryData();
|
// m_Template = new GAIndividualBinaryData();
|
||||||
@ -266,6 +274,17 @@ public class MatlabProblem extends AbstractOptimizationProblem implements Interf
|
|||||||
optimize(optType, outputFilePrefix, null, null);
|
optimize(optType, outputFilePrefix, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start an optimization using the MatlabProblem. The optType references the standard
|
||||||
|
* optimizer as in OptimizerFactory. An output file prefix is optional. In two arrays,
|
||||||
|
* name-value mappings can be given as additional parameters to the optimizer.
|
||||||
|
*
|
||||||
|
* @see OptimizerFactory.getOptRunnable
|
||||||
|
* @param optType
|
||||||
|
* @param outputFilePrefix
|
||||||
|
* @param specParams
|
||||||
|
* @param specValues
|
||||||
|
*/
|
||||||
public void optimize(final int optType, String outputFilePrefix, Object[] specParams, Object[] specValues) {
|
public void optimize(final int optType, String outputFilePrefix, Object[] specParams, Object[] specValues) {
|
||||||
if (allowSingleRunnable && (runnable != null) && (!runnable.isFinished())) {
|
if (allowSingleRunnable && (runnable != null) && (!runnable.isFinished())) {
|
||||||
System.err.println("Please wait for the current optimization to finish");
|
System.err.println("Please wait for the current optimization to finish");
|
||||||
@ -399,21 +418,21 @@ public class MatlabProblem extends AbstractOptimizationProblem implements Interf
|
|||||||
}
|
}
|
||||||
handler.setSolutionSet(solSet);
|
handler.setSolutionSet(solSet);
|
||||||
} else {
|
} else {
|
||||||
int[][] solSet = new int[pop.size()][];
|
BitSet[] solSet = new BitSet[pop.size()];
|
||||||
for (int i=0; i<pop.size(); i++) {
|
for (int i=0; i<pop.size(); i++) {
|
||||||
solSet[i]=((InterfaceDataTypeInteger)pop.getEAIndividual(i)).getIntegerData();
|
solSet[i]=((InterfaceDataTypeBinary)pop.getEAIndividual(i)).getBinaryData();
|
||||||
}
|
}
|
||||||
handler.setSolutionSet(solSet);
|
handler.setSolutionSet(solSet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (isDouble) handler.setSolutionSet((double[][])null);
|
if (isDouble) handler.setSolutionSet((double[][])null);
|
||||||
else handler.setSolutionSet((int[][])null);
|
else handler.setSolutionSet((BitSet[])null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void exportResultToMatlab(OptimizerRunnable runnable) {
|
void exportResultToMatlab(OptimizerRunnable runnable) {
|
||||||
if (isDouble) handler.setSolution(runnable.getDoubleSolution());
|
if (isDouble) handler.setSolution(runnable.getDoubleSolution());
|
||||||
else handler.setSolution(runnable.getIntegerSolution());
|
else handler.setSolution(runnable.getBinarySolution());
|
||||||
}
|
}
|
||||||
|
|
||||||
// void exportResultToMatlab(double[] result) {
|
// void exportResultToMatlab(double[] result) {
|
||||||
@ -504,4 +523,18 @@ public class MatlabProblem extends AbstractOptimizationProblem implements Interf
|
|||||||
log("retrieving initial range..., first entry: " + ((initialRange==null) ? "null" : BeanInspector.toString(initialRange[0])));
|
log("retrieving initial range..., first entry: " + ((initialRange==null) ? "null" : BeanInspector.toString(initialRange[0])));
|
||||||
return initialRange;
|
return initialRange;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getAdditionalDataHeader() {
|
||||||
|
return ToolBox.appendArrays(super.getAdditionalDataHeader(), "matlabSol");
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String[] getAdditionalDataInfo() {
|
||||||
|
return ToolBox.appendArrays(super.getAdditionalDataInfo(), "Additional solution representation");
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object[] getAdditionalDataValue(PopulationInterface pop) {
|
||||||
|
String addStr=((AbstractEAIndividual)pop.getBestIndividual()).getStringRepresentation();
|
||||||
|
return ToolBox.appendArrays(super.getAdditionalDataValue(pop), addStr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user