Merging MK branch revs. 370, 346:363

This commit is contained in:
Marcel Kronfeld 2009-10-19 10:13:56 +00:00
parent 08ab9a7d58
commit d9050fb822
50 changed files with 2166 additions and 1071 deletions

View File

@ -39,7 +39,6 @@ import eva2.server.go.operators.terminators.EvaluationTerminator;
import eva2.server.go.populations.PBILPopulation;
import eva2.server.go.populations.Population;
import eva2.server.go.problems.AbstractOptimizationProblem;
import eva2.server.go.problems.B1Problem;
import eva2.server.go.strategies.ClusterBasedNichingEA;
import eva2.server.go.strategies.ClusteringHillClimbing;
import eva2.server.go.strategies.DifferentialEvolution;
@ -57,6 +56,7 @@ import eva2.server.go.strategies.SimulatedAnnealing;
import eva2.server.go.strategies.Tribes;
import eva2.server.modules.GOParameters;
import eva2.server.stat.InterfaceStatistics;
import eva2.tools.math.RNG;
/**
* <p>
@ -750,6 +750,7 @@ public class OptimizerFactory {
int popSize, AbstractOptimizationProblem problem, long seed,
InterfaceTerminator term) {
Population pop = new Population(popSize);
RNG.setRandomSeed(seed);
problem.initPopulation(pop);
return makeParams(opt, pop, problem, seed, term);
}

View File

@ -25,10 +25,8 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.BeanInfo;
import java.io.Serializable;
import java.net.URL;
import java.util.LinkedList;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
@ -53,18 +51,21 @@ import javax.swing.event.MenuListener;
import eva2.EvAInfo;
import eva2.gui.BeanInspector;
import eva2.gui.EvATabbedFrameMaker;
import eva2.gui.ExtAction;
import eva2.gui.HtmlDemo;
import eva2.gui.JEFrame;
import eva2.gui.JEFrameRegister;
import eva2.gui.JExtMenu;
import eva2.gui.EvATabbedFrameMaker;
import eva2.gui.LogPanel;
import eva2.server.EvAServer;
import eva2.server.go.InterfaceGOParameters;
import eva2.server.modules.AbstractModuleAdapter;
import eva2.server.modules.GenericModuleAdapter;
import eva2.server.modules.ModuleAdapter;
import eva2.server.stat.AbstractStatistics;
import eva2.server.stat.InterfaceStatisticsParameter;
import eva2.server.stat.StatsParameter;
import eva2.tools.EVAERROR;
import eva2.tools.EVAHELP;
import eva2.tools.ReflectPackage;
@ -276,6 +277,19 @@ public class EvAClient implements RemoteStateListener, Serializable {
new Thread(cp).start();
}
/**
* Try to start the optimization with current parameters on the loaded module.
* Return true on success, otherwise false.
*
* @return
*/
public boolean startOptimization() {
if (currentModuleAdapter!=null) {
currentModuleAdapter.startOpt();
return true;
} else return false;
}
/**
* Sets given hostname and tries to load GOParamsters from given file if non null.
*/
@ -704,6 +718,14 @@ public class EvAClient implements RemoteStateListener, Serializable {
return null;
}
public AbstractStatistics getStatistics() {
return ((GenericModuleAdapter)currentModuleAdapter).getStatistics();
}
public InterfaceStatisticsParameter getStatsParams() {
return ((GenericModuleAdapter)currentModuleAdapter).getStatistics().getStatisticsParameter();
}
/**
* Check if there is an optimization currently running.
*

View File

@ -316,11 +316,6 @@ public class FunctionArea extends DArea implements Serializable {
for (int j = 1; j < s.length; j++) { // add column data of place holder if no value in this set
if ((j-1) < pset.getSize()) s[j] = s[j] + " " + pset.getDPoint(j - 1).y;
else s[j] += " #";
// try {
// s[j] = s[j] + " " + pset.getDPoint(j - 1).y;
// } catch (Exception e) {
// s[j] += " ";
// }
}
} else System.err.println("error in FunctionArea::exportToAscii");
}

View File

@ -46,6 +46,7 @@ import com.sun.image.codec.jpeg.JPEGImageEncoder;
import eva2.EvAInfo;
import eva2.tools.chart2d.DPointSet;
import eva2.tools.tool.BasicResourceLoader;
import eva2.server.go.populations.Population;
/*==========================================================================*
* CLASS DECLARATION
*==========================================================================*/
@ -277,6 +278,19 @@ public class Plot implements PlotInterface, Serializable {
m_Frame.setVisible(true);
}
/**
* Draw a population to the Plot instance. Each individual is annotated with the
* given prefix and its fitness.
*
* @param prefix
* @param pop
*/
public void drawPopulation(String prefix, Population pop) {
for (int i=0; i<pop.size(); i++) {
getFunctionArea().drawIcon(1, prefix+" "+pop.getEAIndividual(i).getFitness(0), pop.getEAIndividual(i).getDoublePosition(), 2);
}
}
public void setPreferredSize(Dimension prefSize) {
if (m_Frame != null) {
m_Frame.setPreferredSize(prefSize);

View File

@ -59,13 +59,17 @@ public class TopoPlot extends Plot {
colorScale = color_scale;
}
/**
/**
* Defines the topology (by setting a specific problem) and draws the topology
*/
public void setTopology(Interface2DBorderProblem problem) {
double[][] border = problem.get2DBorder();
double[] sizeXY=Mathematics.shiftRange(border);
setTopology(problem, problem.get2DBorder());
}
/**
* Defines the topology (by setting a specific problem) and draws the topology
*/
public void setTopology(Interface2DBorderProblem problem, double[][] border) {
double[] sizeXY=Mathematics.shiftRange(border);
double deltaX = sizeXY[0]/gridx;
double deltaY = sizeXY[1]/gridy;
double[] pos = new double[2];
@ -86,8 +90,8 @@ public class TopoPlot extends Plot {
m_Frame.setVisible(false);
for (int x=0; x<gridx; x++) {
for (int y=0; y<gridy; y++) {
pos[0] = problem.get2DBorder()[0][0]+x*deltaX;
pos[1] = problem.get2DBorder()[1][0]+y*deltaY;
pos[0] = border[0][0]+x*deltaX;
pos[1] = border[1][0]+y*deltaY;
DRectangle rect = new DRectangle(pos[0],pos[1],deltaX,deltaY);
Color color = new Color(colorBar.getRGB((float)((problem.functionValue(pos)-min)/fitRange)));
// Color color = new Color(255,(int)(problem.doEvaluation(pos)[0]/fitRange*255),(int)(problem.doEvaluation(pos)[0]/fitRange*255));

View File

@ -28,7 +28,8 @@ public enum PSOTopologyEnum {
/**
*
*/
random;
random,
dms;
/**
* A method to translate the "old" integer tags into the enum type.
@ -44,6 +45,7 @@ public enum PSOTopologyEnum {
case 4: return tree;
case 5: return hpso;
case 6: return random;
case 7: return dms;
default: System.err.println("Error: invalid old topology ID in PSOTopologyEnum translateOldID! Returning grid.");
return grid;
}

View File

@ -952,6 +952,28 @@ public abstract class AbstractEAIndividual implements IndividualInterface, java.
return getDefaultStringRepresentation(this);
}
/**
* For any AbstractEAIndividual try retrieve its position as double[].
* Returns null if there is no conversion available. Makes shallow copies if possible.
*
* @param indy
* @return double valued position of an individual or null
*/
public static double[] getDoublePositionShallow(AbstractEAIndividual indy) {
if (indy instanceof InterfaceESIndividual) {
return ((InterfaceESIndividual)indy).getDGenotype();
} else if (indy instanceof InterfaceDataTypeDouble) {
return ((InterfaceDataTypeDouble)indy).getDoubleData();
} else if (indy instanceof InterfaceDataTypeInteger) {
int[] intData = ((InterfaceDataTypeInteger)indy).getIntegerData();
double[] pos = new double[intData.length];
for (int i=0; i<intData.length; i++) pos[i] = (double)intData[i];
return pos;
} // TODO check some more types here?
EVAERROR.errorMsgOnce("Unhandled case in AbstractEAIndividual.getPosition()!");
return null;
}
/**
* For any AbstractEAIndividual try to convert its position to double[] and return it.
* Returns null if there is no conversion available.
@ -961,9 +983,9 @@ public abstract class AbstractEAIndividual implements IndividualInterface, java.
*/
public static double[] getDoublePosition(AbstractEAIndividual indy) {
if (indy instanceof InterfaceESIndividual) {
return ((InterfaceESIndividual)indy).getDGenotype();
return ((InterfaceESIndividual)indy).getDGenotype().clone();
} else if (indy instanceof InterfaceDataTypeDouble) {
return ((InterfaceDataTypeDouble)indy).getDoubleData();
return ((InterfaceDataTypeDouble)indy).getDoubleData().clone();
} else if (indy instanceof InterfaceDataTypeInteger) {
int[] intData = ((InterfaceDataTypeInteger)indy).getIntegerData();
double[] pos = new double[intData.length];
@ -985,6 +1007,7 @@ public abstract class AbstractEAIndividual implements IndividualInterface, java.
return AbstractEAIndividual.getDoublePosition(this);
}
/**
* @return true if parent history logging is activated
*

View File

@ -28,6 +28,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte
this.m_CrossoverProbability = 0.5;
this.m_CrossoverOperator = new CrossoverESDefault();
this.m_Genotype = new double[1];
this.m_Phenotype = null;
this.m_Range = new double[1][2];
this.m_Range[0][0] = -10;
this.m_Range[0][1] = 10;
@ -170,7 +171,8 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte
* @return double[] representing the double data.
*/
public double[] getDoubleDataWithoutUpdate() {
return this.m_Phenotype;
if (m_Phenotype==null) return getDoubleData();
else return this.m_Phenotype;
}
/** This method allows you to set the phenotype double data. To change the genotype,

View File

@ -10,6 +10,7 @@ import eva2.server.go.operators.paramcontrol.InterfaceParameterControl;
import eva2.server.go.operators.paramcontrol.NoParamAdaption;
import eva2.server.go.operators.paramcontrol.ParamAdaption;
import eva2.server.go.operators.paramcontrol.ParameterControlManager;
import eva2.server.go.problems.AbstractProblemDouble;
import eva2.tools.EVAERROR;
/**
@ -25,6 +26,7 @@ public abstract class AbstractConstraint implements InterfaceDoubleConstraint, S
protected ConstraintHandlingEnum handling = ConstraintHandlingEnum.specificTag;
protected static boolean TRACE = false;
protected double equalityEpsilon = 0.0001; // threshold below which equality constraints are seen as satisfied
private AbstractEAIndividual currentIndy = null;
private double penaltyFactor = 1.;
// protected ParamAdaption penaltyFactAdaption = new NoParamAdaption();
@ -62,7 +64,6 @@ public abstract class AbstractConstraint implements InterfaceDoubleConstraint, S
/**
* Return the absolute (positive) degree of violation or zero if the constraint is fulfilled.
* The penalty factor is included here.
*/
public double getViolation(double[] indyX) {
double viol = getRawViolationValue(indyX);
@ -71,13 +72,14 @@ public abstract class AbstractConstraint implements InterfaceDoubleConstraint, S
/**
* Check whether the given individual violates the constraint and immediately add
* the violation if it is the case. Expect that the fitness has already been set.
* the penalty and violation if it is the case. Expect that the fitness has already been set.
* This regards the handling strategy and adds the violation to the fitness (in each dimension) or
* sets the individual constraint violation.
*
* @param indy the individual to check for constraint violation.
*/
public void addViolation(AbstractEAIndividual indy, double[] indyX) {
currentIndy=indy;
double v = getViolation(indyX);
switch (handling) {
case penaltyAdditive:
@ -99,6 +101,54 @@ public abstract class AbstractConstraint implements InterfaceDoubleConstraint, S
if (v>0) indy.addConstraintViolation(v);
break;
}
currentIndy=null;
}
/**
* Some constraints require further information on the individual or work on the
* raw fitness which can be requested using this method.
*
* @return
*/
protected double[] getIndyRawFit(String key) {
return getIndyDblData(AbstractProblemDouble.rawFitKey);
}
/**
* Some constraints require further information on the individual, such as
* additional individual data. This method uses getData of AbstractEAIndividual
* to try to retrieve a double array.
*
* @return
*/
protected double[] getIndyDblData(String key) {
if (currentIndy!=null) {
Object dat = currentIndy.getData(key);
if (dat!=null && (dat instanceof double[])) return (double[])dat;
else {
System.err.println("Error, invalid call to AbstractConstraint.getRawFitness(). Individual had no raw fitness set.");
return null;
}
} else {
System.err.println("Error, invalid call to AbstractConstraint.getRawFitness(). Individual was unknown.");
return null;
}
}
/**
* Some constraints require further information on the individual, such as
* additional individual data. This method uses getData of AbstractEAIndividual
* to try to retrieve a stored object.
*
* @return
*/
protected Object getIndyData(String key) {
if (currentIndy!=null) {
return currentIndy.getData(key);
} else {
System.err.println("Error, invalid call to AbstractConstraint.getRawFitness(). Individual was unknown.");
return null;
}
}
private double getViolationConsideringRelation(double val) {

View File

@ -26,8 +26,8 @@ public class EuclideanMetric implements InterfaceDistanceMetric {
double[] dIndy1, dIndy2;
double result = 0;
dIndy1 = AbstractEAIndividual.getDoublePosition(indy1);
dIndy2 = AbstractEAIndividual.getDoublePosition(indy2);
dIndy1 = AbstractEAIndividual.getDoublePositionShallow(indy1);
dIndy2 = AbstractEAIndividual.getDoublePositionShallow(indy2);
for (int i = 0; (i < dIndy1.length) && (i < dIndy2.length); i++) {
result += Math.pow((dIndy1[i] - dIndy2[i]), 2);

View File

@ -111,7 +111,7 @@ public class PhenotypeMetric implements InterfaceDistanceMetric, java.io.Seriali
double[] d1, d2;
double[][] r1, r2;
double tmpResult = 0, tmp=0;
d1 = ((InterfaceDataTypeDouble) indy1).getDoubleData();
d1 = ((InterfaceDataTypeDouble) indy1).getDoubleData(); // TODO WithoutUpdate would be much quicker - but in which cases is it up to date?
r1 = ((InterfaceDataTypeDouble) indy1).getDoubleRange();
d2 = ((InterfaceDataTypeDouble) indy2).getDoubleData();
r2 = ((InterfaceDataTypeDouble) indy2).getDoubleRange();

View File

@ -98,9 +98,27 @@ class CMAParamSet implements InterfacePopulationChangedEventListener, Serializab
* @return
*/
public static CMAParamSet initCMAParams(CMAParamSet params, int mu, int lambda, Population pop, double initialSigma) {
initCMAParams(params, mu, lambda, pop.getCenter(), ((InterfaceDataTypeDouble)pop.getEAIndividual(0)).getDoubleRange(), initialSigma);
pop.addPopulationChangedEventListener(params);
return params;
}
/**
* Initializes the CMA parameter set for given mu, lambda and a population.
* The initialSigma parameter is used as initial sigma directly unless it is <0, in
* that case the average range is used as initial sigma.
*
* @param params the CMA parameter set to be used - its data are overwritten
* @param mu ES mu parameter
* @param lambda ES lambda parameter
* @param pop associated Population
* @param initialSigma initial sigma or -1 to indicate the usage of average range
* @return
*/
public static CMAParamSet initCMAParams(CMAParamSet params, int mu, int lambda, double[] center, double[][] range, double initialSigma) {
// those are from init:
params.firstAdaptionDone = false;
params.range = ((InterfaceDataTypeDouble)pop.getEAIndividual(0)).getDoubleRange();
params.range = range;
int dim = params.range.length;
// if (TRACE_1) System.out.println("WCMA init " + dim);
@ -129,8 +147,7 @@ class CMAParamSet implements InterfacePopulationChangedEventListener, Serializab
params.sigma = initialSigma;
// System.out.println("INitial sigma: "+sigma);
params.firstSigma = params.sigma;
params.meanX = pop.getCenter(); // this might be ok?
pop.addPopulationChangedEventListener(params);
params.meanX = center; // this might be ok?
return params;
}
@ -286,6 +303,8 @@ public class MutateESRankMuCMA implements InterfaceMutationGenerational, Seriali
int mu,lambda;
mu = selectedP.size();
lambda = oldGen.size();
int generation = oldGen.getGeneration();
if (mu>= lambda) {
EVAERROR.errorMsgOnce("Warning: invalid mu/lambda ratio! Setting mu to lambda/2.");
mu = lambda/2;
@ -300,9 +319,6 @@ public class MutateESRankMuCMA implements InterfaceMutationGenerational, Seriali
params = CMAParamSet.initCMAParams(mu, lambda, oldGen, getInitSigma(oldGen));
} else params = (CMAParamSet)oldGen.getData(cmaParamsKey);
}
int generation = oldGen.getGeneration();
if (TRACE_1) {
System.out.println("WCMA adaptGenerational **********");
// System.out.println("newPop measures: " + BeanInspector.toString(newPop.getPopulationMeasures()));
@ -332,11 +348,16 @@ public class MutateESRankMuCMA implements InterfaceMutationGenerational, Seriali
for (int j = 0; j < dim; ++j) {
sum += params.mB.get(j,i) * BDz[j]; // times B transposed, (Eq 4) in HK04
}
zVect[i] = sum / Math.sqrt(params.eigenvalues[i]);
if (Double.isInfinite(zVect[i])|| Double.isNaN(zVect[i])) {
System.err.println("Error, infinite zVect entry!");
zVect[i]=0; // TODO MK
}
if (params.eigenvalues[i]<0) {
EVAERROR.errorMsgOnce("Warning: negative eigenvalue in MutateESRankMuCMA! (possibly multiple cases)");
zVect[i]=0;
} else {
zVect[i] = sum / Math.sqrt(params.eigenvalues[i]);
if (!checkValidDouble(zVect[i])) {
System.err.println("Error, infinite zVect entry!");
zVect[i]=0; // TODO MK
}
}
}
/* cumulation for sigma (ps) using B*z */
@ -345,7 +366,7 @@ public class MutateESRankMuCMA implements InterfaceMutationGenerational, Seriali
for (int j = 0; j < dim; ++j) sum += params.mB.get(i,j) * zVect[j];
newPathS[i] = (1. - params.c_sig) * params.pathS[i]
+ Math.sqrt(params.c_sig * (2. - params.c_sig)) * sum;
if (Double.isInfinite(newPathS[i]) || Double.isNaN(newPathS[i])) {
if (!checkValidDouble(newPathS[i])) {
System.err.println("Error, infinite pathS!");
}
}
@ -386,8 +407,11 @@ public class MutateESRankMuCMA implements InterfaceMutationGenerational, Seriali
if (Double.isInfinite(sigFact)) params.sigma *= 10.; // in larger search spaces sigma tends to explode after init.
else params.sigma *= sigFact;
testAndCorrectNumerics(params, generation, selectedSorted);
if (!testAndCorrectNumerics(params, generation, selectedSorted)) {
// parameter seemingly exploded...
params = CMAParamSet.initCMAParams(params, mu, lambda, params.meanX, ((InterfaceDataTypeDouble)oldGen.getEAIndividual(0)).getDoubleRange(), params.firstSigma);
}
if (TRACE_1) {
System.out.print("psLen=" + (psNorm) + " ");
outputParams(params, mu);
@ -422,10 +446,13 @@ public class MutateESRankMuCMA implements InterfaceMutationGenerational, Seriali
/**
* Requires selected population to be sorted by fitness.
*
* @param iterations
* @param selected
* @param params refering parameter set
* @param iterations number of iterations performed
* @param selected selected population
* @return true if the parameters seem ok or were corrected, false if new parameters must be produced
*/
void testAndCorrectNumerics(CMAParamSet params, int iterations, Population selected) { // not much left here
private boolean testAndCorrectNumerics(CMAParamSet params, int iterations, Population selected) { // not much left here
boolean corrected = true;
/* Flat Fitness, Test if function values are identical */
if (iterations > 1) {
// selected pop is sorted
@ -438,7 +465,8 @@ public class MutateESRankMuCMA implements InterfaceMutationGenerational, Seriali
if (!checkValidDouble(params.sigma)) {
System.err.println("Error, unstable sigma!");
params.sigma=params.firstSigma; // MK TODO
corrected = false;
// params.sigma=params.firstSigma; // MK TODO
// System.err.println(
}
@ -465,6 +493,7 @@ public class MutateESRankMuCMA implements InterfaceMutationGenerational, Seriali
}
}
}
return corrected;
} // Test...
private boolean nearlySame(double[] bestFitness, double[] worstFitness) {

View File

@ -202,10 +202,10 @@ public class PostProcess {
Population exclude = new Population();
exclude.add(clusters[j].getBestEAIndividual());
result.add(exclude.getEAIndividual(0));
result.addAll(clusters[j].getRandNIndividualsExcept(n-1, exclude));
result.addAll(clusters[j].moveRandNIndividualsExcept(n-1, exclude));
break;
case RAND_ONLY:
result.addAll(clusters[j].getRandNIndividuals(n));
result.addAll(clusters[j].moveRandNIndividuals(n));
break;
default: System.err.println("Unknown mode in PostProcess:clusterBest!"); break;
}
@ -433,7 +433,7 @@ public class PostProcess {
}
/**
* Search for a local minimum using nelder mead and return the solution found and the number of steps
* Search for a local minimum using CMA and return the solution found and the number of steps
* (evaluations) actually performed. This uses the whole population as starting population for nelder mead
* meaning that typically only one best is returned.
* Returns the number of function calls really performed by the method; sets the number of function calls
@ -448,10 +448,8 @@ public class PostProcess {
* @return
*/
public static int processWithCMA(Population pop, AbstractOptimizationProblem problem, InterfaceTerminator term, int baseEvals) {
// GOParameters cmaParams = OptimizerFactory.cmaESIPOP(problem);
MutateESRankMuCMA mutator = new MutateESRankMuCMA();
mutator.setInitializeSigma(ESMutationInitialSigma.avgInitialDistance);
// mutator.
EvolutionStrategies es = OptimizerFactory.createEvolutionStrategy(pop.size()/2, pop.size(), false, mutator, 1., new CrossoverESDefault(), 0.,
new SelectBestIndividuals(), problem, null);
for (int i=0; i<pop.size(); i++) {
@ -1006,16 +1004,26 @@ public class PostProcess {
public static double findNMSPerturb(Population candidates, int i, double maxPerturb) {
double minDistNeighbour = Double.MAX_VALUE;
AbstractEAIndividual indy = candidates.getEAIndividual(i);
boolean found=false;
for (int k=0; k<candidates.size(); k++) {
if (k!=i) {
double dist = PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePosition(indy), AbstractEAIndividual.getDoublePosition(candidates.getEAIndividual(k)));
double dist = PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePositionShallow(indy), AbstractEAIndividual.getDoublePositionShallow(candidates.getEAIndividual(k)));
if (dist == 0.) {
System.err.println("error, equal candidates in findNMSPerturb!");
// System.err.println("error, equal candidates in findNMSPerturb!");
} else if (dist < minDistNeighbour) {
minDistNeighbour = dist;
found=true;
}
}
}
if (!found) {
// System.err.println("warning, equal candidates in PostProcess.findNMSPerturb - converged population?!");
if (maxPerturb>0) return maxPerturb;
else {
System.err.println("error, unable to select perturbance value in PostProcess.findNMSPerturb since all candidates are equal. Converged population?!");
return 0.01;
}
}
if (maxPerturb>0) return Math.min(maxPerturb, minDistNeighbour/3.);
else return minDistNeighbour/3.;
}
@ -1083,9 +1091,9 @@ public class PostProcess {
int cnt = pop.size()-1;
if (cnt == 0) return 0.;
else {
double[] indyPos = AbstractEAIndividual.getDoublePosition(pop.getEAIndividual(index));
double[] indyPos = AbstractEAIndividual.getDoublePositionShallow(pop.getEAIndividual(index));
for (int i=0; i<pop.size(); i++) {
if (i!=index) distSum += PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePosition(pop.getEAIndividual(i)), indyPos);
if (i!=index) distSum += PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePositionShallow(pop.getEAIndividual(i)), indyPos);
}
return distSum/((double)cnt);
}

View File

@ -5,9 +5,9 @@ import eva2.server.go.populations.Population;
import eva2.tools.math.RNG;
/** Random selection typically used for ES a mating selection.
* In case of multiple fitness values the selection
* critria is selected randomly for each selection event. pff
/**
* Random selection typically used for ES as mating selection.
*
* Created by IntelliJ IDEA.
* User: streiche
* Date: 18.03.2003
@ -17,14 +17,21 @@ import eva2.tools.math.RNG;
public class SelectRandom implements InterfaceSelection, java.io.Serializable {
private boolean m_ObeyDebsConstViolationPrinciple = false;
private boolean withReplacement = true;
public SelectRandom() {
}
public SelectRandom(SelectRandom a) {
this.m_ObeyDebsConstViolationPrinciple = a.m_ObeyDebsConstViolationPrinciple;
this.withReplacement = a.withReplacement;
}
public SelectRandom(boolean withRepl) {
withReplacement=withRepl;
if (m_ObeyDebsConstViolationPrinciple) System.err.println("Error, replacement selection not supported for constrained selection (SelectRandom)");
}
public Object clone() {
return (Object) new SelectRandom(this);
}
@ -39,9 +46,9 @@ public class SelectRandom implements InterfaceSelection, java.io.Serializable {
// nothing to prepare here
}
/** This method will select one Individual from the given
* Population in respect to the selection propability of the
* individual.
/** This method will select individuals randomly from the given
* Population. Individuals may be drawn several times or not at all.
*
* @param population The source population where to select from
* @param size The number of Individuals to select
* @return The selected population.
@ -64,17 +71,22 @@ public class SelectRandom implements InterfaceSelection, java.io.Serializable {
}
}
}
} else {
for (int i = 0; i < size; i++) {
result.add(population.get(RNG.randomInt(0, population.size()-1)));
}
if (withReplacement) {
for (int i = 0; i < size; i++) {
result.add(population.get(RNG.randomInt(0, population.size()-1)));
}
} else {
if (size > population.size()) throw new RuntimeException("Error, invalid selection: trying to select more individuals (without replacement) than available in SelectRandom.");
int[] perm = RNG.randomPerm(size);
for (int i=0; i<size; i++) result.add(population.getEAIndividual(perm[i]));
}
}
return result;
}
/** This method allows you to select partners for a given Individual
* @param dad The already seleceted parent
* @param dad The already selected parent
* @param avaiablePartners The mating pool.
* @param size The number of partners needed.
* @return The selected partners.

View File

@ -21,9 +21,10 @@ public class ReplaceDeterministicCrowding implements InterfaceReplacement, java.
return new ReplaceRandom();
}
/** This method will insert the given individual into the population
* by replacing a individual either from the population or the given
* subset
/**
* Take the closest individual within the subset and remove it from pop. Add
* indy as a replacement.
*
* @param indy The individual to insert
* @param pop The population
* @param sub The subset

View File

@ -11,7 +11,7 @@ import eva2.server.go.populations.Population;
* Time: 15:24:03
* To change this template use File | Settings | File Templates.
*/
public class PreplacePreselection implements InterfaceReplacement, java.io.Serializable {
public class ReplacePreselection implements InterfaceReplacement, java.io.Serializable {
/** The ever present clone method
*/

View File

@ -28,15 +28,20 @@ public class ReplacementCrowding implements InterfaceReplacement, java.io.Serial
this.m_C = b.m_C;
}
public ReplacementCrowding(int C) {
setC(C);
}
/** The ever present clone method
*/
public Object clone() {
return new ReplaceRandom();
}
/** This method will insert the given individual into the population
* by replacing a individual either from the population or the given
* subset
/**
* From a random subset of size C, the closest is replaced by the given individual.
* The sub parameter is not regarded.
*
* @param indy The individual to insert
* @param pop The population
* @param sub The subset

View File

@ -89,7 +89,7 @@ Serializable {
}
public boolean isTerminated(PopulationInterface Pop) {
if (!firstTime && isStillConverged(Pop.getBestIndividual())) {
if (!firstTime && isStillConverged(Pop)) {
if (stagnationTimeHasPassed(Pop)) {
// population hasnt improved much for max time, criterion is met
msg = getTerminationMessage(tagString);
@ -137,8 +137,8 @@ Serializable {
* @param curFit
* @return
*/
protected boolean isStillConverged(IndividualInterface indy) {
double[] curFit = indy.getFitness();
protected boolean isStillConverged(PopulationInterface pop) {
double[] curFit = pop.getBestFitness();
double dist = PhenotypeMetric.euclidianDistance(oldFit, curFit);
boolean ret;
if (convergenceCondition.isSelectedString("Relative")) {

View File

@ -33,15 +33,15 @@ public class PhenotypeConvergenceTerminator extends FitnessConvergenceTerminator
* @param curFit
* @return
*/
protected boolean isStillConverged(IndividualInterface indy) {
double dist = pMetric.distance(oldIndy, (AbstractEAIndividual)indy);
protected boolean isStillConverged(PopulationInterface pop) {
double dist = pMetric.distance(oldIndy, (AbstractEAIndividual)pop.getBestIndividual());
boolean ret;
if (getConvergenceCondition().isSelectedString("Relative")) {
ret = (dist < (oldPhenNorm * convThresh));
} else {
ret = (dist < convThresh);
}
if (TRACE) System.out.println("isStillConverged returns " + ret + ", dist " + dist + ", old indy " + BeanInspector.toString(oldIndy) + ", cur indy" + BeanInspector.toString(indy));
if (TRACE) System.out.println("isStillConverged returns " + ret + ", dist " + dist + ", old indy " + BeanInspector.toString(oldIndy) + ", cur indy" + BeanInspector.toString(pop.getBestIndividual()));
return ret;
}

View File

@ -14,6 +14,7 @@ import eva2.server.go.PopulationInterface;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.AbstractEAIndividualComparator;
import eva2.server.go.individuals.GAIndividualBinaryData;
import eva2.server.go.individuals.InterfaceDataTypeDouble;
import eva2.server.go.operators.distancemetric.InterfaceDistanceMetric;
import eva2.server.go.operators.distancemetric.PhenotypeMetric;
import eva2.server.go.operators.selection.probability.AbstractSelProb;
@ -21,6 +22,8 @@ import eva2.tools.EVAERROR;
import eva2.tools.Mathematics;
import eva2.tools.Pair;
import eva2.tools.math.RNG;
import eva2.tools.math.Jama.Matrix;
import eva2.tools.tool.StatisticUtils;
/** This is a basic implementation for a EA Population.
@ -38,8 +41,8 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
protected int m_FunctionCalls = 0;
protected int m_Size = 50;
protected Population m_Archive = null;
transient private ArrayList<InterfacePopulationChangedEventListener> listeners = null;
PopulationInitMethod initMethod = PopulationInitMethod.individualDefault;
transient private ArrayList<InterfacePopulationChangedEventListener> listeners = null;
// transient protected InterfacePopulationChangedEventListener m_Listener = null;
// the evaluation interval at which listeners are notified
@ -148,6 +151,15 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
return res;
}
/**
* Clone the population with shallow copies of the individual. This should be used with care!
* @return
*/
public Population cloneShallowInds() {
Population pop = cloneWithoutInds();
pop.addAll(this);
return pop;
}
/** This method inits the state of the population AFTER the individuals
* have been inited by a problem
@ -162,6 +174,13 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
this.m_Archive.clear();
this.m_Archive.init();
}
switch (initMethod) {
case individualDefault:
break;
case randomLatinHypercube:
createRLHSampling(this, false);
break;
}
firePropertyChangedEvent(Population.populationInitialized);
}
@ -183,6 +202,51 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
}
}
/**
* Create a population instance which distributes the individuals according to
* a random latin hypercube sampling.
*
* @param popSize
* @param template
* @return
*/
public static Population createRLHSampling(int popSize, AbstractEAIndividual template) {
Population pop = new Population(popSize);
pop.add(template);
createRLHSampling(pop, true);
return pop;
}
/**
* Create a population instance which distributes the individuals according to
* a random latin hypercube sampling.
*
* @param popSize
* @param template
* @return
*/
public static void createRLHSampling(Population pop, boolean fillPop) {
if (pop.size()<=0) {
System.err.println("createRLHSampling needs at least one template individual in the population");
return;
}
AbstractEAIndividual template = pop.getEAIndividual(0);
if (fillPop && (pop.size()<pop.getPopulationSize())) {
for (int i=pop.size(); i<pop.getPopulationSize(); i++) pop.add((AbstractEAIndividual)template.clone());
}
if (template instanceof InterfaceDataTypeDouble) {
double[][] range = ((InterfaceDataTypeDouble)template).getDoubleRange();
// Population pop = new Population(popSize);
Matrix rlhM = StatisticUtils.rlh(pop.size(), range, true);
for (int i=0; i<pop.size(); i++) {
AbstractEAIndividual tmpIndy = pop.getEAIndividual(i);
((InterfaceDataTypeDouble)tmpIndy).SetDoubleGenotype(rlhM.getRowShallow(i));
}
} else {
System.err.println("Error: data type double required for Population.createUniformSampling");
}
}
/**
* Activate or deactivate the history tracking, which stores the best individual in every
* generation in the incrGeneration() method.
@ -664,7 +728,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
int skip = 0;
if (!bBestOrWorst) skip = super.size()-n;
ArrayList<AbstractEAIndividual> sorted = getSorted();
ArrayList<AbstractEAIndividual> sorted = getSorted(lastFitCrit);
res.clear();
for (int i = skip; i < skip+n; i++) {
res.add(sorted.get(i));
@ -672,6 +736,37 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
res.setPopulationSize(res.size());
}
/**
* From the given list, remove all but the first n elements.
* @param n
* @param l
*/
public static List<AbstractEAIndividual> toHead(int n, List<AbstractEAIndividual> l) {
l.subList(n, l.size()).clear();
return l;
}
/**
* From the given list, remove all but the last n elements.
* @param n
* @param l
*/
public static List<AbstractEAIndividual> toTail(int n, List<AbstractEAIndividual> l) {
l.subList(0, l.size()-n).clear();
return l;
}
/**
* Return a new population containing only the last n elements of the instance.
* @param n
* @param l
*/
public Population toTail(int n) {
Population retPop = new Population(n);
retPop.addAll(subList(0, size()-n));
return retPop;
}
/**
* Set a fitness criterion for sorting procedures. This also affects getBest
* @param fitIndex
@ -680,16 +775,35 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
getSorted(fitIndex);
}
// /**
// * Reuses the last fitness criterion. Avoid having to sort again in several calls without modifications in between.
// * The returned array should not be modified!
// *
// * @return
// */
// protected ArrayList<AbstractEAIndividual> getSorted() {
// return getSorted(lastFitCrit);
// }
/**
* Reuses the last fitness criterion. Avoid having to sort again in several calls without modifications in between.
* Sort the population returning a new ArrayList.
* The returned array should not be modified!
*
* @param comp A comparator by which sorting is performed
* @return
*/
protected ArrayList<AbstractEAIndividual> getSorted() {
return getSorted(lastFitCrit);
public ArrayList<AbstractEAIndividual> getSorted(AbstractEAIndividualComparator comp) {
PriorityQueue<AbstractEAIndividual> sQueue = new PriorityQueue<AbstractEAIndividual>(super.size(), comp);
for (int i = 0; i < super.size(); i++) {
AbstractEAIndividual indy = getEAIndividual(i);
if (indy != null) sQueue.add(indy);
}
ArrayList<AbstractEAIndividual> sArr = new ArrayList<AbstractEAIndividual>(this.size());
AbstractEAIndividual indy;
while ((indy=sQueue.poll())!=null) sArr.add(indy);
return sArr;
}
/**
* Avoids having to sort again in several calls without modifications in between.
* The returned array should not be modified!
@ -699,38 +813,57 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
*/
protected ArrayList<AbstractEAIndividual> getSorted(int fitIndex) {
if ((fitIndex != lastFitCrit) || (sortedArr == null) || (super.modCount != lastQModCount)) {
lastFitCrit=fitIndex; // TODO check if this works right?
PriorityQueue<AbstractEAIndividual> sQueue = new PriorityQueue<AbstractEAIndividual>(super.size(), new AbstractEAIndividualComparator(fitIndex));
for (int i = 0; i < super.size(); i++) {
AbstractEAIndividual indy = getEAIndividual(i);
if (indy != null) sQueue.add(indy);
}
lastFitCrit=fitIndex;
ArrayList<AbstractEAIndividual> sArr = getSorted(new AbstractEAIndividualComparator(fitIndex));
if (sortedArr==null) sortedArr = sArr;
else {
sortedArr.clear();
sortedArr.addAll(sArr);
}
lastQModCount = super.modCount;
if (sortedArr==null) sortedArr = new ArrayList<AbstractEAIndividual>(this.size());
else sortedArr.clear();
AbstractEAIndividual indy;
while ((indy=sQueue.poll())!=null) sortedArr.add(indy);
}
return sortedArr;
}
/** This method returns n random best individuals from the population.
/**
* This method retrieves n random individuals from the population and
* returns them within a new population.
*
* @param n number of individuals to look out for
* @return The n best individuals
*
*/
public List<AbstractEAIndividual> getRandNIndividuals(int n) {
return getRandNIndividualsExcept(n, new Population());
public Population getRandNIndividuals(int n) {
if (n>=size()) return (Population)clone();
else {
Population pop = cloneShallowInds();
Population retPop = cloneWithoutInds();
moveNInds(n, pop, retPop);
return retPop;
}
}
/**
* This method removes n random individuals from the population and
* returns them within a new population.
*
* @param n number of individuals to look out for
* @return The n best individuals
*
*/
public Population moveRandNIndividuals(int n) {
return moveRandNIndividualsExcept(n, new Population());
}
/** This method returns the n current best individuals from the population in an object array.
/**
* This method removes n random individuals from the population (excluding the given ones)
* and returns them in a new population instance.
*
* @param n number of individuals to look out for
* @return The n best individuals
* @return The n random individuals
*
*/
public Population getRandNIndividualsExcept(int n, Population exclude) {
public Population moveRandNIndividualsExcept(int n, Population exclude) {
return moveNInds(n, filter(exclude), new Population());
}
@ -757,7 +890,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
*/
public static void moveRandIndFromTo(Population src, Population dst) {
int k = RNG.randomInt(src.size());
dst.add(src.remove(k));
dst.add(src.removeIndexSwitched(k));
}
/**
@ -901,6 +1034,10 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
return strB.toString();
}
public String getName() {
return "Population-"+getPopulationSize();
}
/**
* Return a list of individual IDs from the population.
* @return
@ -999,6 +1136,27 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
return prev;
}
/**
* ArrayList does not increase the modCount in set. Why???
* This method keeps the internally sorted array in synch to minimize complete resorting events.
*/
public Object set(int index, Object element, int fitIndex) {
Object prev = super.set(index, element);
modCount++;
if (lastFitCrit==fitIndex && (lastQModCount==(modCount-1))) {
// if nothing happend between this event and the last sorting by the same criterion...
sortedArr.remove(prev);
int i=0;
AbstractEAIndividualComparator comp = new AbstractEAIndividualComparator(fitIndex);
while (i<sortedArr.size() && comp.compare(element, sortedArr.get(i))>0) {
i++;
}
sortedArr.add(i, (AbstractEAIndividual)element);
lastQModCount=modCount;
}
return prev;
}
public boolean addIndividual(IndividualInterface ind) {
super.add(ind);
return true;
@ -1010,10 +1168,12 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
*
* @param index individual index to be removed
*/
public void removeIndexSwitched(int index) {
public AbstractEAIndividual removeIndexSwitched(int index) {
AbstractEAIndividual indy = getEAIndividual(index);
int lastIndex = size()-1;
if (index < lastIndex) set(index, get(lastIndex));
remove(lastIndex);
return indy;
}
/**
@ -1095,8 +1255,8 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
for (int i = 0; i < this.size(); i++) {
for (int j = i+1; j < this.size(); j++) {
if (metric == null) d = PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePosition(getEAIndividual(i)),
AbstractEAIndividual.getDoublePosition(getEAIndividual(j)));
if (metric == null) d = PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePositionShallow(getEAIndividual(i)),
AbstractEAIndividual.getDoublePositionShallow(getEAIndividual(j)));
else d = metric.distance((AbstractEAIndividual)this.get(i), (AbstractEAIndividual)this.get(j));
meanDist += d;
if (d < minDist) minDist = d;
@ -1110,6 +1270,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
res[1]=0;
res[2]=0;
}
// System.out.println("0-1-dist: " + BeanInspector.toString(metric.distance((AbstractEAIndividual)this.get(0), (AbstractEAIndividual)this.get(1))));
return res;
}
@ -1152,6 +1313,32 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
return res;
}
/**
* Search for the closest (farthest) individual to the given position.
* Return a Pair of the individuals index and distance.
* If the population is empty, a Pair of (-1,-1) is returned.
*
* @param pos
* @param pop
* @param closestOrFarthest if true, the closest individual is retrieved, otherwise the farthest
* @return
*/
public static Pair<Integer,Double> getClosestFarthestIndy(double[] pos, Population pop, boolean closestOrFarthest) {
double dist = -1.;
int sel=-1;
for (int i = 0; i < pop.size(); i++) {
AbstractEAIndividual indy = pop.getEAIndividual(i);
double[] indyPos = AbstractEAIndividual.getDoublePositionShallow(indy);
double curDist = PhenotypeMetric.euclidianDistance(pos, indyPos);
if ((dist<0) || (!closestOrFarthest && (dist < curDist))
|| (closestOrFarthest && (dist > curDist))) {
dist = curDist;
sel = i;
}
}
return new Pair<Integer,Double>(sel,dist);
}
/**
* Calculate the average position of the population.
*
@ -1161,7 +1348,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
if (size()==0) EVAERROR.errorMsgOnce("Invalid pop size in DistractingPopulation:getCenter!");
double[] centerPos = AbstractEAIndividual.getDoublePosition(getEAIndividual(0));
for (int i=1; i<size(); i++) {
Mathematics.vvAdd(centerPos, AbstractEAIndividual.getDoublePosition(getEAIndividual(i)), centerPos);
Mathematics.vvAdd(centerPos, AbstractEAIndividual.getDoublePositionShallow(getEAIndividual(i)), centerPos);
}
Mathematics.svDiv(size(), centerPos, centerPos);
return centerPos;
@ -1177,7 +1364,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
double[] centerPos = AbstractEAIndividual.getDoublePosition(getEAIndividual(0));
Mathematics.svMult(weights[0], centerPos, centerPos);
for (int i=1; i<weights.length; i++) {
Mathematics.svvAddScaled(weights[i], AbstractEAIndividual.getDoublePosition(getEAIndividual(i)), centerPos, centerPos);
Mathematics.svvAddScaled(weights[i], AbstractEAIndividual.getDoublePositionShallow(getEAIndividual(i)), centerPos, centerPos);
}
return centerPos;
}
@ -1195,14 +1382,14 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
*/
public double[] getCenterWeighted(AbstractSelProb selProb, int criterion, boolean obeyConst) {
selProb.computeSelectionProbability(this, "Fitness", obeyConst);
double[] mean = AbstractEAIndividual.getDoublePosition(getEAIndividual(0)).clone();
double[] mean = AbstractEAIndividual.getDoublePosition(getEAIndividual(0));
if (mean != null) {
Arrays.fill(mean, 0.);
AbstractEAIndividual indy = null;
for (int i=0; i<size(); i++) {
indy = getEAIndividual(i);
double[] pos = AbstractEAIndividual.getDoublePosition(indy);
double[] pos = AbstractEAIndividual.getDoublePositionShallow(indy);
Mathematics.svvAddScaled(indy.getSelectionProbability(criterion), pos, mean, mean);
}
}
@ -1224,8 +1411,8 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
for (int i = 0; i < size(); ++i){
AbstractEAIndividual currentindy = getEAIndividual(i);
if (!indy.equals(currentindy)){ // dont compare particle to itself or a copy of itself
double dist = PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePosition(indy),
AbstractEAIndividual.getDoublePosition(currentindy));
double dist = PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePositionShallow(indy),
AbstractEAIndividual.getDoublePositionShallow(currentindy));
if (dist < mindist){
mindist = dist;
index = i;
@ -1261,8 +1448,8 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
if (normalizedPhenoMetric){
d = metric.distance(indy, neighbor);
} else {
d = PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePosition(indy),
AbstractEAIndividual.getDoublePosition(neighbor));
d = PhenotypeMetric.euclidianDistance(AbstractEAIndividual.getDoublePositionShallow(indy),
AbstractEAIndividual.getDoublePositionShallow(neighbor));
}
if (calcVariance) distances.add(d);
sum += d;
@ -1340,6 +1527,36 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
setPopulationSize(size());
}
/**
* Update the range of all individuals to the given one. If forceRange is true
* and the individuals are out of range, they are projected into the range
* by force.
*
* @param modRange
* @param b
*/
public void updateRange(double[][] range, boolean forceRange) {
for (int i=0; i<size(); i++) {
((InterfaceDataTypeDouble)getEAIndividual(i)).SetDoubleRange(range);
double[] pos = ((InterfaceDataTypeDouble)getEAIndividual(i)).getDoubleData();
if (!Mathematics.isInRange(pos, range)) {
Mathematics.projectToRange(pos, range);
((InterfaceDataTypeDouble)getEAIndividual(i)).SetDoubleGenotype(pos);
}
}
}
public PopulationInitMethod getInitMethod() {
return initMethod;
}
public void setInitMethod(PopulationInitMethod initMethod) {
this.initMethod = initMethod;
}
public String initMethodTipText() {
return "Define the initial sampling method. Note that anything other than inidividualDefault will override the individual initialization concerning the positions in solution space.";
}
// /**
// * Check whether the population at the current state has been marked as
// * evaluated. This allows to avoid double evaluations.

View File

@ -0,0 +1,5 @@
package eva2.server.go.populations;
public enum PopulationInitMethod {
individualDefault, randomLatinHypercube;
}

View File

@ -319,7 +319,7 @@ public abstract class AbstractOptimizationProblem implements InterfaceOptimizati
// if not provided reasonable values use defaults:
if (mutationStepSize<0) mutationStepSize = 0.0001;
if (numOfFailures<0) numOfFailures = 100*AbstractEAIndividual.getDoublePosition(this.m_Template).length; // scales the effort with the number of problem dimensions
if (numOfFailures<0) numOfFailures = 100*AbstractEAIndividual.getDoublePositionShallow(this.m_Template).length; // scales the effort with the number of problem dimensions
AbstractEAIndividual indy = (AbstractEAIndividual)orig.clone();
this.evaluate(indy); // indy may be evaluated in a normalised way...
@ -384,7 +384,7 @@ public abstract class AbstractOptimizationProblem implements InterfaceOptimizati
if (orig instanceof InterfaceDataTypeDouble) {
initPerturb = epsilonPhenoSpace/(2*(Mathematics.getAvgRange(((InterfaceDataTypeDouble)orig).getDoubleRange())));
dim=((InterfaceDataTypeDouble)orig).getDoubleRange().length;
if (numOfFailures<0) numOfFailures = 100*AbstractEAIndividual.getDoublePosition(this.m_Template).length; // scales the effort with the number of problem dimensions
if (numOfFailures<0) numOfFailures = 100*AbstractEAIndividual.getDoublePositionShallow(this.m_Template).length; // scales the effort with the number of problem dimensions
} else {
System.err.println("Cannot initialize NMS on non-double valued individuals!");
return false;

View File

@ -1,5 +1,6 @@
package eva2.server.go.problems;
import eva2.gui.BeanInspector;
import eva2.gui.GenericObjectEditor;
import eva2.server.go.PopulationInterface;
import eva2.server.go.individuals.AbstractEAIndividual;
@ -40,6 +41,7 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
// PropertySelectableList<AbstractConstraint> constraintList = new PropertySelectableList<AbstractConstraint>(new AbstractConstraint[]{new GenericConstraint()});
private AbstractConstraint[] constraintArray = new AbstractConstraint[]{new GenericConstraint()};
private boolean withConstraints = false;
public static String rawFitKey="UnconstrainedFitnessValue";
public AbstractProblemDouble() {
initTemplate();
@ -104,7 +106,10 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
if (m_Noise != 0) RNG.addNoise(fitness, m_Noise);
// set the fitness
setEvalFitness(individual, x, fitness);
if (isWithConstraints()) addConstraints(individual, x);
if (isWithConstraints()) {
individual.putData(rawFitKey, individual.getFitness());
addConstraints(individual, x);
}
}
/**
@ -187,7 +192,7 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
* @param dim
* @return the lower bound of the double range in the given dimension
*/
protected double getRangeLowerBound(int dim) {
public double getRangeLowerBound(int dim) {
return -getDefaultRange();
}
@ -200,7 +205,7 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
* @param dim
* @return the upper bound of the double range in the given dimension
*/
protected double getRangeUpperBound(int dim) {
public double getRangeUpperBound(int dim) {
return getDefaultRange();
}
@ -369,7 +374,7 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
@Override
public String getAdditionalFileStringHeader(PopulationInterface pop) {
String superHeader = super.getAdditionalFileStringHeader(pop);
if (isWithConstraints()) return superHeader + " \tNum.Viol. \t Sum.Viol.";
if (isWithConstraints()) return superHeader + " \tRawFit. \tNum.Viol. \t Sum.Viol.";
else return superHeader;
}
@ -377,8 +382,9 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
public String getAdditionalFileStringValue(PopulationInterface pop) {
String superVal = super.getAdditionalFileStringValue(pop);
if (isWithConstraints()) {
Pair<Integer,Double> violation= getConstraintViolation((AbstractEAIndividual)pop.getBestIndividual());
return superVal + " \t" + violation.head() + " \t" + violation.tail();
AbstractEAIndividual indy = (AbstractEAIndividual)pop.getBestIndividual();
Pair<Integer,Double> violation= getConstraintViolation(indy);
return superVal + " \t" + BeanInspector.toString(indy.getData(rawFitKey)) + " \t" + violation.head() + " \t" + violation.tail();
} else return superVal;
}

View File

@ -86,7 +86,7 @@ public class ConstrPressureVessel extends AbstractProblemDouble {
}
@Override
protected double getRangeLowerBound(int dim) {
public double getRangeLowerBound(int dim) {
switch (dim) {
case 0:
case 1: return minThickness/2;
@ -100,7 +100,7 @@ public class ConstrPressureVessel extends AbstractProblemDouble {
}
@Override
protected double getRangeUpperBound(int dim) {
public double getRangeUpperBound(int dim) {
switch (dim) {
case 0:
case 1: return maxThickness;

View File

@ -32,8 +32,9 @@ public class ExternalRuntimeProblem extends AbstractOptimizationProblem implemen
protected String m_WorkingDir = "";
protected double m_upperBound = 10;
protected double m_lowerBound = 0;
private String additionalArg="";
// Private Subclass to redirect Streams within an extra Thread to avoid dead
// Private Subclass to redirect Streams within an extra Thread to avoid dead
// locks
private static class MonitorInputStreamThread extends Thread {
private Reader reader;
@ -165,6 +166,9 @@ public class ExternalRuntimeProblem extends AbstractOptimizationProblem implemen
try {
List<String> parameters=new ArrayList<String>();
parameters.add(this.m_Command);
if (additionalArg!=null && (additionalArg.length()>0)) {
parameters.add(additionalArg);
}
for(int i=0;i<this.m_ProblemDimension;i++){
parameters.add(new String(""+x[i]));
}
@ -337,4 +341,14 @@ public class ExternalRuntimeProblem extends AbstractOptimizationProblem implemen
return "Lower bound of the search space in any dimension.";
}
public String additionalArgumentTipText() {
return "Optionally define an additional (first) argument for the command line command.";
}
public String getAdditionalArgument() {
return additionalArg;
}
public void setAdditionalArgument(String additionalArg) {
this.additionalArg = additionalArg;
}
}

View File

@ -17,6 +17,9 @@ public class F2Problem extends F1Problem implements InterfaceMultimodalProblem,
public F2Problem(F2Problem b) {
super(b);
}
public F2Problem(int dim) {
super(dim);
}
/** This method returns a deep clone of the problem.
* @return the clone

View File

@ -28,7 +28,10 @@ public class F6Problem extends F1Problem implements InterfaceMultimodalProblem,
this.m_Omega = b.m_Omega;
}
/** This method inits the Problem to log multiruns
public F6Problem(int dim) {
super(dim);
}
/** This method inits the Problem to log multiruns
*/
public void initProblem() {
super.initProblem();

View File

@ -25,7 +25,11 @@ public class F8Problem extends F1Problem implements InterfaceMultimodalProblem,
this.c = b.c;
}
/** This method returns a deep clone of the problem.
public F8Problem(int dim) {
super(dim);
setDefaultRange(f8Range);
}
/** This method returns a deep clone of the problem.
* @return the clone
*/
public Object clone() {

View File

@ -29,12 +29,12 @@ public class FM0Problem extends AbstractMultiModalProblemKnown implements Interf
// this.m_Extrema[1] = 6;
}
protected double getRangeUpperBound(int dim) {
public double getRangeUpperBound(int dim) {
if (dim == 0) return 2.0;
else return 2.8;
}
protected double getRangeLowerBound(int dim) {
public double getRangeLowerBound(int dim) {
return -1*getRangeUpperBound(dim);
}

View File

@ -1,5 +1,7 @@
package eva2.server.go.problems;
import eva2.server.go.individuals.InterfaceDataTypeDouble;
/**
* A minimal interface for double valued problems.
*
@ -28,4 +30,33 @@ public interface InterfaceProblemDouble {
* @return a range array
*/
public double[][] makeRange();
/**
* Get the EA individual template currently used by the problem.
*
* @return the EA individual template currently used
*/
public InterfaceDataTypeDouble getEAIndividual();
/**
* Get the upper bound of the double range in the given dimension. Override
* this to implement non-symmetric ranges. User setDefaultRange for symmetric ranges.
*
* @see makeRange()
* @see getRangeLowerBound(int dim)
* @param dim
* @return the upper bound of the double range in the given dimension
*/
public double getRangeUpperBound(int dim);
/**
* Get the lower bound of the double range in the given dimension. Override
* this to implement non-symmetric ranges. Use setDefaultRange for symmetric ranges.
*
* @see makeRange()
* @see getRangeUpperBound(int dim)
* @param dim
* @return the lower bound of the double range in the given dimension
*/
public double getRangeLowerBound(int dim);
}

View File

@ -291,9 +291,11 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
public static Object getSensorValue(String sensor, double[] vars, double[] consts) {
if (sensor.charAt(0)=='X') {
try {
if (sensor.length()==1) return vars;
int index=Integer.parseInt(sensor.substring(1));
return new Double(vars[index]);
} catch(Exception e) {
System.err.println("Warning, unable to access " + sensor);
return vars;
}
} else if (sensor.charAt(0)=='C') {

View File

@ -9,6 +9,7 @@ import eva2.gui.GraphPointSet;
import eva2.gui.Plot;
import eva2.gui.TopoPlot;
import eva2.server.go.InterfacePopulationChangedEventListener;
import eva2.server.go.PopulationInterface;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.InterfaceDataTypeDouble;
import eva2.server.go.operators.cluster.ClusteringDensityBased;
@ -20,6 +21,7 @@ import eva2.server.go.populations.Population;
import eva2.server.go.populations.SolutionSet;
import eva2.server.go.problems.B1Problem;
import eva2.server.go.problems.Interface2DBorderProblem;
import eva2.server.go.problems.InterfaceAdditionalPopulationInformer;
import eva2.server.go.problems.InterfaceOptimizationProblem;
import eva2.server.go.problems.TF1Problem;
import eva2.tools.chart2d.DPoint;
@ -29,10 +31,8 @@ import eva2.tools.math.RNG;
/** The infamuos clustering based niching EA, still under construction.
* It should be able to identify and track multiple global/local optima
* at the same time, but currently i'm not sure what size the subpopulations
* is. It is straightforward with a GA but in case of an ES i've changed the
* interpretation of the population size and i guess that the mu/lambda ratio
* is currently lost.. i'll have to fix that some day.
* at the same time.
*
* Copyright: Copyright (c) 2003
* Company: University of Tuebingen, Computer Architecture
* @author Felix Streichert
@ -41,7 +41,7 @@ import eva2.tools.math.RNG;
* $Author: mkron $
*/
public class ClusterBasedNichingEA implements InterfacePopulationChangedEventListener, InterfaceOptimizer, java.io.Serializable {
public class ClusterBasedNichingEA implements InterfacePopulationChangedEventListener, InterfaceAdditionalPopulationInformer, InterfaceOptimizer, java.io.Serializable {
private Population m_Population = new Population();
private transient Population m_Archive = new Population();
@ -71,7 +71,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
private int convergedCnt = 0;
private static boolean TRACE = false;
private int m_ShowCycle = 100;
private int m_ShowCycle = 0;
transient private TopoPlot m_Topology;
private int haltingWindow = 15;
private double muLambdaRatio = 0.5;
@ -639,9 +639,23 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
spec.add(survivor);
}
public int countActiveSpec() {
int k = 0;
for (int i=0; i<m_Species.size(); i++) {
if (isActive(m_Species.get(i))) k++;
}
return k;
}
/** This method allows an optimizer to register a change in the optimizer.
* @param source The source of the event.
* @param name Could be used to indicate the nature of the event.
*/
public void registerPopulationStateChanged(Object source, String name) {
//Population population = ((InterfaceOptimizer)source).getPopulation();
}
public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
this.m_Listener = ea;
}
@ -725,6 +739,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
this.m_Undifferentiated = pop;
pop.setUseHistory(true);
}
public String populationTipText() {
return "Edit the properties of the population used.";
}
@ -882,6 +897,10 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
return "Determines the size of the initial population.";
}
public String[] getGOEPropertyUpdateLinks() {
return new String[] {"population", "populationSize", "populationSize", "population"};
}
// /**
// * @return the muLambdaRatio
// */
@ -914,4 +933,14 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
public String haltingWindowTipText() {
return "Lenght of the halting window defining when a cluster is seen as converged and frozen; set to zero to disable.";
}
public String getAdditionalFileStringHeader(PopulationInterface pop) {
return " Undiff. \t #Act.spec.; \t #Inact.spec.";
}
public String getAdditionalFileStringValue(PopulationInterface pop) {
int actives = countActiveSpec();
return m_Undifferentiated.size() + " \t " + actives + " \t " + (m_Species.size()-actives);
}
}

View File

@ -150,7 +150,7 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
//double[] rand = getUniformRandVect(position.length, range);
Mathematics.vvAdd(newPos, rand, newPos);
if (m_CheckConstraints) {
if (m_CheckRange) {
Mathematics.projectToRange(newPos, range);
}

View File

@ -156,7 +156,7 @@ public class EvolutionStrategyIPOP extends EvolutionStrategies implements Interf
super.init();
bestList = new LinkedList<AbstractEAIndividual>();
best = getPopulation().getBestEAIndividual();
dim = AbstractEAIndividual.getDoublePosition(getPopulation().getEAIndividual(0)).length;
dim = AbstractEAIndividual.getDoublePositionShallow(getPopulation().getEAIndividual(0)).length;
fitConvTerm = new FitnessConvergenceTerminator(stagThreshold, (isStagnationTimeUserDef()) ? stagTimeArbitrary : calcDefaultStagnationTime(), false, true); // gen. based, absolute
getPopulation().addPopulationChangedEventListener(this);

View File

@ -123,9 +123,10 @@ public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializabl
if (this.m_Population == null) System.out.println("population null "+i);
offSprings = tmpIndy.mateWith(this.m_PartnerSelection.findPartnerFor(tmpIndy, this.m_Population, this.m_NumberOfPartners));
for (int j = 0; j < offSprings.length; j++) {
offSprings[j].mutate();
}
// for (int j = 0; j < offSprings.length; j++) {
// offSprings[j].mutate(); // quite useless if n-1 are thrown away...
// }
offSprings[0].mutate();
result.add(i, offSprings[0]);
}
this.evaluatePopulation(result);

View File

@ -138,7 +138,8 @@ public class NelderMeadSimplex implements InterfaceOptimizer, Serializable, Inte
for (int i=0; i<bestpop.size(); i++) {
for (int j=0; j<dim; j++) {
AbstractEAIndividual bestIndi= (AbstractEAIndividual) bestpop.getIndividual(i);
centroid[j] +=((InterfaceDataTypeDouble)bestIndi).getDoubleData()[j]/bestpop.size(); // bug?
double[] bestIndyPos = ((InterfaceDataTypeDouble)bestIndi).getDoubleDataWithoutUpdate();
centroid[j] +=bestIndyPos[j]/bestpop.size(); // bug?
}
}
@ -269,7 +270,7 @@ public class NelderMeadSimplex implements InterfaceOptimizer, Serializable, Inte
// m_Problem.evaluate(ind);
// this.m_Population.incrFunctionCalls();
}
m_Population.set(m_Population.getIndexOfWorstIndividual(fitIndex), ind);
m_Population.set(m_Population.getIndexOfWorstIndividual(fitIndex), ind, fitIndex);
}else{//keine Verbesserung gefunden shrink!!
double[] u_1 = ((InterfaceDataTypeDouble) m_Population.getBestEAIndividual(fitIndex)).getDoubleData();

View File

@ -7,19 +7,25 @@ import eva2.gui.BeanInspector;
import eva2.gui.GenericObjectEditor;
import eva2.gui.TopoPlot;
import eva2.server.go.InterfacePopulationChangedEventListener;
import eva2.server.go.PopulationInterface;
import eva2.server.go.enums.PSOTopologyEnum;
import eva2.server.go.enums.PostProcessMethod;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.AbstractEAIndividualComparator;
import eva2.server.go.individuals.InterfaceDataTypeDouble;
import eva2.server.go.operators.distancemetric.PhenotypeMetric;
import eva2.server.go.operators.paramcontrol.ParamAdaption;
import eva2.server.go.operators.paramcontrol.ParameterControlManager;
import eva2.server.go.operators.postprocess.PostProcess;
import eva2.server.go.populations.InterfaceSolutionSet;
import eva2.server.go.populations.Population;
import eva2.server.go.populations.SolutionSet;
import eva2.server.go.problems.AbstractOptimizationProblem;
import eva2.server.go.problems.F1Problem;
import eva2.server.go.problems.Interface2DBorderProblem;
import eva2.server.go.problems.InterfaceAdditionalPopulationInformer;
import eva2.server.go.problems.InterfaceOptimizationProblem;
import eva2.server.go.problems.InterfaceProblemDouble;
import eva2.tools.EVAERROR;
import eva2.tools.Mathematics;
import eva2.tools.SelectedTag;
@ -46,13 +52,13 @@ import eva2.tools.math.Jama.Matrix;
* Time: 11:23:21
* To change this template use File | Settings | File Templates.
*/
public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Serializable {
public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Serializable, InterfaceAdditionalPopulationInformer {
protected Population m_Population = new Population();
Object[] sortedPop = null;
protected AbstractEAIndividual m_BestIndividual;
protected InterfaceOptimizationProblem m_Problem = new F1Problem();
protected boolean m_CheckConstraints = true;
protected boolean m_CheckRange = true;
protected boolean checkSpeedLimit = false;
protected boolean useAlternative = false;
protected PSOTopologyEnum topology = PSOTopologyEnum.grid;
@ -72,8 +78,12 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
protected int minSubSwarmSize = 2;
protected int treeStruct = 1;
protected boolean wrapTopology = true;
protected int treeBranchDeg = 3;
// protected boolean doLocalSearch = false;
// protected int localSearchGens=100;
// protected int lsStepsPerInd=200;
protected int treeLevels, treeOrphans, treeLastFullLevelNodeCnt;
protected int dmsRegroupInterval = 10;
private transient Vector<int[]> dmsLinks = null;
protected ParameterControlManager paramControl = new ParameterControlManager();
/**
@ -92,6 +102,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
transient final static String multiSwSizeKey="MultiSwarmSize";
transient final static String indexKey="particleIndex";
transient final static String sortedIndexKey="sortedParticleIndex";
transient final static String dmsGroupIndexKey="dmsGroupIndex";
protected String m_Identifier = "";
transient private InterfacePopulationChangedEventListener m_Listener;
@ -110,6 +121,9 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
transient protected boolean m_Show = false;
transient protected eva2.gui.Plot m_Plot;
private boolean externalInitialPop = false;
// private double lsCandidateRatio=0.25;
public ParticleSwarmOptimization() {
// this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" );
@ -143,11 +157,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
/**
* Constructor for most common parameters with constriction based approach.
*
* @param popSize swarm size
* @param p1 the value for phi1
* @param p2 the value for phi1
* @param topo type of the neighbourhood topology
* @param topoRange range of the neighbourhood topology
* @param popSize swarm size
*/
public ParticleSwarmOptimization(int popSize, double p1, double p2, PSOTopologyEnum topo, int topoRange) {
this();
@ -158,6 +172,26 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
topology=topo;
}
// /**
// * Constructor for most common parameters with constriction based approach and local search.
// *
// * @param popSize swarm size
// * @param p1 the value for phi1
// * @param p2 the value for phi1
// * @param topo type of the neighbourhood topology
// * @param topoRange range of the neighbourhood topology
// * @param lsEveryNGens interval of local search steps in generations
// * @param stepsPerInd number of local search steps per individual
// * @param candidateRatio ratio of population on which local search is performed
// */
// public ParticleSwarmOptimization(int popSize, double p1, double p2, PSOTopologyEnum topo, int topoRange, int lsEveryNGens, int stepsPerInd, double candidateRatio) {
// this(popSize, p1, p2, topo, topoRange);
// setDoLocalSearch(true);
// localSearchGens=lsEveryNGens;
// lsStepsPerInd=stepsPerInd;
// lsCandidateRatio = candidateRatio;
// }
public Object clone() {
return (Object) new ParticleSwarmOptimization(this);
}
@ -180,12 +214,13 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
// topoPlot.dispose();
topoPlot = null;
}
this.m_Problem.initPopulation(this.m_Population);
tracedVelocity = null;
if (!externalInitialPop) this.m_Problem.initPopulation(this.m_Population);
// evaluation needs to be done here now, as its omitted if reset is false
initDefaults(this.m_Population);
this.evaluatePopulation(this.m_Population);
initByPopulation(m_Population, false);
initByPopulation(null, false);
externalInitialPop = false;
}
/**
@ -380,12 +415,17 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
return getPopulationVelSpeed(pop, 2, partVelKey, partTypeKey, defaultType)[0];
}
/** This method will init the optimizer with a given population
/**
* This method will init the optimizer with a given population or, if pop is null,
* initialize the current population as if it was new.
* @param pop The initial population
* @param reset If true the population is reset.
*/
public void initByPopulation(Population pop, boolean reset) {
this.m_Population = (Population)pop.clone();
if (pop != null) {
this.m_Population = (Population)pop.clone();
externalInitialPop = true;
}
if (reset) this.m_Population.init();
AbstractEAIndividual indy;
@ -407,13 +447,19 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
treeLevels = 0;
// the HPSO tree will contain layers 0...HPSOLevels, the last one is "incomplete" with only HPSOOrphans number of nodes
while (getMaxNodes(treeBranchDeg, treeLevels) < pop.size()) treeLevels++;
treeOrphans = pop.size()-getMaxNodes(treeBranchDeg, treeLevels-1);
treeLastFullLevelNodeCnt = (int)Math.pow(treeBranchDeg, treeLevels-1);
if (getTopology()==PSOTopologyEnum.hpso || getTopology()==PSOTopologyEnum.tree) {
if (m_TopologyRange<2) System.err.println("Error, tree/hpso requires topology range of at least 2!");
else {
while (getMaxNodes(m_TopologyRange, treeLevels) < m_Population.size()) treeLevels++;
treeOrphans = m_Population.size()-getMaxNodes(m_TopologyRange, treeLevels-1);
treeLastFullLevelNodeCnt = (int)Math.pow(m_TopologyRange, treeLevels-1);
}
}
if (getTopology()==PSOTopologyEnum.dms) dmsLinks=regroupSwarm(m_Population, getTopologyRange());
}
private boolean defaultsDone(AbstractEAIndividual individual) {
return individual.hasData(indexKey);
private boolean defaultsDone(AbstractEAIndividual indy) {
return (indy.hasData(partVelKey) && indy.hasData(indexKey));
}
/**
@ -426,7 +472,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
for (int i = 0; i < pop.size(); i++) {
indy = (AbstractEAIndividual) pop.get(i);
if (indy instanceof InterfaceDataTypeDouble) {
initIndividualDefaults(indy, m_InitialVelocity);
if (!externalInitialPop || (!defaultsDone(indy))) initIndividualDefaults(indy, m_InitialVelocity);
}
indy.putData(indexKey, i);
indy.SetIndividualIndex(i);
@ -460,6 +506,24 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
//try { Thread.sleep(10); } catch(Exception e) {}
}
public static void dumpPop(String prefix, Population pop) {
if (prefix!=null) System.out.println(prefix);
for (int i=0; i<pop.size(); i++) {
AbstractEAIndividual indy=pop.getEAIndividual(i);
String info=getParticleInfo(indy);
System.out.println(info);
}
}
public static String getParticleInfo(AbstractEAIndividual indy) {
String str = AbstractEAIndividual.getDefaultStringRepresentation(indy);
str += " / Vel: " + BeanInspector.toString(indy.getData(partVelKey));
str += " / BestP: " + BeanInspector.toString(indy.getData(partBestPosKey));
str += " / BestF: " + BeanInspector.toString(indy.getData(partBestFitKey));
str += " / PType: " + indy.getData(partTypeKey);
return str;
}
/**
* Compare the given attractor fitness value to the one remembered by the neighbour individual
* if useHistoric is true, else with the current fitness of the neighbour individual.
@ -550,7 +614,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
if (checkSpeedLimit) enforceSpeedLimit(curVelocity, range, getSpeedLimit(index));
// enforce range constraints if necessary
if (m_CheckConstraints) ensureConstraints(curPosition, curVelocity, range);
if (m_CheckRange) ensureConstraints(curPosition, curVelocity, range);
plotIndy(curPosition, curVelocity, (Integer)indy.getData(indexKey));
// finally update the position
@ -603,7 +667,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
for (int i=0; i<pop.size(); i++) {
AbstractEAIndividual indy = (AbstractEAIndividual)pop.get(i);
if (isIndividualToUpdate(indy)) {
updateIndProps(indy);
updateIndProps(indy, indy);
// System.err.println("updated " + i + " - "+ getParticleInfo(indy));
}
}
}
@ -622,13 +687,13 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
}
/**
* Write current stats (fitness and position) as best fitness and position into individual properties.
* Write current fitness and position as best fitness and position into individual properties.
*
* @param indy the individual to update
* @param srcIndy the individual to update
*/
protected void updateIndProps(AbstractEAIndividual indy) {
indy.putData(partBestFitKey, indy.getFitness().clone());
indy.putData(partBestPosKey, ((InterfaceDataTypeDouble)indy).getDoubleData());
protected void updateIndProps(AbstractEAIndividual trgIndy, AbstractEAIndividual srcIndy) {
trgIndy.putData(partBestFitKey, srcIndy.getFitness().clone());
trgIndy.putData(partBestPosKey, ((InterfaceDataTypeDouble)srcIndy).getDoubleData());
}
/**
@ -841,7 +906,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
double[] localBestPosition = null;
double[] localBestFitness = null;
int tmpIndex;
AbstractEAIndividual bestIndy, indy = (AbstractEAIndividual)pop.get(index);
AbstractEAIndividual bestIndy, indy = pop.getEAIndividual(index);
boolean useHistoric = true;
int sortedIndex=-1;
int k;
@ -905,13 +970,13 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
sortedIndex = (Integer)((AbstractEAIndividual)sortedPop[index]).getData(sortedIndexKey);
if (sortedIndex>0) { // its found and its not the root. root has no parent to check for
k = getParentIndex(treeBranchDeg, sortedIndex, pop.size());
k = getParentIndex(m_TopologyRange, sortedIndex, pop.size());
compareAndSetAttractor(localBestFitness, localBestPosition, (AbstractEAIndividual)sortedPop[k], useHistoric);
}
if (treeStruct == 1) { // loop all children
if (isComplete(sortedIndex, pop.size())) { // the node has full degree
k = treeBranchDeg*sortedIndex+1; // this is the offset of the nodes children
for (int i=0; i<treeBranchDeg; i++) {
k = m_TopologyRange*sortedIndex+1; // this is the offset of the nodes children
for (int i=0; i<m_TopologyRange; i++) {
compareAndSetAttractor(localBestFitness, localBestPosition, (AbstractEAIndividual)sortedPop[k+i], useHistoric);
}
} else if (isIncomplete(sortedIndex, pop.size())) { // the node does not have full degree but might have orphans
@ -935,7 +1000,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
break;
case hpso: // Hierarchical PSO
if (index>=0) {
k = getParentIndex(treeBranchDeg, index, pop.size());
k = getParentIndex(m_TopologyRange, index, pop.size());
// compareAndSet(localBestFitness, localBestPosition, (AbstractEAIndividual)pop.get(k), useHistoric);
indy = (AbstractEAIndividual)pop.get(k);
System.arraycopy((double[])indy.getData(partBestFitKey), 0, localBestFitness, 0, localBestFitness.length);
@ -951,6 +1016,18 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
compareAndSetAttractor(localBestFitness, localBestPosition, indy, useHistoric);
}
break;
case dms:
int groupIndex = (Integer)pop.getEAIndividual(index).getData(dmsGroupIndexKey);
int[] groupLinks = dmsLinks.get(groupIndex);
for (int i=0; i<groupLinks.length; i++) {
if (groupLinks[i]!=index) {
// select informant
indy = pop.getEAIndividual(groupLinks[i]);
// set local values
compareAndSetAttractor(localBestFitness, localBestPosition, indy, useHistoric);
}
}
break;
}
return localBestPosition;
}
@ -1028,7 +1105,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
for (int i = 0; i < curPosition.length; i++) {
newPosition[i] = curPosition[i] + curVelocity[i];
}
if (m_CheckConstraints && isOutOfRange(newPosition, range)) {
if (m_CheckRange && isOutOfRange(newPosition, range)) {
System.err.println("error, individual violates constraints!");
}
@ -1036,7 +1113,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
if (indy instanceof InterfaceDataTypeDouble) ((InterfaceDataTypeDouble)indy).SetDoubleGenotype(newPosition);
else {
((InterfaceDataTypeDouble) indy).SetDoubleGenotype(newPosition); // WARNING, this does a checkBounds in any case!
if (!m_CheckConstraints) System.err.println("warning, checkbounds will be forced by InterfaceESIndividual!");
if (!m_CheckRange) System.err.println("warning, checkbounds will be forced by InterfaceESIndividual!");
}
indy.putData(partVelKey, curVelocity);
@ -1090,44 +1167,36 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
if (isOutOfRange(pos, range)) {
System.err.println("warning, ensureConstraints called with already violating position (PSO)... reinitializing particle.");
for (int i=0; i<pos.length; i++) {
pos[i]=RNG.randomDouble(range[i][0],range[i][1]);
newPos[i] = pos[i] + velocity[i];
if (!Mathematics.isInRange(pos[i], range[i][0], range[i][1])) pos[i]=RNG.randomDouble(range[i][0],range[i][1]);
}
} else {
for (int i = 0; i < pos.length; i++) {
if ((newPos[i] < range[i][0]) || (newPos[i] > range[i][1])) {
if ((pos[i] == range[i][0]) || (pos[i] == range[i][1])) {
// bounce?
velocity[i] *= reduceSpeedOnConstViolation; // bounce velocity and reduce
if (((pos[i] == range[i][0]) && (newPos[i] < range[i][0])) || ((pos[i] == range[i][1]) && (newPos[i] > range[i][1]))) {
velocity[i] *= -1; // bounce only if leaving in this direction.
}
newPos[i] = pos[i]+velocity[i];
} else {
// set vel. to land on the bounds
velocity[i] = (newPos[i] < range[i][0]) ? (range[i][0]-pos[i]) : (range[i][1]-pos[i]);
newPos[i] = pos[i]+velocity[i];
if ((newPos[i] < range[i][0]) || (newPos[i] > range[i][1])) {
velocity[i]*=.999; /// beware of floating point errors.
newPos[i] = pos[i]+velocity[i];
}
}
for (int i = 0; i < pos.length; i++) {
if (!Mathematics.isInRange(newPos[i], range[i][0], range[i][1])) {
if ((pos[i] == range[i][0]) || (pos[i] == range[i][1])) {
// bounce?
velocity[i] *= reduceSpeedOnConstViolation; // bounce velocity and reduce
if (((pos[i] == range[i][0]) && (newPos[i] < range[i][0])) || ((pos[i] == range[i][1]) && (newPos[i] > range[i][1]))) {
velocity[i] *= -1; // bounce only if leaving in this direction.
}
while ((newPos[i] < range[i][0]) || (newPos[i] > range[i][1])) {
//System.err.println("missed, pos was " + pos[i] + " vel was "+velocity[i]);
velocity[i]*=reduceSpeedOnConstViolation;
newPos[i] = pos[i]+velocity[i];
} else {
// set vel. to land on the bounds
velocity[i] = (newPos[i] < range[i][0]) ? (range[i][0]-pos[i]) : (range[i][1]-pos[i]);
newPos[i] = pos[i]+velocity[i];
if ((newPos[i] < range[i][0]) || (newPos[i] > range[i][1])) {
velocity[i]*=.999; /// beware of floating point errors.
newPos[i] = pos[i]+velocity[i];
}
}
while ((newPos[i] < range[i][0]) || (newPos[i] > range[i][1])) {
//System.err.println("missed, pos was " + pos[i] + " vel was "+velocity[i]);
velocity[i]*=reduceSpeedOnConstViolation;
newPos[i] = pos[i]+velocity[i];
}
}
//for (int i = 0; i < pos.length; i++) newPos[i] = pos[i] + velocity[i];
if (isOutOfRange(newPos, range)) {
System.err.println("narg, still out of range");
}
// while (isOutOfRange(newPos, range)) {
// for (int i = 0; i < velocity.length; i++) velocity[i] *= reduceSpeedOnConstViolation;
// for (int i = 0; i < pos.length; i++) newPos[i] = pos[i] + velocity[i];
// }
}
if (isOutOfRange(newPos, range)) {
System.err.println("narg, still out of range");
}
}
@ -1149,6 +1218,26 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
// System.out.println(">>> " + m_Population.getBestEAIndividual().getStringRepresentation());
// if (doLocalSearch && (m_Population.getGeneration()%localSearchGens==0)) {
//// System.out.println("Local search at gen "+m_Population.getGeneration());
// Population bestN = m_Population.getBestNIndividuals(Math.max(1,(int)(lsCandidateRatio*m_Population.size())));
//// Population bestN = m_Population.getSortedNIndividuals(Math.max(1,(int)(lsCandidateRatio*m_Population.size())), false);
// Population cands=(Population)bestN.clone();
// int maxSteps=cands.size()*lsStepsPerInd;
// int stepsDone = PostProcess.processSingleCandidates(PostProcessMethod.nelderMead, cands, maxSteps, 0.01, (AbstractOptimizationProblem)this.m_Problem, null);
// for (int i=0; i<cands.size(); i++) {
// if (AbstractEAIndividual.isDominatingFitnessNotEqual(cands.getEAIndividual(i).getFitness(),
// (double[])bestN.getEAIndividual(i).getData(partBestFitKey))) {
//// System.out.println("Improved to " + BeanInspector.toString(cands.getEAIndividual(i).getFitness()) + " from " + BeanInspector.toString((double[])bestN.getEAIndividual(i).getData(partBestFitKey)));
// updateIndProps(bestN.getEAIndividual(i), cands.getEAIndividual(i));
// }
// }
// if (stepsDone>maxSteps) {
//// System.err.println("Warning: more steps performed than alloed in PSO LS: " + stepsDone + " vs. " + maxSteps);
// m_Population.incrFunctionCallsBy(stepsDone);
// } else m_Population.incrFunctionCallsBy(maxSteps);
// }
this.firePropertyChangedEvent(Population.nextGenerationPerformed);
if (sleepTime > 0 ) try { Thread.sleep(sleepTime); } catch(Exception e) {}
@ -1245,7 +1334,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
protected void updateTopology(Population pop) {
// int topoID = this.m_Topology.getSelectedTag().getID();
// this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" );
if (topology == PSOTopologyEnum.dms) { // Dynamic multi-swarm after Liang & Suganthan
if (pop.getGeneration() % getDmsRegroupGens()==0) {
dmsLinks = regroupSwarm(pop, getTopologyRange());
}
}
if ((topology == PSOTopologyEnum.multiSwarm) || (topology == PSOTopologyEnum.tree)) {
sortedPop = pop.toArray();
if ((topology == PSOTopologyEnum.multiSwarm) || (treeStruct>=2)) Arrays.sort(sortedPop, new AbstractEAIndividualComparator());
@ -1312,7 +1405,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
AbstractEAIndividualComparator comp = new AbstractEAIndividualComparator(partBestFitKey);
for (int i=0; i<pop.size(); i++) {
// loop over the part of the tree which is complete (full degree in each level)
parentIndex=getParentIndex(treeBranchDeg, i, pop.size());
parentIndex=getParentIndex(m_TopologyRange, i, pop.size());
if (comp.compare(pop.get(i), pop.get(parentIndex)) < 0) { // sibling is dominant!
// so switch them
indy = (AbstractEAIndividual)pop.get(i);
@ -1349,6 +1442,49 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
}
}
/**
* Randomly assign groups of size groupSize.
*
* @param links
* @param groupSize
*/
private Vector<int[]> regroupSwarm(Population pop, int groupSize) {
int numGroups = pop.size() / groupSize; // truncated integer: last group is larger
int[] perm = RNG.randomPerm(pop.size());
Vector<int[]> links = new Vector<int[]>(numGroups);
for (int i=0; i<numGroups; i++) {
if (i<numGroups-1) links.add(new int[groupSize]);
else links.add(new int[pop.size()-(groupSize*i)]); // the last group is larger
int[] group=links.get(i);
for (int k=0; k<group.length; k++) {
group[k]=perm[groupSize*i+k];
pop.getEAIndividual(group[k]).putData(dmsGroupIndexKey, i);
}
}
return links;
}
// /**
// * Randomly assign groups of size groupSize.
// *
// * @param links
// * @param groupSize
// */
// private int[] regroupSwarm(Population pop, int groupSize) {
// int groupIndex, numGroups = pop.size() / groupSize; // truncated integer: last group is larger
//// int hangover = pop.size()-(numGroups*groupSize); // Ueberhangmandate ... woanders zuteilen um einergruppen zu vermeiden
//
// int[] perm = RNG.randomPerm(pop.size());
//
// for (int k=0; k<perm.length; k++) {
// groupIndex=k/groupSize;
// if (groupIndex>=numGroups) groupIndex--;
// pop.getEAIndividual(perm[k]).putData(dmsGroupIndexKey, groupIndex);
// }
// return perm;
// }
/**
* This method is simply for debugging.
*/
@ -1429,7 +1565,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
* @return The name of the algorithm
*/
public String getName() {
return "PSO-"+getPhi1()+"_"+getPhi2();
// return "PSO-"+getTopology()+getTopologyRange()+(isDoLocalSearch() ? "-ls_" : "_")+getPhi1()+"_"+getPhi2();
return "PSO-"+getTopology()+getTopologyRange()+"_"+getPhi1()+"_"+getPhi2();
}
/** Assuming that all optimizer will store thier data in a population
@ -1614,7 +1751,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
// this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" );
// linear, grid, random
GenericObjectEditor.setShowProperty(cls, "topologyRange", (topology==PSOTopologyEnum.linear) || (topology==PSOTopologyEnum.grid) || (topology==PSOTopologyEnum.random));
GenericObjectEditor.setShowProperty(cls, "topologyRange", (topology==PSOTopologyEnum.linear) || (topology==PSOTopologyEnum.grid) || (topology==PSOTopologyEnum.random) || (topology==PSOTopologyEnum.tree) || (topology==PSOTopologyEnum.hpso) || (topology==PSOTopologyEnum.dms));
// multi swarm
GenericObjectEditor.setShowProperty(cls, "subSwarmRadius", (topology==PSOTopologyEnum.multiSwarm));
// multi swarm
@ -1622,9 +1759,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
// tree
GenericObjectEditor.setShowProperty(cls, "treeStruct", (topology==PSOTopologyEnum.tree));
// tree, hpso
GenericObjectEditor.setShowProperty(cls, "treeBranchDegree", (topology==PSOTopologyEnum.tree) || (topology==PSOTopologyEnum.hpso));
// GenericObjectEditor.setShowProperty(cls, "treeBranchDegree", (topology==PSOTopologyEnum.tree) || (topology==PSOTopologyEnum.hpso));
// linear
GenericObjectEditor.setShowProperty(cls, "wrapTopology", (topology==PSOTopologyEnum.linear));
// dms
GenericObjectEditor.setShowProperty(cls, "dmsRegroupGens", (topology==PSOTopologyEnum.dms));
}
public PSOTopologyEnum getTopology() {
@ -1666,14 +1805,14 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
/** Toggle Check Constraints.
* @param s Check Constraints.
*/
public void setCheckConstraints(boolean s) {
this.m_CheckConstraints = s;
public void setCheckRange(boolean s) {
this.m_CheckRange = s;
}
public boolean isCheckConstraints() {
return this.m_CheckConstraints;
public boolean isCheckRange() {
return this.m_CheckRange;
}
public String checkConstraintsTipText() {
return "Toggle constraints check (whether particles are allowed to leave the range).";
public String checkRangeTipText() {
return "Toggle whether particles are allowed to leave the range.";
}
/**
@ -1773,18 +1912,6 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
// this.useAlternative = useAlternative;
// }
public int getTreeBranchDegree() {
return treeBranchDeg;
}
public void setTreeBranchDegree(int branch) {
treeBranchDeg = branch;
}
public String treeBranchDegreeTipText() {
return "Set the branch degree of the tree topology.";
}
public boolean isWrapTopology() {
return wrapTopology;
}
@ -1829,10 +1956,57 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
for (int i=0; i<population.size(); i++) {
double[] personalBestPos = (double[]) population.getEAIndividual(i).getData(partBestPosKey);
double[] personalBestfit = (double[]) population.getEAIndividual(i).getData(partBestFitKey);
double relDiff = (personalBestfit[0]-((InterfaceProblemDouble)m_Problem).eval(personalBestPos)[0])/personalBestfit[0];
// if (personalBestfit[0]!=((InterfaceProblemDouble)m_Problem).eval(personalBestPos)[0]) {
if (Math.abs(relDiff)>1e-20) {
System.err.println("Warning: mismatching best fitness by " + relDiff);
System.err.println("partInfo: " + i + " - " + getParticleInfo(population.getEAIndividual(i)));
}
if (Math.abs(relDiff)>1e-10) {
System.err.println("partInfo: " + i + " - " + getParticleInfo(population.getEAIndividual(i)));
throw new RuntimeException("Mismatching best fitness!! " + personalBestfit[0] + " vs. " + ((InterfaceProblemDouble)m_Problem).eval(personalBestPos)[0]);
}
((InterfaceDataTypeDouble)indy).SetDoubleGenotype(personalBestPos);
indy.SetFitness(personalBestfit);
bests.add((AbstractEAIndividual)indy.clone());
}
return bests;
}
public int getDmsRegroupGens() {
return dmsRegroupInterval;
}
public void setDmsRegroupGens(int dmsRegroupInterval) {
this.dmsRegroupInterval = dmsRegroupInterval;
}
public String dmsRegroupGensTipText() {
return "The number of generations after which new subswarms are randomly formed.";
}
// public boolean isDoLocalSearch() {
// return doLocalSearch;
// }
//
// public void setDoLocalSearch(boolean doLocalSearch) {
// this.doLocalSearch = doLocalSearch;
// }
public String getAdditionalFileStringHeader(PopulationInterface pop) {
if (emaPeriods > 0) return " \tMeanCurSpeed \tMeanEMASpeed";
else return " \tMeanCurSpeed";
}
public String getAdditionalFileStringValue(PopulationInterface pop) {
String res=" \t";
AbstractEAIndividual indy = (AbstractEAIndividual)pop.get(0);
if (emaPeriods>0) {
if (indy instanceof InterfaceDataTypeDouble) {
res = getRelativeEMASpeed(((InterfaceDataTypeDouble)indy).getDoubleRange()) + " \t";
} else res=Double.NaN + " \t";;
}
res += getPopulationAvgNormedVelocity((Population) pop);
return res;
}
}

View File

@ -5,18 +5,12 @@ import java.io.Serializable;
import eva2.gui.GenericObjectEditor;
import eva2.server.go.InterfaceGOParameters;
import eva2.server.go.InterfacePopulationChangedEventListener;
import eva2.server.go.InterfaceTerminator;
import eva2.server.go.enums.PSOTopologyEnum;
import eva2.server.go.operators.selection.InterfaceSelection;
import eva2.server.go.operators.terminators.EvaluationTerminator;
import eva2.server.go.populations.Population;
import eva2.server.go.problems.B1Problem;
import eva2.server.go.problems.F1Problem;
import eva2.server.go.problems.InterfaceOptimizationProblem;
import eva2.server.go.strategies.InterfaceOptimizer;
import eva2.server.go.strategies.ParticleSwarmOptimization;
import eva2.server.go.strategies.PopulationBasedIncrementalLearning;
import eva2.tools.SelectedTag;
import eva2.tools.Serializer;
@ -174,14 +168,14 @@ public class PSOParameters extends AbstractGOParameters implements InterfaceGOPa
/** Toggle Check Constraints.
* @param s Check Constraints.
*/
public void setCheckConstraints(boolean s) {
((ParticleSwarmOptimization)this.m_Optimizer).setCheckConstraints(s);
public void setCheckRange(boolean s) {
((ParticleSwarmOptimization)this.m_Optimizer).setCheckRange(s);
}
public boolean isCheckConstraints() {
return ((ParticleSwarmOptimization)this.m_Optimizer).isCheckConstraints();
public boolean isCheckRange() {
return ((ParticleSwarmOptimization)this.m_Optimizer).isCheckRange();
}
public String checkConstraintsTipText() {
return ((ParticleSwarmOptimization)this.m_Optimizer).checkConstraintsTipText();
return ((ParticleSwarmOptimization)this.m_Optimizer).checkRangeTipText();
}
/** This method allows you to choose the topology type.
@ -264,23 +258,23 @@ public class PSOParameters extends AbstractGOParameters implements InterfaceGOPa
return ((ParticleSwarmOptimization)this.m_Optimizer).algoTypeTipText();
}
/**
* @return the treeBranchDeg
*/
public int getTreeBranchDegree() {
return ((ParticleSwarmOptimization)this.m_Optimizer).getTreeBranchDegree();
}
/**
* @param treeBranchDeg the treeBranchDeg to set
*/
public void setTreeBranchDegree(int treeBranchDeg) {
((ParticleSwarmOptimization)this.m_Optimizer).setTreeBranchDegree(treeBranchDeg);
}
public String treeBranchDegreeTipText() {
return ((ParticleSwarmOptimization)this.m_Optimizer).treeBranchDegreeTipText();
}
// /**
// * @return the treeBranchDeg
// */
// public int getTreeBranchDegree() {
// return ((ParticleSwarmOptimization)this.m_Optimizer).getTreeBranchDegree();
// }
//
// /**
// * @param treeBranchDeg the treeBranchDeg to set
// */
// public void setTreeBranchDegree(int treeBranchDeg) {
// ((ParticleSwarmOptimization)this.m_Optimizer).setTreeBranchDegree(treeBranchDeg);
// }
//
// public String treeBranchDegreeTipText() {
// return ((ParticleSwarmOptimization)this.m_Optimizer).treeBranchDegreeTipText();
// }
/**
* @return the wrapTopology

View File

@ -290,12 +290,14 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
else if (terminator instanceof EvaluationTerminator)
args = new Object[] {optimizer, optimizer.getPopulation(), optimizer.getPopulation().getFunctionCalls(), ((EvaluationTerminator)terminator).getFitnessCalls()};
// ((InterfaceParameterControl)paramCtrl).updateParameters(optimizer, optimizer.getPopulation().getFunctionCalls(), ((EvaluationTerminator)terminator).getFitnessCalls());
else args = new Object[]{optimizer, optimizer.getPopulation()};
else args = null;//new Object[]{optimizer, optimizer.getPopulation()};
// ((InterfaceParameterControl)paramCtrl).updateParameters(optimizer);
iterateParamCtrl(optimizer, "updateParameters", args);
args[0]=goParams.getProblem();
iterateParamCtrl(goParams.getProblem(), "updateParameters", args);
if (args != null) { // only if iteration counting is available
iterateParamCtrl(optimizer, "updateParameters", args);
args[0]=goParams.getProblem();
iterateParamCtrl(goParams.getProblem(), "updateParameters", args);
}
}
/**

View File

@ -13,9 +13,11 @@ import eva2.gui.BeanInspector;
import eva2.server.go.IndividualInterface;
import eva2.server.go.PopulationInterface;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.operators.distancemetric.InterfaceDistanceMetric;
import eva2.server.go.populations.Population;
import eva2.server.go.problems.InterfaceAdditionalPopulationInformer;
import eva2.tools.Mathematics;
import eva2.tools.Pair;
/**
* An abstract class handling statistics. Most important stuff happens in startOptPerformed, stopOptPerformed
@ -42,7 +44,7 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
*/
private boolean refineMultiRuns = true;
private ArrayList<double[][]> meanCollection;
private Double[] additionalInfoSums = null, lastAdditionalInfoSums=null;
// say whether the object should be written to a file every time
private boolean saveParams = true;
@ -67,13 +69,15 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
// protected double[] meanBestOfRunFitness;
protected double avgPopDist;
protected double maxPopDist;
protected IndividualInterface bestCurrentIndividual, bestRunIndividual, bestRunFeasibleIndy, bestFeasibleAllover, bestIndividualAllover;
protected IndividualInterface bestCurrentIndy, bestOfRunIndy, bestOfRunFeasibleIndy, bestFeasibleAllRuns, bestIndyAllRuns;
// collect feasible results of a run
private ArrayList<IndividualInterface> runBestFeasibleList;
private ArrayList<IndividualInterface> runBestFitList;
private ArrayList<InterfaceTextListener> textListeners;
private List<InterfaceAdditionalPopulationInformer> lastInformerList = null;
private PopulationInterface lastPop = null;
public AbstractStatistics() {
firstPlot = true;
@ -99,7 +103,7 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
* @param infoString
*/
protected void initOutput(String infoString) {
SimpleDateFormat formatter = new SimpleDateFormat("E'_'yyyy.MM.dd'_at_'hh.mm.ss");
SimpleDateFormat formatter = new SimpleDateFormat("E'_'yyyy.MM.dd'_at_'HH.mm.ss");
String startDate = formatter.format(new Date());
// open the result file:
if (doFileOutput() // not "text-window only"
@ -148,22 +152,26 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
convergenceCnt = 0;
if (saveParams) m_StatsParams.saveInstance();
initOutput(infoString);
bestIndividualAllover = null;
bestFeasibleAllover = null;
bestIndyAllRuns = null;
bestFeasibleAllRuns = null;
// meanBestOfRunFitness = null;
// meanBestFeasibleFit = null;
runBestFeasibleList = new ArrayList<IndividualInterface>();
runBestFitList = new ArrayList<IndividualInterface>();
if (refineMultiRuns) meanCollection = new ArrayList<double[][]>();
else meanCollection = null;
additionalInfoSums = null;
lastAdditionalInfoSums = null;
feasibleFoundAfterSum=-1;
numOfRunsFeasibleFound=0;
}
feasibleFoundAfter=-1;
bestCurrentIndividual = null;
bestRunIndividual = null;
bestCurrentIndy = null;
bestOfRunIndy = null;
currentBestFeasibleFit=null;
bestRunFeasibleIndy = null;
bestOfRunFeasibleIndy = null;
lastInformerList = null;
lastPop = null;
runIterCnt = 0;
if (printRunIntroVerbosity()) printToTextListener("\n****** Multirun "+runNumber);
if (params != null) {
@ -187,15 +195,15 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
if (printRunStoppedVerbosity() && (stopMessage != null)) printToTextListener(" Termination message: " + stopMessage + "\n");
if (printRunStoppedVerbosity()) printToTextListener(" Function calls run: " + functionCalls + ", sum: " + functionCallSum + "\n");
// check for convergence
if (bestCurrentIndividual != null) {
if (Mathematics.norm(bestCurrentIndividual.getFitness()) < this.m_StatsParams.getConvergenceRateThreshold()) {
if (bestCurrentIndy != null) {
if (Mathematics.norm(bestCurrentIndy.getFitness()) < this.m_StatsParams.getConvergenceRateThreshold()) {
convergenceCnt++;
}
if (printRunStoppedVerbosity()) printIndy("Last best", bestCurrentIndividual);
if (printRunStoppedVerbosity()) printIndy("Last best", bestCurrentIndy);
}
if (bestRunIndividual != null) {
runBestFitList.add(bestRunIndividual);
if (printRunStoppedVerbosity()) printIndy("Run best", bestRunIndividual);
if (bestOfRunIndy != null) {
runBestFitList.add(bestOfRunIndy);
if (printRunStoppedVerbosity()) printIndy("Run best", bestOfRunIndy);
// if (meanBestOfRunFitness==null) {
// meanBestOfRunFitness=bestRunIndividual.getFitness().clone();
// } else addSecond(meanBestOfRunFitness, bestRunIndividual.getFitness());
@ -205,31 +213,46 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
} else {
if (printRunStoppedVerbosity()) printToTextListener(" NO feasible individual found.\n");
}
if (bestRunFeasibleIndy != null) {
runBestFeasibleList.add(bestRunFeasibleIndy);
if (bestOfRunFeasibleIndy != null) {
runBestFeasibleList.add(bestOfRunFeasibleIndy);
// if (meanBestFeasibleFit==null) {
// meanBestFeasibleFit=bestRunFeasibleIndy.getFitness().clone();
// } else addSecond(meanBestFeasibleFit, bestRunFeasibleIndy.getFitness());
if (printRunStoppedVerbosity()) {
if ((bestRunFeasibleIndy instanceof AbstractEAIndividual) && ((AbstractEAIndividual)bestRunFeasibleIndy).equalGenotypes((AbstractEAIndividual)bestRunIndividual)) {
if ((bestOfRunFeasibleIndy instanceof AbstractEAIndividual) && ((AbstractEAIndividual)bestOfRunFeasibleIndy).equalGenotypes((AbstractEAIndividual)bestOfRunIndy)) {
printToTextListener("* Run best feasible individual equals best individual.\n");
} else {
if (bestRunIndividual instanceof AbstractEAIndividual) {
if (((AbstractEAIndividual)bestRunIndividual).violatesConstraint())
printToTextListener(" Run best individual violates constraints by " + ((AbstractEAIndividual)bestRunIndividual).getConstraintViolation() + "\n");
if (((AbstractEAIndividual)bestRunIndividual).isMarkedPenalized())
if (bestOfRunIndy instanceof AbstractEAIndividual) {
if (((AbstractEAIndividual)bestOfRunIndy).violatesConstraint())
printToTextListener(" Run best individual violates constraints by " + ((AbstractEAIndividual)bestOfRunIndy).getConstraintViolation() + "\n");
if (((AbstractEAIndividual)bestOfRunIndy).isMarkedPenalized())
printToTextListener(" Run best individual is penalized.\n");
}
printIndy("Run best feasible", bestRunFeasibleIndy);
printIndy("Run best feasible", bestOfRunFeasibleIndy);
}
}
}
if (printFinalVerbosity()) printToTextListener(".");
if (m_StatsParams.isOutputAdditionalInfo()) updateLastAdditionalInfo();
// if (currentBestFit!= null) {
// if (printRunStoppedVerbosity()) printToTextListener(" Best Fitness: " + BeanInspector.toString(currentBestFit) + "\n");
// }
if (optRunsPerformed == m_StatsParams.getMultiRuns()) finalizeOutput();
if (optRunsPerformed >= m_StatsParams.getMultiRuns()) {
if (printFinalVerbosity()) printToTextListener("\n");
finalizeOutput();
}
}
private PopulationInterface makeStatsPop() {
Population pop = new Population(4);
if (bestCurrentIndy!=null) pop.add(bestCurrentIndy);
if (bestOfRunIndy!=null) pop.add(bestOfRunIndy);
if (bestOfRunFeasibleIndy!=null) pop.add(bestOfRunFeasibleIndy);
if (bestIndyAllRuns!=null) pop.add(bestIndyAllRuns);
return pop;
}
private void printIndy(String prefix, IndividualInterface indy) {
printToTextListener("* " + prefix + " ind.: " + BeanInspector.toString(indy) + '\n');
printToTextListener(" solution data : " + AbstractEAIndividual.getDefaultDataString(indy) + '\n');
@ -251,7 +274,17 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
printToTextListener(" Average evaluations until feasible ind. was found in " + numOfRunsFeasibleFound + " runs: " + feasibleFoundAfterSum/numOfRunsFeasibleFound + " evaluations\n");
}
if (printFinalVerbosity() && (bestIndividualAllover != null)) printIndy("Overall best", bestIndividualAllover);
if (printFinalVerbosity() && (additionalInfoSums != null)) {
printToTextListener(" Averaged additional info: ");
for (int i=0; i<additionalInfoSums.length; i++) if (additionalInfoSums[i]!=null) printToTextListener(" \t"+(additionalInfoSums[i]/optRunsPerformed));
printToTextListener("\n Averaged last additional info: ");
for (int i=0; i<lastAdditionalInfoSums.length; i++) if (lastAdditionalInfoSums[i]!=null) printToTextListener(" \t"+(lastAdditionalInfoSums[i]/optRunsPerformed));
printToTextListener("\n");
}
if (printFinalVerbosity() && (bestIndyAllRuns != null)) printIndy("Overall best", bestIndyAllRuns);
if (printFinalVerbosity() && (m_StatsParams.isOutputAdditionalInfo())) printToTextListener(getFinalAdditionalInfo()+'\n');
if (optRunsPerformed>1) {
if (runBestFitList.size()>0) {
// Mathematics.svDiv((double)optRunsPerformed, meanBestOfRunFitness, meanBestOfRunFitness);
@ -262,7 +295,7 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
printToTextListener(" MultiRun stats: Median best fitn.: " + BeanInspector.toString(calcMedianFit(runBestFitList))+"\n");
}
}
if (printFinalVerbosity() && (bestFeasibleAllover != null)) printIndy("Overall best feasible", bestFeasibleAllover);
if (printFinalVerbosity() && (bestFeasibleAllRuns != null)) printIndy("Overall best feasible", bestFeasibleAllRuns);
// if ((runBestFeasibleList.size()>0) && (!equalLists(runBestFeasibleList, runBestFitList))) { // is there a difference between best feasibles and best fit?
if (runBestFeasibleList.size()>0) { // always output feasible stats even if theyre equal
if (printFinalVerbosity()) {
@ -290,6 +323,15 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
}
}
private String getFinalAdditionalInfo() {
PopulationInterface bestPop = makeStatsPop();
StringBuffer sbuf = new StringBuffer("Overall best additional data: "+ getAdditionalInfoHeader(lastInformerList, bestPop));
sbuf.append('\n');
appendAdditionalInfo(lastInformerList, bestPop, sbuf);
// getOutputLine(lastInformerList, makeStatsPop());
return sbuf.toString();
}
/**
* Perform a deep equals test on the fitness vectors of both individual lists.
* @param l1
@ -402,15 +444,21 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
if ((informerList == null) || !m_StatsParams.isOutputAdditionalInfo()) {
return headline;
} else {
for (InterfaceAdditionalPopulationInformer informer : informerList) {
headline = headline + "\t " + informer.getAdditionalFileStringHeader(pop);
}
return headline;
return headline + getAdditionalInfoHeader(informerList, pop);
}
}
protected String getOutputLine(List<InterfaceAdditionalPopulationInformer> informerList, PopulationInterface pop) {
protected String getAdditionalInfoHeader(List<InterfaceAdditionalPopulationInformer> informerList, PopulationInterface pop) {
String hdr="";
for (InterfaceAdditionalPopulationInformer informer : informerList) {
hdr = hdr + "\t " + informer.getAdditionalFileStringHeader(pop);
}
return hdr;
}
protected Pair<String,Double[]> getOutputLine(List<InterfaceAdditionalPopulationInformer> informerList, PopulationInterface pop) {
StringBuffer sbuf = new StringBuffer(Integer.toString(functionCalls));
Double[] addNums = null;
sbuf.append(" \t ");
sbuf.append(BeanInspector.toString(currentBestFit));
if (meanFitness != null) {
@ -421,15 +469,54 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
sbuf.append(" \t ");
sbuf.append(BeanInspector.toString(currentWorstFit));
} else sbuf.append(" # \t");
if (informerList != null && m_StatsParams.isOutputAdditionalInfo()) {
for (InterfaceAdditionalPopulationInformer informer : informerList) {
sbuf.append(" \t ");
sbuf.append(informer.getAdditionalFileStringValue(pop));
}
}
return sbuf.toString();
if (m_StatsParams.isOutputAdditionalInfo()) addNums = appendAdditionalInfo(informerList, pop, sbuf);
return new Pair<String,Double[]>(sbuf.toString(),addNums);
}
/**
* Append additional informer informations to the given StringBuffer.
*
* @param informerList
* @param pop
* @param sbuf
*/
protected Double[] appendAdditionalInfo(List<InterfaceAdditionalPopulationInformer> informerList, PopulationInterface pop, StringBuffer sbuf) {
if (informerList != null) {
StringBuffer addBuffer = new StringBuffer();
for (InterfaceAdditionalPopulationInformer informer : informerList) {
addBuffer.append(" \t ");
addBuffer.append(informer.getAdditionalFileStringValue(pop));
}
String addInfo = addBuffer.toString();
Double[] retVals = parseDoubles(addInfo, "\t");
if (sbuf!=null) sbuf.append(addInfo);
return retVals;
}
return null;
}
/**
* Parse Double from a String separated by the given regular expression.
* For Substrings which do not convert to Double by Double.parseDouble(String),
* a null value is added as representative.
*
* @param str
* @param colSplit
* @return
*/
public static Double[] parseDoubles(String str, String splitRegExp) {
ArrayList<Double> vals = new ArrayList<Double>();
String[] entries = str.split(splitRegExp);
for (int i=0; i<entries.length; i++) {
Double d = null;
try {
d = Double.parseDouble(entries[i]);
} catch(Exception e) { }
vals.add(d); // null if unsuccessfull
}
return (Double[])vals.toArray(new Double[vals.size()]);
}
/**
* @deprecated The method {@link #createNextGenerationPerformed(PopulationInterface, List)} should be used instead.
*/
@ -448,11 +535,52 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
}
if ((runIterCnt == 0) && printHeaderByVerbosity()) printToTextListener(getOutputHeader(null, null)+'\n');
if (doTextOutput() && printLineByVerbosity(calls)) printToTextListener(getOutputLine(null, null)+'\n');
if (doTextOutput() && printLineByVerbosity(calls)) {
Pair<String,Double[]> addInfo = getOutputLine(null, null);
printToTextListener(addInfo.head()+'\n');
if (addInfo.tail()!=null) {
additionalInfoSums = updateAdditionalInfo(additionalInfoSums, addInfo.tail());
}
}
plotCurrentResults();
runIterCnt++;
}
/**
* Add the given array to the member array. Do some checks etc.
* If a resultSum array is provided, it is used to add the info and returned. Otherwise
* a new array is allocated.
*
* @param curInfo
*/
private Double[] updateAdditionalInfo(Double[] resultSum, Double[] curInfo) {
if (resultSum==null) {
resultSum = curInfo.clone();
} else {
if (curInfo.length != resultSum.length) {
System.err.println("Error in AbstractStatistics.updateAdditionalInfo: mismatching info arrays!");
} else {
for (int i=0; i<curInfo.length; i++) {
if (resultSum[i]==null || (curInfo[i]==null)) resultSum[i]=null;
else resultSum[i]+=curInfo[i];
}
}
}
return resultSum;
}
/**
* Re-request the last additional information from the lastInfomerList and update the
* Double value sums.
*
* @param pop
*/
private void updateLastAdditionalInfo() {
// TODO Auto-generated method stub
Double[] lastVals = appendAdditionalInfo(lastInformerList, lastPop, null);
lastAdditionalInfoSums = updateAdditionalInfo(lastAdditionalInfoSums, lastVals);
}
/**
* If the population returns a specific data array, this method is called instead of doing standard output
* @param pop
@ -473,6 +601,8 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
*/
public synchronized void createNextGenerationPerformed(PopulationInterface
pop, List<InterfaceAdditionalPopulationInformer> informerList) {
lastInformerList = informerList;
lastPop = pop;
if (firstPlot) {
initPlots(m_StatsParams.getPlotDescriptions());
// if (doTextOutput()) printToTextListener(getOutputHeader(informer, pop)+'\n');
@ -486,20 +616,20 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
return;
}
// by default plotting only the best
bestCurrentIndividual = pop.getBestIndividual().getClone();
if ((bestIndividualAllover == null) || (secondIsBetter(bestIndividualAllover, bestCurrentIndividual))) {
bestIndividualAllover = bestCurrentIndividual;
bestCurrentIndy = pop.getBestIndividual().getClone();
if ((bestIndyAllRuns == null) || (secondIsBetter(bestIndyAllRuns, bestCurrentIndy))) {
bestIndyAllRuns = bestCurrentIndy;
// printToTextListener("new best found!, last was " + BeanInspector.toString(bestIndividualAllover) + "\n");
}
if ((bestRunIndividual==null) || (secondIsBetter(bestRunIndividual, bestCurrentIndividual))) {
bestRunIndividual=bestCurrentIndividual;
if ((bestOfRunIndy==null) || (secondIsBetter(bestOfRunIndy, bestCurrentIndy))) {
bestOfRunIndy=bestCurrentIndy;
}
// IndividualInterface WorstInd = Pop.getWorstIndividual();
if (bestCurrentIndividual == null) {
if (bestCurrentIndy == null) {
System.err.println("createNextGenerationPerformed BestInd==null");
}
currentBestFit = bestCurrentIndividual.getFitness().clone();
currentBestFit = bestCurrentIndy.getFitness().clone();
if (currentBestFit == null) {
System.err.println("BestFitness==null !");
}
@ -513,12 +643,12 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
feasibleFoundAfterSum+=feasibleFoundAfter;
}
currentBestFeasibleFit = curBestFeasible.getFitness().clone();
if ((bestRunFeasibleIndy==null) || (secondIsBetter(bestRunFeasibleIndy, curBestFeasible))) {
bestRunFeasibleIndy=(AbstractEAIndividual)curBestFeasible.clone();
if ((bestOfRunFeasibleIndy==null) || (secondIsBetter(bestOfRunFeasibleIndy, curBestFeasible))) {
bestOfRunFeasibleIndy=(AbstractEAIndividual)curBestFeasible.clone();
// System.out.println("New best feasible: " + AbstractEAIndividual.getDefaultStringRepresentation((AbstractEAIndividual)bestRunFeasibleIndy));
}
if ((bestFeasibleAllover == null) || (secondIsBetter(bestFeasibleAllover, bestRunFeasibleIndy))) {
bestFeasibleAllover = bestRunFeasibleIndy;
if ((bestFeasibleAllRuns == null) || (secondIsBetter(bestFeasibleAllRuns, bestOfRunFeasibleIndy))) {
bestFeasibleAllRuns = bestOfRunFeasibleIndy;
}
}
} else System.err.println("INVALID POPULATION (AbstractStatistics)");
@ -528,7 +658,7 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
functionCalls = pop.getFunctionCalls();
if (GraphSelectionEnum.doPlotAvgDist(m_StatsParams.getGraphSelection())
|| GraphSelectionEnum.doPlotMaxPopDist(m_StatsParams.getGraphSelection())) {
double[] measures = pop.getPopulationMeasures();
double[] measures = ((Population)pop).getPopulationMeasures((InterfaceDistanceMetric)null);
if (measures != null) {
avgPopDist = measures[0];
maxPopDist = measures[2];
@ -554,12 +684,26 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
}
// meanCollection.set(pop.getGenerations()-1, means);
if (doTextOutput() && printLineByVerbosity(runIterCnt)) printToTextListener(getOutputLine(informerList, pop)+'\n');
if (doTextOutput()) {
Pair<String,Double[]> addInfo = getOutputLine(informerList, pop);
if (printLineByVerbosity(runIterCnt)) printToTextListener(addInfo.head()+'\n');
// updateAdditionalInfo(addInfo.tail());
if (addInfo.tail()!=null) {
additionalInfoSums = updateAdditionalInfo(additionalInfoSums, addInfo.tail());
}
}
plotCurrentResults();
runIterCnt++;
}
/**
* Returns true if the given iteration is a verbose one according to StatsParameter - meaning
* that full iteration data should be plotted.
*
* @param iteration
* @return
*/
private boolean printLineByVerbosity(int iteration) {
return (m_StatsParams.getOutputVerbosity().getSelectedTagID() > StatsParameter.VERBOSITY_KTH_IT)
|| ((m_StatsParams.getOutputVerbosity().getSelectedTagID() == StatsParameter.VERBOSITY_KTH_IT)
@ -567,7 +711,8 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
}
private boolean printRunIntroVerbosity() {
return (m_StatsParams.getOutputVerbosity().getSelectedTagID() >= StatsParameter.VERBOSITY_KTH_IT);
return (m_StatsParams.getOutputVerbosity().getSelectedTagID() >= StatsParameter.VERBOSITY_KTH_IT)
|| (optRunsPerformed==0 && (m_StatsParams.getOutputVerbosity().getSelectedTagID() >= StatsParameter.VERBOSITY_FINAL));
}
private boolean printRunStoppedVerbosity() {
@ -622,11 +767,11 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
}
public IndividualInterface getBestSolution() {
return bestIndividualAllover;
return bestIndyAllRuns;
}
public IndividualInterface getRunBestSolution() {
return bestRunIndividual;
return bestOfRunIndy;
}
public int getFitnessCalls() {

View File

@ -60,17 +60,16 @@ public class StatisticsStandalone extends AbstractStatistics implements Interfac
}
public StatisticsStandalone(String resultFileName) {
this(StatsParameter.getInstance());
m_StatsParams.SetResultFilePrefix(resultFileName);
m_StatsParams.setOutputTo(m_StatsParams.getOutputTo().setSelectedTag(StatsParameter.OUTPUT_FILE));
this(resultFileName, 1, resultFileName==null ? StatsParameter.VERBOSITY_NONE : StatsParameter.VERBOSITY_FINAL, false);
}
public StatisticsStandalone(String resultFileName, int multiRuns, int verbosity) {
this(StatsParameter.getInstance());
public StatisticsStandalone(String resultFileName, int multiRuns, int verbosity, boolean showAdditionalInfo) {
this(StatsParameter.getInstance(false));
m_StatsParams.setMultiRuns(multiRuns);
m_StatsParams.setOutputVerbosity(m_StatsParams.getOutputVerbosity().setSelectedTag(verbosity));
m_StatsParams.SetResultFilePrefix(resultFileName);
m_StatsParams.setOutputTo(m_StatsParams.getOutputTo().setSelectedTag(StatsParameter.OUTPUT_FILE));
if (resultFileName==null) m_StatsParams.getOutputTo().setSelectedTag(StatsParameter.OUTPUT_WINDOW);
else m_StatsParams.setOutputTo(m_StatsParams.getOutputTo().setSelectedTag(StatsParameter.OUTPUT_FILE));
}
public StatisticsStandalone() {
@ -120,9 +119,9 @@ public class StatisticsStandalone extends AbstractStatistics implements Interfac
public void stopOptPerformed(boolean normal, String stopMessage) {
super.stopOptPerformed(normal, stopMessage);
if (bestCurrentIndividual != null) {
m_SumOfBestFit = m_SumOfBestFit + bestCurrentIndividual.getFitness()[0];
m_BestFitnessAtEnd[optRunsPerformed-1] = bestCurrentIndividual.getFitness()[0];
if (bestCurrentIndy != null) {
m_SumOfBestFit = m_SumOfBestFit + bestCurrentIndy.getFitness()[0];
m_BestFitnessAtEnd[optRunsPerformed-1] = bestCurrentIndy.getFitness()[0];
}
//System.out.println("stopOptPerformed :"+m_OptRunsPerformed);

View File

@ -86,11 +86,11 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl
if ((Client == null) || Client.getHostName().equals(m_MyHostName)) {
m_StatsParams = StatsParameter.getInstance();
m_StatsParams = StatsParameter.getInstance(true);
m_ProxyPrinter = new JTextoutputFrame("TextOutput of " + m_MyHostName);
} else { // we use RMI
m_StatsParams = (InterfaceStatisticsParameter)RMIProxyLocal.newInstance(
StatsParameter.getInstance());
StatsParameter.getInstance(true));
m_ProxyPrinter = (JTextoutputFrameInterface) RMIProxyRemote.newInstance(new
JTextoutputFrame("TextOutput " + m_MyHostName),
m_MainAdapterClient);
@ -246,7 +246,7 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl
int subGraph=0;
if (doPlotCurrentBest) plotFitnessPoint(0, subGraph++, functionCalls, currentBestFit[0]);
if (doPlotRunBest) plotFitnessPoint(0, subGraph++, functionCalls, bestRunIndividual.getFitness()[0]);
if (doPlotRunBest) plotFitnessPoint(0, subGraph++, functionCalls, bestOfRunIndy.getFitness()[0]);
if (doPlotWorst) {// schlechteste Fitness plotten
if (currentWorstFit == null) {
@ -261,8 +261,8 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl
if (doPlotCurBestFeasible && currentBestFeasibleFit!=null) {
plotFitnessPoint(0, subGraph++, functionCalls, currentBestFeasibleFit[0]);
}
if (doPlotRunBestFeasible && bestRunFeasibleIndy!=null) {
plotFitnessPoint(0, subGraph++, functionCalls, bestRunFeasibleIndy.getFitness()[0]);
if (doPlotRunBestFeasible && bestOfRunFeasibleIndy!=null) {
plotFitnessPoint(0, subGraph++, functionCalls, bestOfRunFeasibleIndy.getFitness()[0]);
}
}
}

View File

@ -64,9 +64,17 @@ public class StatsParameter implements InterfaceStatisticsParameter, Serializabl
/**
*
*/
public static StatsParameter getInstance() {
public static StatsParameter getInstance(boolean loadDefaultSerFile) {
if (loadDefaultSerFile) return getInstance("Statistics.ser");
else return new StatsParameter();
}
/**
* Try to load instance from serialized file. If impossible, instantiate a new one.
*/
public static StatsParameter getInstance(String serFileName) {
if (TRACE ) System.out.println("Loading serialized stats..");
StatsParameter Instance = (StatsParameter) Serializer.loadObject("Statistics.ser");
StatsParameter Instance = (StatsParameter) Serializer.loadObject(serFileName);
if (Instance == null) {
Instance = new StatsParameter();
if (TRACE) System.out.println("Loading failed!");
@ -79,7 +87,7 @@ public class StatsParameter implements InterfaceStatisticsParameter, Serializabl
*/
public StatsParameter() {
m_Name = "Statistics";
outputVerbosity.setSelectedTag(2);
outputVerbosity.setSelectedTag(VERBOSITY_KTH_IT);
outputTo.setSelectedTag(1);
}

View File

@ -36,9 +36,13 @@ public class Mathematics {
if (cloneX) in = (double[]) x.clone();
else in = x;
Arrays.sort(in);
if (in.length % 2 != 0) return in[(in.length-1) / 2];
else return (in[in.length/2] + in[(in.length/2)+1]) / 2.;
if (in.length==1) return in[0];
else if (in.length==2) return (in[0]+in[1])/2.;
else {
Arrays.sort(in);
if (in.length % 2 != 0) return in[(in.length-1) / 2];
else return (in[in.length/2] + in[(in.length/2)+1]) / 2.;
}
}
/**
@ -892,6 +896,24 @@ public class Mathematics {
return viols;
}
/**
* Scale a range by the given factor, meaning that the interval in each dimension is
* extended (fact>1) or reduced (fact<1) by the defined ratio around the center.
*
* @param rangeScaleFact
* @param range
*/
public static void scaleRange(double rangeScaleFact, double[][] range) {
double[] intervalLengths=Mathematics.shiftRange(range);
double[] tmpInts=Mathematics.svMult(rangeScaleFact, intervalLengths);
Mathematics.vvSub(tmpInts, intervalLengths, tmpInts); // this is what must be added to range interval
for (int i=0; i<range.length; i++) {
range[i][0]-=tmpInts[i]/2;
range[i][1]+=tmpInts[i]/2;
}
}
/**
* Project the value to the range given.
*
@ -933,7 +955,7 @@ public class Mathematics {
double d = 0.;
for (int i=0; i<x.length; i++) {
double dimLen = range[i][1]-range[i][0];
if (dimLen <= 0.) System.err.println("Error in reflectBounds: empty range!");
if (dimLen <= 0.) EVAERROR.errorMsgOnce("Error in reflectBounds: empty range! (possibly multiple errors)");
if (x[i]<range[i][0]) {
viols++;
d = range[i][0]-x[i];
@ -999,4 +1021,18 @@ public class Mathematics {
}
return prod;
}
/**
* Intersect two ranges resulting in the maximum range contained in both.
*
* @param modRange
* @param makeRange
* @param destRange
*/
public static void intersectRange(double[][] r1, double[][] r2, double[][] destRange) {
for (int i=0; i<r1.length && i<r2.length; i++) {
destRange[i][0] = Math.max(r1[i][0], r2[i][0]);
destRange[i][1] = Math.min(r1[i][1], r2[i][1]);
}
}
}

View File

@ -0,0 +1,40 @@
package eva2.tools;
import java.util.Comparator;
import eva2.gui.BeanInspector;
public class PairComparator implements Comparator<Pair<?,?>> {
boolean useHead = true;
/**
* A constructor. Set useHd to true to compare based on the head, otherwise
* based on the tail.
*/
public PairComparator(boolean useHd) {
useHead = useHead;
}
/**
* Compare two Pairs of which head or tail is a primitive type that
* can be converted to double.
* Return 1 if the first is larger, -1 if the second is larger, 0 if they
* are equal or not comparable.
*/
public int compare(Pair<?,?> o1, Pair<?,?> o2) {
Pair<?,?> p1=(Pair<?,?>)o1;
Pair<?,?> p2=(Pair<?,?>)o2;
double d1, d2;
try {
d1=BeanInspector.toDouble(useHead ? p1.head() : p1.tail());
d2=BeanInspector.toDouble(useHead ? p2.head() : p2.tail());
} catch (IllegalArgumentException e) {
System.err.println("Error, mismatching types, thus uncomparable Pairs: " + p1.toString() + " / " + p2.toString());
return 0;
}
if (d1==d2) return 0;
else if (d1 > d2) return 1;
else return -1;
}
}

View File

@ -311,6 +311,7 @@ public class DMeasures implements Serializable
}
}
catch( IllegalArgumentException nde ){ return null; }
catch( NullPointerException npe) {return null;}
return new SlimRect( x1, y1, x2 - x1, y2 - y1 );
}
// SlimRect getSourceOf( double xpos, double ypos, double width, double height){

View File

@ -114,16 +114,22 @@ public class Matrix implements Cloneable, java.io.Serializable {
*/
public Matrix (double[][] A) {
m = A.length;
n = A[0].length;
for (int i = 0; i < m; i++) {
if (A[i].length != n) {
throw new IllegalArgumentException("All rows must have the same length.");
}
}
this.A = A;
this(A, true);
}
public Matrix (double[][] A, boolean checkDims) {
m = A.length;
n = A[0].length;
if (checkDims) {
for (int i = 0; i < m; i++) {
if (A[i].length != n) {
throw new IllegalArgumentException("All rows must have the same length.");
}
}
}
this.A = A;
}
/** Construct a matrix quickly without checking arguments.
@param A Two-dimensional array of doubles.
@param m Number of rows.
@ -660,7 +666,24 @@ public class Matrix implements Cloneable, java.io.Serializable {
}
return this;
}
/** A = A + B
@param B another matrix
@return A + B
*/
public Matrix plusEqualsArrayAsMatrix(double[][] B) {
if ((B.length!=m) || (B[0].length!=n)) {
throw new IllegalArgumentException("Matrix dimensions must agree.");
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = A[i][j] + B[i][j];
}
}
return this;
}
/** C = A - B
@param B another matrix
@return A - B
@ -805,6 +828,19 @@ public class Matrix implements Cloneable, java.io.Serializable {
return X;
}
/** Multiply a matrix in place by a scalar, A = s*A. Returns A.
* @param s scalar
* @return s*A
*/
public Matrix timesInplace(double s) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = s*A[i][j];
}
}
return this;
}
/** Multiply a matrix by a vector, returning A*v.
*
@param v vector

View File

@ -8,395 +8,422 @@ import eva2.tools.EVAHELP;
import eva2.tools.Mathematics;
public class RNG extends Random {
private static Random random;
private static long randomSeed;
/**
*
*/
static {
randomSeed=System.currentTimeMillis();
random=new Random(randomSeed);
}
/**
*
*/
public static void setRandomSeed(long new_seed){
//counter++;
randomSeed=new_seed;
if (randomSeed == 0) setRandomSeed();
else random.setSeed(randomSeed);
}
/**
* Set the random seed without replacing zero with current system time.
*/
public static void setRandomSeedStrict(long new_seed){
randomSeed=new_seed;
random.setSeed(randomSeed);
}
/**
*
*/
public static void setRandomSeed() {
randomSeed=System.currentTimeMillis();
random=new Random(randomSeed);
}
/**
*
*/
public static void setRandom(Random base_random) {
random=base_random;
}
/**
*
*/
public static long getRandomSeed() {
return randomSeed;
}
/**
* Returns 0 or 1 evenly distributed.
*/
public static int randomInt() {
return randomInt(0,1);
}
/**
* Returns an evenly distributes int value between zero and
* upperLim-1.
* @param upperLim upper exclusive limit of the random int
*/
public static int randomInt(int upperLim) {
return randomInt(0,upperLim-1);
}
/** This method returns a evenly distributed int value.
* The boundarys are included.
* @param lo Lower bound.
* @param hi Upper bound.
* @return int
*/
public static int randomInt(int lo,int hi) {
if (hi<lo) {
System.err.println("Invalid boundary values! Returning zero.");
return -1;
private static Random random;
private static long randomSeed;
/**
*
*/
static {
randomSeed=System.currentTimeMillis();
random=new Random(randomSeed);
}
int result = (Math.abs(random.nextInt())%(hi-lo+1))+lo;
if ((result < lo) || (result > hi)) {
System.err.println("Error in RNG.randomInt!");
result = Math.abs(random.nextInt()%(hi-lo+1))+lo;
}
return result;
}
/** This method returns a random permutation of n int values
* @param length The number of int values
* @return The permutation [0-length-1]
*/
public static int[] randomPermutation(int length) {
boolean[] validList = new boolean[length];
int[] result = new int[length];
int index;
for (int i = 0; i < validList.length; i++) validList[i] = true;
for (int i = 0; i < result.length; i++) {
index = randomInt(0, length-1);
while (!validList[index]) {
index++;
if (index == length) index = 0;
}
validList[index] = false;
result[i] = index;
}
return result;
}
/** This method returns a random permutation of n int values
* @param length The number of int values
* @return The permutation [0-length-1]
*/
public static int[] randomPerm(int length) {
ArrayList<Integer> intList = new ArrayList<Integer>(length);
int[] result = new int[length];
for (int i = 0; i < length; i++) {
intList.add(new Integer(i));
}
for (int i = 0; i < length-1; i++) {
int index = randomInt(intList.size());
result[i] = intList.get(index);
intList.remove(index);
}
if (intList.size()>1) System.err.println("Error in randomPerm!");
result[length-1] = intList.get(0);
return result;
}
/**
*
*/
public static long randomLong() {
return randomLong(0,1);
}
/**
*
*/
public static long randomLong(long lo,long hi) {
return (Math.abs(random.nextLong())%(hi-lo+1))+lo;
}
/**
*
*/
public static float randomFloat() {
return random.nextFloat();
}
/**
*
*/
public static float randomFloat(float lo,float hi) {
return (hi-lo)*random.nextFloat()+lo;
}
/**
* A random double value between 0 and 1.
*/
public static double randomDouble() {
return random.nextDouble();
}
/**
*
*/
public static double randomDouble(double lo,double hi) {
return (hi-lo)*random.nextDouble()+lo;
}
/**
* Create a uniform random vector within the given bounds.
*/
public static double[] randomDoubleArray(double[] lo,double[] hi) {
double[] xin = new double[lo.length];
for (int i=0;i<lo.length;i++)
xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i];
return xin;
}
/**
* Create a uniform random vector within the given bounds.
*/
public static double[] randomDoubleArray(double[][] range) {
double[] xin = new double[range.length];
for (int i=0;i<xin.length;i++)
xin[i] = (range[i][1]-range[i][0])*random.nextDouble()+range[i][0];
return xin;
}
/**
* Create a uniform random double vector within the given bounds (inclusive) in every dimension.
*
* @param lower
* @param upper
* @param size
* @return
*/
public static double[] randomDoubleArray(double lower, double upper, int size) {
double[] result = new double[size];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomDouble(lower, upper);
/**
*
*/
public static void setRandomSeed(long new_seed){
//counter++;
randomSeed=new_seed;
if (randomSeed == 0) setRandomSeed();
else random = new Random(randomSeed);
}
/**
* Set the random seed without replacing zero with current system time.
*/
public static void setRandomSeedStrict(long new_seed){
randomSeed=new_seed;
random.setSeed(randomSeed);
}
/**
*
*/
public static void setRandomSeed() {
randomSeed=System.currentTimeMillis();
random=new Random(randomSeed);
}
return result;
// double[] xin = new double[size];
// for (int i=0;i<size;i++)
// xin[i] = (hi-lo)*random.nextDouble()+lo;
// return xin;
}
/**
*
*/
public static double[] randomDoubleArray(double[] lo,double[] hi,double[] xin) {
//counter++;
for (int i=0;i<lo.length;i++)
xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i];
return xin;
}
/**
* Create a uniform random integer vector within the given bounds (inclusive) in every dimension.
*
* @param n
* @param lower
* @param upper
* @return
*/
public static int[] randomIntArray(int lower, int upper, int size) {
int[] result = new int[size];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomInt(lower, upper);
/**
*
*/
public static void setRandom(Random base_random) {
random=base_random;
}
return result;
}
/**
*
*/
public static boolean randomBoolean() {
//counter++;
return (randomInt()==1);
}
/**
*
*/
public static int randomBit() {
//counter++;
return randomInt();
}
/**
* Returns true with probability p.
*
* @param p
* @return true with probability p, else false
*/
public static boolean flipCoin(double p) {
//counter++;
return (randomDouble()<p ? true : false);
}
/**
*
*/
public static float gaussianFloat(float dev) {
//counter++;
return (float)random.nextGaussian()*dev;
}
/**
* Return a Gaussian double with mean 0 and deviation dev.
*
* @param dev the deviation of the distribution.
* @return a Gaussian double with mean 0 and given deviation.
*/
public static double gaussianDouble(double dev) {
//counter++;
return random.nextGaussian()*dev;
}
/**
*
*/
public static float exponentialFloat(float mean) {
//counter++;
return (float)(-mean*Math.log(randomDouble()));
}
/**
*
*/
public static double exponentialDouble(double mean) {
//counter++;
return -mean*Math.log(randomDouble());
}
/**
* Returns a vector denoting a random point around the center
* - inside a hypersphere of uniform distribution if nonUnif=0,
* - inside a hypersphere of non-uniform distribution if nonUnif > 0,
* - inside a D-Gaussian if nonUnif < 0.
* For case 2, the nonUnif parameter is used as standard deviation (instead of 1/D), the parameter
* is not further used in the other two cases.
* Original code by Maurice Clerc, from the TRIBES package
*
* @param center center point of the distribution
* @param radius radius of the distribution
* @param nonUnif kind of distribution
*
**/
public static double[] randHypersphere(double[] center, double radius, double nonUnif) {
double[] x = new double[center.length];
int j;
double xLen, r;
int D=center.length;
// ----------------------------------- Step 1. Direction
xLen = 0;
for (j=0; j<D; j++) {
r = gaussianDouble(1);
x[j] = r;
xLen += x[j]*x[j];
/**
*
*/
public static long getRandomSeed() {
return randomSeed;
}
/**
* Returns 0 or 1 evenly distributed.
*/
public static int randomInt() {
return randomInt(0,1);
}
xLen=Math.sqrt(xLen);
//----------------------------------- Step 2. Random radius
r=randomDouble();
if (nonUnif < 0) r = gaussianDouble(r/2); // D-Gaussian
else if (nonUnif > 0) r = Math.pow(r,nonUnif); // non-uniform hypersphere
else r=Math.pow(r,1./D); // Real hypersphere
for (j=0;j<D;j++) {
x[j] = center[j]+radius*r*x[j]/xLen;
/**
* Returns an evenly distributes int value between zero and
* upperLim-1.
* @param upperLim upper exclusive limit of the random int
*/
public static int randomInt(int upperLim) {
return randomInt(0,upperLim-1);
}
/** This method returns a evenly distributed int value.
* The boundarys are included.
* @param lo Lower bound.
* @param hi Upper bound.
* @return int
*/
public static int randomInt(int lo,int hi) {
if (hi<lo) {
System.err.println("Invalid boundary values! Returning zero.");
return -1;
}
int result = (Math.abs(random.nextInt())%(hi-lo+1))+lo;
if ((result < lo) || (result > hi)) {
System.err.println("Error, invalid value " + result + " in RNG.randomInt! boundaries were lo/hi: " + lo + " / " + hi);
result = Math.abs(random.nextInt()%(hi-lo+1))+lo;
}
return result;
}
return x;
}
/**
* Adds Gaussian noise to a double vector
* @param v the double vector
* @param dev the Gaussian deviation
*/
public static void addNoise(double[] v, double dev) {
for (int i=0; i<v.length; i++) {
// add noise to the value
v[i] += gaussianDouble(dev);
}
}
/**
* Create a normalized random vector with gaussian random double entries.
*
* @param n
* @param dev
* @return
*/
public static double[] gaussianVector(int n, double dev, boolean normalize) {
double[] result = new double[n];
gaussianVector(dev, result, normalize);
return result;
}
/**
* Create a normalized random vector with gaussian random double entries.
*
* @param n
* @return
*/
public static double[] gaussianVector(double dev, double[] result, boolean normalize) {
for (int i = 0; i < result.length; i++) {
result[i] = RNG.gaussianDouble(dev);
}
if (normalize) Mathematics.normVect(result, result);
return result;
}
public static void main(String[] args) {
double[] v = new double[2];
for (int i=0; i<1000; i++) {
gaussianVector(1., v, false);
EVAHELP.logString(Arrays.toString(v)+"\n", "randtest.dat");
// System.out.println(Arrays.toString(v));
}
}
/**
* Create a uniform random double vector within the given bounds (inclusive) in every dimension.
*
* @param n
* @param lower
* @param upper
* @return
*/
// public static double[] randomVector(int n, double lower, double upper) {
// double[] result = new double[n];
// for (int i = 0; i < result.length; i++) {
// result[i] = RNG.randomDouble(lower, upper);
// }
// return result;
// }
}
/** This method returns a random permutation of n int values
* @param length The number of int values
* @return The permutation [0-length-1]
*/
public static int[] randomPermutation(int length) {
boolean[] validList = new boolean[length];
int[] result = new int[length];
int index;
for (int i = 0; i < validList.length; i++) validList[i] = true;
for (int i = 0; i < result.length; i++) {
index = randomInt(0, length-1);
while (!validList[index]) {
index++;
if (index == length) index = 0;
}
validList[index] = false;
result[i] = index;
}
return result;
}
/** This method returns a random permutation of n int values
* @param length The number of int values
* @return The permutation [0-length-1]
*/
public static int[] randomPerm(int length) {
ArrayList<Integer> intList = new ArrayList<Integer>(length);
int[] result = new int[length];
for (int i = 0; i < length; i++) {
intList.add(new Integer(i));
}
for (int i = 0; i < length-1; i++) {
int index = randomInt(intList.size());
result[i] = intList.get(index);
intList.remove(index);
}
if (intList.size()>1) System.err.println("Error in randomPerm!");
result[length-1] = intList.get(0);
return result;
}
/**
* Returns a random long between 0 and Long.MAX_VALUE-1 (inclusively).
*/
public static long randomLong() {
return randomLong(0,Long.MAX_VALUE-1);
}
/**
* Returns a random long between the given values (inclusively).
*/
public static long randomLong(long lo,long hi) {
return (Math.abs(random.nextLong())%(hi-lo+1))+lo;
}
/**
*
*/
public static float randomFloat() {
return random.nextFloat();
}
/**
*
*/
public static float randomFloat(float lo,float hi) {
return (hi-lo)*random.nextFloat()+lo;
}
/**
* A random double value between 0 and 1.
*/
public static double randomDouble() {
return random.nextDouble();
}
/**
*
*/
public static double randomDouble(double lo,double hi) {
return (hi-lo)*random.nextDouble()+lo;
}
/**
* Create a uniform random vector within the given bounds.
*/
public static double[] randomDoubleArray(double[] lo,double[] hi) {
double[] xin = new double[lo.length];
for (int i=0;i<lo.length;i++)
xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i];
return xin;
}
/**
* Create a uniform random vector within the given bounds.
*/
public static double[] randomDoubleArray(double[][] range) {
double[] xin = new double[range.length];
for (int i=0;i<xin.length;i++)
xin[i] = (range[i][1]-range[i][0])*random.nextDouble()+range[i][0];
return xin;
}
/**
* Create a uniform random double vector within the given bounds (inclusive) in every dimension.
*
* @param lower
* @param upper
* @param size
* @return
*/
public static double[] randomDoubleArray(double lower, double upper, int size) {
double[] result = new double[size];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomDouble(lower, upper);
}
return result;
// double[] xin = new double[size];
// for (int i=0;i<size;i++)
// xin[i] = (hi-lo)*random.nextDouble()+lo;
// return xin;
}
/**
*
*/
public static double[] randomDoubleArray(double[] lo,double[] hi,double[] xin) {
//counter++;
for (int i=0;i<lo.length;i++)
xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i];
return xin;
}
/**
* Create a uniform random integer vector within the given bounds (inclusive) in every dimension.
*
* @param n
* @param lower
* @param upper
* @return
*/
public static int[] randomIntArray(int lower, int upper, int size) {
int[] result = new int[size];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomInt(lower, upper);
}
return result;
}
/**
*
*/
public static boolean randomBoolean() {
//counter++;
return (randomInt()==1);
}
/**
*
*/
public static int randomBit() {
//counter++;
return randomInt();
}
/**
* Returns true with probability p.
*
* @param p
* @return true with probability p, else false
*/
public static boolean flipCoin(double p) {
//counter++;
return (randomDouble()<p ? true : false);
}
/**
*
*/
public static float gaussianFloat(float dev) {
//counter++;
return (float)random.nextGaussian()*dev;
}
/**
* Return a Gaussian double with mean 0 and deviation dev.
*
* @param dev the deviation of the distribution.
* @return a Gaussian double with mean 0 and given deviation.
*/
public static double gaussianDouble(double dev) {
//counter++;
return random.nextGaussian()*dev;
}
/**
*
*/
public static float exponentialFloat(float mean) {
//counter++;
return (float)(-mean*Math.log(randomDouble()));
}
/**
*
*/
public static double exponentialDouble(double mean) {
//counter++;
return -mean*Math.log(randomDouble());
}
/**
* Returns a vector denoting a random point around the center
* - inside a hypersphere of uniform distribution if nonUnif=0,
* - inside a hypersphere of non-uniform distribution if nonUnif > 0,
* - inside a D-Gaussian if nonUnif < 0.
* For case 2, the nonUnif parameter is used as standard deviation (instead of 1/D), the parameter
* is not further used in the other two cases.
* Original code by Maurice Clerc, from the TRIBES package
*
* @param center center point of the distribution
* @param radius radius of the distribution
* @param nonUnif kind of distribution
*
**/
public static double[] randHypersphere(double[] center, double radius, double nonUnif) {
double[] x = new double[center.length];
int j;
double xLen, r;
int D=center.length;
// ----------------------------------- Step 1. Direction
xLen = 0;
for (j=0; j<D; j++) {
r = gaussianDouble(1);
x[j] = r;
xLen += x[j]*x[j];
}
xLen=Math.sqrt(xLen);
//----------------------------------- Step 2. Random radius
r=randomDouble();
if (nonUnif < 0) r = gaussianDouble(r/2); // D-Gaussian
else if (nonUnif > 0) r = Math.pow(r,nonUnif); // non-uniform hypersphere
else r=Math.pow(r,1./D); // Real hypersphere
for (j=0;j<D;j++) {
x[j] = center[j]+radius*r*x[j]/xLen;
}
return x;
}
/**
* Adds Gaussian noise to a double vector
* @param v the double vector
* @param dev the Gaussian deviation
*/
public static void addNoise(double[] v, double dev) {
for (int i=0; i<v.length; i++) {
// add noise to the value
v[i] += gaussianDouble(dev);
}
}
/**
* Create a normalized random vector with gaussian random double entries.
*
* @param n
* @param dev
* @return
*/
public static double[] gaussianVector(int n, double dev, boolean normalize) {
double[] result = new double[n];
gaussianVector(dev, result, normalize);
return result;
}
/**
* Create a normalized random vector with gaussian random double entries.
*
* @param n
* @return
*/
public static double[] gaussianVector(double dev, double[] result, boolean normalize) {
for (int i = 0; i < result.length; i++) {
result[i] = RNG.gaussianDouble(dev);
}
if (normalize) Mathematics.normVect(result, result);
return result;
}
// public static int testRndInt(long seed, int bits) {
// return (int)(seed >>> (48 - bits));
// }
//
// public static int testRandomInt(int lo, int hi, long seed) {
// if (hi<lo) {
// System.err.println("Invalid boundary values! Returning zero.");
// return -1;
// }
// int result = (Math.abs(testRndInt(seed,32))%(hi-lo+1))+lo;
// if ((result < lo) || (result > hi)) {
// System.err.println("Error, invalid value " + result + " in RNG.randomInt! boundaries were lo/hi: " + lo + " / " + hi);
// System.out.println("Error, invalid value " + result + " in RNG.randomInt! boundaries were lo/hi: " + lo + " / " + hi);
// }
// return result;
// }
//
// public static void testRand(long initSeed) {
// for (long seed=initSeed; seed<=Long.MAX_VALUE; seed++) {
// int rnd = testRandomInt(0,8,seed);
// if (seed % 100000000 == 0) System.out.println("Seed at " + seed);
// }
// }
// public static void main(String[] args) {
// testRand(24000000000l);
// System.out.println("RNG Done");
// double[] v = new double[2];
// for (int i=0; i<1000; i++) {
// gaussianVector(1., v, false);
// EVAHELP.logString(Arrays.toString(v)+"\n", "randtest.dat");
//// System.out.println(Arrays.toString(v));
// }
// }
/**
* Create a uniform random double vector within the given bounds (inclusive) in every dimension.
*
* @param n
* @param lower
* @param upper
* @return
*/
// public static double[] randomVector(int n, double lower, double upper) {
// double[] result = new double[n];
// for (int i = 0; i < result.length; i++) {
// result[i] = RNG.randomDouble(lower, upper);
// }
// return result;
// }
}

View File

@ -256,7 +256,7 @@ public class BasicResourceLoader implements ResourceLoader
/**
* Fill a line of an array with double values parsed from a String array. A subset of
* Columns may be selected by giving their indeces in an integer array cols. If cols
* Columns may be selected by giving their indices in an integer array cols. If cols
* is null, all are converted.
*
* @param dest
@ -288,7 +288,8 @@ public class BasicResourceLoader implements ResourceLoader
}
/**
* Test a string for prefixes. If a prefix matches, return its index, else return -1.
* Test a string for prefixes. For the first matching prefix, the index
* of the prefix within the prefix array is returned. If there is no match -1 is returned.
* @param str
* @param pref
* @return
@ -627,23 +628,23 @@ public class BasicResourceLoader implements ResourceLoader
if (bytes != null) {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
prop.load(bais);
if (prop != null) return prop;
}
if (prop != null)
return prop;
/////////////
int slInd = resourceName.lastIndexOf('/');
if (slInd != -1)
resourceName = resourceName.substring(slInd + 1);
String planBResName;
if (slInd != -1) planBResName = resourceName.substring(slInd + 1);
else planBResName = resourceName;
Properties userProps = new Properties();
File propFile = new File(File.separatorChar + "resources" + File.separatorChar + resourceName);
File propFile = new File(File.separatorChar + "resources" + File.separatorChar + planBResName);
if (propFile.exists()) {
try {
userProps.load(new FileInputStream(propFile));
} catch (Exception ex) {
System.out.println("Problem reading user properties: " + propFile);
System.err.println("Problem reading user properties: " + propFile);
}
}
} else System.err.println("Warning in readProperties: neither " + resourceName + " nor " + planBResName + " could be read.");
return userProps;
}

View File

@ -14,419 +14,423 @@
package eva2.tools.tool;
import java.util.ArrayList;
import java.util.Collections;
import eva2.tools.math.Jama.Matrix;
/**
* Statistic utils.
*/
public class StatisticUtils
{
/** The natural logarithm of 2. */
public static double log2 = Math.log(2);
/** The natural logarithm of 2. */
public static double log2 = Math.log(2);
/** The small deviation allowed in double comparisons */
public static double SMALL = 1e-6;
/** The small deviation allowed in double comparisons */
public static double SMALL = 1e-6;
/**
* Returns the correlation coefficient of two double vectors.
*
* @param y1 double vector 1
* @param y2 double vector 2
* @param n the length of two double vectors
* @return the correlation coefficient
*/
public final static double correlation(double y1[],double y2[],int n) {
/**
* Returns the correlation coefficient of two double vectors.
*
* @param y1 double vector 1
* @param y2 double vector 2
* @param n the length of two double vectors
* @return the correlation coefficient
*/
public final static double correlation(double y1[],double y2[],int n) {
int i;
double av1 = 0.0, av2 = 0.0, y11 = 0.0, y22 = 0.0, y12 = 0.0, c;
int i;
double av1 = 0.0, av2 = 0.0, y11 = 0.0, y22 = 0.0, y12 = 0.0, c;
if (n <= 1) {
return 1.0;
}
for (i = 0; i < n; i++) {
av1 += y1[i];
av2 += y2[i];
}
av1 /= (double) n;
av2 /= (double) n;
for (i = 0; i < n; i++) {
y11 += (y1[i] - av1) * (y1[i] - av1);
y22 += (y2[i] - av2) * (y2[i] - av2);
y12 += (y1[i] - av1) * (y2[i] - av2);
}
if (y11 * y22 == 0.0) {
c=1.0;
} else {
c = y12 / Math.sqrt(Math.abs(y11 * y22));
}
if (n <= 1) {
return 1.0;
}
for (i = 0; i < n; i++) {
av1 += y1[i];
av2 += y2[i];
}
av1 /= (double) n;
av2 /= (double) n;
for (i = 0; i < n; i++) {
y11 += (y1[i] - av1) * (y1[i] - av1);
y22 += (y2[i] - av2) * (y2[i] - av2);
y12 += (y1[i] - av1) * (y2[i] - av2);
}
if (y11 * y22 == 0.0) {
c=1.0;
} else {
c = y12 / Math.sqrt(Math.abs(y11 * y22));
}
return c;
}
return c;
}
/**
* Computes differential shannon entropy
*
* @return DSE=SE(AB)-0.5*[SE(A)+SE(B)]
*/
public static double differentialShannon(int counts1[],int counts2[], int n, int countsSum1, int countsSum2)
{
double seA=0.0;
double seB=0.0;
double seAB=0.0;
double c=0.0;
int AB;
int allSum = countsSum1+countsSum2;
for(int i=0;i<n;i++)
{
AB = counts1[i] + counts2[i];
seA -= xlogx(((double)counts1[i])/((double)countsSum1));
seB -= xlogx(((double)counts2[i])/((double)countsSum2));
seAB -= xlogx(((double)AB)/((double)allSum));
}
/**
* Computes differential shannon entropy
*
* @return DSE=SE(AB)-0.5*[SE(A)+SE(B)]
*/
public static double differentialShannon(int counts1[],int counts2[], int n, int countsSum1, int countsSum2)
{
double seA=0.0;
double seB=0.0;
double seAB=0.0;
double c=0.0;
int AB;
int allSum = countsSum1+countsSum2;
for(int i=0;i<n;i++)
{
AB = counts1[i] + counts2[i];
seA -= xlogx(((double)counts1[i])/((double)countsSum1));
seB -= xlogx(((double)counts2[i])/((double)countsSum2));
seAB -= xlogx(((double)AB)/((double)allSum));
}
c= seAB - 0.5*(seA+seB);
c= seAB - 0.5*(seA+seB);
return c;
}
return c;
}
/**
* Computes entropy for an array of integers.
*
* @param counts array of counts
* @return - a log2 a - b log2 b - c log2 c + (a+b+c) log2 (a+b+c)
* when given array [a b c]
*/
public static double info(int counts[]) {
/**
* Computes entropy for an array of integers.
*
* @param counts array of counts
* @return - a log2 a - b log2 b - c log2 c + (a+b+c) log2 (a+b+c)
* when given array [a b c]
*/
public static double info(int counts[]) {
int total = 0;
int c;
double x = 0;
for (int j = 0; j < counts.length; j++) {
x -= xlogx(counts[j]);
total += counts[j];
}
return x + xlogx(total);
}
int total = 0;
int c;
double x = 0;
for (int j = 0; j < counts.length; j++) {
x -= xlogx(counts[j]);
total += counts[j];
}
return x + xlogx(total);
}
/**
* Computes shannon entropy for an array of integers.
*
* @param counts array of counts
* @return - a log2 a - b log2 b - c log2 c
* when given array [a b c]
*/
public static double shannon(int counts[], int countsSum) {
/**
* Computes shannon entropy for an array of integers.
*
* @param counts array of counts
* @return - a log2 a - b log2 b - c log2 c
* when given array [a b c]
*/
public static double shannon(int counts[], int countsSum) {
double x = 0;
for (int j = 0; j < counts.length; j++) {
x -= xlogx( ((double)counts[j])/ ((double)countsSum));
}
return x;
}
double x = 0;
for (int j = 0; j < counts.length; j++) {
x -= xlogx( ((double)counts[j])/ ((double)countsSum));
}
return x;
}
/**
* Returns the logarithm of a for base 2.
*
* @param a a double
*/
public static final double log2(double a) {
/**
* Returns the logarithm of a for base 2.
*
* @param a a double
*/
public static final double log2(double a) {
return Math.log(a) / log2;
}
return Math.log(a) / log2;
}
/**
* Returns index of maximum element in a given
* array of doubles. First maximum is returned.
*
* @param doubles the array of doubles
* @return the index of the maximum element
*/
public static int maxIndex(double [] doubles) {
/**
* Returns index of maximum element in a given
* array of doubles. First maximum is returned.
*
* @param doubles the array of doubles
* @return the index of the maximum element
*/
public static int maxIndex(double [] doubles) {
double maximum = 0;
int maxIndex = 0;
double maximum = 0;
int maxIndex = 0;
for (int i = 0; i < doubles.length; i++) {
if ((i == 0) || (doubles[i] > maximum)) {
maxIndex = i;
maximum = doubles[i];
}
}
for (int i = 0; i < doubles.length; i++) {
if ((i == 0) || (doubles[i] > maximum)) {
maxIndex = i;
maximum = doubles[i];
}
}
return maxIndex;
}
return maxIndex;
}
/**
* Returns index of maximum element in a given
* array of integers. First maximum is returned.
*
* @param ints the array of integers
* @return the index of the maximum element
*/
public static int maxIndex(int [] ints) {
/**
* Returns index of maximum element in a given
* array of integers. First maximum is returned.
*
* @param ints the array of integers
* @return the index of the maximum element
*/
public static int maxIndex(int [] ints) {
int maximum = 0;
int maxIndex = 0;
int maximum = 0;
int maxIndex = 0;
for (int i = 0; i < ints.length; i++) {
if ((i == 0) || (ints[i] > maximum)) {
maxIndex = i;
maximum = ints[i];
}
}
for (int i = 0; i < ints.length; i++) {
if ((i == 0) || (ints[i] > maximum)) {
maxIndex = i;
maximum = ints[i];
}
}
return maxIndex;
}
return maxIndex;
}
/**
* Returns index of minimum element in a given
* array of integers. First minimum is returned.
*
* @param ints the array of integers
* @return the index of the minimum element
*/
public static int minIndex(int [] ints) {
/**
* Returns index of minimum element in a given
* array of integers. First minimum is returned.
*
* @param ints the array of integers
* @return the index of the minimum element
*/
public static int minIndex(int [] ints) {
int minimum = 0;
int minIndex = 0;
int minimum = 0;
int minIndex = 0;
for (int i = 0; i < ints.length; i++) {
if ((i == 0) || (ints[i] < minimum)) {
minIndex = i;
minimum = ints[i];
}
}
for (int i = 0; i < ints.length; i++) {
if ((i == 0) || (ints[i] < minimum)) {
minIndex = i;
minimum = ints[i];
}
}
return minIndex;
}
return minIndex;
}
/**
* Returns index of minimum element in a given
* array of doubles. First minimum is returned.
*
* @param doubles the array of doubles
* @return the index of the minimum element
*/
public static int minIndex(double [] doubles) {
/**
* Returns index of minimum element in a given
* array of doubles. First minimum is returned.
*
* @param doubles the array of doubles
* @return the index of the minimum element
*/
public static int minIndex(double [] doubles) {
double minimum = 0;
int minIndex = 0;
double minimum = 0;
int minIndex = 0;
for (int i = 0; i < doubles.length; i++) {
if ((i == 0) || (doubles[i] < minimum)) {
minIndex = i;
minimum = doubles[i];
}
}
for (int i = 0; i < doubles.length; i++) {
if ((i == 0) || (doubles[i] < minimum)) {
minIndex = i;
minimum = doubles[i];
}
}
return minIndex;
}
return minIndex;
}
/**
* Computes the variance for an array of doubles.
*
* @param vector the array
* @return the variance
*/
public static double variance(double[] vector) {
/**
* Computes the variance for an array of doubles.
*
* @param vector the array
* @return the variance
*/
public static double variance(double[] vector) {
double sum = 0, sumSquared = 0;
double sum = 0, sumSquared = 0;
if (vector.length <= 1) {
return 0;
}
for (int i = 0; i < vector.length; i++) {
sum += vector[i];
sumSquared += (vector[i] * vector[i]);
}
double result = (sumSquared - (sum * sum / (double) vector.length)) /
(double) (vector.length - 1);
if (vector.length <= 1) {
return 0;
}
for (int i = 0; i < vector.length; i++) {
sum += vector[i];
sumSquared += (vector[i] * vector[i]);
}
double result = (sumSquared - (sum * sum / (double) vector.length)) /
(double) (vector.length - 1);
// We don't like negative variance
if (result < 0) {
return 0;
} else {
return result;
}
}
// We don't like negative variance
if (result < 0) {
return 0;
} else {
return result;
}
}
/**
* Returns c*log2(c) for a given integer value c.
*
* @param c an integer value
* @return c*log2(c) (but is careful to return 0 if c is 0)
*/
public static final double xlogx(int c) {
/**
* Returns c*log2(c) for a given integer value c.
*
* @param c an integer value
* @return c*log2(c) (but is careful to return 0 if c is 0)
*/
public static final double xlogx(int c) {
if (c == 0) {
return 0.0;
}
return c * StatisticUtils.log2((double) c);
}
if (c == 0) {
return 0.0;
}
return c * StatisticUtils.log2((double) c);
}
/**
* Returns c*log2(c) for a given value c.
*
* @param c an integer value
* @return c*log2(c) (but is careful to return 0 if c is 0)
*/
public static final double xlogx(double c) {
/**
* Returns c*log2(c) for a given value c.
*
* @param c an integer value
* @return c*log2(c) (but is careful to return 0 if c is 0)
*/
public static final double xlogx(double c) {
if (c == 0) {
return 0.0;
}
return c * StatisticUtils.log2( c);
}
if (c == 0) {
return 0.0;
}
return c * StatisticUtils.log2( c);
}
/**
* Tests if a is equal to b.
*
* @param a a double
* @param b a double
*/
public static final boolean eq(double a, double b){
/**
* Tests if a is equal to b.
*
* @param a a double
* @param b a double
*/
public static final boolean eq(double a, double b){
return (a - b < SMALL) && (b - a < SMALL);
}
return (a - b < SMALL) && (b - a < SMALL);
}
/**
* Tests if a is smaller or equal to b.
*
* @param a a double
* @param b a double
*/
public static final boolean smOrEq(double a,double b) {
/**
* Tests if a is smaller or equal to b.
*
* @param a a double
* @param b a double
*/
public static final boolean smOrEq(double a,double b) {
return (a-b < SMALL);
}
return (a-b < SMALL);
}
/**
* Tests if a is greater or equal to b.
*
* @param a a double
* @param b a double
*/
public static final boolean grOrEq(double a,double b) {
/**
* Tests if a is greater or equal to b.
*
* @param a a double
* @param b a double
*/
public static final boolean grOrEq(double a,double b) {
return (b-a < SMALL);
}
return (b-a < SMALL);
}
/**
* Tests if a is smaller than b.
*
* @param a a double
* @param b a double
*/
public static final boolean sm(double a,double b) {
/**
* Tests if a is smaller than b.
*
* @param a a double
* @param b a double
*/
public static final boolean sm(double a,double b) {
return (b-a > SMALL);
}
return (b-a > SMALL);
}
/**
* Tests if a is greater than b.
*
* @param a a double
* @param b a double
*/
public static final boolean gr(double a,double b) {
/**
* Tests if a is greater than b.
*
* @param a a double
* @param b a double
*/
public static final boolean gr(double a,double b) {
return (a-b > SMALL);
}
return (a-b > SMALL);
}
/**
* returns root mean square error.
*/
public static final double rmsError(double array1[], double array2[])
{
if ((array1 == null) || (array2 == null)) { return -1.0; }
/**
* returns root mean square error.
*/
public static final double rmsError(double array1[], double array2[])
{
if ((array1 == null) || (array2 == null)) { return -1.0; }
double errorValueRMS = 0;
for (int i=0; i<array1.length; i++)
{
// add squared error value
errorValueRMS += (array1[i] - array2[i]) * (array1[i] - array2[i]);
}
// calculate mean and root of the sum of the squared error values
errorValueRMS = Math.sqrt(errorValueRMS / (double) array1.length);
double errorValueRMS = 0;
for (int i=0; i<array1.length; i++)
{
// add squared error value
errorValueRMS += (array1[i] - array2[i]) * (array1[i] - array2[i]);
}
// calculate mean and root of the sum of the squared error values
errorValueRMS = Math.sqrt(errorValueRMS / (double) array1.length);
return errorValueRMS;
}
return errorValueRMS;
}
/**
* Returns the correlation coefficient r^2.
*
* Correlation ("Statistik", 7 Aufl., Hartung, 1989, Kapitel 9 und 10, S.546-608):
* a=yMess[i];
* b=yWahr[i];
* aa=a*a;
* bb=b*b;
* ab=a*b;
* numerator=sumAB-(sumA*sumB/n);
* denominator=sqrt[(sumAA-(sumA*sumA/n))*(sumBB-(sumB*sumB/n))];
* R=correlationcoefficient=numerator/denominator;
*
* @author Joerg K. Wegner
*/
public static double getCorrelationCoefficient(double array1[], double array2[])
{
if ((array1 == null) || (array2 == null)) { return -2.0; }
/**
* Returns the correlation coefficient r^2.
*
* Correlation ("Statistik", 7 Aufl., Hartung, 1989, Kapitel 9 und 10, S.546-608):
* a=yMess[i];
* b=yWahr[i];
* aa=a*a;
* bb=b*b;
* ab=a*b;
* numerator=sumAB-(sumA*sumB/n);
* denominator=sqrt[(sumAA-(sumA*sumA/n))*(sumBB-(sumB*sumB/n))];
* R=correlationcoefficient=numerator/denominator;
*
* @author Joerg K. Wegner
*/
public static double getCorrelationCoefficient(double array1[], double array2[])
{
if ((array1 == null) || (array2 == null)) { return -2.0; }
double sumA = 0;
double sumB = 0;
double sumAB = 0;
double sumAA = 0;
double sumBB = 0;
double sumA = 0;
double sumB = 0;
double sumAB = 0;
double sumAA = 0;
double sumBB = 0;
for (int i=0; i<array1.length; i++)
{
double a = array1[i];
double b = array2[i];
for (int i=0; i<array1.length; i++)
{
double a = array1[i];
double b = array2[i];
sumA += a;
sumB += b;
sumAA += a*a;
sumBB += b*b;
sumAB += a*b;
}
sumA += a;
sumB += b;
sumAA += a*a;
sumBB += b*b;
sumAB += a*b;
}
double n = (double) array1.length;
double numerator = sumAB - (sumA*sumB/n);
double denominator = Math.sqrt((sumAA - (sumA*sumA/n)) * (sumBB - (sumB*sumB/n)));
double corrCoefficient = numerator / denominator;
corrCoefficient *= corrCoefficient;
double n = (double) array1.length;
double numerator = sumAB - (sumA*sumB/n);
double denominator = Math.sqrt((sumAA - (sumA*sumA/n)) * (sumBB - (sumB*sumB/n)));
double corrCoefficient = numerator / denominator;
corrCoefficient *= corrCoefficient;
return corrCoefficient;
}
return corrCoefficient;
}
/**
* Main method for testing this class.
*/
public static void main(String[] ops) {
/**
* Main method for testing this class.
*/
public static void main(String[] ops) {
// System.out.println("test (0.5, 100): " +
// StatisticUtils.test(100));
}
// System.out.println("test (0.5, 100): " +
// StatisticUtils.test(100));
}
// The following methods got mysteriously lost maybe during cvs-svn refactoring.
// For the time being I add method thunks which give a warning when called. (mkron)
public static double quadratic_entropy(double[] ds) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
return 0;
}
// The following methods got mysteriously lost maybe during cvs-svn refactoring.
// For the time being I add method thunks which give a warning when called. (mkron)
public static double quadratic_entropy(double[] ds) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
return 0;
}
public static double mutual_information(double[] ds, double[] ds2, int nbins) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
return 0;
}
public static double mutual_information(double[] ds, double[] ds2, int nbins) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
return 0;
}
public static double quadratic_mutinf(double[] feature, double[] labels) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
return 0;
}
public static double quadratic_mutinf(double[] feature, double[] labels, int[] classes) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
@ -444,4 +448,148 @@ public class StatisticUtils
System.err.println("warning, not implemented!");
return 0;
}
/**
* Random Latin Hypercube Sampling within a given double range.
*
* @see #rlh(int, int, double, double, boolean)
*
* @param samples
* @param range
* @param edges
* @return
*/
public static Matrix rlh( int samples, double[][] range, boolean edges) {
Matrix rlhM = rlh(samples, range.length, 0., 1., edges);
for( int j=0; j<samples; ++j ) {
for( int i=0; i<range.length;++i ) {
// carsten hatte nen bug im RLH - zählweise der indices beginnt bei 0, nicht bei 1
rlhM.set(j, i, range[i][0]+rlhM.get(j,i)*(range[i][1]-range[i][0]));
}
}
return rlhM;
}
/**
* Random Latin Hypercube Sampling, from "Engineering Design via Surrogate Modelling", p.17.
* The returned matrix is of dimension Matrix(samples,dim). If edges is true, the boundary values
* are included. Initial version by C. Henneges.
*
* @param samples
* @param dim
* @param lb
* @param ub
* @param edges
* @return
*/
public static Matrix rlh( int samples, int dim, double lb, double ub, boolean edges) {
// System.out.println( "Latin Hypercube sampling" );
// ++rlhsamplings;
// perform Latin-Hypercube-Sampling to obtain a stratified sample of
// function values over the optimization domain
// Pre-allocate memory
Matrix X = new Matrix( samples,dim );
ArrayList<Integer> indices = new ArrayList<Integer>( samples );
for( int i=0; i<samples; ++i ) { indices.add( i ); }
// for i = 1:k
for( int i=0; i<dim; ++i ) {
// X(:,i) = randperm(n)';
Collections.shuffle( indices );
for( int j=0; j<indices.size(); ++j ) {
X.set( j,i,indices.get(j) );
}
// end
}
// if edges==1
if( edges ) {
// X = (X-1)/(n-1);
for( int i=0; i<X.getRowDimension(); ++i ) {
for( int j=0; j<X.getColumnDimension(); ++j ) {
double v = X.get(i,j);
v = (v)/(samples-1);
X.set( i,j,v );
}
}
// else
} else {
// X = (X-0.5)/n;
for( int i=0; i<X.getRowDimension(); ++i ) {
for( int j=0; j<X.getColumnDimension(); ++j ) {
double v = X.get(i,j);
v = (v + 0.5)/samples;
X.set( i,j,v );
}
}
}
// end
// ------
// Transform and Scale random values to [lb,ub]
for( int i=0; i<X.getRowDimension(); ++i ) {
for( int j=0; j<X.getColumnDimension(); ++j ) {
double v = X.get(i,j);
v = (ub-lb)*v + lb;
X.set( i,j,v );
}
}
return X;
}
/**
* Returns a list of point matrices which form a latin hypercube sampling of the given space.
* Each entry is a Matrix(dim,1).
*
* @param samples
* @param dim
* @param lb
* @param ub
* @param edges
* @return
*/
public static ArrayList<Matrix> rlhPoints(int samples, int dim, double lb, double ub, boolean edges)
{
ArrayList<Matrix> samplePoints = new ArrayList<Matrix>(samples);
Matrix p = rlh( samples,dim,lb,ub,edges );
for( int i=0; i<samples;++i ) {
Matrix x = new Matrix(dim,1);
for( int j=0; j<dim; ++j ) {
x.set( j,0,p.get( i,j ) );
}
samplePoints.add( x );
}
return samplePoints;
}
/**
* Returns a list of point matrices which form a latin hypercube sampling of the given space.
* @param samples
* @param dim
* @param lb
* @param ub
* @param edges
* @return
*/
public static ArrayList<Matrix> rlhPoints(int samples, double[][] range, boolean edges)
{
ArrayList<Matrix> rlhM = rlhPoints(samples, range.length, 0, 1, edges);
for( int i=0; i<rlhM.size();++i ) {
Matrix p = rlhM.get(i);
for( int j=0; j<range.length; ++j ) {
// carsten hatte nen bug im RLH - zählweise der indices beginnt bei 0, nicht bei 1
p.set(j, 0, range[j][0]+p.get(j,0)*(range[j][1]-range[j][0]));
}
}
return rlhM;
}
}