/**
   * 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 #2
0
  /** {@inheritDoc} */
  @Override
  protected void algorithmIteration() {
    for (HoneyBee bee : workerBees) {
      bee.updatePosition();
      if (bestBee == null) {
        bestBee = bee.getClone();
      } else if (bee.getBestFitness().compareTo(bestBee.getBestFitness()) > 0) {
        bestBee = bee.getClone();
      }
    }

    for (HoneyBee bee : onlookerBees) {
      HoneyBee selectedBee = dancingSelectionStrategy.on(workerBees).select();
      bee.setPosition(Vector.copyOf((Vector) selectedBee.getPosition()));
      bee.updatePosition();
      if (bestBee == null) {
        bestBee = bee;
      } else if (bee.getBestFitness().compareTo(bestBee.getBestFitness()) > 0) {
        bestBee = bee;
      }
    }
  }
  /**
   * Perform an iteration of the DE algorithm defined as the DE/x/y/z implementation but including a
   * parameter adaptation step.
   *
   * @param ec The {@linkplain EC} on which to perform this iteration.
   */
  @Override
  public void performIteration(EC ec) {
    Topology<SaDEIndividual> topology = (Topology<SaDEIndividual>) ec.getTopology();

    for (int i = 0; i < topology.size(); i++) {
      SaDEIndividual current = topology.get(i);
      current.calculateFitness();

      // Create the trial vector by applying mutation
      SaDEIndividual targetEntity =
          (SaDEIndividual) targetVectorSelectionStrategy.on(topology).exclude(current).select();

      // Create the trial vector / entity
      SaDEIndividual trialEntity =
          (SaDEIndividual)
              current.getTrialVectorCreationStrategy().create(targetEntity, current, topology);

      // Create the offspring by applying cross-over
      List<SaDEIndividual> offspring =
          (List<SaDEIndividual>)
              current
                  .getCrossoverStrategy()
                  .crossover(Arrays.asList(current, trialEntity)); // Order is VERY important here!!

      // Replace the parent (current) if the offspring is better
      SaDEIndividual offspringEntity = offspring.get(0);
      boundaryConstraint.enforce(offspringEntity);
      offspringEntity.calculateFitness();

      if (offspringEntity.getFitness().compareTo(current.getFitness())
          > 0) { // the trial vector is better than the parent
        topology.set(i, offspringEntity); // Replace the parent with the offspring individual
      }

      topology.get(i).updateParameters();
    }
  }