History tracking stores the best individual per generation within the population and is now disabled by default.

This commit is contained in:
Marcel Kronfeld 2007-12-19 16:21:12 +00:00
parent 1e8b14b0f1
commit e201eafcb2
2 changed files with 24 additions and 5 deletions

View File

@ -28,7 +28,8 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
protected int m_Size = 50; protected int m_Size = 50;
protected Population m_Archive = null; protected Population m_Archive = null;
public ArrayList m_History = new ArrayList(); boolean useHistory = false;
public ArrayList<AbstractEAIndividual> m_History = new ArrayList<AbstractEAIndividual>();
public Population() { 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. /** This method will allow cou to increment the current number of function calls.
*/ */
public void incrFunctionCalls() { public void incrFunctionCalls() {
@ -117,7 +128,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* Stagnation measured etc. pp. * Stagnation measured etc. pp.
*/ */
public void incrGeneration() { 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<size(); i++) ((AbstractEAIndividual)get(i)).incrAge(); for (int i=0; i<size(); i++) ((AbstractEAIndividual)get(i)).incrAge();
this.m_Generation++; this.m_Generation++;
} }
@ -241,8 +252,8 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* *
*/ */
public Object[] getBestNIndividuals(int n) { public Object[] getBestNIndividuals(int n) {
LinkedList indList = new LinkedList(); LinkedList<AbstractEAIndividual> indList = new LinkedList<AbstractEAIndividual>();
PriorityQueue queue = new PriorityQueue(n); PriorityQueue<Double> queue = new PriorityQueue<Double>(n);
AbstractEAIndividual indy; AbstractEAIndividual indy;
double curNBestFitness = Double.POSITIVE_INFINITY; double curNBestFitness = Double.POSITIVE_INFINITY;
@ -253,7 +264,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
indList.removeLast(); indList.removeLast();
queue.remove(); queue.remove();
} }
indList.addFirst(super.get(i)); indList.addFirst((AbstractEAIndividual)super.get(i));
// use negative fitness, because queue orders the smallest to top. // use negative fitness, because queue orders the smallest to top.
queue.add(new Double(- indy.getFitness(0))); queue.add(new Double(- indy.getFitness(0)));
if (indList.size() == n) curNBestFitness = - ((Double)queue.peek()).doubleValue(); if (indList.size() == n) curNBestFitness = - ((Double)queue.peek()).doubleValue();

View File

@ -93,9 +93,11 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
public void init() { public void init() {
this.m_Optimizer.addPopulationChangedEventListener(this); this.m_Optimizer.addPopulationChangedEventListener(this);
this.m_Undifferentiated = new Population(); this.m_Undifferentiated = new Population();
this.m_Undifferentiated.setUseHistory(true);
this.m_Undifferentiated.setPopulationSize(this.m_PopulationSize); this.m_Undifferentiated.setPopulationSize(this.m_PopulationSize);
this.m_Species = new ArrayList<Population>(); this.m_Species = new ArrayList<Population>();
this.m_Problem.initPopulation(this.m_Undifferentiated); this.m_Problem.initPopulation(this.m_Undifferentiated);
this.getPopulation().setUseHistory(true);
this.evaluatePopulation(this.m_Undifferentiated); this.evaluatePopulation(this.m_Undifferentiated);
this.firePropertyChangedEvent("FirstGenerationPerformed"); this.firePropertyChangedEvent("FirstGenerationPerformed");
} }
@ -106,6 +108,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
*/ */
public void initByPopulation(Population pop, boolean reset) { public void initByPopulation(Population pop, boolean reset) {
this.m_Optimizer.addPopulationChangedEventListener(this); this.m_Optimizer.addPopulationChangedEventListener(this);
pop.setUseHistory(true);
this.m_Undifferentiated = (Population)pop.clone(); this.m_Undifferentiated = (Population)pop.clone();
if (reset) this.m_Undifferentiated.init(); if (reset) this.m_Undifferentiated.init();
this.m_Undifferentiated.setPopulationSize(this.m_PopulationSize); this.m_Undifferentiated.setPopulationSize(this.m_PopulationSize);
@ -241,6 +244,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
*/ */
private Population initializeIndividuals(int n) { private Population initializeIndividuals(int n) {
Population result = new Population(); Population result = new Population();
result.setUseHistory(true);
result.setPopulationSize(n); result.setPopulationSize(n);
//@todo: crossover between species is to be impelemented //@todo: crossover between species is to be impelemented
this.m_Problem.initPopulation(result); this.m_Problem.initPopulation(result);
@ -374,6 +378,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
ClusterResult = this.m_CAForSpeciesDifferentation.cluster(this.m_Undifferentiated); ClusterResult = this.m_CAForSpeciesDifferentation.cluster(this.m_Undifferentiated);
this.m_Undifferentiated = ClusterResult[0]; this.m_Undifferentiated = ClusterResult[0];
for (int j = 1; j < ClusterResult.length; j++) { for (int j = 1; j < ClusterResult.length; j++) {
ClusterResult[j].setUseHistory(true);
ClusterResult[j].m_History = new ArrayList(); ClusterResult[j].m_History = new ArrayList();
newSpecies.add(ClusterResult[j]); newSpecies.add(ClusterResult[j]);
} }
@ -382,9 +387,11 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
// only active populations are clustered // only active populations are clustered
ClusterResult = this.m_CAForSpeciesDifferentation.cluster((Population)this.m_Species.get(i)); ClusterResult = this.m_CAForSpeciesDifferentation.cluster((Population)this.m_Species.get(i));
this.m_Undifferentiated.addPopulation(ClusterResult[0]); this.m_Undifferentiated.addPopulation(ClusterResult[0]);
ClusterResult[0].setUseHistory(true);
this.m_Undifferentiated.setPopulationSize(this.m_Undifferentiated.getPopulationSize() + ClusterResult[0].size()); this.m_Undifferentiated.setPopulationSize(this.m_Undifferentiated.getPopulationSize() + ClusterResult[0].size());
for (int j = 1; j < ClusterResult.length; j++) { for (int j = 1; j < ClusterResult.length; j++) {
ClusterResult[j].setPopulationSize(ClusterResult[j].size()); ClusterResult[j].setPopulationSize(ClusterResult[j].size());
ClusterResult[j].setUseHistory(true);
if (ClusterResult.length > 2) ClusterResult[j].m_History = new ArrayList(); if (ClusterResult.length > 2) ClusterResult[j].m_History = new ArrayList();
newSpecies.add(ClusterResult[j]); newSpecies.add(ClusterResult[j]);
} }
@ -486,6 +493,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
// output the result // output the result
if (this.m_Debug) System.out.println("-Number of species: " + this.m_Species.size()); if (this.m_Debug) System.out.println("-Number of species: " + this.m_Species.size());
this.m_Population = new Population(); this.m_Population = new Population();
m_Population.setUseHistory(true);
this.m_Population = (Population)this.m_Undifferentiated.clone(); this.m_Population = (Population)this.m_Undifferentiated.clone();
if (this.m_Debug) System.out.println("initing with " + this.m_Undifferentiated.size()); if (this.m_Debug) System.out.println("initing with " + this.m_Undifferentiated.size());
for (int i = 0; i < this.m_Species.size(); i++) { for (int i = 0; i < this.m_Species.size(); i++) {