NonDominatedSort.h
Go to the documentation of this file.
1 ///
2 /// \brief Swapper method for non-dominated sorting.
3 ///
4 /// \author T. Glasmachers
5 /// \date 2016
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_NONDOMINATEDSORT_H
29 #define SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_DOMINATION_NONDOMINATEDSORT_H
30 
31 #include "FastNonDominatedSort.h"
32 #include "DCNonDominatedSort.h"
33 
34 
35 namespace shark {
36 
37 /// \brief Frontend for non-dominated sorting algorithms.
38 ///
39 /// Assembles subsets/fronts of mutually non-dominated individuals.
40 /// Afterwards every individual is assigned a rank by pop[i].rank() = frontIndex.
41 /// The front of non-dominated points has the value 1.
42 ///
43 /// Depending on dimensionality m and number of points n, either the
44 /// fastNonDominatedSort algorithm with O(n^2 m) or the dcNonDominatedSort
45 /// alforithm with complexity O(n log(n)^m) is called.
46 template<class PointRange, class RankRange>
47 void nonDominatedSort(PointRange const& points, RankRange& ranks) {
48  SIZE_CHECK(points.size() == ranks.size());
49  std::size_t n = points.size();
50  if(n == 0) return;
51  std::size_t m = points[0].size();
52  // heuristic switching strategy based on simple benchmarks
53  if (m == 2 || n > 5000 || std::log(n) / log(3.0) < m + 1.0)
54  {
55  dcNonDominatedSort(points,ranks);
56  }
57  else
58  {
59  fastNonDominatedSort(points,ranks);
60  }
61 }
62 
63 //version that takes temporary ranges as second argument.
64 //this allows nonDominatedSort(points,ranks(population) as the second argument will return a temporary proxy
65 //we would like to use r-value references here but gcc 4.8 appears to be buggy in that regard
66 template<class PointRange, class RankRange>
67 //~ void nonDominatedSort(PointRange const& points, RankRange&& ranks) {
68 void nonDominatedSort(PointRange const& points, RankRange const& ranks) {
69  RankRange ranksCopy=ranks;
70  nonDominatedSort(points,ranksCopy);
71 }
72 
73 
74 } // namespace shark
75 #endif