Merging Population from MK branch

This commit is contained in:
Marcel Kronfeld 2011-05-03 09:36:01 +00:00
parent 87a6f444f7
commit 88dde22b4b
2 changed files with 54 additions and 14 deletions

View File

@ -75,6 +75,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
// a sorted queue (for efficiency) // a sorted queue (for efficiency)
transient private ArrayList<AbstractEAIndividual> sortedArr = null; transient private ArrayList<AbstractEAIndividual> sortedArr = null;
private Comparator<Object> lastSortingComparator = null; private Comparator<Object> lastSortingComparator = null;
private InterfaceDistanceMetric popDistMetric = null; // an associated metric
// private AbstractEAIndividualComparator historyComparator = null; // private AbstractEAIndividualComparator historyComparator = null;
public static final String funCallIntervalReached = "FunCallIntervalReached"; public static final String funCallIntervalReached = "FunCallIntervalReached";
@ -190,7 +191,8 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
this.notifyEvalInterval = population.notifyEvalInterval; this.notifyEvalInterval = population.notifyEvalInterval;
this.initMethod = population.initMethod; this.initMethod = population.initMethod;
this.aroundDist = population.aroundDist; this.aroundDist = population.aroundDist;
this.seedCardinality = population.seedCardinality.clone(); this.seedCardinality = population.seedCardinality.clone();
if (population.getPopMetric()!=null) this.popDistMetric = (InterfaceDistanceMetric)population.popDistMetric.clone();
if (population.seedPos!=null) this.seedPos = population.seedPos.clone(); if (population.seedPos!=null) this.seedPos = population.seedPos.clone();
// this.m_Listener = population.m_Listener; // this.m_Listener = population.m_Listener;
if (population.listeners != null) this.listeners = (ArrayList<InterfacePopulationChangedEventListener>)population.listeners.clone(); if (population.listeners != null) this.listeners = (ArrayList<InterfacePopulationChangedEventListener>)population.listeners.clone();
@ -282,6 +284,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
this.m_History = new LinkedList<AbstractEAIndividual>(); this.m_History = new LinkedList<AbstractEAIndividual>();
this.m_Generation = 0; this.m_Generation = 0;
this.m_FunctionCalls = 0; this.m_FunctionCalls = 0;
double[] popSeed = null;
// evaluationTimeHashes = null; // evaluationTimeHashes = null;
// evaluationTimeModCount = -1; // evaluationTimeModCount = -1;
if (this.m_Archive != null) { if (this.m_Archive != null) {
@ -295,23 +298,45 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
createRLHSampling(this, false); createRLHSampling(this, false);
break; break;
case aroundSeed: case aroundSeed:
case aroundRandomSeed:
AbstractEAIndividual template = (AbstractEAIndividual) getEAIndividual(0).clone(); AbstractEAIndividual template = (AbstractEAIndividual) getEAIndividual(0).clone();
if (template.getDoublePosition().length<=seedPos.length) { // use random initial position or the predefined one
if (template.getDoublePosition().length<seedPos.length) { if (initMethod==PopulationInitMethod.aroundRandomSeed) popSeed=RNG.randomDoubleArray(PostProcess.getDoubleRange(template));
else popSeed = seedPos;
if (template.getDoublePosition().length<=popSeed.length) {
if (template.getDoublePosition().length<popSeed.length) {
double[] smallerSeed = new double[template.getDoublePosition().length]; double[] smallerSeed = new double[template.getDoublePosition().length];
System.arraycopy(seedPos, 0, smallerSeed, 0, smallerSeed.length); System.arraycopy(popSeed, 0, smallerSeed, 0, smallerSeed.length);
AbstractEAIndividual.setDoublePosition(template, smallerSeed); AbstractEAIndividual.setDoublePosition(template, smallerSeed);
} else AbstractEAIndividual.setDoublePosition(template, seedPos); } else AbstractEAIndividual.setDoublePosition(template, popSeed);
PostProcess.createPopInSubRange(this, aroundDist, this.getTargetSize(), template); PostProcess.createPopInSubRange(this, aroundDist, this.getTargetSize(), template);
} else System.err.println("Warning, skipping seed initialization: too small individual seed!"); } else System.err.println("Warning, skipping seed initialization: too small individual seed!");
break; break;
case binCardinality: case binCardinality:
createBinCardinality(this, true, seedCardinality.head(), seedCardinality.tail()); createBinCardinality(this, true, seedCardinality.head(), seedCardinality.tail());
break; break;
} }
//System.out.println("After pop init: " + this.getStringRepresentation());
firePropertyChangedEvent(Population.populationInitialized); firePropertyChangedEvent(Population.populationInitialized);
} }
/**
* Reset all values changing during the "life" of a population (such as history,
* generation counter etc).
*/
public void resetProperties() {
m_Generation = 0;
m_FunctionCalls = 0;
if (m_Archive!=null) {
m_Archive.clear();
m_Archive.clearHistory();
}
clearHistory();
modCount++;
// a sorted queue (for efficiency)
sortedArr = null;
}
public double[] getInitPos() { public double[] getInitPos() {
return seedPos ; return seedPos ;
} }
@ -438,6 +463,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
} }
} }
} }
/** /**
* Activate or deactivate the history tracking, which stores the best individual in every * Activate or deactivate the history tracking, which stores the best individual in every
* generation in the incrGeneration() method. * generation in the incrGeneration() method.
@ -1732,13 +1758,24 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
} }
/** /**
* Returns the average, minimal and maximal phenotypic individual distance as diversity measure for the population. * Returns the average, minimal and maximal individual distance as diversity measure for the population.
* Distances are thus scaled by the problem range. * This uses the default population metric of the instance.
* *
* @return the average, minimal and maximal mean distance of individuals in an array of three * @return the average, minimal and maximal mean distance of individuals in an array of three
*/ */
public double[] getPopulationMeasures() { public double[] getPopulationMeasures() {
return getPopulationMeasures(new PhenotypeMetric()); return getPopulationMeasures(getPopMetric());
}
public InterfaceDistanceMetric getPopMetric() {
if (popDistMetric==null) popDistMetric = new PhenotypeMetric();
return popDistMetric;
}
public void setPopMetric(InterfaceDistanceMetric metric) {
popDistMetric=metric;
}
public String popMetricTipText() {
return "Set a default distance metric to be used with the population.";
} }
/** /**
@ -2093,7 +2130,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* @param normalizedPhenoMetric * @param normalizedPhenoMetric
* @return a double array containing the average (or average and variance) of the distance of each individual to its closest neighbor * @return a double array containing the average (or average and variance) of the distance of each individual to its closest neighbor
*/ */
public double[] getAvgDistToClosestNeighbor(boolean normalizedPhenoMetric, boolean calcVariance){ public double[] getAvgDistToClosestNeighbor(boolean normalizedPhenoMetric, boolean calcVariance) {
PhenotypeMetric metric = new PhenotypeMetric(); PhenotypeMetric metric = new PhenotypeMetric();
ArrayList<Double> distances = null; ArrayList<Double> distances = null;
if (calcVariance) distances = new ArrayList<Double>(size()); if (calcVariance) distances = new ArrayList<Double>(size());
@ -2103,7 +2140,10 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
AbstractEAIndividual neighbor, indy = getEAIndividual(i); AbstractEAIndividual neighbor, indy = getEAIndividual(i);
int neighborIndex = getNeighborIndex(i); int neighborIndex = getNeighborIndex(i);
if (neighborIndex >= 0) neighbor = getEAIndividual(neighborIndex); if (neighborIndex >= 0) neighbor = getEAIndividual(neighborIndex);
else return null; else {
System.err.println("Warning, neigbhorIndex<0 in Population.getAvgDistToClosestNeighbor");
return null;
}
if (normalizedPhenoMetric){ if (normalizedPhenoMetric){
d = metric.distance(indy, neighbor); d = metric.distance(indy, neighbor);
} else { } else {
@ -2355,7 +2395,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
} }
public void clearHistory() { public void clearHistory() {
m_History.clear(); if (m_History!=null) m_History.clear();
} }
/** /**

View File

@ -1,5 +1,5 @@
package eva2.server.go.populations; package eva2.server.go.populations;
public enum PopulationInitMethod { public enum PopulationInitMethod {
individualDefault, randomLatinHypercube, aroundSeed, binCardinality; individualDefault, randomLatinHypercube, aroundSeed, binCardinality, aroundRandomSeed;
} }