/** {@inheritDoc} */ @Override public void absorb(Niche algorithm) { for (PopulationBasedAlgorithm pba : algorithm.getPopulations()) { RadiusVisitor radiusVisitor = new RadiusVisitor(); pba.accept(radiusVisitor); double radius = radiusVisitor.getResult().doubleValue(); Topology<? extends Entity> mainSwarmTopology = algorithm.getMainSwarm().getTopology(); for (int i = 0; i < mainSwarmTopology.size(); i++) { Entity entity = mainSwarmTopology.get(i); double distance = distanceMeasure.distance( entity.getCandidateSolution(), Topologies.getBestEntity(pba.getTopology()).getCandidateSolution()); if (distance <= radius) { Particle p = (Particle) entity; p.setVelocityProvider(new GCVelocityProvider()); p.setNeighbourhoodBest((Particle) Topologies.getBestEntity(pba.getTopology())); Topology<Particle> topology = (Topology<Particle>) pba.getTopology(); topology.add(p); algorithm.getMainSwarm().getTopology().remove(entity); } } } }
/** Test velocity clamping. */ @Test public void testClamping() { Particle particle = createParticle(Vector.of(0.0)); Particle nBest = createParticle(Vector.of(1.0)); particle.setNeighbourhoodBest(nBest); nBest.setNeighbourhoodBest(nBest); ClampingVelocityProvider velocityProvider = new ClampingVelocityProvider(); velocityProvider.setVMax(new ConstantControlParameter(0.5)); Vector velocity = velocityProvider.get(particle); for (Numeric number : velocity) { Assert.assertTrue(Double.compare(number.doubleValue(), 0.5) <= 0); Assert.assertTrue(Double.compare(number.doubleValue(), -0.5) >= 0); } }