Exemplo n.º 1
0
 /** {@inheritDoc} */
 @Override
 public void randomise() {
   double tmp =
       Rand.nextDouble() * (getBounds().getUpperBound() - getBounds().getLowerBound())
           + getBounds().getLowerBound();
   this.value = Double.valueOf(tmp).intValue();
 }
  @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();
  }
Exemplo n.º 3
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();
  }
  /**
   * After every {@link #interval} iteration, pick {@link #numberOfSentries a number of} random
   * entities from the given {@link Algorithm algorithm's} topology and compare their previous
   * fitness values with their current fitness values. An environment change is detected when the
   * difference between the previous and current fitness values are &gt;= the specified {@link
   * #epsilon} value.
   *
   * @param algorithm used to get hold of topology of entities and number of iterations
   * @return true if a change has been detected, false otherwise
   */
  @Override
  public <A extends HasTopology & Algorithm & HasNeighbourhood> boolean detect(A algorithm) {
    if ((AbstractAlgorithm.get().getIterations() % interval == 0)
        && (AbstractAlgorithm.get().getIterations() != 0)) {
      List all = Java.List_ArrayList().f(algorithm.getTopology());

      for (int i = 0; i < numberOfSentries.getParameter(); i++) {
        // select random sentry entity
        int random = Rand.nextInt(all.size());
        StandardParticle sentry = (StandardParticle) all.get(random);

        // check for change
        // double previousFitness = sentry.getFitness().getValue();

        boolean detectedChange = false;

        if (sentry.getFitness().getClass().getName().matches("MinimisationFitness")) {
          Fitness previousFitness = sentry.getFitness();
          sentry.calculateFitness();
          Fitness currentFitness = sentry.getFitness();

          if (Math.abs(previousFitness.getValue() - currentFitness.getValue()) >= epsilon) {
            detectedChange = true;
          }
        } else if (sentry.getFitness().getClass().getName().matches("StandardMOFitness")) {
          MOFitness previousFitness = (MOFitness) sentry.getFitness();
          sentry.calculateFitness();
          MOFitness currentFitness = (MOFitness) sentry.getFitness();

          for (int k = 0; k < previousFitness.getDimension(); k++)
            if (Math.abs(
                    previousFitness.getFitness(k).getValue()
                        - currentFitness.getFitness(k).getValue())
                >= epsilon) {
              detectedChange = true;
              break;
            }
        }
        if (detectedChange) {
          System.out.println("Detected a change");
          return true;
        }

        // remove the selected element from the all list preventing it from being selected again
        all.remove(random);
      }
    }
    return false;
  }
  /** {@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;
  }
  /** 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()));
  }