From 5c40d4a72e3380c73b89e8123eae8d667dc0a43d Mon Sep 17 00:00:00 2001 From: Marcel Kronfeld Date: Wed, 4 May 2011 11:15:41 +0000 Subject: [PATCH] Updates to problem classes --- .../AbstractMultiModalProblemKnown.java | 18 +++-- .../problems/AbstractOptimizationProblem.java | 65 +++++++++++++++---- .../go/problems/AbstractProblemDouble.java | 33 ++++------ src/eva2/server/go/problems/B1Problem.java | 3 +- 4 files changed, 81 insertions(+), 38 deletions(-) diff --git a/src/eva2/server/go/problems/AbstractMultiModalProblemKnown.java b/src/eva2/server/go/problems/AbstractMultiModalProblemKnown.java index 08551b63..df887dc0 100644 --- a/src/eva2/server/go/problems/AbstractMultiModalProblemKnown.java +++ b/src/eva2/server/go/problems/AbstractMultiModalProblemKnown.java @@ -14,7 +14,8 @@ import eva2.tools.EVAERROR; import eva2.tools.ToolBox; import eva2.tools.math.Mathematics; -public abstract class AbstractMultiModalProblemKnown extends AbstractProblemDouble implements Interface2DBorderProblem, InterfaceMultimodalProblemKnown { +public abstract class AbstractMultiModalProblemKnown extends AbstractProblemDouble +implements Interface2DBorderProblem, InterfaceMultimodalProblemKnown { protected static InterfaceDistanceMetric m_Metric = new PhenotypeMetric(); private double m_GlobalOpt = 0; protected Population m_ListOfOptima; @@ -126,6 +127,11 @@ public abstract class AbstractMultiModalProblemKnown extends AbstractProblemDoub return ToolBox.appendArrays(new String[]{"numOptsFound", "maxPeakRatio"}, super.getAdditionalDataHeader()); } + @Override + public String[] getAdditionalDataInfo() { + return ToolBox.appendArrays(new String[]{"The number of optima identified with default accuracy", "The maximum peak ratio measure in [0,1], best at 1, if multiple local optima are known."}, super.getAdditionalDataInfo()); + } + @Override public Object[] getAdditionalDataValue(PopulationInterface pop) { Object[] result = new Object[2]; @@ -333,7 +339,9 @@ public abstract class AbstractMultiModalProblemKnown extends AbstractProblemDoub /** * Returns -1 if the full list is not available. Otherwise calculates the maximum peak ratio * based on the full list of known optima. Assumes that both realOpts and pop have fitness - * values assigned as in a maximization problem. This is the standard formulation of MPR. + * values assigned as in a minimization problem. Using a fitness value fitThreshold, they are inverted + * and then treated as in maximization. An optimum which has not been found closely (by epsilon) + * in the population is assumed to be covered with an individual of fitness fitThreshold. * * @param mmProb * @param pop @@ -348,13 +356,15 @@ public abstract class AbstractMultiModalProblemKnown extends AbstractProblemDoub for (int i=0; ifitThreshold) System.err.println("Warning: The fitness threshold to turn minimization fitness values into " + + if (realFits[i]>fitThreshold) EVAERROR.errorMsgOnce("Warning: The fitness threshold to turn minimization fitness values into " + "maximization values should be larger than any optimal fitness! (AbstractMultiModalProblemKnown)"); if (i==0 || (minOpt>realFits[i])) minOpt = realFits[i]; // check if the opt. was found and store the corr. found fitness if (optsFound[i]!=null) { foundFits[i] = new Double(optsFound[i].getFitness(fitCrit)); - } else foundFits[i]=fitThreshold; // note that it wasnt found -- will result in zero + } else { + foundFits[i]=fitThreshold; // note that it wasnt found -- will result in zero + } } // now we mirror all values with the threshold - provided they are below the threshold... for (int i=0; i changeListeners = null; - private double defaultAccuracy = 0.01; // default accuracy for identifying optima. + private double defaultAccuracy = 0.001; // default accuracy for identifying optima. /** This method returns a deep clone of the problem. * @return the clone @@ -252,8 +253,39 @@ implements InterfaceOptimizationProblem /*, InterfaceParamControllable*/, Serial * @return String */ public String[] getAdditionalDataHeader() { - if (this instanceof InterfaceInterestingHistogram) return new String[]{STAT_SOLUTION_HEADER,"histogram","score"}; - else return new String[]{STAT_SOLUTION_HEADER}; + String[] header = null; + if (this instanceof InterfaceInterestingHistogram) header = new String[]{STAT_SOLUTION_HEADER,"histogram","score"}; + else header = new String[]{STAT_SOLUTION_HEADER}; + + header = (String[])checkAndAppendAdd(0, getIndividualTemplate(), header, null); + header = (String[])checkAndAppendAdd(0, getIndividualTemplate().getCrossoverOperator(), header, null); + header = (String[])checkAndAppendAdd(0, getIndividualTemplate().getMutationOperator(), header, null); + return header; + } + + /** + * Generic method to append additional information of another object. + * + * @param type indicate header (0), info (1), or value (2) + * @param o the object to retrieve data from + * @param dat the data array to which to append to + * @param pop the current population + * @return + */ + private static Object[] checkAndAppendAdd(int type, Object o, Object[] dat, PopulationInterface pop) { + if (o instanceof InterfaceAdditionalPopulationInformer) { + switch(type) { + case 0: // header + return ToolBox.appendArrays((String[])dat, (String[])((InterfaceAdditionalPopulationInformer)o).getAdditionalDataHeader()); + case 1: // info + return ToolBox.appendArrays((String[])dat, (String[])((InterfaceAdditionalPopulationInformer)o).getAdditionalDataInfo()); + case 2: // value + return ToolBox.appendArrays(dat, ((InterfaceAdditionalPopulationInformer)o).getAdditionalDataValue(pop)); + default: + System.err.println("Error, invalid type in AbstractOptimizationProblem.appendAdd"); + return dat; + } + } else return dat; } /** This method returns the header for the additional data that is to be written into a file @@ -261,12 +293,17 @@ implements InterfaceOptimizationProblem /*, InterfaceParamControllable*/, Serial * @return String */ public String[] getAdditionalDataInfo() { - if (this instanceof InterfaceInterestingHistogram) { - return new String[]{"Representation of the current best individual", + String[] info=null; + if (this instanceof InterfaceInterestingHistogram) + info = new String[]{"Representation of the current best individual", "Fitness histogram of the current population", "Fitness threshold based score of the current population"}; - } - return new String[]{"Representation of the current best individual"}; + else info = new String[]{"Representation of the current best individual"}; + + info = (String[])checkAndAppendAdd(1, getIndividualTemplate(), info, null); + info = (String[])checkAndAppendAdd(1, getIndividualTemplate().getCrossoverOperator(), info, null); + info = (String[])checkAndAppendAdd(1, getIndividualTemplate().getMutationOperator(), info, null); + return info; } /** This method returns the additional data that is to be written into a file @@ -274,9 +311,8 @@ implements InterfaceOptimizationProblem /*, InterfaceParamControllable*/, Serial * @return String */ public Object[] getAdditionalDataValue(PopulationInterface pop) { - Object solObj; -// solObj = AbstractEAIndividual.getDefaultDataString(pop.getBestIndividual()); - solObj = AbstractEAIndividual.getDefaultDataObject(pop.getBestIndividual()); + String solStr = AbstractEAIndividual.getDefaultDataString(pop.getBestIndividual()); + Object[] vals = null; if (this instanceof InterfaceInterestingHistogram) { int fitCrit=0; SolutionHistogram hist = ((InterfaceInterestingHistogram)this).getHistogram(); @@ -288,8 +324,13 @@ implements InterfaceOptimizationProblem /*, InterfaceParamControllable*/, Serial } Population sols = PostProcess.clusterBestUpdateHistogram((Population)maybeFiltered, this, hist, fitCrit, getDefaultAccuracy()); } - return new Object[]{solObj, hist, hist.getScore()}; - } else return new Object[]{solObj}; + vals = new Object[]{solStr, hist, hist.getScore()}; + } else vals = new Object[]{solStr}; + + vals = checkAndAppendAdd(2, pop.getBestIndividual(), vals, pop); + vals = checkAndAppendAdd(2, ((AbstractEAIndividual)pop.getBestIndividual()).getCrossoverOperator(), vals, pop); + vals = checkAndAppendAdd(2, ((AbstractEAIndividual)pop.getBestIndividual()).getMutationOperator(), vals, pop); + return vals; } /** diff --git a/src/eva2/server/go/problems/AbstractProblemDouble.java b/src/eva2/server/go/problems/AbstractProblemDouble.java index 1fd78dbe..86476db8 100644 --- a/src/eva2/server/go/problems/AbstractProblemDouble.java +++ b/src/eva2/server/go/problems/AbstractProblemDouble.java @@ -142,19 +142,18 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem double[] x; double[] fitness; - x = getEvalArray(individual); - ((InterfaceDataTypeDouble) individual).SetDoublePhenotype(x); - // evaluate the vector - fitness = this.eval(x); - // if indicated, add Gaussian noise - if (m_Noise != 0) - RNG.addNoise(fitness, m_Noise); - // set the fitness - setEvalFitness(individual, x, fitness); - if (isWithConstraints()) { - individual.putData(rawFitKey, individual.getFitness().clone()); - addConstraints(individual, x); - } + x = getEvalArray(individual); + ((InterfaceDataTypeDouble)individual).SetDoublePhenotype(x); + // evaluate the vector + fitness = this.eval(x); + // if indicated, add Gaussian noise + if (m_Noise != 0) RNG.addNoise(fitness, m_Noise); + // set the fitness + setEvalFitness(individual, x, fitness); + if (isWithConstraints()) { + individual.putData(rawFitKey, individual.getFitness().clone()); + addConstraints(individual, x); + } } protected double[] rotateMaybe(double[] x) { @@ -573,10 +572,6 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem return "(De-)Activate constraints for the problem."; } - /* - * (non-Javadoc) - * @see eva2.server.go.problems.AbstractOptimizationProblem#getAdditionalDataHeader() - */ @Override public String[] getAdditionalDataHeader() { String[] superHeader = super.getAdditionalDataHeader(); @@ -587,10 +582,6 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem return superHeader; } - /* - * (non-Javadoc) - * @see eva2.server.go.problems.AbstractOptimizationProblem#getAdditionalDataInfo() - */ @Override public String[] getAdditionalDataInfo() { String[] superInfo = super.getAdditionalDataInfo(); diff --git a/src/eva2/server/go/problems/B1Problem.java b/src/eva2/server/go/problems/B1Problem.java index 83141e74..9a0342ff 100644 --- a/src/eva2/server/go/problems/B1Problem.java +++ b/src/eva2/server/go/problems/B1Problem.java @@ -11,7 +11,8 @@ import eva2.server.go.operators.mutation.MutateGAUniform; import eva2.server.go.strategies.InterfaceOptimizer; /** - * Created by IntelliJ IDEA. + * The minimize bits problem for binary optimization. + * * User: streiche * Date: 21.03.2003 * Time: 13:05:33