/** Test of reinitialiseContext method, of class CooperativeDataClusteringPSOIterationStrategy. */
  @Test
  public void testReinitialiseContext() {
    Rand.setSeed(0);
    DataClusteringPSO instance = new DataClusteringPSO();

    SlidingWindow window = new SlidingWindow();
    window.setSourceURL("library/src/test/resources/datasets/iris2.arff");
    QuantisationErrorMinimisationProblem problem = new QuantisationErrorMinimisationProblem();
    problem.setWindow(window);
    problem.setDomain("R(-5.12:5.12)");
    IterationStrategy strategy = new StandardDataClusteringIterationStrategy();
    CentroidBoundaryConstraint constraint = new CentroidBoundaryConstraint();
    constraint.setDelegate(new RandomBoundaryConstraint());
    strategy.setBoundaryConstraint(constraint);
    instance.setOptimisationProblem(problem);
    DataDependantPopulationInitialisationStrategy init =
        new DataDependantPopulationInitialisationStrategy();

    init.setEntityType(new ClusterParticle());
    init.setEntityNumber(2);
    instance.setInitialisationStrategy(init);

    instance.setOptimisationProblem(problem);
    instance.addStoppingCondition(new MeasuredStoppingCondition());

    CooperativePSO cooperative = new CooperativePSO();
    cooperative.addStoppingCondition(
        new MeasuredStoppingCondition(new Iterations(), new Maximum(), 30));
    cooperative.addPopulationBasedAlgorithm(instance);
    cooperative.setOptimisationProblem(problem);

    cooperative.performInitialisation();

    ClusterParticle particleBefore = instance.getTopology().head().getClone();

    cooperative.run();

    ClusterParticle particleAfter = instance.getTopology().head().getClone();

    Assert.assertFalse(particleAfter.getPosition().containsAll(particleBefore.getPosition()));
  }
  @Override
  public void performIteration(PSO algorithm) {
    delegate.performIteration(algorithm);
    Topology<Particle> topology = algorithm.getTopology();

    // calculate vAvg
    Vector avgV =
        Vectors.mean(
            Lists.transform(
                topology,
                new Function<Particle, Vector>() {
                  @Override
                  public Vector apply(Particle f) {
                    return (Vector) f.getVelocity();
                  }
                }));

    Vector.Builder builder = Vector.newBuilder();
    for (Numeric n : avgV) {
      if (Math.abs(n.doubleValue()) > vMax.getParameter()) {
        builder.add(vMax.getParameter());
      } else {
        builder.add(n);
      }
    }

    avgV = builder.build();

    // mutation
    Particle gBest = Topologies.getBestEntity(topology, new SocialBestFitnessComparator());
    Particle mutated = gBest.getClone();
    Vector pos = (Vector) gBest.getBestPosition();
    final Bounds bounds = pos.boundsOf(0);

    pos =
        pos.plus(
            avgV.multiply(
                new Supplier<Number>() {
                  @Override
                  public Number get() {
                    return distribution.getRandomNumber() * bounds.getRange()
                        + bounds.getLowerBound();
                  }
                }));

    mutated.setCandidateSolution(pos);
    mutated.calculateFitness();

    if (gBest.getBestFitness().compareTo(mutated.getFitness()) < 0) {
      gBest.getProperties().put(EntityType.Particle.BEST_FITNESS, mutated.getBestFitness());
      gBest.getProperties().put(EntityType.Particle.BEST_POSITION, mutated.getBestPosition());
    }
  }
  /**
   * Structure of Dynamic Heterogeneous iteration strategy:
   *
   * <ol>
   *   <li>For each particle:
   *   <li>Check if particle must change its behavior
   *   <li>If particle must change its behavior:
   *       <ol>
   *         <li>Select a new behavior to the particle from the behavior pool
   *       </ol>
   *   <li>Perform normal iteration
   * </ol>
   *
   * @see
   *     net.sourceforge.cilib.pso.iterationstrategies.SynchronousIterationStrategy#performIteration()
   */
  @Override
  public void performIteration(PSO algorithm) {
    checkState(
        behaviorPool.size() > 0, "You must add particle behaviors to the behavior pool first.");

    for (Entity e : algorithm.getTopology()) {
      Particle p = (Particle) e;

      if (detectionStrategy.detect(p)) {
        p.setParticleBehavior(behaviorSelectionRecipe.on(behaviorPool).select());
      }
    }

    iterationStrategy.performIteration(algorithm);
  }
Exemple #4
0
 @Override
 public void algorithmIteration() {
   multiSwarmsIterationStrategy.performIteration(this);
 }