Exemplo n.º 1
0
  private void calculateNewNiche() {
    setCurrentNiche(getCurrentNiche() + 1);
    VBPSO sub = mainSwarm.getClone();
    sub.getTopology().clear();
    yhead.setNicheID(getCurrentNiche());
    yhead.setNeighbourhoodBest(yhead.getClone());
    sub.getTopology().add(yhead);

    for (VBParticle p : mainSwarm.getTopology()) {
      if ((p.getDotProduct() > 0 && p.getRadius() < nicheRadius)
          || (p.getBestPosition().equals(yhead.getBestPosition()))) {
        p.setNicheID(getCurrentNiche());
        p.setNeighbourhoodBest(yhead.getClone());
        sub.getTopology().add(p);
      }
    }

    for (VBParticle p : sub.getTopology()) {
      mainSwarm.getTopology().remove(p);
    }
    VBParticle tmp = yhead.getClone();
    while (sub.getTopology().size() < 3) {
      tmp.reinitialise();
      tmp.setNeighbourhoodBest(yhead.getClone());
      tmp.setPersonalBest(tmp.getPosition().getClone());
      sub.getTopology().add(tmp);
    }

    subPopulationsAlgorithms.add(sub);
  }
Exemplo n.º 2
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--;
     }
   }
 }