From e201eafcb26bf783b3e35405986fce5baf7a802f Mon Sep 17 00:00:00 2001 From: Marcel Kronfeld Date: Wed, 19 Dec 2007 16:21:12 +0000 Subject: [PATCH] History tracking stores the best individual per generation within the population and is now disabled by default. --- .../server/go/populations/Population.java | 21 ++++++++++++++----- .../go/strategies/ClusterBasedNichingEA.java | 8 +++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/javaeva/server/go/populations/Population.java b/src/javaeva/server/go/populations/Population.java index a98f607f..2e750bbd 100644 --- a/src/javaeva/server/go/populations/Population.java +++ b/src/javaeva/server/go/populations/Population.java @@ -28,7 +28,8 @@ public class Population extends ArrayList implements PopulationInterface, Clonea protected int m_Size = 50; protected Population m_Archive = null; - public ArrayList m_History = new ArrayList(); + boolean useHistory = false; + public ArrayList m_History = new ArrayList(); public Population() { } @@ -84,6 +85,16 @@ public class Population extends ArrayList implements PopulationInterface, Clonea } } + /** + * Activate or deactivate the history tracking, which stores the best individual in every + * generation in the incrGeneration() method. + * + * @param useHist + */ + public void setUseHistory(boolean useHist) { + useHistory = useHist; + } + /** This method will allow cou to increment the current number of function calls. */ public void incrFunctionCalls() { @@ -117,7 +128,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea * Stagnation measured etc. pp. */ public void incrGeneration() { - if (this.size() >= 1) this.m_History.add(this.getBestEAIndividual()); + if (useHistory && (this.size() >= 1)) this.m_History.add(this.getBestEAIndividual()); for (int i=0; i indList = new LinkedList(); + PriorityQueue queue = new PriorityQueue(n); AbstractEAIndividual indy; double curNBestFitness = Double.POSITIVE_INFINITY; @@ -253,7 +264,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea indList.removeLast(); queue.remove(); } - indList.addFirst(super.get(i)); + indList.addFirst((AbstractEAIndividual)super.get(i)); // use negative fitness, because queue orders the smallest to top. queue.add(new Double(- indy.getFitness(0))); if (indList.size() == n) curNBestFitness = - ((Double)queue.peek()).doubleValue(); diff --git a/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java b/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java index 9065178e..ebbab00f 100644 --- a/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java +++ b/src/javaeva/server/go/strategies/ClusterBasedNichingEA.java @@ -93,9 +93,11 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis public void init() { this.m_Optimizer.addPopulationChangedEventListener(this); this.m_Undifferentiated = new Population(); + this.m_Undifferentiated.setUseHistory(true); this.m_Undifferentiated.setPopulationSize(this.m_PopulationSize); this.m_Species = new ArrayList(); this.m_Problem.initPopulation(this.m_Undifferentiated); + this.getPopulation().setUseHistory(true); this.evaluatePopulation(this.m_Undifferentiated); this.firePropertyChangedEvent("FirstGenerationPerformed"); } @@ -106,6 +108,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis */ public void initByPopulation(Population pop, boolean reset) { this.m_Optimizer.addPopulationChangedEventListener(this); + pop.setUseHistory(true); this.m_Undifferentiated = (Population)pop.clone(); if (reset) this.m_Undifferentiated.init(); this.m_Undifferentiated.setPopulationSize(this.m_PopulationSize); @@ -241,6 +244,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis */ private Population initializeIndividuals(int n) { Population result = new Population(); + result.setUseHistory(true); result.setPopulationSize(n); //@todo: crossover between species is to be impelemented this.m_Problem.initPopulation(result); @@ -374,6 +378,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis ClusterResult = this.m_CAForSpeciesDifferentation.cluster(this.m_Undifferentiated); this.m_Undifferentiated = ClusterResult[0]; for (int j = 1; j < ClusterResult.length; j++) { + ClusterResult[j].setUseHistory(true); ClusterResult[j].m_History = new ArrayList(); newSpecies.add(ClusterResult[j]); } @@ -382,9 +387,11 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis // only active populations are clustered ClusterResult = this.m_CAForSpeciesDifferentation.cluster((Population)this.m_Species.get(i)); this.m_Undifferentiated.addPopulation(ClusterResult[0]); + ClusterResult[0].setUseHistory(true); this.m_Undifferentiated.setPopulationSize(this.m_Undifferentiated.getPopulationSize() + ClusterResult[0].size()); for (int j = 1; j < ClusterResult.length; j++) { ClusterResult[j].setPopulationSize(ClusterResult[j].size()); + ClusterResult[j].setUseHistory(true); if (ClusterResult.length > 2) ClusterResult[j].m_History = new ArrayList(); newSpecies.add(ClusterResult[j]); } @@ -486,6 +493,7 @@ 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(); if (this.m_Debug) System.out.println("initing with " + this.m_Undifferentiated.size()); for (int i = 0; i < this.m_Species.size(); i++) {