//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
// 
//  You should have received a copy of the GNU Lesser General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

package org.uma.jmetal.util.archive.impl;

import org.uma.jmetal.solution.Solution;
import org.uma.jmetal.util.AdaptiveGrid;
import org.uma.jmetal.util.comparator.DominanceComparator;

import java.util.Comparator;
import java.util.Iterator;

/**
 * This class implements an archive (solution list) based on an adaptive grid used in PAES
 *
 * @author Antonio J. Nebro <antonio@lcc.uma.es>
 * @author Juan J. Durillo
 */
@SuppressWarnings("serial")
public class AdaptiveGridArchive<S extends Solution<?>> extends AbstractBoundedArchive<S> {

  private AdaptiveGrid<S> grid;

  private Comparator<S> dominanceComparator;

  /**
   * Constructor.
   *
   * @param maxSize    The maximum size of the setArchive
   * @param bisections The maximum number of bi-divisions for the adaptive
   *                   grid.
   * @param objectives The number of objectives.
   */
  public AdaptiveGridArchive(int maxSize, int bisections, int objectives) {
    super(maxSize);
    dominanceComparator = new DominanceComparator<S>();
    grid = new AdaptiveGrid<S>(bisections, objectives);
  }

  /**
   * Adds a <code>Solution</code> to the setArchive. If the <code>Solution</code>
   * is dominated by any member of the setArchive then it is discarded. If the
   * <code>Solution</code> dominates some members of the setArchive, these are
   * removed. If the setArchive is full and the <code>Solution</code> has to be
   * inserted, one <code>Solution</code> of the most populated hypercube of the
   * adaptive grid is removed.
   *
   * @param solution The <code>Solution</code>
   * @return true if the <code>Solution</code> has been inserted, false
   * otherwise.
   */
  @Override
  public boolean add(S solution) {
    //Iterator of individuals over the list
    Iterator<S> iterator = getSolutionList().iterator();

    while (iterator.hasNext()) {
      S element = iterator.next();
      int flag = dominanceComparator.compare(solution, element);
      if (flag == -1) { // The Individual to insert dominates other
        // individuals in  the setArchive
        iterator.remove(); //Delete it from the setArchive
        int location = grid.location(element);
        if (grid.getLocationDensity(location) > 1) {//The hypercube contains
          grid.removeSolution(location);            //more than one individual
        } else {
          grid.updateGrid(getSolutionList());
        }
      }
      else if (flag == 1) { // An Individual into the file dominates the
        // solution to insert
        return false; // The solution will not be inserted
      }
    }

    // At this point, the solution may be inserted
    if (this.size() == 0) { //The setArchive is empty
      this.getSolutionList().add(solution);
      grid.updateGrid(getSolutionList());
      return true;
    }

    if (this.getSolutionList().size() < this.getMaxSize()) { //The setArchive is not full
      grid.updateGrid(solution, getSolutionList()); // Update the grid if applicable
      int location;
      location = grid.location(solution); // Get the location of the solution
      grid.addSolution(location); // Increment the density of the hypercube
      getSolutionList().add(solution); // Add the solution to the list
      return true;
    }

    // At this point, the solution has to be inserted and the setArchive is full
    grid.updateGrid(solution, getSolutionList());
    int location = grid.location(solution);
    if (location == grid.getMostPopulatedHypercube()) { // The solution is in the
      // most populated hypercube
      return false; // Not inserted
    } else {
      // Remove an solution from most populated area
      prune();
      // A solution from most populated hypercube has been removed,
      // insert now the solution
      grid.addSolution(location);
      getSolutionList().add(solution);
    }
    return true;
  }

  public AdaptiveGrid<S> getGrid() {
    return grid;
  }
  
  public void prune() {
    Iterator<S> iterator = getSolutionList().iterator();
    boolean removed = false;
    while (iterator.hasNext()) {
      if (!removed) {
        S element = iterator.next();
        int location = grid.location(element);
        if (location == grid.getMostPopulatedHypercube()) {
          iterator.remove();
          grid.removeSolution(location);
          removed = true ;
        }
      }
    }
  }

  @Override
  public Comparator<S> getComparator() {
    return null ; // TODO
  }

  @Override
  public void computeDensityEstimator() {
    // TODO
  }
}

@@ -4665,37 +4665,8 @@
 ();%0A
-    boolean removed = false;%0A
     
@@ -4698,32 +4698,8 @@
 ) %7B%0A
-      if (!removed) %7B%0A  
     
@@ -4731,26 +4731,24 @@
 xt();%0A      
- 
- 
 int location
@@ -4766,34 +4766,32 @@
 ation(element);%0A
-  
       if (locati
@@ -4836,26 +4836,24 @@
 ) %7B%0A        
-  
 iterator.rem
@@ -4859,34 +4859,32 @@
 move();%0A        
- 
- 
 grid.removeSolut
@@ -4910,36 +4910,15 @@
     
-  removed = true ;%0A        %7D
+return;
 %0A   

Fix a bug in class AdaptiveGridArchive