Implement isAutoAging to deactivate auto-aging of individuals on incrGeneration.

Used by various instances of the ABC
This commit is contained in:
Fabian Becker 2014-12-01 12:11:14 +00:00
parent d7b677c6b8
commit d80ac15142
2 changed files with 31 additions and 18 deletions

View File

@ -99,6 +99,12 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
public static final String POPULATION_INITIALIZED = "PopulationReinitOccured";
public static final String NEXT_GENERATION_PERFORMED = "NextGenerationPerformed";
/**
* With <code>autoAging = true</code> #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<AbstractEAIndividual> 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,9 +726,11 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
}
this.historyList.add((AbstractEAIndividual) this.getBestEAIndividual().clone());
}
if (isAutoAging()) {
for (AbstractEAIndividual individual : this) {
individual.incrAge();
}
}
this.generationCount++;
firePropertyChangedEvent(NEXT_GENERATION_PERFORMED);
}

View File

@ -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,29 +113,23 @@ 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);
}
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() {
@ -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