From d80ac1514230dec6e7d975a9943b874b575f1f3f Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Mon, 1 Dec 2014 12:11:14 +0000 Subject: [PATCH] Implement isAutoAging to deactivate auto-aging of individuals on incrGeneration. Used by various instances of the ABC --- .../optimization/population/Population.java | 21 ++++++++++++-- .../strategies/ArtificialBeeColony.java | 28 ++++++++----------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/eva2/optimization/population/Population.java b/src/eva2/optimization/population/Population.java index 2c399edb..f9fd982e 100644 --- a/src/eva2/optimization/population/Population.java +++ b/src/eva2/optimization/population/Population.java @@ -99,6 +99,12 @@ public class Population extends ArrayList implements Popul public static final String POPULATION_INITIALIZED = "PopulationReinitOccured"; public static final String NEXT_GENERATION_PERFORMED = "NextGenerationPerformed"; + /** + * With autoAging = true #incrGeneration automatically + * ages individuals. + */ + private boolean autoAging = true; + public Population() { LOGGER.log(Level.FINER, "New population has been created."); } @@ -575,6 +581,15 @@ public class Population extends ArrayList implements Popul return historyMaxLen != 0; } + @Hidden + public void setAutoAging(boolean autoAging) { + this.autoAging = autoAging; + } + + public boolean isAutoAging() { + return this.autoAging; + } + public void setMaxHistoryLength(int len) { historyMaxLen = len; } @@ -711,8 +726,10 @@ public class Population extends ArrayList implements Popul } this.historyList.add((AbstractEAIndividual) this.getBestEAIndividual().clone()); } - for (AbstractEAIndividual individual : this) { - individual.incrAge(); + if (isAutoAging()) { + for (AbstractEAIndividual individual : this) { + individual.incrAge(); + } } this.generationCount++; firePropertyChangedEvent(NEXT_GENERATION_PERFORMED); diff --git a/src/eva2/optimization/strategies/ArtificialBeeColony.java b/src/eva2/optimization/strategies/ArtificialBeeColony.java index a97901ad..32700bf7 100644 --- a/src/eva2/optimization/strategies/ArtificialBeeColony.java +++ b/src/eva2/optimization/strategies/ArtificialBeeColony.java @@ -65,6 +65,8 @@ public class ArtificialBeeColony extends AbstractOptimizer implements Serializab @Override public void initializeByPopulation(Population pop, boolean reset) { this.population = (Population) pop.clone(); + // We handle aging ourselves + this.population.setAutoAging(false); if (reset) { this.population.initialize(); this.evaluatePopulation(this.population); @@ -111,31 +113,25 @@ public class ArtificialBeeColony extends AbstractOptimizer implements Serializab /** * Remember best Individual */ - if (bestIndividual != null && bestIndividual.getFitness(0) < this.population.getBestEAIndividual().getFitness(0)) { - bestIndividual = this.population.getBestEAIndividual(); - } else { - bestIndividual = this.population.getBestEAIndividual(); - } + memorizeBestSolution(); /** * Send scout bee */ sendScoutBees(); - /** - * ToDo: This is ugly. - * - * incrGeneration increments the age of all indies. Age management however happens - * in the algorithm itself (for ABC) so we have to -1 all ages. - */ this.population.incrGeneration(); - for (AbstractEAIndividual individual : this.population) { - individual.setAge(individual.getAge() - 1); - } - this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED); } + protected void memorizeBestSolution() { + if (bestIndividual != null && bestIndividual.getFitness(0) < this.population.getBestEAIndividual().getFitness(0)) { + bestIndividual = this.population.getBestEAIndividual(); + } else { + bestIndividual = this.population.getBestEAIndividual(); + } + } + protected void sendScoutBees() { AbstractEAIndividual oldestIndy = getOldestIndividual(); if (oldestIndy.getAge() > this.maxTrials) { @@ -228,7 +224,7 @@ public class ArtificialBeeColony extends AbstractOptimizer implements Serializab } /** - * In the standard ABC the onlookers behave exaclty like the employed bees. + * In the standard ABC the onlookers behave exactly like the employed bees. * * @param baseIndividual * @param index