HypervolumeCalculator2D.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implementation of the exact hypervolume calculation in 2 dimensions.
5  *
6  *
7  * \author O.Krause, T. Glasmachers
8  * \date 2014-2016
9  *
10  *
11  * \par Copyright 1995-2017 Shark Development Team
12  *
13  * <BR><HR>
14  * This file is part of Shark.
15  * <http://shark-ml.org/>
16  *
17  * Shark is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Lesser General Public License as published
19  * by the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * Shark is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31 #ifndef SHARK_ALGORITHMS_DIRECTSEARCH_HYPERVOLUMECALCULATOR_2D_H
32 #define SHARK_ALGORITHMS_DIRECTSEARCH_HYPERVOLUMECALCULATOR_2D_H
33 
34 #include <shark/LinAlg/Base.h>
36 
37 #include <algorithm>
38 #include <vector>
39 
40 namespace shark{
41 /// \brief Implementation of the exact hypervolume calculation in 2 dimensions.
42 ///
43 /// The algorithm has complexity n log(n) and works by ordering the data by the first dimension
44 /// and then computing the upper riemann sum skipping dominated points
46 
47  /// \brief Executes the algorithm.
48  /// \param [in] points The set \f$S\f$ of points for which to compute the volume
49  /// \param [in] refPoint The reference point \f$\vec{r} \in \mathbb{R}^2\f$ for the hypervolume calculation, needs to fulfill: \f$ \forall s \in S: s \preceq \vec{r}\f$. .
50  template<typename Set, typename VectorType >
51  double operator()( Set const& points, VectorType const& refPoint){
52  if(points.empty())
53  return 0;
54  SIZE_CHECK( points.begin()->size() == 2 );
55  SIZE_CHECK( refPoint.size() == 2 );
56 
57  //copy set and order by first argument
58  std::vector<shark::KeyValuePair<double,double> > set(points.size());
59  for(std::size_t i = 0; i != points.size(); ++i){
60  set[i].key = points[i][0];
61  set[i].value = points[i][1];
62  }
63  std::sort( set.begin(), set.end());
64 
65  //go over the ordered set, skip dominated points and perform the integration
66  double volume = ( refPoint[0] - set[0].key ) * ( refPoint[1] - set[0].value);
67 
68  std::size_t lastValidIndex = 0;
69  for( std::size_t i = 1; i < set.size(); ++i ) {
70  double diffDim1 = set[lastValidIndex].value - set[i].value;
71 
72  //diffDim1 <= 0 => point is dominated, so skip it
73  if( diffDim1 > 0 ) {
74  volume += ( refPoint[0] - set[i].key ) * diffDim1;
75  lastValidIndex = i;
76  }
77  }
78  return volume;
79  }
80 };
81 
82 }
83 #endif