Fixing several broken observer pattern implementations

This commit is contained in:
Fabian Becker 2014-01-28 16:44:33 +01:00
parent b363e46ad1
commit b24dbac6a1
8 changed files with 164 additions and 134 deletions

View File

@ -11,7 +11,7 @@ import java.io.Serializable;
/**
* Generalized Rastrigin's function.
*/
@Description("Generalized Rastrigins's function.")
@Description("Generalized Rastrigin's function.")
public class F6Problem extends AbstractProblemDoubleOffset
implements InterfaceMultimodalProblem, InterfaceFirstOrderDerivableProblem, InterfaceLocalSearchable, Serializable, InterfaceInterestingHistogram {
private double m_A = 10;

View File

@ -173,7 +173,7 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
private void plotBestIndy() {
if (m_Plot != null) {
double[] curPosition = ((InterfaceDataTypeDouble) m_Population.getBestEAIndividual()).getDoubleData();
double[] curPosition = ((InterfaceDataTypeDouble) population.getBestEAIndividual()).getDoubleData();
if (lastBestPlot != null) {
this.m_Plot.setConnectedPoint(lastBestPlot[0], lastBestPlot[1], 0);
@ -187,7 +187,7 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
protected void plotIndy(double[] curPosition, double[] curVelocity, int index) {
if (this.m_Show) {
if (plotBestOnly) {
if (index != ((Integer) (m_Population.getBestEAIndividual().getData(indexKey)))) {
if (index != ((Integer) (population.getBestEAIndividual().getData(indexKey)))) {
return;
} else {
// if (lastBestPlot != null) this.m_Plot.setConnectedPoint(lastBestPlot[0], lastBestPlot[1], index);
@ -314,7 +314,7 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
*/
@Override
protected double getSpeedLimit(int index) {
if (index >= ((double) (m_Population.size() * highEnergyRatio))) {
if (index >= ((double) (population.size() * highEnergyRatio))) {
return speedLimit;
} else {
if (highEnergyRaise == 0.) {
@ -330,11 +330,11 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
protected void startOptimize() {
super.startOptimize();
if (detectAnchor >= 0) { // set the new detection anchor individual
detectAnchor = RNG.randomInt(0, m_Population.size() - 1);
detectAnchor = RNG.randomInt(0, population.size() - 1);
if (detectFit == null) {
detectFit = (m_Population.getIndividual(detectAnchor).getFitness()).clone();
detectFit = (population.getIndividual(detectAnchor).getFitness()).clone();
} else {
System.arraycopy(m_Population.getIndividual(detectAnchor).getFitness(), 0, detectFit, 0, detectFit.length);
System.arraycopy(population.getIndividual(detectAnchor).getFitness(), 0, detectFit, 0, detectFit.length);
}
}
}
@ -351,8 +351,8 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
double quantumCount = 0.;
// do what the usual function does plus announce quantum particles
if (quantumRatio > 0.) {
for (int i = 0; i < this.m_Population.size(); i++) {
AbstractEAIndividual indy = (AbstractEAIndividual) m_Population.get(i);
for (int i = 0; i < this.population.size(); i++) {
AbstractEAIndividual indy = (AbstractEAIndividual) population.get(i);
if (i >= quantumCount) {
indy.putData(partTypeKey, quantumType);
quantumCount += 1. / quantumRatio;
@ -405,7 +405,7 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
plotBestIndy();
}
envHasChanged = detectChange(m_Population);
envHasChanged = detectChange(this.population);
// if (envHasChanged) {
// System.out.println("environmental change detected!");
@ -427,8 +427,8 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
@Override
protected void logBestIndividual() {
// log the best individual of the population
if (envHasChanged || (this.m_Population.getBestEAIndividual().isDominatingDebConstraints(this.m_BestIndividual))) {
this.m_BestIndividual = (AbstractEAIndividual) this.m_Population.getBestEAIndividual().clone();
if (envHasChanged || (this.population.getBestEAIndividual().isDominatingDebConstraints(this.m_BestIndividual))) {
this.m_BestIndividual = (AbstractEAIndividual) this.population.getBestEAIndividual().clone();
this.m_BestIndividual.putData(partBestFitKey, this.m_BestIndividual.getFitness());
this.m_BestIndividual.putData(partBestPosKey, ((InterfaceDataTypeDouble) this.m_BestIndividual).getDoubleData());
//System.out.println("-- best ind set to " + ((InterfaceDataTypeDouble)this.m_BestIndividual).getDoubleData()[0] + "/" + ((InterfaceDataTypeDouble)this.m_BestIndividual).getDoubleData()[1]);
@ -449,7 +449,7 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
switch (changeDetectStrategy.getSelectedTag().getID()) {
case 0:
if (detectAnchor >= 0) {
return !(java.util.Arrays.equals(detectFit, m_Population.getIndividual(detectAnchor).getFitness()));
return !(java.util.Arrays.equals(detectFit, this.population.getIndividual(detectAnchor).getFitness()));
} else {
System.err.println("warning, inconsistency in detectChange");
}
@ -501,7 +501,7 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization
strB.append("Dynamic Particle Swarm Optimization:\nOptimization Problem: ");
strB.append(this.m_Problem.getStringRepresentationForProblem(this));
strB.append("\n");
strB.append(this.m_Population.getStringRepresentation());
strB.append(this.population.getStringRepresentation());
return strB.toString();
}

View File

@ -14,6 +14,8 @@ import eva2.optimization.problems.InterfaceOptimizationProblem;
import eva2.util.annotation.Description;
import eva2.util.annotation.Parameter;
import java.util.Vector;
/**
* Evolution strategies by Rechenberg and Schwefel, but please remember that
* this only gives the generation strategy and not the coding. But this is the
@ -35,6 +37,7 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ
@Parameter(description = "Determines whether the +-Strategy should be used.", name = "usePlus")
protected boolean usePlusStrategy = false;
protected Population population = new Population();
protected InterfaceOptimizationProblem optimizationProblem = new B1Problem();
private InterfaceSelection parentSelection = new SelectRandom();
@ -44,7 +47,7 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ
protected int origPopSize = -1; // especially for CBN
private boolean forceOrigPopSize = true;// especially for CBN
transient private String identifier = "";
transient private InterfacePopulationChangedEventListener changeListener;
transient private Vector<InterfacePopulationChangedEventListener> changeListener;
public static final String esMuParam = "EvolutionStrategyMuParameter";
public static final String esLambdaParam = "EvolutionStrategyLambdaParameter";
@ -247,13 +250,16 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ
*/
@Override
public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
this.changeListener = ea;
if (this.changeListener == null) {
this.changeListener = new Vector<InterfacePopulationChangedEventListener>();
}
this.changeListener.add(ea);
}
@Override
public boolean removePopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
if (changeListener == ea) {
changeListener = null;
public boolean removePopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
if (changeListener != null && changeListener.removeElement(ea)) {
return true;
} else {
return false;
@ -262,10 +268,14 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ
/**
* Something has changed
*
* @param name
*/
protected void firePropertyChangedEvent(String name) {
if (this.changeListener != null) {
this.changeListener.registerPopulationStateChanged(this, name);
for (int i = 0; i < this.changeListener.size(); i++) {
this.changeListener.get(i).registerPopulationStateChanged(this, name);
}
}
}

View File

@ -11,6 +11,9 @@ import eva2.optimization.problems.F1Problem;
import eva2.optimization.problems.InterfaceOptimizationProblem;
import eva2.util.annotation.Description;
import java.util.ArrayList;
import java.util.List;
/**
* Evolutionary programming by Fogel. Works fine but is actually a quite greedy
* local search strategy solely based on mutation. To prevent any confusion, the
@ -20,21 +23,21 @@ import eva2.util.annotation.Description;
@Description("This is a basic Evolutionary Programming scheme.")
public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Serializable {
private int m_PopulationSize = 0;
private Population m_Population = new Population();
private InterfaceOptimizationProblem m_Problem = new F1Problem();
private InterfaceSelection m_EnvironmentSelection = new SelectEPTournaments();
private String m_Identifier = "";
transient private InterfacePopulationChangedEventListener m_Listener;
private int populationSize = 0;
private Population population = new Population();
private InterfaceOptimizationProblem optimizationProblem = new F1Problem();
private InterfaceSelection environmentSelection = new SelectEPTournaments();
private String identifier = "";
transient private List<InterfacePopulationChangedEventListener> populationChangedEventListeners = new ArrayList<>();
public EvolutionaryProgramming() {
}
public EvolutionaryProgramming(EvolutionaryProgramming a) {
this.m_Population = (Population) a.m_Population.clone();
this.m_Problem = (InterfaceOptimizationProblem) a.m_Problem.clone();
this.m_Identifier = a.m_Identifier;
this.m_EnvironmentSelection = (InterfaceSelection) a.m_EnvironmentSelection.clone();
this.population = (Population) a.population.clone();
this.optimizationProblem = (InterfaceOptimizationProblem) a.optimizationProblem.clone();
this.identifier = a.identifier;
this.environmentSelection = (InterfaceSelection)a.environmentSelection.clone();
}
@Override
@ -44,9 +47,9 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
@Override
public void init() {
this.m_Problem.initializePopulation(this.m_Population);
this.evaluatePopulation(this.m_Population);
this.m_PopulationSize = this.m_Population.size();
this.optimizationProblem.initializePopulation(this.population);
this.evaluatePopulation(this.population);
this.populationSize = this.population.size();
this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED);
}
@ -57,10 +60,10 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
*/
@Override
public void initByPopulation(Population pop, boolean reset) {
this.m_Population = (Population) pop.clone();
this.population = (Population) pop.clone();
if (reset) {
this.m_Population.init();
this.evaluatePopulation(this.m_Population);
this.population.init();
this.evaluatePopulation(this.population);
this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED);
}
}
@ -71,7 +74,7 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
* @param population The population that is to be evaluated
*/
private void evaluatePopulation(Population population) {
this.m_Problem.evaluate(population);
this.optimizationProblem.evaluate(population);
population.incrGeneration();
}
@ -80,12 +83,12 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
* population of evaluated individuals.
*/
private Population generateChildren() {
Population result = (Population) this.m_Population.cloneWithoutInds();
Population result = (Population) this.population.cloneWithoutInds();
AbstractEAIndividual mutant;
result.clear();
for (int i = 0; i < this.m_Population.size(); i++) {
mutant = (AbstractEAIndividual) ((AbstractEAIndividual) this.m_Population.get(i)).clone();
for (int i = 0; i < this.population.size(); i++) {
mutant = (AbstractEAIndividual) ((AbstractEAIndividual) this.population.get(i)).clone();
double tmpD = mutant.getMutationProbability();
mutant.setMutationProbability(1.0);
mutant.mutate();
@ -99,14 +102,14 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
public void optimize() {
Population nextGeneration, parents;
this.m_EnvironmentSelection.prepareSelection(this.m_Population);
parents = this.m_EnvironmentSelection.selectFrom(this.m_Population, this.m_PopulationSize);
this.m_Population.clear();
this.m_Population.addPopulation(parents);
this.environmentSelection.prepareSelection(this.population);
parents = this.environmentSelection.selectFrom(this.population, this.populationSize);
this.population.clear();
this.population.addPopulation(parents);
nextGeneration = this.generateChildren();
this.evaluatePopulation(nextGeneration);
nextGeneration.addPopulation(this.m_Population);
this.m_Population = nextGeneration;
nextGeneration.addPopulation(this.population);
this.population = nextGeneration;
this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED);
}
@ -118,26 +121,21 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
*/
@Override
public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
this.m_Listener = ea;
this.populationChangedEventListeners.add(ea);
}
@Override
public boolean removePopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
if (m_Listener == ea) {
m_Listener = null;
return true;
} else {
return false;
}
return this.populationChangedEventListeners.remove(ea);
}
/**
* Something has changed
*/
protected void firePropertyChangedEvent(String name) {
if (this.m_Listener != null) {
this.m_Listener.registerPopulationStateChanged(this, name);
for(InterfacePopulationChangedEventListener l : this.populationChangedEventListeners) {
l.registerPopulationStateChanged(this, name);
}
}
@ -148,12 +146,12 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
*/
@Override
public void setProblem(InterfaceOptimizationProblem problem) {
this.m_Problem = problem;
this.optimizationProblem = problem;
}
@Override
public InterfaceOptimizationProblem getProblem() {
return this.m_Problem;
return this.optimizationProblem;
}
/**
@ -167,8 +165,8 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
String result = "";
result += "Evolutionary Programming:\n";
result += "Optimization Problem: ";
result += this.m_Problem.getStringRepresentationForProblem(this) + "\n";
result += this.m_Population.getStringRepresentation();
result += this.optimizationProblem.getStringRepresentationForProblem(this) + "\n";
result += this.population.getStringRepresentation();
return result;
}
@ -179,12 +177,12 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
*/
@Override
public void setIdentifier(String name) {
this.m_Identifier = name;
this.identifier = name;
}
@Override
public String getIdentifier() {
return this.m_Identifier;
return this.identifier;
}
/**
@ -206,12 +204,12 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
*/
@Override
public Population getPopulation() {
return this.m_Population;
return this.population;
}
@Override
public void setPopulation(Population pop) {
this.m_Population = pop;
this.population = pop;
}
public String populationTipText() {
@ -229,11 +227,11 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri
* @param selection
*/
public void setEnvironmentSelection(InterfaceSelection selection) {
this.m_EnvironmentSelection = selection;
this.environmentSelection = selection;
}
public InterfaceSelection getEnvironmentSelection() {
return this.m_EnvironmentSelection;
return this.environmentSelection;
}
public String environmentSelectionTipText() {

View File

@ -13,6 +13,8 @@ import eva2.optimization.problems.F1Problem;
import eva2.optimization.problems.InterfaceOptimizationProblem;
import eva2.util.annotation.Description;
import java.util.Vector;
/**
* The traditional genetic algorithms as devised by Holland. To only special
* here it the plague factor which reduces the population size to tune from a
@ -30,7 +32,7 @@ public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializabl
private int plague = 0;
private int numberOfPartners = 1;
private String identifier = "";
transient private InterfacePopulationChangedEventListener popChangedListener;
transient private Vector<InterfacePopulationChangedEventListener> changeListener;
public GeneticAlgorithm() {
}
@ -163,14 +165,16 @@ public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializabl
*/
@Override
public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
this.popChangedListener = ea;
if (this.changeListener == null) {
this.changeListener = new Vector<InterfacePopulationChangedEventListener>();
}
this.changeListener.add(ea);
}
@Override
public boolean removePopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
if (popChangedListener == ea) {
popChangedListener = null;
if (changeListener != null && changeListener.removeElement(ea)) {
return true;
} else {
return false;
@ -178,11 +182,15 @@ public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializabl
}
/**
* Something has changed.
* Something has changed
*
* @param name
*/
protected void firePropertyChangedEvent(String name) {
if (this.popChangedListener != null) {
this.popChangedListener.registerPopulationStateChanged(this, name);
if (this.changeListener != null) {
for (int i = 0; i < this.changeListener.size(); i++) {
this.changeListener.get(i).registerPopulationStateChanged(this, name);
}
}
}

View File

@ -537,7 +537,7 @@ public class NichePSO implements InterfaceAdditionalPopulationInformer, Interfac
ParticleSubSwarmOptimization currentsubswarm = getSubSwarms().get(i);
if (currentsubswarm.isActive()) {
actCnt++;
avgSize += currentsubswarm.m_Population.size();
avgSize += currentsubswarm.population.size();
}
}
if (actCnt > 0) {

View File

@ -114,8 +114,8 @@ public class ParticleSubSwarmOptimization extends ParticleSwarmOptimizationGCPSO
* and sets the current fitness as the first value
*/
protected void initIndividuals() {
for (int i = 0; i < m_Population.size(); ++i) {
AbstractEAIndividual indy = (AbstractEAIndividual) (m_Population.getEAIndividual(i));
for (int i = 0; i < population.size(); ++i) {
AbstractEAIndividual indy = (AbstractEAIndividual) (population.getEAIndividual(i));
initSubSwarmDefaultsOf(indy);
}
}
@ -283,8 +283,8 @@ public class ParticleSubSwarmOptimization extends ParticleSwarmOptimizationGCPSO
*/
public void updateFitnessArchives() {
//int lim = 3; // maximal number of fitnessvalues remembered from former iterations
for (int i = 0; i < m_Population.size(); ++i) {
AbstractEAIndividual indy = (AbstractEAIndividual) m_Population.getEAIndividual(i);
for (int i = 0; i < population.size(); ++i) {
AbstractEAIndividual indy = (AbstractEAIndividual) population.getEAIndividual(i);
Vector<Double> fitArchive_old = (Vector<Double>) (indy.getData(NichePSO.fitArchiveKey));
double scalarFitness = sum(indy.getFitness()); // if multiobjective, use the sum of all fitnessvalues (dont use the norm because fitnessvalues may be negative)
Double fitness = new Double(scalarFitness);
@ -310,8 +310,8 @@ public class ParticleSubSwarmOptimization extends ParticleSwarmOptimizationGCPSO
* to the std deviation over the last 3 fitness values
*/
public void updateFitnessStdDev() {
for (int i = 0; i < m_Population.size(); ++i) {
AbstractEAIndividual currentindy = m_Population.getEAIndividual(i);
for (int i = 0; i < population.size(); ++i) {
AbstractEAIndividual currentindy = population.getEAIndividual(i);
Vector<Double> fitnessArchive = (Vector<Double>) (currentindy.getData(NichePSO.fitArchiveKey));
// the stddev is computed over 3 values as suggested in
// "a niching particle swarm optimizer" by Brits et al.
@ -325,8 +325,8 @@ public class ParticleSubSwarmOptimization extends ParticleSwarmOptimizationGCPSO
* update the personal best representation if the current individual is better than the pbest
*/
public void updatePersonalBest() {
for (int i = 0; i < m_Population.size(); ++i) {
AbstractEAIndividual currentindy = m_Population.getEAIndividual(i);
for (int i = 0; i < population.size(); ++i) {
AbstractEAIndividual currentindy = population.getEAIndividual(i);
AbstractEAIndividual pbest = (AbstractEAIndividual) currentindy.getData("PersonalBestKey");
if (currentindy.isDominating(pbest)) {
initPersonalBestOf(currentindy);
@ -529,10 +529,10 @@ public class ParticleSubSwarmOptimization extends ParticleSwarmOptimizationGCPSO
* adds a population and its function calls to this.population
*/
public void addPopulation(Population pop) {
m_Population.addPopulation(pop);
population.addPopulation(pop);
// dont peculate the function calls from the added population (which is going to be deleted in NichePSO)
m_Population.incrFunctionCallsBy(pop.getFunctionCalls());
population.incrFunctionCallsBy(pop.getFunctionCalls());
}
/**
@ -544,7 +544,7 @@ public class ParticleSubSwarmOptimization extends ParticleSwarmOptimizationGCPSO
public boolean addIndividual(IndividualInterface ind) {
// nothing to do regarding function calls
// old calls were counted in old population new calls are now counted in this population
return m_Population.addIndividual(ind);
return population.addIndividual(ind);
}
/**
@ -571,7 +571,7 @@ public class ParticleSubSwarmOptimization extends ParticleSwarmOptimizationGCPSO
* removes an individual
*/
public boolean removeSubIndividual(IndividualInterface ind) {
return m_Population.removeMember(ind);
return population.removeMember(ind);
}
public void removeSubPopulation(Population pop, boolean allowMissing) { // this is very slow...
@ -621,8 +621,8 @@ public class ParticleSubSwarmOptimization extends ParticleSwarmOptimizationGCPSO
double max = Double.NEGATIVE_INFINITY;
//PhenotypeMetric metric = new PhenotypeMetric();
for (int i = 0; i < m_Population.size(); ++i) {
AbstractEAIndividual indy = m_Population.getEAIndividual(i);
for (int i = 0; i < population.size(); ++i) {
AbstractEAIndividual indy = population.getEAIndividual(i);
//double dist = metric.distance(m_BestIndividual, indy);
double sqrdDist = EuclideanMetric.squaredEuclideanDistance(AbstractEAIndividual.getDoublePositionShallow(m_BestIndividual),
AbstractEAIndividual.getDoublePositionShallow(indy));

View File

@ -46,7 +46,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
* Generated serial version uid.
*/
private static final long serialVersionUID = -149996122795669589L;
protected Population m_Population = new Population();
protected Population population = new Population();
Object[] sortedPop = null;
protected AbstractEAIndividual m_BestIndividual = null;
protected InterfaceOptimizationProblem m_Problem = new F1Problem();
@ -94,7 +94,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
transient final static String sortedIndexKey = "sortedParticleIndex";
transient final static String dmsGroupIndexKey = "dmsGroupIndex";
protected String m_Identifier = "";
transient private InterfacePopulationChangedEventListener m_Listener;
transient private Vector<InterfacePopulationChangedEventListener> changeListener;
transient private TopoPlot topoPlot = null;
/// sleep time so that visual plot can be followed easier
protected int sleepTime = 0;
@ -128,7 +128,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
if (a.algType != null) {
this.algType = (SelectedTag) a.algType.clone();
}
this.m_Population = (Population) a.m_Population.clone();
this.population = (Population) a.population.clone();
this.m_Problem = a.m_Problem;
this.m_Identifier = a.m_Identifier;
this.initialVelocity = a.initialVelocity;
@ -153,7 +153,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
public ParticleSwarmOptimization(int popSize, double p1, double p2, PSOTopologyEnum topo, int topoRange) {
this();
algType.setSelectedTag(1); // set to constriction
m_Population = new Population(popSize);
population = new Population(popSize);
setPhiValues(p1, p2);
topologyRange = topoRange;
topology = topo;
@ -205,13 +205,13 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
}
tracedVelocity = null;
if (!externalInitialPop) {
this.m_Problem.initializePopulation(this.m_Population);
this.m_Problem.initializePopulation(this.population);
}
// evaluation needs to be done here now, as its omitted if reset is false
initDefaults(this.m_Population);
this.evaluatePopulation(this.m_Population);
initDefaults(this.population);
this.evaluatePopulation(this.population);
if (m_BestIndividual == null) {
m_BestIndividual = m_Population.getBestEAIndividual();
m_BestIndividual = population.getBestEAIndividual();
}
initByPopulation(null, false);
externalInitialPop = false;
@ -409,7 +409,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
}
protected boolean isParticleTypeByIndex(int index, int type) {
return isParticleType((AbstractEAIndividual) m_Population.get(index), type);
return isParticleType((AbstractEAIndividual) population.get(index), type);
}
protected boolean isParticleType(AbstractEAIndividual indy, int type) {
@ -448,31 +448,31 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
@Override
public void initByPopulation(Population pop, boolean reset) {
if (pop != null) {
this.m_Population = (Population) pop.clone();
this.population = (Population) pop.clone();
externalInitialPop = true;
}
if (reset) {
this.m_Population.init();
this.population.init();
}
AbstractEAIndividual indy;
if (!defaultsDone(m_Population.getEAIndividual(0))) {
initDefaults(m_Population);
if (!defaultsDone(population.getEAIndividual(0))) {
initDefaults(population);
}
if (reset) {
this.evaluatePopulation(this.m_Population);
this.evaluatePopulation(this.population);
}
for (int i = 0; i < this.m_Population.size(); i++) {
indy = (AbstractEAIndividual) this.m_Population.get(i);
for (int i = 0; i < this.population.size(); i++) {
indy = (AbstractEAIndividual) this.population.get(i);
if (indy instanceof InterfaceDataTypeDouble) {
initIndividualMemory(indy);
}
}
this.m_BestIndividual = (AbstractEAIndividual) this.m_Population.getBestEAIndividual().clone();
this.m_BestIndividual = (AbstractEAIndividual) this.population.getBestEAIndividual().clone();
if (reset) {
this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED);
@ -484,15 +484,15 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
if (topologyRange < 2) {
System.err.println("Error, tree/hpso requires topology range of at least 2!");
} else {
while (getMaxNodes(topologyRange, treeLevels) < m_Population.size()) {
while (getMaxNodes(topologyRange, treeLevels) < population.size()) {
treeLevels++;
}
treeOrphans = m_Population.size() - getMaxNodes(topologyRange, treeLevels - 1);
treeOrphans = population.size() - getMaxNodes(topologyRange, treeLevels - 1);
treeLastFullLevelNodeCnt = (int) Math.pow(topologyRange, treeLevels - 1);
}
}
if (getTopology() == PSOTopologyEnum.dms) {
dmsLinks = regroupSwarm(m_Population, getTopologyRange());
dmsLinks = regroupSwarm(population, getTopologyRange());
}
}
@ -871,18 +871,18 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
private double[] getSuccessfulVel(int index) {
if (true) {
return (double[]) m_Population.getEAIndividual(index).getData(lastSuccessKey);
return (double[]) population.getEAIndividual(index).getData(lastSuccessKey);
} else { // random one
ArrayList<Integer> successes = new ArrayList<Integer>();
for (int i = 0; i < this.m_Population.size(); i++) {
double[] succVel = (double[]) m_Population.getEAIndividual(i).getData(lastSuccessKey);
for (int i = 0; i < this.population.size(); i++) {
double[] succVel = (double[]) population.getEAIndividual(i).getData(lastSuccessKey);
if (succVel != null) {
successes.add(new Integer(i));
}
}
if (successes.size() > 0) {
int i = successes.get(RNG.randomInt(successes.size()));
return (double[]) m_Population.getEAIndividual(i).getData(lastSuccessKey);
return (double[]) population.getEAIndividual(i).getData(lastSuccessKey);
} else {
return null;
}
@ -1360,10 +1360,10 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
updatePopulation();
// evaluate the population
this.evaluatePopulation(this.m_Population);
this.evaluatePopulation(this.population);
// update the individual memory
updateSwarmMemory(m_Population);
updateSwarmMemory(population);
// log the best individual of the population
logBestIndividual();
@ -1403,9 +1403,9 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
}
protected void maybeClearPlot() {
if (((m_Population.getGeneration() % 23) == 0) && isShow() && (m_Plot != null)) {
if (((population.getGeneration() % 23) == 0) && isShow() && (m_Plot != null)) {
m_Plot.clearAll();
InterfaceDataTypeDouble indy = (InterfaceDataTypeDouble) this.m_Population.get(0);
InterfaceDataTypeDouble indy = (InterfaceDataTypeDouble) this.population.get(0);
double[][] range = indy.getDoubleRange();
m_Plot.setCornerPoints(range, 0);
}
@ -1416,8 +1416,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
*/
protected void startOptimize() {
if (TRACE) {
for (int i = 0; i < m_Population.size(); i++) {
AbstractEAIndividual indy = m_Population.getEAIndividual(i);
for (int i = 0; i < population.size(); i++) {
AbstractEAIndividual indy = population.getEAIndividual(i);
System.out.println(BeanInspector.toString(indy.getData(partTypeKey)));
System.out.println(BeanInspector.toString(indy.getData(partBestPosKey)));
System.out.println(BeanInspector.toString(indy.getData(partBestFitKey)));
@ -1434,8 +1434,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
* Log the best individual so far.
*/
protected void logBestIndividual() {
if (this.m_Population.getBestEAIndividual().isDominatingDebConstraints(this.m_BestIndividual)) {
this.m_BestIndividual = (AbstractEAIndividual) this.m_Population.getBestEAIndividual().clone();
if (this.population.getBestEAIndividual().isDominatingDebConstraints(this.m_BestIndividual)) {
this.m_BestIndividual = (AbstractEAIndividual) this.population.getBestEAIndividual().clone();
this.m_BestIndividual.putData(partBestFitKey, this.m_BestIndividual.getFitness().clone());
this.m_BestIndividual.putData(partBestPosKey, ((InterfaceDataTypeDouble) this.m_BestIndividual).getDoubleData());
// System.out.println("new best: "+m_BestIndividual.toString());
@ -1447,10 +1447,10 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
*/
protected void updatePopulation() {
updateTopology(this.m_Population);
updateTopology(this.population);
for (int i = 0; i < this.m_Population.size(); i++) {
this.updateIndividual(i, (AbstractEAIndividual) m_Population.get(i), this.m_Population);
for (int i = 0; i < this.population.size(); i++) {
this.updateIndividual(i, (AbstractEAIndividual) population.get(i), this.population);
}
if (m_Show) {
@ -1467,8 +1467,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
this.topoPlot.setTopology((Interface2DBorderProblem) this.m_Problem);
}
for (int i = 0; i < this.m_Population.size(); i++) {
tmpIndy1 = (InterfaceDataTypeDouble) this.m_Population.get(i);
for (int i = 0; i < this.population.size(); i++) {
tmpIndy1 = (InterfaceDataTypeDouble) this.population.get(i);
popRep.addDPoint(new DPoint(tmpIndy1.getDoubleData()[0], tmpIndy1.getDoubleData()[1]));
}
this.topoPlot.getFunctionArea().addDElement(popRep);
@ -1651,33 +1651,47 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
*/
protected void show() {
if (this.m_Plot == null) {
InterfaceDataTypeDouble indy = (InterfaceDataTypeDouble) this.m_Population.get(0);
InterfaceDataTypeDouble indy = (InterfaceDataTypeDouble) this.population.get(0);
double[][] range = indy.getDoubleRange();
this.m_Plot = new Plot("PSO " + m_Population.getGeneration(), "x1", "x2", range[0], range[1]);
this.m_Plot = new Plot("PSO " + population.getGeneration(), "x1", "x2", range[0], range[1]);
// this.m_Plot.setUnconnectedPoint(range[0][0], range[1][0], 0);
// this.m_Plot.setUnconnectedPoint(range[0][1], range[1][1], 0);
}
}
/**
* This method allows you to add the LectureGUI as listener to the Optimizer
*
* @param ea
*/
@Override
public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
this.m_Listener = ea;
if (this.changeListener == null) {
this.changeListener = new Vector<InterfacePopulationChangedEventListener>();
}
this.changeListener.add(ea);
}
@Override
public boolean removePopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
if (m_Listener == ea) {
m_Listener = null;
if (changeListener != null && changeListener.removeElement(ea)) {
return true;
} else {
return false;
}
}
/**
* Something has changed
*
* @param name
*/
protected void firePropertyChangedEvent(String name) {
if (this.m_Listener != null) {
this.m_Listener.registerPopulationStateChanged(this, name);
if (this.changeListener != null) {
for (int i = 0; i < this.changeListener.size(); i++) {
this.changeListener.get(i).registerPopulationStateChanged(this, name);
}
}
}
@ -1708,7 +1722,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
result += "Particle Swarm Optimization:\n";
result += "Optimization Problem: ";
result += this.m_Problem.getStringRepresentationForProblem(this) + "\n";
result += this.m_Population.getStringRepresentation();
result += this.population.getStringRepresentation();
return result;
}
@ -1740,12 +1754,12 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
@Override
public Population getPopulation() {
return this.m_Population;
return this.population;
}
@Override
public void setPopulation(Population pop) {
this.m_Population = pop;
this.population = pop;
if (pop.size() != pop.getTargetSize()) { // new particle count!
tracedVelocity = null;
initByPopulation(null, false);