From b363e46ad1fce532f1b58820fce2bf00649fd602 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Fri, 24 Jan 2014 18:03:17 +0100 Subject: [PATCH] Implement several CLI params for PSO Refactor PSO implementation --- src/eva2/cli/Main.java | 50 ++++++- .../optimization/enums/PSOTopologyEnum.java | 4 +- .../DynamicParticleSwarmOptimization.java | 16 +-- .../optimization/strategies/NichePSO.java | 2 +- .../strategies/ParticleSwarmOptimization.java | 136 +++++++++--------- 5 files changed, 123 insertions(+), 85 deletions(-) diff --git a/src/eva2/cli/Main.java b/src/eva2/cli/Main.java index 9ba850ae..930ebad2 100644 --- a/src/eva2/cli/Main.java +++ b/src/eva2/cli/Main.java @@ -4,6 +4,7 @@ import com.google.gson.*; import eva2.OptimizerFactory; import eva2.optimization.OptimizationStateListener; import eva2.optimization.enums.DETypeEnum; +import eva2.optimization.enums.PSOTopologyEnum; import eva2.optimization.go.InterfacePopulationChangedEventListener; import eva2.optimization.modules.OptimizationParameters; import eva2.optimization.operator.crossover.CrossoverESDefault; @@ -15,8 +16,6 @@ import eva2.optimization.operator.selection.SelectXProbRouletteWheel; import eva2.optimization.operator.terminators.CombinedTerminator; import eva2.optimization.operator.terminators.FitnessValueTerminator; import eva2.optimization.population.Population; -import eva2.optimization.problems.AbstractOptimizationProblem; -import eva2.optimization.problems.AbstractProblemDouble; import eva2.optimization.problems.AbstractProblemDoubleOffset; import eva2.optimization.strategies.DifferentialEvolution; import eva2.optimization.strategies.InterfaceOptimizer; @@ -493,15 +492,42 @@ public class Main implements OptimizationStateListener, InterfacePopulationChang break; } case "ParticleSwarmOptimization": { - opt.addOption("initialVelocity", true, "Initial Velocity"); + double phi1 = 2.05, phi2 = 2.05, speedLimit = 0.1; + int topoRange = 2; + PSOTopologyEnum selectedTopology = PSOTopologyEnum.star; + opt.addOption("speedLimit", true, "Speed Limit"); opt.addOption("topology", true, "Particle Swarm Topology (0-7)"); - opt.addOption("swarmRadius", true, "Radius of the Swarm"); opt.addOption("phi1", true, "Phi 1"); opt.addOption("phi2", true, "Phi 2"); - opt.addOption("inertnessOrChi", true, "Inertness or Chi"); - opt.addOption("algType", true, "Type of PSO"); + /** + * Parse default options. + */ + try { + commandLine = cliParser.parse(opt, optimizerParams); + } catch (ParseException e) { + showHelp(opt); + System.exit(-1); + } + + if (commandLine.hasOption("phi1")) { + phi1 = Double.parseDouble(commandLine.getOptionValue("phi1")); + } + + if (commandLine.hasOption("phi2")) { + phi2 = Double.parseDouble(commandLine.getOptionValue("phi2")); + } + + if (commandLine.hasOption("topology")) { + selectedTopology = PSOTopologyEnum.getFromId(Integer.parseInt(commandLine.getOptionValue("topology"))); + } + + if (commandLine.hasOption("speedLimit")) { + speedLimit = Double.parseDouble(commandLine.getOptionValue("speedLimit")); + } + + this.optimizer = OptimizerFactory.createParticleSwarmOptimization(problem, this.populationSize, phi1, phi2, speedLimit, selectedTopology, topoRange, this); break; } case "EvolutionStrategies": { @@ -537,6 +563,18 @@ public class Main implements OptimizationStateListener, InterfacePopulationChang pc = 0.9; } + if (commandLine.hasOption("mu")) { + mu = Integer.parseInt(commandLine.getOptionValue("mu")); + } + + if (commandLine.hasOption("lambda")) { + lambda = Integer.parseInt(commandLine.getOptionValue("lambda")); + } + + if (commandLine.hasOption("plusStrategy")) { + plusStrategy = Boolean.parseBoolean(commandLine.getOptionValue("plusStrategy")); + } + this.optimizer = OptimizerFactory.createEvolutionStrategy(mu, lambda, plusStrategy, this.mutator, pm, this.crossover, pc, this.selection, problem, this); break; } diff --git a/src/eva2/optimization/enums/PSOTopologyEnum.java b/src/eva2/optimization/enums/PSOTopologyEnum.java index fa05846c..7cec33c6 100644 --- a/src/eva2/optimization/enums/PSOTopologyEnum.java +++ b/src/eva2/optimization/enums/PSOTopologyEnum.java @@ -37,7 +37,7 @@ public enum PSOTopologyEnum { * @param oldID * @return */ - public static PSOTopologyEnum translateOldID(int oldID) { + public static PSOTopologyEnum getFromId(int oldID) { switch (oldID) { case 0: return linear; @@ -56,7 +56,7 @@ public enum PSOTopologyEnum { case 7: return dms; default: - System.err.println("Error: invalid old topology ID in PSOTopologyEnum translateOldID! Returning grid."); + System.err.println("Error: invalid old topology ID in PSOTopologyEnum getFromId! Returning grid."); return grid; } } diff --git a/src/eva2/optimization/strategies/DynamicParticleSwarmOptimization.java b/src/eva2/optimization/strategies/DynamicParticleSwarmOptimization.java index 908bfe75..2d0eca55 100644 --- a/src/eva2/optimization/strategies/DynamicParticleSwarmOptimization.java +++ b/src/eva2/optimization/strategies/DynamicParticleSwarmOptimization.java @@ -151,7 +151,7 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization //double[] rand = getUniformRandVect(position.length, range); Mathematics.vvAdd(newPos, rand, newPos); - if (m_CheckRange) { + if (checkRange) { Mathematics.projectToRange(newPos, range); } @@ -256,9 +256,9 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization /* old ways curVelocity[i] = this.m_Inertness * velocity[i]; - curVelocity[i] += (this.m_Phi1 * getSpeedLimit(index) * (range[i][1] - range[i][0]) * RNG.randomDouble(-1., 1.)); + curVelocity[i] += (this.phi1 * getSpeedLimit(index) * (range[i][1] - range[i][0]) * RNG.randomDouble(-1., 1.)); // the component from the social model - curVelocity[i] += this.m_Phi2*RNG.randomDouble(0,1)*(localBestPos[i]-curPosition[i]); + curVelocity[i] += this.phi2*RNG.randomDouble(0,1)*(localBestPos[i]-curPosition[i]); */ for (int i = 0; i < lastVelocity.length; i++) { @@ -301,9 +301,9 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization * @return a double value */ protected double getIndySocialModifier(int index, double chiVal) { - return (this.m_Phi2 * chiVal * RNG.randomDouble(0, 1)); -// if (index < 50) return (this.m_Phi2 * chi * RNG.randomDouble(0,1)); -// else return (this.m_Phi2 * chi * (-1.) * RNG.randomDouble(0,1)); + return (this.phi2 * chiVal * RNG.randomDouble(0, 1)); +// if (index < 50) return (this.phi2 * chi * RNG.randomDouble(0,1)); +// else return (this.phi2 * chi * (-1.) * RNG.randomDouble(0,1)); } /** @@ -315,12 +315,12 @@ public class DynamicParticleSwarmOptimization extends ParticleSwarmOptimization @Override protected double getSpeedLimit(int index) { if (index >= ((double) (m_Population.size() * highEnergyRatio))) { - return m_SpeedLimit; + return speedLimit; } else { if (highEnergyRaise == 0.) { return maxSpeedLimit; } else { - return m_SpeedLimit * highEnergyRaise; + return speedLimit * highEnergyRaise; } } } diff --git a/src/eva2/optimization/strategies/NichePSO.java b/src/eva2/optimization/strategies/NichePSO.java index 86b501b7..c2fbdbe3 100644 --- a/src/eva2/optimization/strategies/NichePSO.java +++ b/src/eva2/optimization/strategies/NichePSO.java @@ -1017,7 +1017,7 @@ public class NichePSO implements InterfaceAdditionalPopulationInformer, Interfac // } public void SetMainSwarmTopologyTag(int mainSwarmTopologyTag) { // Possible topologies are: "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" in that order starting by 0. - this.mainSwarmTopology = PSOTopologyEnum.translateOldID(mainSwarmTopologyTag); + this.mainSwarmTopology = PSOTopologyEnum.getFromId(mainSwarmTopologyTag); } public PSOTopologyEnum getMainSwarmTopology() { diff --git a/src/eva2/optimization/strategies/ParticleSwarmOptimization.java b/src/eva2/optimization/strategies/ParticleSwarmOptimization.java index deb3c9c5..9034559e 100644 --- a/src/eva2/optimization/strategies/ParticleSwarmOptimization.java +++ b/src/eva2/optimization/strategies/ParticleSwarmOptimization.java @@ -50,7 +50,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se Object[] sortedPop = null; protected AbstractEAIndividual m_BestIndividual = null; protected InterfaceOptimizationProblem m_Problem = new F1Problem(); - protected boolean m_CheckRange = true; + protected boolean checkRange = true; protected boolean checkSpeedLimit = false; protected boolean useAlternative = false; protected PSOTopologyEnum topology = PSOTopologyEnum.grid; @@ -59,13 +59,13 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * constriction (using chi) */ protected SelectedTag algType; - protected int m_TopologyRange = 2; - protected double m_InitialVelocity = 0.2; - protected double m_SpeedLimit = 0.1; - protected double m_Phi1 = 2.05; - protected double m_Phi2 = 2.05; + protected int topologyRange = 2; + protected double initialVelocity = 0.2; + protected double speedLimit = 0.1; + protected double phi1 = 2.05; + protected double phi2 = 2.05; // for multi-swarm topology: radius of the swarm relative to the range - protected double m_swarmRadius = 0.2; + protected double swarmRadius = 0.2; // for multi-swarm: maximum sub swarm size. zero means unlimited protected int maxSubSwarmSize = 0; protected int minSubSwarmSize = 2; @@ -131,12 +131,12 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se this.m_Population = (Population) a.m_Population.clone(); this.m_Problem = a.m_Problem; this.m_Identifier = a.m_Identifier; - this.m_InitialVelocity = a.m_InitialVelocity; - this.m_SpeedLimit = a.m_SpeedLimit; - this.m_Phi1 = a.m_Phi1; - this.m_Phi2 = a.m_Phi2; + this.initialVelocity = a.initialVelocity; + this.speedLimit = a.speedLimit; + this.phi1 = a.phi1; + this.phi2 = a.phi2; this.inertnessOrChi = a.inertnessOrChi; - this.m_TopologyRange = a.m_TopologyRange; + this.topologyRange = a.topologyRange; this.paramControl = (ParameterControlManager) a.paramControl.clone(); //this.setCheckSpeedLimit(a.isCheckSpeedLimit()); } @@ -155,7 +155,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se algType.setSelectedTag(1); // set to constriction m_Population = new Population(popSize); setPhiValues(p1, p2); - m_TopologyRange = topoRange; + topologyRange = topoRange; topology = topo; } @@ -481,14 +481,14 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se treeLevels = 0; // the HPSO tree will contain layers 0...HPSOLevels, the last one is "incomplete" with only HPSOOrphans number of nodes if (getTopology() == PSOTopologyEnum.hpso || getTopology() == PSOTopologyEnum.tree) { - if (m_TopologyRange < 2) { + if (topologyRange < 2) { System.err.println("Error, tree/hpso requires topology range of at least 2!"); } else { - while (getMaxNodes(m_TopologyRange, treeLevels) < m_Population.size()) { + while (getMaxNodes(topologyRange, treeLevels) < m_Population.size()) { treeLevels++; } - treeOrphans = m_Population.size() - getMaxNodes(m_TopologyRange, treeLevels - 1); - treeLastFullLevelNodeCnt = (int) Math.pow(m_TopologyRange, treeLevels - 1); + treeOrphans = m_Population.size() - getMaxNodes(topologyRange, treeLevels - 1); + treeLastFullLevelNodeCnt = (int) Math.pow(topologyRange, treeLevels - 1); } } if (getTopology() == PSOTopologyEnum.dms) { @@ -511,7 +511,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se indy = (AbstractEAIndividual) pop.get(i); if (indy instanceof InterfaceDataTypeDouble) { if (!externalInitialPop || (!defaultsDone(indy))) { - initIndividualDefaults(indy, m_InitialVelocity); + initIndividualDefaults(indy, initialVelocity); } } indy.putData(indexKey, i); @@ -602,7 +602,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se } protected void resetIndividual(AbstractEAIndividual indy) { - resetIndividual(indy, m_InitialVelocity); + resetIndividual(indy, initialVelocity); plotIndy(((InterfaceDataTypeDouble) indy).getDoubleData(), null, (Integer) indy.getData(indexKey)); } @@ -666,7 +666,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se } // enforce range constraints if necessary - if (m_CheckRange) { + if (checkRange) { ensureConstraints(curPosition, curVelocity, range); } @@ -792,8 +792,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se // if (algType.getSelectedTag().getID()==1) chi=inertnessOrChi; // else chi = 1.; // -// double scaleCog = this.m_Phi1*chi*RNG.randomDouble(0,1); -// double scaleNei = this.m_Phi2*chi*RNG.randomDouble(0,1); +// double scaleCog = this.phi1*chi*RNG.randomDouble(0,1); +// double scaleNei = this.phi2*chi*RNG.randomDouble(0,1); // // // for (int i=0; i 0) { // its found and its not the root. root has no parent to check for - k = getParentIndex(m_TopologyRange, sortedIndex, pop.size()); + k = getParentIndex(topologyRange, sortedIndex, pop.size()); compareAndSetAttractor(localBestFitness, localBestPosition, (AbstractEAIndividual) sortedPop[k], useHistoric); } if (treeStruct == 1) { // loop all children if (isComplete(sortedIndex, pop.size())) { // the node has full degree - k = m_TopologyRange * sortedIndex + 1; // this is the offset of the nodes children - for (int i = 0; i < m_TopologyRange; i++) { + k = topologyRange * sortedIndex + 1; // this is the offset of the nodes children + for (int i = 0; i < topologyRange; i++) { compareAndSetAttractor(localBestFitness, localBestPosition, (AbstractEAIndividual) sortedPop[k + i], useHistoric); } } else if (isIncomplete(sortedIndex, pop.size())) { // the node does not have full degree but might have orphans @@ -1129,7 +1129,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se break; case hpso: // Hierarchical PSO if (index >= 0) { - k = getParentIndex(m_TopologyRange, index, pop.size()); + k = getParentIndex(topologyRange, index, pop.size()); // compareAndSet(localBestFitness, localBestPosition, (AbstractEAIndividual)pop.get(k), useHistoric); indy = (AbstractEAIndividual) pop.get(k); System.arraycopy((double[]) indy.getData(partBestFitKey), 0, localBestFitness, 0, localBestFitness.length); @@ -1137,8 +1137,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se } break; - case random: // m_TopologyRange random informants, may be the same several times - for (int i = 0; i < m_TopologyRange; i++) { + case random: // topologyRange random informants, may be the same several times + for (int i = 0; i < topologyRange; i++) { // select random informant indy = (AbstractEAIndividual) pop.get(RNG.randomInt(0, pop.size() - 1)); // set local values @@ -1245,7 +1245,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se for (int i = 0; i < curPosition.length; i++) { newPosition[i] = curPosition[i] + curVelocity[i]; } - if (m_CheckRange && isOutOfRange(newPosition, range)) { + if (checkRange && isOutOfRange(newPosition, range)) { System.err.println("error, individual violates constraints!"); } @@ -1254,7 +1254,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se ((InterfaceDataTypeDouble) indy).setDoubleGenotype(newPosition); } else { ((InterfaceDataTypeDouble) indy).setDoubleGenotype(newPosition); // WARNING, this does a checkBounds in any case! - if (!m_CheckRange) { + if (!checkRange) { System.err.println("warning, checkbounds will be forced by InterfaceESIndividual!"); } } @@ -1516,7 +1516,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se for (int i = 0; i < leaders.size(); i++) { dist = metric.distance((AbstractEAIndividual) sortedPop[cur], leaders.get(i)); //System.out.println("dist is "+dist); - if ((m_swarmRadius * 2.) > dist) { // a formal leader is found + if ((swarmRadius * 2.) > dist) { // a formal leader is found int sSize = (Integer) (leaders.get(i)).getData(multiSwSizeKey); if ((maxSubSwarmSize > 0) && (sSize >= maxSubSwarmSize)) { // swarm is too big already @@ -1546,7 +1546,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se cur++; } // for (int i=0; imetric.distance((AbstractEAIndividual)pop.get(i), (AbstractEAIndividual)pop.get(index)))) { +// if ((i!=index) && ((swarmRadius*2.)>metric.distance((AbstractEAIndividual)pop.get(i), (AbstractEAIndividual)pop.get(index)))) { // this.compareAndSet(localBestFitness, localBestPosition, (AbstractEAIndividual)pop.get(i)); // } // } @@ -1563,7 +1563,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se AbstractEAIndividualComparator comp = new AbstractEAIndividualComparator(partBestFitKey); for (int i = 0; i < pop.size(); i++) { // loop over the part of the tree which is complete (full degree in each level) - parentIndex = getParentIndex(m_TopologyRange, i, pop.size()); + parentIndex = getParentIndex(topologyRange, i, pop.size()); if (comp.compare(pop.get(i), pop.get(parentIndex)) < 0) { // sibling is dominant! // so switch them indy = (AbstractEAIndividual) pop.get(i); @@ -1755,7 +1755,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se if (indy == null) { System.err.println("Error in PSO.setPopulation!"); } else if (!indy.hasData(partTypeKey)) { - initIndividualDefaults(indy, m_InitialVelocity); + initIndividualDefaults(indy, initialVelocity); initIndividualMemory(indy); indy.putData(indexKey, i); indy.setIndividualIndex(i); @@ -1787,11 +1787,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * @param f */ public void setInitialVelocity(double f) { - this.m_InitialVelocity = f; + this.initialVelocity = f; } public double getInitialVelocity() { - return this.m_InitialVelocity; + return this.initialVelocity; } public String initialVelocityTipText() { @@ -1810,11 +1810,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se if (k > 1) { k = 1; } - this.m_SpeedLimit = k; + this.speedLimit = k; } public double getSpeedLimit() { - return this.m_SpeedLimit; + return this.speedLimit; } /** @@ -1824,7 +1824,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * @return */ protected double getSpeedLimit(int index) { - return this.m_SpeedLimit; + return this.speedLimit; } public String speedLimitTipText() { @@ -1853,8 +1853,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se if (!getAlgoType().isSelectedString("Constriction")) { System.err.println("Warning, PSO algorithm variant constriction expected!"); } - m_Phi1 = tau1; - m_Phi2 = tau2; + phi1 = tau1; + phi2 = tau2; setInertnessOrChi(2. / (Math.abs(2 - pSum - Math.sqrt((pSum * pSum) - (4 * pSum))))); } } @@ -1882,14 +1882,14 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * @param l */ public void setPhi1(double l) { - this.m_Phi1 = l; + this.phi1 = l; if (algType.getSelectedTag().getID() == 1) { setConstriction(getPhi1(), getPhi2()); } } public double getPhi1() { - return this.m_Phi1; + return this.phi1; } public String phi1TipText() { @@ -1902,14 +1902,14 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * @param l */ public void setPhi2(double l) { - this.m_Phi2 = l; + this.phi2 = l; if (algType.getSelectedTag().getID() == 1) { setConstriction(getPhi1(), getPhi2()); } } public double getPhi2() { - return this.m_Phi2; + return this.phi2; } public String phi2TipText() { @@ -1924,8 +1924,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * @param phi2 */ public void setPhiValues(double phi1, double phi2) { - m_Phi1 = phi1; - m_Phi2 = phi2; + this.phi1 = phi1; + this.phi2 = phi2; if (algType.isSelectedString("Constriction")) { setConstriction(phi1, phi2); } @@ -1940,8 +1940,8 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * @param inertness */ public void setParameterValues(double phi1, double phi2, double inertness) { - m_Phi1 = phi1; - m_Phi2 = phi2; + this.phi1 = phi1; + this.phi2 = phi2; setInertnessOrChi(inertness); } @@ -2008,11 +2008,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * @param s The range. */ public void setTopologyRange(int s) { - this.m_TopologyRange = s; + this.topologyRange = s; } public int getTopologyRange() { - return this.m_TopologyRange; + return this.topologyRange; } public String topologyRangeTipText() { @@ -2025,11 +2025,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * @param s Check Constraints. */ public void setCheckRange(boolean s) { - this.m_CheckRange = s; + this.checkRange = s; } public boolean isCheckRange() { - return this.m_CheckRange; + return this.checkRange; } public String checkRangeTipText() { @@ -2102,11 +2102,11 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se } public double getSubSwarmRadius() { - return m_swarmRadius; + return swarmRadius; } public void setSubSwarmRadius(double radius) { - m_swarmRadius = radius; + swarmRadius = radius; } public String subSwarmRadiusTipText() {