@Override
  public Vector get(Particle particle) {
    Vector localGuide = (Vector) particle.getLocalGuide(); // personal best (yi)
    Vector globalGuide = (Vector) particle.getGlobalGuide(); // global best (y^i)

    Vector.Builder builder = Vector.newBuilder();
    for (int i = 0; i < particle.getDimension(); ++i) {
      // double tmp1 = cognitive.getParameter();
      // double tmp2 = social.getParameter();

      double sigma = Math.abs(localGuide.doubleValueOf(i) - globalGuide.doubleValueOf(i));
      if (sigma == 0) {
        sigma = 1;
      }
      // System.out.println("Sigma: "+sigma);
      // according to Kennedy
      double mean = (localGuide.doubleValueOf(i) + globalGuide.doubleValueOf(i)) / 2;
      // andries proposal: double mean = (tmp1*personalBestPosition.getReal(i) +
      // tmp2*nBestPosition.getReal(i)) / (tmp1+tmp2);
      if (Rand.nextDouble() < p) {
        builder.add(
            localGuide.doubleValueOf(i) + this.cauchyDistribution.getRandomNumber(mean, sigma));
      } else {
        builder.add(
            globalGuide.doubleValueOf(i) + this.gaussianDistribution.getRandomNumber(mean, sigma));
      }
    }
    return builder.build();
  }
Example #2
0
  @Override
  public Vector get(Particle particle) {
    Vector velocity = (Vector) particle.getVelocity();
    Vector position = (Vector) particle.getPosition();
    PSO algorithm = (PSO) AbstractAlgorithm.get();
    int ns = (int) nSize.getParameter();
    fj.data.List<Particle> neighbours =
        algorithm
            .getTopology()
            .sort(
                Ord.ord(
                    VectorBasedFunctions.sortByDistance(particle, new EuclideanDistanceMeasure())))
            .take(ns);

    Vector.Builder builder = Vector.newBuilder();
    for (int i = 0; i < particle.getDimension(); ++i) {
      double informationSum = 0.0;
      double randomSum = 0;

      for (Particle currentTarget : neighbours) {
        Vector currentTargetPosition = (Vector) currentTarget.getBestPosition();
        double randomComponent = Rand.nextDouble() * (4.1 / ns);
        informationSum += randomComponent * currentTargetPosition.doubleValueOf(i);
        randomSum += randomComponent;
      }

      double value =
          inertiaWeight.getParameter()
              * (velocity.doubleValueOf(i)
                  + randomSum * ((informationSum / (ns * randomSum) - position.doubleValueOf(i))));
      builder.add(value);
    }

    return builder.build();
  }
  /**
   * Update personal best if and only if the particle is within the bounds of the search space /
   * problem.
   *
   * @param particle The particle to update.
   */
  @Override
  public void updatePersonalBest(Particle particle) {
    if (!Types.isInsideBounds(particle.getPosition())) {
      particle.getProperties().put(EntityType.FITNESS, InferiorFitness.instance());
      return;
    }

    delegate.updatePersonalBest(particle);
  }
Example #4
0
  @Test
  public void testGetCandidateSolutions() {
    Vector v1 = Vector.of(1.0, 2.0, 3.0);
    Vector v2 = Vector.of(4.0, 5.0, 6.0);

    Particle p1 = new StandardParticle();
    Particle p2 = new StandardParticle();

    p1.setCandidateSolution(v1);
    p2.setCandidateSolution(v2);

    List<Vector> list = Entities.<Vector>getCandidateSolutions(Arrays.asList(p1, p2));

    assertEquals(v1, list.get(0));
    assertEquals(v2, list.get(1));
  }
  @Override
  public Vector get(Particle particle) {
    Vector newPos = (Vector) delegate.get(particle);

    Particle tmp = particle.getClone();
    tmp.setPosition(newPos);
    Fitness newFitness = particle.getBehaviour().getFitnessCalculator().getFitness(tmp);

    final UniformDistribution uniform = new UniformDistribution();
    Vector newPBest =
        newPos.plus(
            Vector.newBuilder()
                .repeat(newPos.size(), Real.valueOf(1.0))
                .build()
                .multiply(
                    new P1<Number>() {
                      @Override
                      public Number _1() {
                        return uniform.getRandomNumber(
                            -granularity.getParameter(), granularity.getParameter());
                      }
                    }));
    tmp.setPosition(newPos);
    Fitness newPBestFitness = particle.getBehaviour().getFitnessCalculator().getFitness(tmp);

    if (newPBestFitness.compareTo(newFitness) < 0) {
      Vector tmpVector = Vector.copyOf(newPos);
      newPos = newPBest;
      newPBest = tmpVector;

      newPBestFitness = newFitness;
    }

    double dot =
        ((Vector) particle.getNeighbourhoodBest().getBestPosition())
            .subtract(newPos)
            .dot(newPBest.subtract(newPos));

    if (dot < 0) {
      return (Vector) particle.getPosition();
    }

    particle.put(Property.BEST_POSITION, newPBest);
    particle.put(Property.BEST_FITNESS, newPBestFitness);

    return newPos;
  }
  /** {@inheritDoc} */
  @Override
  public int compare(E o1, E o2) {
    SinglePopulationBasedAlgorithm populationBasedAlgorithm =
        (SinglePopulationBasedAlgorithm) AbstractAlgorithm.getAlgorithmList().index(0);
    MOOptimisationProblem problem =
        ((MOOptimisationProblem) populationBasedAlgorithm.getOptimisationProblem());

    Particle p1 = (Particle) o1;
    Particle p2 = (Particle) o2;
    MOFitness fitness1 = ((MOFitness) problem.getFitness(p1.getBestPosition()));
    MOFitness fitness2 = ((MOFitness) problem.getFitness(p2.getBestPosition()));

    int value = fitness1.compareTo(fitness2);
    if (fitness1.compareTo(fitness2) == 0) {
      int random = Rand.nextInt(20);
      if (random > 10) value *= -1;
    }
    return value;
  }
Example #7
0
  /** {@inheritDoc} */
  @Override
  public StringType getValue(Algorithm algorithm) {
    final StringBuilder tmp = new StringBuilder();
    final PSO pso = (PSO) algorithm;
    for (Particle particle : pso.getTopology()) {
      tmp.append("\nParticle: ");
      tmp.append(" Current Fitness: ");
      tmp.append(particle.getFitness().getValue());
      tmp.append(" Best Fitness: ");
      tmp.append(particle.getBestFitness().getValue());
      tmp.append(" Position: ");

      Vector v = (Vector) particle.getPosition();
      for (int j = 0; j < particle.getDimension(); ++j) {
        tmp.append(v.doubleValueOf(j));
        tmp.append(" ");
      }
    }

    return new StringType(tmp.toString());
  }
  /**
   * 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);
        }
      }
    }
  }