Example #1
0
 /**
  * Constructor. Creates a new instance of Spea2Fitness for a given <code>SolutionSet</code>.
  *
  * @param solutionSet The <code>SolutionSet</code>
  */
 public Spea2Fitness(SolutionSet solutionSet) {
   distance = distance_.distanceMatrix(solutionSet);
   solutionSet_ = solutionSet;
   for (int i = 0; i < solutionSet_.size(); i++) {
     solutionSet_.get(i).setLocation(i);
   } // for
 } // Spea2Fitness
Example #2
0
  /**
   * Gets 'size' elements from a population of more than 'size' elements using for this de
   * enviromentalSelection truncation
   *
   * @param size The number of elements to get.
   */
  public SolutionSet environmentalSelection(int size) {

    if (solutionSet_.size() < size) {
      size = solutionSet_.size();
    }

    // Create a new auxiliar population for no alter the original population
    SolutionSet aux = new SolutionSet(solutionSet_.size());

    int i = 0;
    while (i < solutionSet_.size()) {
      if (solutionSet_.get(i).getFitness() < 1.0) {
        aux.add(solutionSet_.get(i));
        solutionSet_.remove(i);
      } else {
        i++;
      } // if
    } // while

    if (aux.size() < size) {
      Comparator comparator = new FitnessComparator();
      solutionSet_.sort(comparator);
      int remain = size - aux.size();
      for (i = 0; i < remain; i++) {
        aux.add(solutionSet_.get(i));
      }
      return aux;
    } else if (aux.size() == size) {
      return aux;
    }

    double[][] distance = distance_.distanceMatrix(aux);
    List<List<DistanceNode>> distanceList = new LinkedList<List<DistanceNode>>();
    for (int pos = 0; pos < aux.size(); pos++) {
      aux.get(pos).setLocation(pos);
      List<DistanceNode> distanceNodeList = new ArrayList<DistanceNode>();
      for (int ref = 0; ref < aux.size(); ref++) {
        if (pos != ref) {
          distanceNodeList.add(new DistanceNode(distance[pos][ref], ref));
        } // if
      } // for
      distanceList.add(distanceNodeList);
    } // for

    for (int q = 0; q < distanceList.size(); q++) {
      Collections.sort(distanceList.get(q), distanceNodeComparator);
    } // for

    while (aux.size() > size) {
      double minDistance = Double.MAX_VALUE;
      int toRemove = 0;
      i = 0;
      Iterator<List<DistanceNode>> iterator = distanceList.iterator();
      while (iterator.hasNext()) {
        List<DistanceNode> dn = iterator.next();
        if (dn.get(0).getDistance() < minDistance) {
          toRemove = i;
          minDistance = dn.get(0).getDistance();
          // i y toRemove have the same distance to the first solution
        } else if (dn.get(0).getDistance() == minDistance) {
          int k = 0;
          while ((dn.get(k).getDistance() == distanceList.get(toRemove).get(k).getDistance())
              && k < (distanceList.get(i).size() - 1)) {
            k++;
          }

          if (dn.get(k).getDistance() < distanceList.get(toRemove).get(k).getDistance()) {
            toRemove = i;
          } // if
        } // if
        i++;
      } // while

      int tmp = aux.get(toRemove).getLocation();
      aux.remove(toRemove);
      distanceList.remove(toRemove);

      Iterator<List<DistanceNode>> externIterator = distanceList.iterator();
      while (externIterator.hasNext()) {
        Iterator<DistanceNode> interIterator = externIterator.next().iterator();
        while (interIterator.hasNext()) {
          if (interIterator.next().getReference() == tmp) {
            interIterator.remove();
            continue;
          } // if
        } // while
      } // while
    } // while
    return aux;
  } // environmentalSelection