From e3f2768a29e3fa8dcf89c2babdd7d8b3d464f5d2 Mon Sep 17 00:00:00 2001 From: Marcel Kronfeld Date: Tue, 15 Jan 2008 13:14:19 +0000 Subject: [PATCH] a few cosmetics --- .../individuals/GAIndividualIntegerData.java | 18 ++--- .../go/operators/mutation/MutateESGlobal.java | 19 ++++-- .../server/go/problems/FM0Problem.java | 48 ++++++++++---- .../go/strategies/ClusterBasedNichingEA.java | 65 +++++++++++++++---- .../server/go/strategies/HillClimbing.java | 2 +- src/javaeva/server/modules/Processor.java | 7 +- src/javaeva/tools/ReflectPackage.java | 14 ++-- 7 files changed, 127 insertions(+), 46 deletions(-) diff --git a/src/javaeva/server/go/individuals/GAIndividualIntegerData.java b/src/javaeva/server/go/individuals/GAIndividualIntegerData.java index 311412ec..1f54fd1c 100644 --- a/src/javaeva/server/go/individuals/GAIndividualIntegerData.java +++ b/src/javaeva/server/go/individuals/GAIndividualIntegerData.java @@ -196,14 +196,16 @@ public class GAIndividualIntegerData extends AbstractEAIndividual implements Int */ public void SetIntegerDataLamarkian(int[] doubleData) { this.SetIntegerData(doubleData); - int[] locus = new int[2]; - locus[0] = 0; - locus[1] = 0; - for (int i = 0; i < doubleData.length; i++) { - locus[0] += locus[1]; - locus[1] = this.m_CodingLenghts[i]; - this.m_IntegerCoding.codeValue(doubleData[i], this.m_Range[i], this.m_Genotype, locus); - } + if (doubleData != null) { + int[] locus = new int[2]; + locus[0] = 0; + locus[1] = 0; + for (int i = 0; i < doubleData.length; i++) { + locus[0] += locus[1]; + locus[1] = this.m_CodingLenghts[i]; + this.m_IntegerCoding.codeValue(doubleData[i], this.m_Range[i], this.m_Genotype, locus); + } + } } /************************************************************************************ diff --git a/src/javaeva/server/go/operators/mutation/MutateESGlobal.java b/src/javaeva/server/go/operators/mutation/MutateESGlobal.java index c2b4bcec..831ddc6a 100644 --- a/src/javaeva/server/go/operators/mutation/MutateESGlobal.java +++ b/src/javaeva/server/go/operators/mutation/MutateESGlobal.java @@ -25,11 +25,12 @@ public class MutateESGlobal implements InterfaceMutation, java.io.Serializable { protected SelectedTag m_CrossoverType; public MutateESGlobal() { - Tag[] tag = new Tag[3]; - tag[0] = new Tag(0, "None"); - tag[1] = new Tag(1, "Intermediate"); - tag[2] = new Tag(2, "Discrete"); - this.m_CrossoverType = new SelectedTag(0, tag); + initTags(); + } + + public MutateESGlobal(double mutationStepSize) { + initTags(); + setMutationStepSize(mutationStepSize); } public MutateESGlobal(MutateESGlobal mutator) { @@ -39,6 +40,14 @@ public class MutateESGlobal implements InterfaceMutation, java.io.Serializable { this.m_CrossoverType = (SelectedTag)mutator.m_CrossoverType.clone(); } + protected void initTags() { + Tag[] tag = new Tag[3]; + tag[0] = new Tag(0, "None"); + tag[1] = new Tag(1, "Intermediate"); + tag[2] = new Tag(2, "Discrete"); + this.m_CrossoverType = new SelectedTag(0, tag); + } + /** This method will enable you to clone a given mutation operator * @return The clone */ diff --git a/src/javaeva/server/go/problems/FM0Problem.java b/src/javaeva/server/go/problems/FM0Problem.java index 1bf64cf0..21ba1488 100644 --- a/src/javaeva/server/go/problems/FM0Problem.java +++ b/src/javaeva/server/go/problems/FM0Problem.java @@ -21,8 +21,7 @@ public class FM0Problem extends F1Problem implements Interface2DBorderProblem, I protected InterfaceDistanceMetric m_Metric = new PhenotypeMetricDoubleData(); protected double m_GlobalOpt = 0; protected Population m_Optima; - protected double m_XEpsilon = 0.05; - protected double m_YEpsilon = 0.05; + protected double m_Epsilon = 0.05; protected boolean m_UseXCrit = true; protected boolean m_UseYCrit = true; protected double[][] m_Range; @@ -64,12 +63,9 @@ public class FM0Problem extends F1Problem implements Interface2DBorderProblem, I this.m_UseTestConstraint = b.m_UseTestConstraint; //FM0Problem this.m_GlobalOpt = b.m_GlobalOpt; - this.m_XEpsilon = b.m_XEpsilon; - this.m_YEpsilon = b.m_YEpsilon; + this.m_Epsilon = b.m_Epsilon; this.m_UseXCrit = b.m_UseXCrit; this.m_UseYCrit = b.m_UseYCrit; - this.m_XEpsilon = b.m_XEpsilon; - this.m_YEpsilon = b.m_YEpsilon; this.m_UseXCrit = b.m_UseXCrit; if (b.m_Metric != null) this.m_Metric = (InterfaceDistanceMetric)((InterfaceDistanceMetric)b.m_Metric).clone(); @@ -187,15 +183,21 @@ public class FM0Problem extends F1Problem implements Interface2DBorderProblem, I * @param y */ protected void add2DOptimum(double x, double y) { - InterfaceDataTypeDouble tmpIndy; - double[] point; - - tmpIndy = (InterfaceDataTypeDouble)((AbstractEAIndividual)this.m_Template).clone(); - point = new double[2]; + double[] point = new double[2]; point[0] = x; point[1] = y; + addOptimum(point); + } + + /** This method allows you to add a 2d optima to the list of optima + * @param x + * @param y + */ + protected void addOptimum(double[] point) { + InterfaceDataTypeDouble tmpIndy; + tmpIndy = (InterfaceDataTypeDouble)((AbstractEAIndividual)this.m_Template).clone(); tmpIndy.SetDoubleDataLamarkian(point); - ((AbstractEAIndividual)tmpIndy).SetFitness(0, this.doEvaluationUnNormalized(point)[0]); + ((AbstractEAIndividual)tmpIndy).SetFitness(this.doEvaluationUnNormalized(point)); this.m_GlobalOpt = Math.max(this.m_GlobalOpt, ((AbstractEAIndividual)tmpIndy).getFitness(0)); this.m_Optima.add(tmpIndy); } @@ -244,7 +246,7 @@ public class FM0Problem extends F1Problem implements Interface2DBorderProblem, I for (int j = 0; j < this.m_Optima.size(); j++) { opt = (AbstractEAIndividual) this.m_Optima.get(j); if (!found[j]) { - if (this.m_Metric.distance(posOpt, opt) < this.m_XEpsilon) found[j] = true; + if (this.m_Metric.distance(posOpt, opt) < this.m_Epsilon) found[j] = true; } } } @@ -271,7 +273,7 @@ public class FM0Problem extends F1Problem implements Interface2DBorderProblem, I for (int j = 0; j < this.m_Optima.size(); j++) { if (!found[j]) { opt = (AbstractEAIndividual) this.m_Optima.get(j); - if (this.m_Metric.distance(posOpt, opt) < this.m_XEpsilon) { + if (this.m_Metric.distance(posOpt, opt) < this.m_Epsilon) { found[j] = true; result += this.m_GlobalOpt - posOpt.getFitness(0); //System.out.println("Found Optimum " + j + ".: " + (this.m_GlobalOpt - posOpt.getFitness(0))); @@ -324,4 +326,22 @@ public class FM0Problem extends F1Problem implements Interface2DBorderProblem, I range[1] = 5; return range; } + + /** + * @return the m_Epsilon + */ + public double getEpsilon() { + return m_Epsilon; + } + + /** + * @param epsilon the m_Epsilon to set + */ + public void setEpsilon(double epsilon) { + m_Epsilon = epsilon; + } + + public String epsilonTipText() { + return "Epsilon criterion indicating whether an optimum was found"; + } } diff --git a/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java b/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java index ebbab00f..0167f7af 100644 --- a/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java +++ b/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java @@ -58,9 +58,10 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis private int m_PopulationSize = 50; private boolean m_Debug = true; - private int m_ShowCycle = 2; - private TopoPlot m_Topology; + private int m_ShowCycle = 10; + transient private TopoPlot m_Topology; private int haltingWindow = 15; + private double muLambdaRatio = 0.5; public ClusterBasedNichingEA() { @@ -284,6 +285,23 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis return true; } + private Population optimizeSpecies(Population species) { + m_Optimizer.setPopulation(species); + if (m_Optimizer instanceof EvolutionStrategies) { + EvolutionStrategies es = (EvolutionStrategies)m_Optimizer; + int mu = (int)(muLambdaRatio*species.size()); + if (mu < 1) mu = 1; + else if (mu >= species.size()) { + if (m_Debug) System.err.println("warning, muLambdaRatio produced mu >= lambda.. reducing to mu=lambda-1"); + mu = species.size() - 1; + } + es.setMyu(mu); + es.setLambda(species.size()); + } + this.m_Optimizer.optimize(); + return m_Optimizer.getPopulation(); + } + public void optimize() { // plot the populations if (this.m_ShowCycle > 0) { @@ -304,9 +322,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis this.m_Undifferentiated.setPopulationSize(this.m_Undifferentiated.size()); if (isActive(m_Undifferentiated)) { this.capMutationRate(this.m_Undifferentiated, 0); // MK TODO this sets mutation rate to 0! why? - this.m_Optimizer.setPopulation(this.m_Undifferentiated); - this.m_Optimizer.optimize(); - this.m_Undifferentiated = this.m_Optimizer.getPopulation(); + m_Undifferentiated = optimizeSpecies(m_Undifferentiated); } else { this.m_Undifferentiated.incrGeneration(); } @@ -352,13 +368,15 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis } else { // actually optimize D_i this.capMutationRate(curSpecies, 0.05); - this.m_Optimizer.setPopulation(curSpecies); - this.m_Optimizer.optimize(); - this.m_Species.set(i, this.m_Optimizer.getPopulation()); + //this.m_Optimizer.setPopulation(curSpecies); + //this.m_Optimizer.optimize(); + //this.m_Species.set(i, this.m_Optimizer.getPopulation()); + this.m_Species.set(i, optimizeSpecies(curSpecies)); curSpecies = ((Population)this.m_Species.get(i)); // reset to expected population, just to be sure } } else { // a single individual species, this element is inactive + if (m_Debug) System.out.println("inactive species not optimized"); } // This is necessary to keep track to the function calls needed this.m_Undifferentiated.SetFunctionCalls(this.m_Undifferentiated.getFunctionCalls() + curSpecies.getFunctionCalls()); @@ -492,9 +510,8 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis } // output the result if (this.m_Debug) System.out.println("-Number of species: " + this.m_Species.size()); - this.m_Population = new Population(); - m_Population.setUseHistory(true); this.m_Population = (Population)this.m_Undifferentiated.clone(); + m_Population.setUseHistory(true); if (this.m_Debug) System.out.println("initing with " + this.m_Undifferentiated.size()); for (int i = 0; i < this.m_Species.size(); i++) { if (this.m_Debug) System.out.println("Adding deme " + i + " with size " + ((Population)this.m_Species.get(i)).size()); @@ -596,7 +613,8 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis * @return The population of current solutions to a given problem. */ public Population getPopulation() { - this.m_Population = (Population)this.m_Undifferentiated.clone(); + this.m_Population = (Population)m_Undifferentiated.clone(); +// m_Population.addPopulation(this.m_Undifferentiated); for (int i = 0; i < this.m_Species.size(); i++) this.m_Population.addPopulation((Population)this.m_Species.get(i)); return this.m_Population; } @@ -669,12 +687,16 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis } public void setOptimizer(InterfaceOptimizer b){ this.m_Optimizer = b; + if (b instanceof EvolutionStrategies) { + EvolutionStrategies es = (EvolutionStrategies)b; + setMuLambdaRatio(es.getMyu()/(double)es.getLambda()); + } } public String optimizerTipText() { return "Choose a population based optimizing technique to use."; } - /** The cluster algorithm on which the species differentation is based + /** The cluster algorithm on which the species differentiation is based * @return The current clustering method */ public InterfaceClustering getDifferentationCA() { @@ -737,4 +759,23 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis public String populationSizeTipText() { return "Determines the size of the initial population."; } + +// /** +// * @return the muLambdaRatio +// */ +// public double getMuLambdaRatio() { +// return muLambdaRatio; +// } + + /** + * This is now set if an ES is set as optimizer. + * @param muLambdaRatio the muLambdaRatio to set + */ + public void setMuLambdaRatio(double muLambdaRatio) { + this.muLambdaRatio = muLambdaRatio; + } + +// public String muLambdaRatioTipText() { +// return "ratio between mu and lambda for a CBN-ES"; +// } } diff --git a/src/javaeva/server/go/strategies/HillClimbing.java b/src/javaeva/server/go/strategies/HillClimbing.java index dc35b72e..a346bbb6 100644 --- a/src/javaeva/server/go/strategies/HillClimbing.java +++ b/src/javaeva/server/go/strategies/HillClimbing.java @@ -83,7 +83,7 @@ public class HillClimbing implements InterfaceOptimizer, java.io.Serializable { if (((AbstractEAIndividual)original.get(i)).isDominatingDebConstraints(((AbstractEAIndividual)this.m_Population.get(i)))) { this.m_Population.remove(i); this.m_Population.add(i, original.get(i)); - } + } // else: mutation improved the individual } this.m_Population.incrGeneration(); // for (int i = 0; i < this.m_Population.size(); i++) { diff --git a/src/javaeva/server/modules/Processor.java b/src/javaeva/server/modules/Processor.java index 64a73fac..06d08a73 100644 --- a/src/javaeva/server/modules/Processor.java +++ b/src/javaeva/server/modules/Processor.java @@ -16,6 +16,7 @@ import javaeva.server.go.PopulationInterface; import javaeva.server.go.individuals.AbstractEAIndividual; import javaeva.server.go.operators.terminators.EvaluationTerminator; import javaeva.server.go.populations.Population; +import javaeva.server.go.problems.InterfaceMultimodalProblem; import javaeva.server.go.strategies.InterfaceOptimizer; import javaeva.server.go.tools.RandomNumberGenerator; import javaeva.server.stat.Statistics; @@ -208,7 +209,11 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo // m_Statistics.createNextGenerationPerformed((PopulationInterface)this.m_ModulParameter.getOptimizer().getPopulation()); // m_ListenerModule.updateProgress(getStatusPercent(m_ModulParameter.getOptimizer().getPopulation(), runCounter, m_Statistics.getStatisticsParameter().getMultiRuns())); // } // end of while (m_doOpt==true) - + if (m_ModulParameter.getProblem() instanceof InterfaceMultimodalProblem) { + // TODO improve this? + InterfaceMultimodalProblem mmProb = (InterfaceMultimodalProblem)m_ModulParameter.getProblem(); + System.out.println("no optima found: " + mmProb.getNumberOfFoundOptima(m_ModulParameter.getOptimizer().getPopulation())); + } m_Statistics.stopOptPerformed(isOptRunning()); // stop is "normal" if opt wasnt set false by the user } setOptRunning(false); // normal finish diff --git a/src/javaeva/tools/ReflectPackage.java b/src/javaeva/tools/ReflectPackage.java index dabc852a..28ebdcb8 100644 --- a/src/javaeva/tools/ReflectPackage.java +++ b/src/javaeva/tools/ReflectPackage.java @@ -16,7 +16,7 @@ import java.util.jar.JarInputStream; /** * Allow for java to list Classes that exist in one package and can be instantiated from * the classpath, either directly or through a jar on the classpath. - * So far, jars which are located whithin another jar will not be searched. + * So far, jars which are located within another jar will not be searched. * * @author mkron * @@ -44,26 +44,30 @@ public class ReflectPackage { try { // Get a File object for the package File directory = null; + String dir = null; try { ClassLoader cld = ClassLoader.getSystemClassLoader(); if (cld == null) { throw new ClassNotFoundException("Can't get class loader."); } - String dir = path + "/" + pckgname.replace(".","/"); + dir = path + "/" + pckgname.replace(".","/"); - if (TRACE) System.out.println(path); + if (TRACE) System.out.println(".. opening " + path); directory = new File(dir); } catch (NullPointerException x) { - if (TRACE) System.err.println(directory.getPath()+ " not found in " + path); + if (TRACE) { + System.err.println(directory.getPath()+ " not found in " + path); + System.err.println("directory " + (directory.exists() ? "exists" : "doesnt exist")); + } return set; } if (directory.exists()) { // Get the list of the files contained in the package getClassesFromDirFltr(set, directory, pckgname, includeSubs, reqSuperCls); } else { - if (TRACE) System.err.println(directory.getPath()+ " not found in " + path); + if (TRACE) System.err.println(directory.getPath() + " doesnt exist in " + path + ", dir was " + dir); } } catch(ClassNotFoundException e) { System.err.println(e.getMessage());