/** {@inheritDoc} */
  @Override
  public void absorb(Niche algorithm) {
    for (PopulationBasedAlgorithm pba : algorithm.getPopulations()) {
      RadiusVisitor radiusVisitor = new RadiusVisitor();
      pba.accept(radiusVisitor);

      double radius = radiusVisitor.getResult().doubleValue();

      Topology<? extends Entity> mainSwarmTopology = algorithm.getMainSwarm().getTopology();
      for (int i = 0; i < mainSwarmTopology.size(); i++) {
        Entity entity = mainSwarmTopology.get(i);
        double distance =
            distanceMeasure.distance(
                entity.getCandidateSolution(),
                Topologies.getBestEntity(pba.getTopology()).getCandidateSolution());
        if (distance <= radius) {
          Particle p = (Particle) entity;
          p.setVelocityProvider(new GCVelocityProvider());
          p.setNeighbourhoodBest((Particle) Topologies.getBestEntity(pba.getTopology()));
          Topology<Particle> topology = (Topology<Particle>) pba.getTopology();
          topology.add(p);
          algorithm.getMainSwarm().getTopology().remove(entity);
        }
      }
    }
  }
  /*
   * Adds the data patterns closest to a centrid to its data pattern list
   * @param candidateSolution The solution holding all the centroids
   * @param dataset The dataset holding all the data patterns
   */
  public void assignDataPatternsToParticle(CentroidHolder candidateSolution, DataTable dataset) {
    double euclideanDistance;
    Vector addedPattern;
    DistanceMeasure aDistanceMeasure = new EuclideanDistanceMeasure();

    for (int i = 0; i < dataset.size(); i++) {
      euclideanDistance = Double.POSITIVE_INFINITY;
      addedPattern = Vector.of();
      Vector pattern = ((StandardPattern) dataset.getRow(i)).getVector();
      int centroidIndex = 0;
      int patternIndex = 0;
      for (ClusterCentroid centroid : candidateSolution) {
        if (aDistanceMeasure.distance(centroid.toVector(), pattern) < euclideanDistance) {
          euclideanDistance = aDistanceMeasure.distance(centroid.toVector(), pattern);
          addedPattern = Vector.copyOf(pattern);
          patternIndex = centroidIndex;
        }
        centroidIndex++;
      }

      candidateSolution.get(patternIndex).addDataItem(euclideanDistance, addedPattern);
    }
  }
Beispiel #3
0
  private void createNiches() {
    calculateNeighbourhoodBest();
    double smallR = 1000000000000000.0;
    double radius;

    for (VBParticle p : mainSwarm.getTopology()) {
      p.setNeighbourhoodBest(yhead.getClone());
      p.calculateVG();
      p.calculateDotProduct();
      radius = distanceMeaser.distance(yhead.getBestPosition(), p.getPosition());
      p.setRadius(radius);
      if (radius < smallR && p.getDotProduct() < 0) {
        smallR = radius;
      }
    }
    nicheRadius = smallR;
    calculateNewNiche();
  }
Beispiel #4
0
 private void merge() {
   if (subPopulationsAlgorithms.size() < 2) return;
   VBPSO subi, subj;
   double d1, d2;
   VBParticle yi, yj, swap;
   for (int i = 0; i < subPopulationsAlgorithms.size() - 1; i++) {
     subi = (VBPSO) subPopulationsAlgorithms.get(i);
     yi = (VBParticle) subi.getTopology().get(0).getNeighbourhoodBest();
     for (int j = i + 1; j < subPopulationsAlgorithms.size(); j++) {
       subj = (VBPSO) subPopulationsAlgorithms.get(j);
       yj = (VBParticle) subj.getTopology().get(0).getNeighbourhoodBest();
       d1 = distanceMeaser.distance(yi.getBestPosition(), yj.getBestPosition());
       if (d1 < granularity) {
         if (yi.getBestFitness().compareTo(yj.getBestFitness()) >= 0.0) {
           int n = subj.getTopology().size();
           VBParticle p;
           if (n == 1) {
             swap = subj.getTopology().get(0).getClone();
             swap.setNeighbourhoodBest(yi);
             swap.setNicheID(i);
             swap.calculateVP();
             swap.calculateVG();
             swap.calculateDotProduct();
             subi.getTopology().add(swap);
             subj.getTopology().remove(0);
           } else {
             for (int k = 0; k < n; k++) {
               p = subj.getTopology().get(k);
               d2 = distanceMeaser.distance(p.getBestPosition(), yi.getBestPosition());
               if (d2 < granularity) {
                 swap = p.getClone();
                 swap.setNeighbourhoodBest(yi);
                 swap.setNicheID(i);
                 swap.calculateVP();
                 swap.calculateVG();
                 swap.calculateDotProduct();
                 subi.getTopology().add(swap);
                 subj.getTopology().remove(k);
                 k--;
                 n--;
               } else if (n == 1) {
                 swap = subj.getTopology().get(0).getClone();
                 swap.setNeighbourhoodBest(yi);
                 swap.setNicheID(i);
                 swap.calculateVP();
                 swap.calculateVG();
                 swap.calculateDotProduct();
                 subi.getTopology().add(swap);
                 subj.getTopology().remove(0);
                 break;
               }
             }
           }
         } else {
           int n = subi.getTopology().size();
           VBParticle p;
           if (n == 1) {
             swap = subi.getTopology().get(0);
             swap.setNeighbourhoodBest(yj);
             swap.setNicheID(j);
             swap.calculateVP();
             swap.calculateVG();
             swap.calculateDotProduct();
             subj.getTopology().add(swap);
             subi.getTopology().remove(0);
           } else {
             for (int k = 0; k < n; k++) {
               p = subi.getTopology().get(k);
               d2 = distanceMeaser.distance(p.getPosition(), yj.getBestPosition());
               if (d2 < granularity) {
                 swap = p.getClone();
                 swap.setNeighbourhoodBest(yj);
                 swap.setNicheID(j);
                 swap.calculateVP();
                 swap.calculateVG();
                 swap.calculateDotProduct();
                 subj.getTopology().add(swap);
                 subi.getTopology().remove(k);
                 k--;
                 n--;
               } else if (n == 1) {
                 swap = subi.getTopology().get(0);
                 swap.setNeighbourhoodBest(yj);
                 swap.setNicheID(j);
                 swap.calculateVP();
                 swap.calculateVG();
                 swap.calculateDotProduct();
                 subj.getTopology().add(swap);
                 subi.getTopology().remove(0);
               }
             }
           }
         }
       }
       subPopulationsAlgorithms.set(j, subj);
       if (subj.getTopology().size() == 0) {
         subPopulationsAlgorithms.remove(j);
         j--;
       }
     }
     subPopulationsAlgorithms.set(i, subi);
     if (subi.getTopology().size() == 0) {
       subPopulationsAlgorithms.remove(i);
       i--;
     }
   }
 }