diff --git a/src/eva2/OptimizerFactory.java b/src/eva2/OptimizerFactory.java index 57ca7e2b..14865594 100644 --- a/src/eva2/OptimizerFactory.java +++ b/src/eva2/OptimizerFactory.java @@ -10,6 +10,7 @@ import eva2.server.go.IndividualInterface; import eva2.server.go.InterfacePopulationChangedEventListener; import eva2.server.go.InterfaceTerminator; import eva2.server.go.enums.DETypeEnum; +import eva2.server.go.enums.PSOTopologyEnum; import eva2.server.go.enums.PostProcessMethod; import eva2.server.go.individuals.AbstractEAIndividual; import eva2.server.go.individuals.InterfaceDataTypeBinary; @@ -428,7 +429,7 @@ public class OptimizerFactory { AbstractOptimizationProblem problem, int popsize, double phi1, double phi2, double k, InterfacePopulationChangedEventListener listener, - int selectedTopology) { + PSOTopologyEnum selectedTopology) { problem.initProblem(); @@ -444,7 +445,8 @@ public class OptimizerFactory { pso.setPhi1(phi1); pso.setPhi2(phi2); pso.setSpeedLimit(k); - pso.getTopology().setSelectedTag(selectedTopology); +// pso.getTopology().setSelectedTag(selectedTopology); + pso.setTopology(selectedTopology); pso.addPopulationChangedEventListener(listener); pso.init(); @@ -1278,7 +1280,8 @@ public class OptimizerFactory { AbstractOptimizationProblem problem) { ParticleSwarmOptimization pso = new ParticleSwarmOptimization(); 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()); } diff --git a/src/eva2/server/go/enums/PSOTopologyEnum.java b/src/eva2/server/go/enums/PSOTopologyEnum.java new file mode 100644 index 00000000..ea9fb77e --- /dev/null +++ b/src/eva2/server/go/enums/PSOTopologyEnum.java @@ -0,0 +1,32 @@ +package eva2.server.go.enums; + +public enum PSOTopologyEnum { + /** + * + */ + linear, + /** + * + */ + grid, + /** + * + */ + star, + /** + * + */ + multiSwarm, + /** + * + */ + tree, + /** + * + */ + hpso, + /** + * + */ + random; +} diff --git a/src/eva2/server/go/strategies/ParticleSwarmOptimization.java b/src/eva2/server/go/strategies/ParticleSwarmOptimization.java index 77fc7068..fbaae6e0 100644 --- a/src/eva2/server/go/strategies/ParticleSwarmOptimization.java +++ b/src/eva2/server/go/strategies/ParticleSwarmOptimization.java @@ -11,6 +11,7 @@ import eva2.gui.BeanInspector; import eva2.gui.GenericObjectEditor; import eva2.gui.TopoPlot; import eva2.server.go.InterfacePopulationChangedEventListener; +import eva2.server.go.enums.PSOTopologyEnum; import eva2.server.go.individuals.AbstractEAIndividual; import eva2.server.go.individuals.AbstractEAIndividualComparator; 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.Population; import eva2.server.go.populations.SolutionSet; -import eva2.server.go.problems.AbstractOptimizationProblem; import eva2.server.go.problems.F1Problem; import eva2.server.go.problems.Interface2DBorderProblem; 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 checkSpeedLimit = 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) */ @@ -112,9 +112,9 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se public ParticleSwarmOptimization() { - this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" ); - m_Topology.setSelectedTag(1); - +// this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" ); +// m_Topology.setSelectedTag(1); + topology = PSOTopologyEnum.grid; algType = new SelectedTag("Inertness", "Constriction"); algType.setSelectedTag(1); @@ -123,8 +123,9 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se } public ParticleSwarmOptimization(ParticleSwarmOptimization a) { - if (a.m_Topology != null) - this.m_Topology = (SelectedTag)a.m_Topology.clone(); + topology=a.topology; +// if (a.m_Topology != null) +// this.m_Topology = (SelectedTag)a.m_Topology.clone(); if (a.algType != null) this.algType = (SelectedTag)a.algType.clone(); this.m_Population = (Population)a.m_Population.clone(); @@ -833,8 +834,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se bestIndy = pop.getBestEAIndividual(); } - switch (this.m_Topology.getSelectedTag().getID()) { - case 0: + switch (topology) { + case linear: // linear for (int x = -this.m_TopologyRange; x <= this.m_TopologyRange; x++) { if (wrapTopology) tmpIndex = (index + x + pop.size()) % pop.size(); @@ -845,7 +846,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se } } break; - case 1: + case grid: // grid int corner = 1+(int)Math.sqrt(pop.size()); for (int x = -this.m_TopologyRange; x <= this.m_TopologyRange; x++) { @@ -857,11 +858,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se } } break; - case 2: + case star: // star: only compare to the absolutely best this.compareAndSetAttractor(localBestFitness, localBestPosition, bestIndy, useHistoric); break; - case 3: + case multiSwarm: // self-organised multi-swarms AbstractEAIndividual leader = (AbstractEAIndividual)indy.getData(multiSwTypeKey); 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!"); } break; - case 4: // Sorted Tree + case tree: // Sorted Tree sortedIndex = (Integer)((AbstractEAIndividual)sortedPop[index]).getData(sortedIndexKey); 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; - case 5: // Hierarchical PSO + case hpso: // Hierarchical PSO if (index>=0) { k = getParentIndex(treeBranchDeg, index, pop.size()); // compareAndSet(localBestFitness, localBestPosition, (AbstractEAIndividual)pop.get(k), useHistoric); @@ -922,7 +923,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se } 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=2)) Arrays.sort(sortedPop, new AbstractEAIndividualComparator()); + if ((topology == PSOTopologyEnum.multiSwarm) || (treeStruct>=2)) Arrays.sort(sortedPop, new AbstractEAIndividualComparator()); else Arrays.sort(sortedPop, new AbstractEAIndividualComparator(partBestFitKey)); addSortedIndicesTo(sortedPop, pop); } - if (topoID == 3) { + if (topology == PSOTopologyEnum.multiSwarm) { // prepare multi swarm topology PhenotypeMetric metric = new PhenotypeMetric(); Vector leaders = new Vector(pop.size()); @@ -1283,7 +1285,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se // } //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; AbstractEAIndividual indy; 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. * @param s The type. */ - public void setTopology(SelectedTag s) { - this.m_Topology = s; + public void setTopology(PSOTopologyEnum t) { + this.topology = t; setGOEShowProperties(getClass()); } public void setGOEShowProperties(Class cls) { - GenericObjectEditor.setShowProperty(cls, "topologyRange", (m_Topology.getSelectedTag().getID() < 2) || (m_Topology.getSelectedTag().getID() == 6)); - GenericObjectEditor.setShowProperty(cls, "subSwarmRadius", (m_Topology.getSelectedTag().getID() == 3)); - GenericObjectEditor.setShowProperty(cls, "maxSubSwarmSize", (m_Topology.getSelectedTag().getID() == 3)); - GenericObjectEditor.setShowProperty(cls, "treeStruct", (m_Topology.getSelectedTag().getID() == 4)); - GenericObjectEditor.setShowProperty(cls, "treeBranchDegree", (m_Topology.getSelectedTag().getID() == 4) || (m_Topology.getSelectedTag().getID() == 5)); - GenericObjectEditor.setShowProperty(cls, "wrapTopology", (m_Topology.getSelectedTag().getID() == 0)); +// this.m_Topology = new SelectedTag( "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" ); + + // linear, grid, random + GenericObjectEditor.setShowProperty(cls, "topologyRange", (topology==PSOTopologyEnum.linear) || (topology==PSOTopologyEnum.grid) || (topology==PSOTopologyEnum.random)); + // multi swarm + 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() { return "Choose the topology type (preliminary)."; diff --git a/src/eva2/server/modules/PSOParameters.java b/src/eva2/server/modules/PSOParameters.java index 05e3259c..d144ee8f 100644 --- a/src/eva2/server/modules/PSOParameters.java +++ b/src/eva2/server/modules/PSOParameters.java @@ -7,6 +7,7 @@ import eva2.gui.GenericObjectEditor; import eva2.server.go.InterfaceGOParameters; import eva2.server.go.InterfacePopulationChangedEventListener; import eva2.server.go.InterfaceTerminator; +import eva2.server.go.enums.PSOTopologyEnum; import eva2.server.go.operators.selection.InterfaceSelection; import eva2.server.go.operators.terminators.EvaluationTerminator; 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. - * @param s The type. + * @param t The type. */ - public void setTopology(SelectedTag s) { - ((ParticleSwarmOptimization)this.m_Optimizer).setTopology(s); + public void setTopology(PSOTopologyEnum t) { + ((ParticleSwarmOptimization)this.m_Optimizer).setTopology(t); ((ParticleSwarmOptimization)this.m_Optimizer).setGOEShowProperties(getClass()); } - public SelectedTag getTopology() { + public PSOTopologyEnum getTopology() { return ((ParticleSwarmOptimization)this.m_Optimizer).getTopology(); } public String topologyTipText() {