HypervolumeContribution2D.h
Go to the documentation of this file.
1 /*!
2  *
3  * \author O.Krause, T. Glasmachers
4  * \date 2014-2016
5  *
6  *
7  * \par Copyright 1995-2017 Shark Development Team
8  *
9  * <BR><HR>
10  * This file is part of Shark.
11  * <http://shark-ml.org/>
12  *
13  * Shark is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published
15  * by the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * Shark is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27 #ifndef SHARK_ALGORITHMS_DIRECTSEARCH_HYPERVOLUME_CONTRIBUTION_2D_H
28 #define SHARK_ALGORITHMS_DIRECTSEARCH_HYPERVOLUME_CONTRIBUTION_2D_H
29 
30 #include <shark/LinAlg/Base.h>
32 
33 #include <algorithm>
34 #include <vector>
35 
36 namespace shark{
37 /// \brief Finds the smallest/largest Contributors given 2D points
38 ///
39 /// The algorithm sorts the points by their first coordinate and then
40 /// calculates the volume of the hypervolume dominated by each Pointseparately
41 /// returning the index of the elements with the minimum/maximum contribution.
43 private:
44  struct Point{
45  Point(){}
46 
47  Point(double f1, double f2, std::size_t index)
48  : f1(f1)
49  , f2(f2)
50  , index(index)
51  {}
52 
53  bool operator<(Point const& rhs) const{//for lexicographic sorting
54  if (f1 < rhs.f1) return true;
55  if (f1 > rhs.f1) return false;
56  return (f2 < rhs.f2);
57  }
58 
59  double f1;
60  double f2;
61  std::size_t index;
62  };
63 
64  ///\brief Stores the k elements with the best contribution(highest,lowest) as indicated by comparator
65  ///
66  /// The algorithm returns the indizes of the front front[i].index in order of contribution.
67  /// The input is a set of points sorted by x-value. the edge-points are never selected.
68  ///
69  /// This is implemented by using a min-heap that stores the k best elements,
70  /// but having the smallest element on top so that we can quickly decide which
71  /// element to remove.
72  template<class Comparator>
73  std::vector<KeyValuePair<double,std::size_t> > bestContributors( std::vector<Point> const& front, std::size_t k, Comparator comp)const{
74 
75  std::vector<KeyValuePair<double,std::size_t> > bestK(k+1);
76  auto heapStart = bestK.begin();
77  auto heapEnd = bestK.begin();
78 
79  auto pointComp = [&](KeyValuePair<double,std::size_t> const& lhs, KeyValuePair<double,std::size_t> const& rhs){return comp(lhs.key,rhs.key);};
80 
81  //compute the hypervalue contribution for each Pointexcept the endpoints;
82  for(std::size_t i = 1; i < front.size()-1;++i){
83  double contribution = (front[i+1].f1 - front[i].f1)*(front[i-1].f2 - front[i].f2);
84  *heapEnd = makeKeyValuePair(contribution,front[i].index);
85  ++heapEnd;
86  std::push_heap(heapStart,heapEnd,pointComp);
87 
88  if(heapEnd == bestK.end()){
89  std::pop_heap(heapStart,heapEnd,pointComp);
90  --heapEnd;
91  }
92  }
93  std::sort_heap(heapStart,heapEnd,pointComp);
94  bestK.pop_back();//remove the k+1th element
95  return bestK;
96  }
97 public:
98  /// \brief Returns the index of the points with smallest contribution.
99  ///
100  /// \param [in] points The set \f$S\f$ of points from which to select the smallest contributor.
101  /// \param [in] k The number of points to select.
102  /// \param [in] referencePointThe 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$.
103  template<typename Set, typename VectorType>
104  std::vector<KeyValuePair<double,std::size_t> > smallest( Set const& points, std::size_t k, VectorType const& referencePoint)const{
105  SHARK_RUNTIME_CHECK(points.size() >= k, "There must be at least k points in the set");
106  std::vector<Point> front;
107  front.emplace_back(0,referencePoint[1],points.size()+1);//add reference point
108  for(std::size_t i = 0; i != points.size(); ++i)
109  front.emplace_back(points[i][0],points[i][1],i);
110  front.emplace_back(referencePoint[0],0,points.size()+1);//add reference point
111  std::sort(front.begin()+1,front.end()-1);
112 
113  return bestContributors(front,k,[](double con1, double con2){return con1 < con2;});
114  }
115 
116  /// \brief Returns the index of the points with smallest contribution.
117  ///
118  /// As no reference point is given, the edge points can not be computed and are not selected.
119  ///
120  /// \param [in] points The set \f$S\f$ of points from which to select the smallest contributor.
121  /// \param [in] k The number of points to select.
122  template<typename Set>
123  std::vector<KeyValuePair<double,std::size_t> > smallest( Set const& points, std::size_t k)const{
124  SHARK_RUNTIME_CHECK(points.size() >= k, "There must be at least k points in the set");
125  std::vector<Point> front;
126  for(std::size_t i = 0; i != points.size(); ++i)
127  front.emplace_back(points[i][0],points[i][1],i);
128  std::sort(front.begin(),front.end());
129 
130  return bestContributors(front,k,[](double con1, double con2){return con1 < con2;});
131  }
132 
133  /// \brief Returns the index of the points with largest contribution.
134  ///
135  /// \param [in] points The set \f$S\f$ of points from which to select the smallest contributor.
136  /// \param [in] referencePointThe 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$.
137  template<typename Set, typename VectorType>
138  std::vector<KeyValuePair<double,std::size_t> > largest( Set const& points, std::size_t k, VectorType const& referencePoint)const{
139  SHARK_RUNTIME_CHECK(points.size() >= k, "There must be at least k points in the set");
140  std::vector<Point> front;
141  front.emplace_back(0,referencePoint[1],points.size()+1);//add reference point
142  for(std::size_t i = 0; i != points.size(); ++i)
143  front.emplace_back(points[i][0],points[i][1],i);
144  front.emplace_back(referencePoint[0],0,points.size()+1);//add reference point
145  std::sort(front.begin()+1,front.end()-1);
146 
147  return bestContributors(front,k,[](double con1, double con2){return con1 > con2;});
148  }
149 
150  /// \brief Returns the index of the points with largest contribution.
151  ///
152  /// As no reference point is given, the edge points can not be computed and are not selected.
153  ///
154  /// \param [in] points The set \f$S\f$ of points from which to select the smallest contributor.
155  /// \param [in] k The number of points to select.
156  template<typename Set>
157  std::vector<KeyValuePair<double,std::size_t> > largest( Set const& points, std::size_t k)const{
158  SHARK_RUNTIME_CHECK(points.size() >= k, "There must be at least k points in the set");
159  std::vector<Point> front;
160  for(std::size_t i = 0; i != points.size(); ++i)
161  front.emplace_back(points[i][0],points[i][1],i);
162  std::sort(front.begin(),front.end());
163 
164  return bestContributors(front,k,[](double con1, double con2){return con1 > con2;});
165  }
166 
167 };
168 
169 }
170 #endif