FastNonDominatedSort.h
Go to the documentation of this file.
1 /*!
2  * \brief Implements the fast non-dominated sort algorithm.
3  *
4  * \author T. Voss, O. Krause
5  * \date 2015
6  *
7  *
8  * \par Copyright 1995-2017 Shark Development Team
9  *
10  * <BR><HR>
11  * This file is part of Shark.
12  * <http://shark-ml.org/>
13  *
14  * Shark is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * Shark is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
26  *
27  */
28 #ifndef SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_DOMINATION_FASTNONDOMINATEDSORT_H
29 #define SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_DOMINATION_FASTNONDOMINATEDSORT_H
30 
32 #include <vector>
33 
34 namespace shark {
35 
36 
37 /// \brief Implements the well-known non-dominated sorting algorithm.
38 ///
39 /// Assembles subsets/fronts of mututally non-dominating individuals.
40 /// Afterwards every individual is assigned a rank by points[i].rank() = frontNumber.
41 /// The front of dominating points has the value 1.
42 ///
43 /// The algorithm is dscribed in Deb et al,
44 /// A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II
45 /// IEEE Transactions on Evolutionary Computation, 2002
46 /// \param points [in] pointsulation to subdivide into fronts of non-dominated individuals.
47 /// \param ranks [out] Set of integers storing the rank of the i-th point. must have the same size
48 template<class PointRange, class RankRange>
49 void fastNonDominatedSort(PointRange const& points, RankRange& ranks) {
50  SIZE_CHECK(points.size() == ranks.size());
51  //stores for the i-th point which points are dominated by i
52  std::vector<std::vector<std::size_t> > s(points.size());
53  //stores for every point how many points are dominating it
54  std::vector<std::size_t> numberOfDominatingPoints(points.size(), 0);
55  //stores initially the front of non-dominated points
56  std::vector<std::size_t> front;
57  for (std::size_t i = 0; i < points.size(); i++) {
58  //check which points j are dominated by i and add them to s[i]
59  //also increment n[j] for every i dominating j
60  for (std::size_t j = 0; j < points.size(); j++) {
61  if (i == j) continue;
62  DominanceRelation rel = dominance(points[i], points[j]);
63  if (rel == LHS_DOMINATES_RHS)
64  s[i].push_back(j);
65  else if (rel == RHS_DOMINATES_LHS)
66  numberOfDominatingPoints[i]++;
67  }
68  //all non-dominated points form the first front
69  if (numberOfDominatingPoints[i] == 0){
70  front.push_back(i);
71  ranks[i] = 1;//non-dominated points have rank 1
72  }
73  }
74  //find subsequent fronts.
75  unsigned frontCounter = 2;
76  std::vector<std::size_t> nextFront;
77 
78  //as long as we can find fronts
79  //visit all points of the last front found and remove them from the
80  //set. All points which are not dominated anymore form the next front
81  while (!front.empty()) {
82  //visit all points of the current front and remove them
83  // if any point is not dominated, it is part the next front.
84  for(std::size_t element = 0; element != front.size(); ++element) {
85  //visit all points dominated by the element
86  std::vector<std::size_t> const& dominatedPoints = s[front[element]];
87  for (std::size_t j = 0; j != dominatedPoints.size(); ++j){
88  std::size_t point = dominatedPoints[j];
89  numberOfDominatingPoints[point]--;
90  // if no more points are dominating this, add to the next front.
91  if (numberOfDominatingPoints[point] == 0){
92  nextFront.push_back(point);
93  ranks[point] = frontCounter;
94  }
95  }
96  }
97 
98  // make the newly found front the current one
99  front.swap(nextFront);
100  nextFront.clear();
101  frontCounter++;
102  }
103 }
104 
105 }//namespace shark
106 #endif