Replacing SelectedTag with Enum for PSO topologies.

This commit is contained in:
Marcel Kronfeld 2009-05-04 15:30:59 +00:00
parent a39054a51f
commit 55655911fd
4 changed files with 85 additions and 38 deletions

View File

@ -10,6 +10,7 @@ import eva2.server.go.IndividualInterface;
import eva2.server.go.InterfacePopulationChangedEventListener; import eva2.server.go.InterfacePopulationChangedEventListener;
import eva2.server.go.InterfaceTerminator; import eva2.server.go.InterfaceTerminator;
import eva2.server.go.enums.DETypeEnum; import eva2.server.go.enums.DETypeEnum;
import eva2.server.go.enums.PSOTopologyEnum;
import eva2.server.go.enums.PostProcessMethod; import eva2.server.go.enums.PostProcessMethod;
import eva2.server.go.individuals.AbstractEAIndividual; import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.InterfaceDataTypeBinary; import eva2.server.go.individuals.InterfaceDataTypeBinary;
@ -428,7 +429,7 @@ public class OptimizerFactory {
AbstractOptimizationProblem problem, int popsize, double phi1, AbstractOptimizationProblem problem, int popsize, double phi1,
double phi2, double k, double phi2, double k,
InterfacePopulationChangedEventListener listener, InterfacePopulationChangedEventListener listener,
int selectedTopology) { PSOTopologyEnum selectedTopology) {
problem.initProblem(); problem.initProblem();
@ -444,7 +445,8 @@ public class OptimizerFactory {
pso.setPhi1(phi1); pso.setPhi1(phi1);
pso.setPhi2(phi2); pso.setPhi2(phi2);
pso.setSpeedLimit(k); pso.setSpeedLimit(k);
pso.getTopology().setSelectedTag(selectedTopology); // pso.getTopology().setSelectedTag(selectedTopology);
pso.setTopology(selectedTopology);
pso.addPopulationChangedEventListener(listener); pso.addPopulationChangedEventListener(listener);
pso.init(); pso.init();
@ -1278,7 +1280,8 @@ public class OptimizerFactory {
AbstractOptimizationProblem problem) { AbstractOptimizationProblem problem) {
ParticleSwarmOptimization pso = new ParticleSwarmOptimization(); ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
pso.setPhiValues(2.05, 2.05); pso.setPhiValues(2.05, 2.05);
pso.getTopology().setSelectedTag("Grid"); // pso.getTopology().setSelectedTag("Grid");
pso.setTopology(PSOTopologyEnum.grid);
return makeParams(pso, 30, problem, randSeed, makeDefaultTerminator()); return makeParams(pso, 30, problem, randSeed, makeDefaultTerminator());
} }

View File

@ -0,0 +1,32 @@
package eva2.server.go.enums;
public enum PSOTopologyEnum {
/**
*
*/
linear,
/**
*
*/
grid,
/**
*
*/
star,
/**
*
*/
multiSwarm,
/**
*
*/
tree,
/**
*
*/
hpso,
/**
*
*/
random;
}

View File

@ -11,6 +11,7 @@ import eva2.gui.BeanInspector;
import eva2.gui.GenericObjectEditor; import eva2.gui.GenericObjectEditor;
import eva2.gui.TopoPlot; import eva2.gui.TopoPlot;
import eva2.server.go.InterfacePopulationChangedEventListener; import eva2.server.go.InterfacePopulationChangedEventListener;
import eva2.server.go.enums.PSOTopologyEnum;
import eva2.server.go.individuals.AbstractEAIndividual; import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.AbstractEAIndividualComparator; import eva2.server.go.individuals.AbstractEAIndividualComparator;
import eva2.server.go.individuals.InterfaceDataTypeDouble; import eva2.server.go.individuals.InterfaceDataTypeDouble;
@ -20,7 +21,6 @@ import eva2.server.go.operators.paramcontrol.InterfaceParameterControl;
import eva2.server.go.populations.InterfaceSolutionSet; import eva2.server.go.populations.InterfaceSolutionSet;
import eva2.server.go.populations.Population; import eva2.server.go.populations.Population;
import eva2.server.go.populations.SolutionSet; import eva2.server.go.populations.SolutionSet;
import eva2.server.go.problems.AbstractOptimizationProblem;
import eva2.server.go.problems.F1Problem; import eva2.server.go.problems.F1Problem;
import eva2.server.go.problems.Interface2DBorderProblem; import eva2.server.go.problems.Interface2DBorderProblem;
import eva2.server.go.problems.InterfaceOptimizationProblem; import eva2.server.go.problems.InterfaceOptimizationProblem;
@ -55,7 +55,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
protected boolean m_CheckConstraints = true; protected boolean m_CheckConstraints = true;
protected boolean checkSpeedLimit = false; protected boolean checkSpeedLimit = false;
protected boolean useAlternative = false; protected boolean useAlternative = false;
protected SelectedTag m_Topology; protected PSOTopologyEnum topology = PSOTopologyEnum.grid;
/** /**
* Defines which version of PSO is applied, classical inertness or constriction (using chi) * Defines which version of PSO is applied, classical inertness or constriction (using chi)
*/ */
@ -112,9 +112,9 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
public ParticleSwarmOptimization() { public ParticleSwarmOptimization() {
this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" ); // this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" );
m_Topology.setSelectedTag(1); // m_Topology.setSelectedTag(1);
topology = PSOTopologyEnum.grid;
algType = new SelectedTag("Inertness", "Constriction"); algType = new SelectedTag("Inertness", "Constriction");
algType.setSelectedTag(1); algType.setSelectedTag(1);
@ -123,8 +123,9 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
} }
public ParticleSwarmOptimization(ParticleSwarmOptimization a) { public ParticleSwarmOptimization(ParticleSwarmOptimization a) {
if (a.m_Topology != null) topology=a.topology;
this.m_Topology = (SelectedTag)a.m_Topology.clone(); // if (a.m_Topology != null)
// this.m_Topology = (SelectedTag)a.m_Topology.clone();
if (a.algType != null) if (a.algType != null)
this.algType = (SelectedTag)a.algType.clone(); this.algType = (SelectedTag)a.algType.clone();
this.m_Population = (Population)a.m_Population.clone(); this.m_Population = (Population)a.m_Population.clone();
@ -833,8 +834,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
bestIndy = pop.getBestEAIndividual(); bestIndy = pop.getBestEAIndividual();
} }
switch (this.m_Topology.getSelectedTag().getID()) { switch (topology) {
case 0: case linear:
// linear // linear
for (int x = -this.m_TopologyRange; x <= this.m_TopologyRange; x++) { for (int x = -this.m_TopologyRange; x <= this.m_TopologyRange; x++) {
if (wrapTopology) tmpIndex = (index + x + pop.size()) % pop.size(); if (wrapTopology) tmpIndex = (index + x + pop.size()) % pop.size();
@ -845,7 +846,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
} }
} }
break; break;
case 1: case grid:
// grid // grid
int corner = 1+(int)Math.sqrt(pop.size()); int corner = 1+(int)Math.sqrt(pop.size());
for (int x = -this.m_TopologyRange; x <= this.m_TopologyRange; x++) { for (int x = -this.m_TopologyRange; x <= this.m_TopologyRange; x++) {
@ -857,11 +858,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
} }
} }
break; break;
case 2: case star:
// star: only compare to the absolutely best // star: only compare to the absolutely best
this.compareAndSetAttractor(localBestFitness, localBestPosition, bestIndy, useHistoric); this.compareAndSetAttractor(localBestFitness, localBestPosition, bestIndy, useHistoric);
break; break;
case 3: case multiSwarm:
// self-organised multi-swarms // self-organised multi-swarms
AbstractEAIndividual leader = (AbstractEAIndividual)indy.getData(multiSwTypeKey); AbstractEAIndividual leader = (AbstractEAIndividual)indy.getData(multiSwTypeKey);
if (leader != null) { // refer to position of leader, this may be the individual itself if (leader != null) { // refer to position of leader, this may be the individual itself
@ -880,7 +881,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
System.err.println("no leader present!"); System.err.println("no leader present!");
} }
break; break;
case 4: // Sorted Tree case tree: // Sorted Tree
sortedIndex = (Integer)((AbstractEAIndividual)sortedPop[index]).getData(sortedIndexKey); sortedIndex = (Integer)((AbstractEAIndividual)sortedPop[index]).getData(sortedIndexKey);
if (sortedIndex>0) { // its found and its not the root. root has no parent to check for if (sortedIndex>0) { // its found and its not the root. root has no parent to check for
@ -912,7 +913,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
// } // }
} }
break; break;
case 5: // Hierarchical PSO case hpso: // Hierarchical PSO
if (index>=0) { if (index>=0) {
k = getParentIndex(treeBranchDeg, index, pop.size()); k = getParentIndex(treeBranchDeg, index, pop.size());
// compareAndSet(localBestFitness, localBestPosition, (AbstractEAIndividual)pop.get(k), useHistoric); // compareAndSet(localBestFitness, localBestPosition, (AbstractEAIndividual)pop.get(k), useHistoric);
@ -922,7 +923,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
} }
break; break;
case 6: // m_TopologyRange random informants, may be the same several times case random: // m_TopologyRange random informants, may be the same several times
for (int i=0; i<m_TopologyRange; i++) { for (int i=0; i<m_TopologyRange; i++) {
// select random informant // select random informant
indy = (AbstractEAIndividual)pop.get(RNG.randomInt(0, pop.size()-1)); indy = (AbstractEAIndividual)pop.get(RNG.randomInt(0, pop.size()-1));
@ -1221,15 +1222,16 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
} }
protected void updateTopology(Population pop) { protected void updateTopology(Population pop) {
int topoID = this.m_Topology.getSelectedTag().getID(); // int topoID = this.m_Topology.getSelectedTag().getID();
// this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" );
if ((topoID == 3) || (topoID == 4)) { if ((topology == PSOTopologyEnum.multiSwarm) || (topology == PSOTopologyEnum.tree)) {
sortedPop = pop.toArray(); sortedPop = pop.toArray();
if ((topoID == 3) || (treeStruct>=2)) Arrays.sort(sortedPop, new AbstractEAIndividualComparator()); if ((topology == PSOTopologyEnum.multiSwarm) || (treeStruct>=2)) Arrays.sort(sortedPop, new AbstractEAIndividualComparator());
else Arrays.sort(sortedPop, new AbstractEAIndividualComparator(partBestFitKey)); else Arrays.sort(sortedPop, new AbstractEAIndividualComparator(partBestFitKey));
addSortedIndicesTo(sortedPop, pop); addSortedIndicesTo(sortedPop, pop);
} }
if (topoID == 3) { if (topology == PSOTopologyEnum.multiSwarm) {
// prepare multi swarm topology // prepare multi swarm topology
PhenotypeMetric metric = new PhenotypeMetric(); PhenotypeMetric metric = new PhenotypeMetric();
Vector<AbstractEAIndividual> leaders = new Vector<AbstractEAIndividual>(pop.size()); Vector<AbstractEAIndividual> leaders = new Vector<AbstractEAIndividual>(pop.size());
@ -1283,7 +1285,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
// } // }
//System.out.println(" -- best " + m_Population.indexOf(m_Population.getBestEAIndividual())); //System.out.println(" -- best " + m_Population.indexOf(m_Population.getBestEAIndividual()));
} }
if (topoID == 5) { // HPSO sorting the population if (topology == PSOTopologyEnum.hpso) { // HPSO sorting the population
int parentIndex; int parentIndex;
AbstractEAIndividual indy; AbstractEAIndividual indy;
AbstractEAIndividualComparator comp = new AbstractEAIndividualComparator(partBestFitKey); AbstractEAIndividualComparator comp = new AbstractEAIndividualComparator(partBestFitKey);
@ -1580,21 +1582,30 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
/** This method allows you to choose the topology type. /** This method allows you to choose the topology type.
* @param s The type. * @param s The type.
*/ */
public void setTopology(SelectedTag s) { public void setTopology(PSOTopologyEnum t) {
this.m_Topology = s; this.topology = t;
setGOEShowProperties(getClass()); setGOEShowProperties(getClass());
} }
public void setGOEShowProperties(Class<?> cls) { public void setGOEShowProperties(Class<?> cls) {
GenericObjectEditor.setShowProperty(cls, "topologyRange", (m_Topology.getSelectedTag().getID() < 2) || (m_Topology.getSelectedTag().getID() == 6)); // this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" );
GenericObjectEditor.setShowProperty(cls, "subSwarmRadius", (m_Topology.getSelectedTag().getID() == 3));
GenericObjectEditor.setShowProperty(cls, "maxSubSwarmSize", (m_Topology.getSelectedTag().getID() == 3)); // linear, grid, random
GenericObjectEditor.setShowProperty(cls, "treeStruct", (m_Topology.getSelectedTag().getID() == 4)); GenericObjectEditor.setShowProperty(cls, "topologyRange", (topology==PSOTopologyEnum.linear) || (topology==PSOTopologyEnum.grid) || (topology==PSOTopologyEnum.random));
GenericObjectEditor.setShowProperty(cls, "treeBranchDegree", (m_Topology.getSelectedTag().getID() == 4) || (m_Topology.getSelectedTag().getID() == 5)); // multi swarm
GenericObjectEditor.setShowProperty(cls, "wrapTopology", (m_Topology.getSelectedTag().getID() == 0)); GenericObjectEditor.setShowProperty(cls, "subSwarmRadius", (topology==PSOTopologyEnum.multiSwarm));
// multi swarm
GenericObjectEditor.setShowProperty(cls, "maxSubSwarmSize", (topology==PSOTopologyEnum.multiSwarm));
// tree
GenericObjectEditor.setShowProperty(cls, "treeStruct", (topology==PSOTopologyEnum.tree));
// tree, hpso
GenericObjectEditor.setShowProperty(cls, "treeBranchDegree", (topology==PSOTopologyEnum.tree) || (topology==PSOTopologyEnum.hpso));
// linear
GenericObjectEditor.setShowProperty(cls, "wrapTopology", (topology==PSOTopologyEnum.linear));
} }
public SelectedTag getTopology() {
return this.m_Topology; public PSOTopologyEnum getTopology() {
return topology;
} }
public String topologyTipText() { public String topologyTipText() {
return "Choose the topology type (preliminary)."; return "Choose the topology type (preliminary).";

View File

@ -7,6 +7,7 @@ import eva2.gui.GenericObjectEditor;
import eva2.server.go.InterfaceGOParameters; import eva2.server.go.InterfaceGOParameters;
import eva2.server.go.InterfacePopulationChangedEventListener; import eva2.server.go.InterfacePopulationChangedEventListener;
import eva2.server.go.InterfaceTerminator; import eva2.server.go.InterfaceTerminator;
import eva2.server.go.enums.PSOTopologyEnum;
import eva2.server.go.operators.selection.InterfaceSelection; import eva2.server.go.operators.selection.InterfaceSelection;
import eva2.server.go.operators.terminators.EvaluationTerminator; import eva2.server.go.operators.terminators.EvaluationTerminator;
import eva2.server.go.populations.Population; import eva2.server.go.populations.Population;
@ -184,13 +185,13 @@ public class PSOParameters extends AbstractGOParameters implements InterfaceGOPa
} }
/** This method allows you to choose the topology type. /** This method allows you to choose the topology type.
* @param s The type. * @param t The type.
*/ */
public void setTopology(SelectedTag s) { public void setTopology(PSOTopologyEnum t) {
((ParticleSwarmOptimization)this.m_Optimizer).setTopology(s); ((ParticleSwarmOptimization)this.m_Optimizer).setTopology(t);
((ParticleSwarmOptimization)this.m_Optimizer).setGOEShowProperties(getClass()); ((ParticleSwarmOptimization)this.m_Optimizer).setGOEShowProperties(getClass());
} }
public SelectedTag getTopology() { public PSOTopologyEnum getTopology() {
return ((ParticleSwarmOptimization)this.m_Optimizer).getTopology(); return ((ParticleSwarmOptimization)this.m_Optimizer).getTopology();
} }
public String topologyTipText() { public String topologyTipText() {