Permalink
Browse files

Added parallel/enhanced VBPSO. Renamed/deleted a few niche related cl…

…asses.
  • Loading branch information...
filinep committed May 2, 2012
1 parent d747cd9 commit f3e65e8cc743b1cba2e30d8ab4f429969aba084c
@@ -32,22 +32,24 @@
import net.sourceforge.cilib.pso.PSO;
import net.sourceforge.cilib.pso.iterationstrategies.SynchronousIterationStrategy;
-
-public class Speciation extends AbstractIterationStrategy<PSO> {
+/**
+ * Reinitialises redundant particles but leaves the neighbourhood bests intact.
+ */
+public class EnhancedSpeciation extends AbstractIterationStrategy<PSO> {
private IterationStrategy<PSO> delegate;
- public Speciation() {
+ public EnhancedSpeciation() {
this.delegate = (IterationStrategy) new SynchronousIterationStrategy();
}
- public Speciation(Speciation copy) {
+ public EnhancedSpeciation(EnhancedSpeciation copy) {
this.delegate = copy.delegate.getClone();
}
@Override
- public Speciation getClone() {
- return new Speciation(this);
+ public EnhancedSpeciation getClone() {
+ return new EnhancedSpeciation(this);
}
@Override
@@ -1,88 +0,0 @@
-/**
- * Computational Intelligence Library (CIlib)
- * Copyright (C) 2003 - 2010
- * Computational Intelligence Research Group (CIRG@UP)
- * Department of Computer Science
- * University of Pretoria
- * South Africa
- *
- * This library is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, see <http://www.gnu.org/licenses/>.
- */
-package net.sourceforge.cilib.niching.merging.detection;
-
-import net.sourceforge.cilib.algorithm.population.PopulationBasedAlgorithm;
-import net.sourceforge.cilib.entity.Particle;
-import net.sourceforge.cilib.entity.Topologies;
-import net.sourceforge.cilib.entity.comparator.SocialBestFitnessComparator;
-import net.sourceforge.cilib.type.types.container.Vector;
-
-/**
- * A merge detection strategy which indicates swarms can merge if they overlap
- * and if they are moving in the same direction. They are moving in the same
- * direction if the dot product of the velocities of the best particle from each
- * swarm is greater than zero i.e. the angle between their direction of motion is
- * greater than zero and less than 90 degrees.
- */
-public class DirectionBasedMergeDetection extends MergeDetection {
-
- private MergeDetection mergeDetector;
-
- /**
- * Default constructor.
- */
- public DirectionBasedMergeDetection() {
- this.mergeDetector = new RadiusOverlapMergeDetection();
- }
-
- /**
- * Only sub-swarms moving towards the same niche will be merged into one if
- * the areas they cover in search space overlap.
- *
- * @param swarm1 The first subSwarm
- * @param swarm2 The second subSwarm
- * @return True if the swarms overlap and are moving in the same direction,
- * false otherwise.
- */
- @Override
- public Boolean f(PopulationBasedAlgorithm swarm1, PopulationBasedAlgorithm swarm2) {
- Particle swarm1Best = (Particle) Topologies.getBestEntity(swarm1.getTopology(), new SocialBestFitnessComparator());
- Particle swarm2Best = (Particle) Topologies.getBestEntity(swarm2.getTopology(), new SocialBestFitnessComparator());
-
- Vector velocity1 = ((Vector) swarm1Best.getVelocity()).normalize();
- Vector velocity2 = ((Vector) swarm2Best.getVelocity()).normalize();
-
- double direction = velocity1.dot(velocity2);
-
- return direction > 0 && mergeDetector.f(swarm1, swarm2);
- }
-
- /**
- * Sets the merge detection strategy to use in combination with this one.
- * Mainly used to change parameters of the radius overlap of sub-swarms.
- *
- * @param intersectionDetector
- */
- public void setMergeDetector(MergeDetection mergeDetector) {
- this.mergeDetector = mergeDetector;
- }
-
- /**
- * Gets the merge detection strategy used with this one.
- *
- * @return
- */
- public MergeDetection getMergeDetector() {
- return mergeDetector;
- }
-}
@@ -0,0 +1,101 @@
+/**
+ * Computational Intelligence Library (CIlib)
+ * Copyright (C) 2003 - 2010
+ * Computational Intelligence Research Group (CIRG@UP)
+ * Department of Computer Science
+ * University of Pretoria
+ * South Africa
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+package net.sourceforge.cilib.pso.positionprovider;
+
+import com.google.common.base.Supplier;
+import net.sourceforge.cilib.controlparameter.ConstantControlParameter;
+import net.sourceforge.cilib.controlparameter.ControlParameter;
+import net.sourceforge.cilib.entity.Particle;
+import net.sourceforge.cilib.math.random.UniformDistribution;
+import net.sourceforge.cilib.problem.Fitness;
+import net.sourceforge.cilib.type.types.container.Vector;
+
+public class VectorBasedPositionProvider implements PositionProvider {
+
+ private ControlParameter granularity;
+ private PositionProvider delegate;
+
+ public VectorBasedPositionProvider() {
+ this.granularity = ConstantControlParameter.of(0.5);
+ this.delegate = new StandardPositionProvider();
+ }
+
+ public VectorBasedPositionProvider(VectorBasedPositionProvider copy) {
+ this.granularity = copy.granularity.getClone();
+ this.delegate = copy.delegate.getClone();
+ }
+
+ @Override
+ public PositionProvider getClone() {
+ return new VectorBasedPositionProvider(this);
+ }
+
+ @Override
+ public Vector get(Particle particle) {
+ Vector newPos = delegate.get(particle);
+
+ Particle tmp = particle.getClone();
+ tmp.setCandidateSolution(newPos);
+ Fitness newFitness = particle.getFitnessCalculator().getFitness(tmp);
+
+ final UniformDistribution uniform = new UniformDistribution();
+ Vector newPBest = newPos.multiply(new Supplier<Number>() {
+ @Override
+ public Number get() {
+ return uniform.getRandomNumber(-granularity.getParameter(), granularity.getParameter());
+ }
+ }).plus(newPos);
+ tmp.setCandidateSolution(newPos);
+ Fitness newPBestFitness = particle.getFitnessCalculator().getFitness(tmp);
+
+ if (newPBestFitness.compareTo(newFitness) < 0) {
+ Vector tmpVector = Vector.copyOf(newPos);
+ newPos = newPBest;
+ newPBest = tmpVector;
+ }
+
+ double dot = ((Vector) particle.getNeighbourhoodBest().getBestPosition())
+ .subtract(newPos).dot(newPBest.subtract(newPos));
+
+ if (dot < 0) {
+ return (Vector) particle.getPosition();
+ }
+
+ return newPos;
+ }
+
+ public void setDelegate(PositionProvider delegate) {
+ this.delegate = delegate;
+ }
+
+ public PositionProvider getDelegate() {
+ return delegate;
+ }
+
+ public void setGranularity(ControlParameter granularity) {
+ this.granularity = granularity;
+ }
+
+ public ControlParameter getGranularity() {
+ return granularity;
+ }
+}
@@ -1,94 +0,0 @@
-/**
- * Computational Intelligence Library (CIlib)
- * Copyright (C) 2003 - 2010
- * Computational Intelligence Research Group (CIRG@UP)
- * Department of Computer Science
- * University of Pretoria
- * South Africa
- *
- * This library is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, see <http://www.gnu.org/licenses/>.
- */
-package net.sourceforge.cilib.niching.merging.detection;
-
-import net.sourceforge.cilib.entity.EntityType;
-import net.sourceforge.cilib.entity.Particle;
-import net.sourceforge.cilib.entity.topologies.GBestTopology;
-import net.sourceforge.cilib.niching.NichingFunctionsTest;
-import net.sourceforge.cilib.problem.MinimisationFitness;
-import net.sourceforge.cilib.pso.PSO;
-import net.sourceforge.cilib.type.types.container.Vector;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- *
- * @author filipe
- */
-public class DirectionBasedMergeDetectionTest {
- @Test
- public void testFalseMergeDetection() {
- DirectionBasedMergeDetection detector = new DirectionBasedMergeDetection();
-
- PSO pso1 = new PSO();
- PSO pso2 = new PSO();
-
- pso1.setTopology(new GBestTopology());
- pso2.setTopology(new GBestTopology());
-
- Particle p1 = NichingFunctionsTest.createParticle(new MinimisationFitness(0.0), Vector.of(0.0, 1.0));
- p1.getProperties().put(EntityType.Particle.VELOCITY, Vector.of(1.0, 1.0));
-
- Particle p2 = NichingFunctionsTest.createParticle(new MinimisationFitness(1.0), Vector.of(0.0, 0.0));
- p2.getProperties().put(EntityType.Particle.VELOCITY, Vector.of(-1.0, -1.0));
-
- Particle p3 = NichingFunctionsTest.createParticle(new MinimisationFitness(2.0), Vector.of(1.0, 1.0));
- p3.getProperties().put(EntityType.Particle.VELOCITY, Vector.of(-1.0, -1.0));
-
- Particle p4 = NichingFunctionsTest.createParticle(new MinimisationFitness(3.0), Vector.of(1.0, 0.0));
- p4.getProperties().put(EntityType.Particle.VELOCITY, Vector.of(1.0, 2.0));
-
- pso1.getTopology().add(p1); pso1.getTopology().add(p2);
- pso2.getTopology().add(p3); pso2.getTopology().add(p4);
-
- Assert.assertFalse(detector.f(pso1, pso2));
- }
-
- @Test
- public void testTrueMergeDetection() {
- DirectionBasedMergeDetection detector = new DirectionBasedMergeDetection();
-
- PSO pso1 = new PSO();
- PSO pso2 = new PSO();
-
- pso1.setTopology(new GBestTopology());
- pso2.setTopology(new GBestTopology());
-
- Particle p1 = NichingFunctionsTest.createParticle(new MinimisationFitness(0.0), Vector.of(0.0, 1.0));
- p1.getProperties().put(EntityType.Particle.VELOCITY, Vector.of(1.0, 1.0));
-
- Particle p2 = NichingFunctionsTest.createParticle(new MinimisationFitness(1.0), Vector.of(0.0, 0.0));
- p2.getProperties().put(EntityType.Particle.VELOCITY, Vector.of(-1.0, -1.0));
-
- Particle p3 = NichingFunctionsTest.createParticle(new MinimisationFitness(2.0), Vector.of(1.0, 1.0));
- p3.getProperties().put(EntityType.Particle.VELOCITY, Vector.of(1.0, 0.3));
-
- Particle p4 = NichingFunctionsTest.createParticle(new MinimisationFitness(3.0), Vector.of(1.0, 0.0));
- p4.getProperties().put(EntityType.Particle.VELOCITY, Vector.of(1.0, 2.0));
-
- pso1.getTopology().add(p1); pso1.getTopology().add(p2);
- pso2.getTopology().add(p3); pso2.getTopology().add(p4);
-
- Assert.assertTrue(detector.f(pso1, pso2));
- }
-}
View
@@ -14,7 +14,7 @@
</algorithm>
<algorithm id="speciationWithRedundantParticleReinitialization" class="pso.PSO">
- <iterationStrategy class="niching.iterationstrategies.Speciation"/>
+ <iterationStrategy class="niching.iterationstrategies.EnhancedSpeciation"/>
<topology class="entity.topologies.SpeciesSeedTopology">
<radius class="controlparameter.ConstantControlParameter" parameter="0.1"/>
</topology>
Oops, something went wrong.

0 comments on commit f3e65e8

Please sign in to comment.