From a62b85ec29b81ba59feda183f04e84557a433fc1 Mon Sep 17 00:00:00 2001 From: Marcel Kronfeld Date: Mon, 31 Mar 2008 16:22:07 +0000 Subject: [PATCH] CBN should now also work with (m+l)-ES --- .../go/operators/postprocess/PostProcess.java | 12 +++--- .../go/strategies/ClusterBasedNichingEA.java | 20 +++++++--- .../go/strategies/EvolutionStrategies.java | 37 +++++++++++++++---- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/javaeva/server/go/operators/postprocess/PostProcess.java b/src/javaeva/server/go/operators/postprocess/PostProcess.java index 3612093a..b0dd063b 100644 --- a/src/javaeva/server/go/operators/postprocess/PostProcess.java +++ b/src/javaeva/server/go/operators/postprocess/PostProcess.java @@ -321,14 +321,16 @@ public class PostProcess { * @param maxSteps * @param fixedStepSize */ - public static void processWithHC(Population pop, AbstractOptimizationProblem problem, int maxSteps, double fixedStepSize) { + public static int processWithHC(Population pop, AbstractOptimizationProblem problem, int maxSteps, double fixedStepSize) { // pop.SetFunctionCalls(0); // or else optimization wont restart on an "old" population // pop.setGenerationTo(0); + int stepsBef = pop.getFunctionCalls(); processWithHC(pop, problem, new EvaluationTerminator(pop.getFunctionCalls()+maxSteps), new MutateESFixedStepSize(fixedStepSize)); + return pop.getFunctionCalls()-stepsBef; } - public static void processWithHC(Population pop, AbstractOptimizationProblem problem, int maxSteps) { - processWithHC(pop, problem, maxSteps, defaultMutationStepSize); + public static int processWithHC(Population pop, AbstractOptimizationProblem problem, int maxSteps) { + return processWithHC(pop, problem, maxSteps, defaultMutationStepSize); } /** * Optimize a population with a default hill-climbing heuristic with a given termination criterion and mutation operator. @@ -504,8 +506,8 @@ public class PostProcess { } else clusteredPop = inputPop; if (params.getPostProcessSteps() > 0) { - processWithHC(clusteredPop, problem, params.getPostProcessSteps()); - if (listener != null) listener.println("HC post processing: " + params.getPostProcessSteps() + " steps done."); + int stepsDone = processWithHC(clusteredPop, problem, params.getPostProcessSteps()); + if (listener != null) listener.println("HC post processing: " + stepsDone + " steps done."); // some individuals may have now converged again if (params.getPostProcessClusterSigma() > 0) { // so if wished, cluster again. diff --git a/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java b/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java index a6e5c59f..b5616a9d 100644 --- a/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java +++ b/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java @@ -307,22 +307,30 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis // m_Optimizer.initByPopulation(species, false); 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()) { + int mu = Math.max(1,(int)(muLambdaRatio*species.size())); + if (mu >= species.size()) { if (TRACE) System.err.println("warning, muLambdaRatio produced mu >= lambda.. reducing to mu=lambda-1"); mu = species.size() - 1; } es.setMu(mu); es.setLambda(species.size()); + if (TRACE) System.out.println("mu: "+es.getMu() + " / lambda: " + es.getLambda()); } if (TRACE) { - System.out.println("spec size: " + species.size() + ", says its " + species.getPopulationSize()); + System.out.println("Bef: spec size: " + species.size() + ", says its " + species.getPopulationSize()); System.out.println("Best bef: " + BeanInspector.toString(m_Optimizer.getPopulation().getBestFitness())); } this.m_Optimizer.optimize(); - if (TRACE) System.out.println("Best aft: " + BeanInspector.toString(m_Optimizer.getPopulation().getBestFitness())); - return m_Optimizer.getPopulation(); + Population retPop = m_Optimizer.getPopulation(); + if (TRACE) { + System.out.println("Aft: spec size: " + retPop.size() + ", says its " + retPop.getPopulationSize()); + System.out.println("Best aft: " + BeanInspector.toString(retPop.getBestFitness())); + } + if (retPop.size() != retPop.getPopulationSize()) { + if (TRACE) System.out.println("correcting popsize after opt: " + retPop.getPopulationSize() + " to " + retPop.size()); + retPop.setPopulationSize(retPop.size()); + } + return retPop; } public void optimize() { diff --git a/src/javaeva/server/go/strategies/EvolutionStrategies.java b/src/javaeva/server/go/strategies/EvolutionStrategies.java index 46de3f44..bfffbc6f 100644 --- a/src/javaeva/server/go/strategies/EvolutionStrategies.java +++ b/src/javaeva/server/go/strategies/EvolutionStrategies.java @@ -39,7 +39,9 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ private InterfaceSelection m_PartnerSelection = new SelectRandom(); private InterfaceSelection m_EnvironmentSelection = new SelectBestIndividuals(); private int m_NumberOfPartners = 1; + private int origPopSize = -1; // especially for CBN private double[] m_FitnessOfParents = null; + private boolean forceOrigPopSize = true;// especially for CBN transient private String m_Identifier = ""; transient private InterfacePopulationChangedEventListener m_Listener; @@ -75,14 +77,14 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ public void init() { // @todo In case of CBN-ES i need to read the population size!? // @todo but how!? I guess this will never do... - int orgPopSize = this.m_Population.getPopulationSize(); - if (this.m_InitialPopulationSize > orgPopSize) { - this.m_Population.setPopulationSize(this.m_InitialPopulationSize); - } +// int orgPopSize = this.m_Population.getPopulationSize(); +// if (this.m_InitialPopulationSize > orgPopSize) { +// this.m_Population.setPopulationSize(this.m_InitialPopulationSize); +// } //System.out.println("init"); this.m_Problem.initPopulation(this.m_Population); this.evaluatePopulation(this.m_Population); - this.m_Population.setPopulationSize(orgPopSize); +// this.m_Population.setPopulationSize(orgPopSize); this.firePropertyChangedEvent("NextGenerationPerformed"); // int myu = this.m_Population.getPopulationSize(); // int initPopSize = 0; @@ -101,6 +103,8 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ * @param reset If true the population is reset. */ public void initByPopulation(Population pop, boolean reset) { + origPopSize = pop.getPopulationSize(); +// System.out.println("ES: orig popsize is " + origPopSize); this.m_Population = (Population)pop.clone(); if (reset) this.m_Population.init(); this.evaluatePopulation(this.m_Population); @@ -181,11 +185,14 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ //// System.out.println("Population Strategy: ("+ this.m_Myu+","+this.m_Lambda+")"); // } //System.out.println("optimize"); + // first perform the environment selection to select myu parents this.m_EnvironmentSelection.prepareSelection(this.m_Population); parents = this.m_EnvironmentSelection.selectFrom(this.m_Population, this.m_Mu); this.m_Population.clear(); this.m_Population.addPopulation(parents); + + // now generate the lambda offsprings this.m_FitnessOfParents = null; nextGeneration = this.generateChildren(); @@ -199,7 +206,21 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ this.applySuccessRule((rate/((double)this.m_FitnessOfParents.length)), this.m_Population, nextGeneration); } if (this.m_UsePlusStrategy) nextGeneration.addPopulation(this.m_Population); - this.m_Population = nextGeneration; + + if (forceOrigPopSize && (origPopSize > 0) && (origPopSize < nextGeneration.size())) { + // this is especially for CBN: + this.m_EnvironmentSelection.prepareSelection(nextGeneration); + Population tmpPop = (Population)nextGeneration.clone(); + nextGeneration.clear(); + nextGeneration.addPopulation(this.m_EnvironmentSelection.selectFrom(tmpPop, origPopSize)); +// System.out.println("ES post selection! " + origPopSize + " from " + tmpPop.size()); + m_Population = nextGeneration; + } else { + if ((origPopSize > 0) && (origPopSize != nextGeneration.size())) { + System.err.println("Warning in ES! orig: " + origPopSize + " / " + nextGeneration.size()); + } + this.m_Population = nextGeneration; + } //System.out.println("Population size: " + this.m_Population.size()); //System.out.println("-- Best Fitness " + this.m_Population.getBestFitness()[0]); @@ -208,7 +229,7 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ /** This method is just a shortcut to set the mutation step size for * all individuals of these two populations for the 1/5 Success rule. - * This is only necessary because i decieded to make the variable + * This is only necessary because i decided to make the variable * non static * @param successRate The success rate * @param oldPop The old population @@ -353,6 +374,8 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ return this.m_Population; } public void setPopulation(Population pop){ + origPopSize = pop.size(); +// System.out.println("ES: orig popsize is " + origPopSize); this.m_Population = pop; } public String populationTipText() {