Example #1
0
 @Override
 public OptimisationSolution getBestSolution() {
   OptimisationSolution bestSolution = subPopulationsAlgorithms.get(0).getBestSolution();
   for (PopulationBasedAlgorithm currentAlgorithm : subPopulationsAlgorithms) {
     if (bestSolution.compareTo(currentAlgorithm.getBestSolution()) < 0) {
       bestSolution = currentAlgorithm.getBestSolution();
     }
   }
   return bestSolution;
 }
  /**
   * Perform an algorithm iteration, then restart the {@linkplain Algorithm} and increment the
   * number of restarts.
   */
  @Override
  public void algorithmIteration() {
    algorithm.run();
    singleIteration.reset();

    OptimisationSolution tmp = optimisationAlgorithm.getBestSolution();
    if (tmp.getFitness().compareTo(fitness) > 0) {
      fitness = tmp.getFitness();
      solution = tmp;
    }

    if (algorithm.isFinished()) {
      problem.resetFitnessCounter();
      algorithm.performInitialisation();
      ++restarts;
    }
  }
  /**
   * This is an Synchronous strategy:
   *
   * <ol>
   *   <li>For all particles:
   *       <ol>
   *         <li>Update the particle velocity
   *         <li>Update the particle position
   *       </ol>
   *   <li>For all particles:
   *       <ol>
   *         <li>Calculate the particle fitness
   *         <li>For all particles in the current particle's neighbourhood:
   *             <ol>
   *               <li>Update the neighbourhood best
   *             </ol>
   *       </ol>
   * </ol>
   *
   * @see
   *     net.sourceforge.cilib.PSO.IterationStrategy#performIteration(net.sourceforge.cilib.PSO.PSO)
   * @param pso The {@link PSO} to have an iteration applied.
   */
  @Override
  public void performIteration(PSO pso) {
    Topology<Particle> topology = pso.getTopology();

    for (Particle current : topology) {
      current.updateVelocity();
      current.updatePosition(); // TODO: replace with visitor (will simplify particle interface)

      boundaryConstraint.enforce(current);
    }

    Problem problem = AbstractAlgorithm.getAlgorithmList().get(0).getOptimisationProblem();

    for (Particle current : topology) {
      current.calculateFitness();
      for (Particle other : topology.neighbourhood(current)) {
        Particle p1 = current.getNeighbourhoodBest().getClone();
        Particle p2 = other.getNeighbourhoodBest().getClone();
        OptimisationSolution s1 =
            new OptimisationSolution(
                p1.getCandidateSolution().getClone(),
                problem.getFitness(p1.getCandidateSolution().getClone()));
        OptimisationSolution s2 =
            new OptimisationSolution(
                p2.getCandidateSolution().getClone(),
                problem.getFitness(p2.getCandidateSolution().getClone()));
        MOFitness fitness1 = (MOFitness) s1.getFitness();
        MOFitness fitness2 = (MOFitness) s2.getFitness();
        //                System.out.println("fitness1 = ");
        //                for (int i=0; i < fitness1.getDimension(); i++)
        //                    System.out.println(fitness1.getFitness(i).getValue());
        //
        //                System.out.println("fitness2 = ");
        //                for (int i=0; i < fitness2.getDimension(); i++)
        //                    System.out.println(fitness2.getFitness(i).getValue());
        if (fitness1.compareTo(fitness2) > 0) {
          other.setNeighbourhoodBest(current);
        }
      }
    }
  }
Example #4
0
  @Override
  public double getPercentageCompleted(Algorithm algorithm) {
    // check if this is the first iteration
    if (previousBest == null) {
      previousBest = algorithm.getBestSolution();

      return 0.0;
    }

    // get the distance between previous and current best
    double distance =
        distMeasure.distance(
            (Vector) previousBest.getPosition(),
            (Vector) algorithm.getBestSolution().getPosition());

    // compare to see change
    if (distance < minChange) minChangeCounter++;
    else minChangeCounter = 0;

    return minChangeCounter / maxConsecutiveMinChange;
  }