Ejemplo n.º 1
0
  public R2Ranking<S> computeRanking(List<S> population) {

    for (S solution : population) {
      solution.setAttribute(getAttributeIdentifier(), new R2SolutionData());
    }

    for (int i = 0; i < this.utilityFunctions.getSize(); i++) {
      for (S solution : population) {
        R2SolutionData solutionData = this.getAttribute(solution);
        solutionData.alpha = this.utilityFunctions.evaluate(solution, i);

        if (solutionData.alpha < solutionData.utility) solutionData.utility = solutionData.alpha;
      }

      Collections.sort(
          population,
          new Comparator<S>() {
            @Override
            public int compare(S o1, S o2) {
              R2RankingAttribute<S> attribute = new R2RankingAttribute<>();
              R2SolutionData data1 = (R2SolutionData) attribute.getAttribute(o1);
              R2SolutionData data2 = (R2SolutionData) attribute.getAttribute(o2);

              if (data1.alpha < data2.alpha) return -1;
              else if (data1.alpha > data2.alpha) return 1;
              else return 0;
            }
          });

      int rank = 1;
      for (S p : population) {
        R2SolutionData r2Data = this.getAttribute(p);
        if (rank < r2Data.rank) {
          r2Data.rank = rank;
          numberOfRanks = Math.max(numberOfRanks, rank);
        }
        rank = rank + 1;
      }
    }

    Map<Integer, List<S>> fronts = new TreeMap<>(); // sorted on key
    for (S solution : population) {
      R2SolutionData r2Data = this.getAttribute(solution);
      if (fronts.get(r2Data.rank) == null) fronts.put(r2Data.rank, new LinkedList<S>());

      fronts.get(r2Data.rank).add(solution);
    }

    this.rankedSubpopulations = new ArrayList<>(fronts.size());
    Iterator<Integer> iterator = fronts.keySet().iterator();
    while (iterator.hasNext()) this.rankedSubpopulations.add(fronts.get(iterator.next()));

    return this;
  }