BoxConstraintHandler.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Base class for constraints.
6  *
7  *
8  * \author O.Krause
9  * \date 2013
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 //===========================================================================
33 #ifndef SHARK_OBJECTIVEFUNCTIONS_BOXCONSTRAINTHANDLER_H
34 #define SHARK_OBJECTIVEFUNCTIONS_BOXCONSTRAINTHANDLER_H
35 
37 #include <shark/Core/Random.h>
38 
39 namespace shark{
40 
41 template<class Vector>
43 public:
44  BoxConstraintHandler(Vector const& lower, Vector const& upper)
45  :m_lower(lower),m_upper(upper){
46  SIZE_CHECK(lower.size() == upper.size());
47  typedef AbstractConstraintHandler<Vector> base_type;
48  this->m_features |= base_type::CAN_PROVIDE_CLOSEST_FEASIBLE;
49  this->m_features |= base_type::IS_BOX_CONSTRAINED;
50  this->m_features |= base_type::CAN_GENERATE_RANDOM_POINT;
51  }
52  BoxConstraintHandler(std::size_t dim, double lower, double upper)
53  :m_lower(Vector(dim,lower)),m_upper(Vector(dim,upper)){
54  typedef AbstractConstraintHandler<Vector> base_type;
55  this->m_features |= base_type::CAN_PROVIDE_CLOSEST_FEASIBLE;
56  this->m_features |= base_type::IS_BOX_CONSTRAINED;
57  this->m_features |= base_type::CAN_GENERATE_RANDOM_POINT;
58  }
59 
61  typedef AbstractConstraintHandler<Vector> base_type;
62  this->m_features |= base_type::CAN_PROVIDE_CLOSEST_FEASIBLE;
63  this->m_features |= base_type::IS_BOX_CONSTRAINED;
64  this->m_features |= base_type::CAN_GENERATE_RANDOM_POINT;
65  }
66 
67  std::size_t dimensions()const{
68  return m_lower.size();
69  }
70 
71  bool isFeasible(Vector const& point)const{
72  SIZE_CHECK(point.size() == dimensions());
73  for(std::size_t i = 0; i != dimensions();++i){
74  if(point(i) + 1.e-13 < m_lower(i)||point(i) - 1.e-13 > m_upper(i))
75  return false;
76  }
77  return true;
78  }
79  void closestFeasible(Vector& point )const{
80  SIZE_CHECK(point.size() == dimensions());
81  for(std::size_t i = 0; i != dimensions();++i){
82  point(i) = std::max(point(i),m_lower(i));
83  point(i) = std::min(point(i),m_upper(i));
84  }
85  }
86 
87  virtual void generateRandomPoint(random::rng_type& rng, Vector & startingPoint )const {
88  startingPoint.resize(dimensions());
89  for(std::size_t i = 0; i != dimensions(); ++i){
90  startingPoint(i) = random::uni(rng, m_lower(i),m_upper(i));
91  }
92  }
93 
94 
95  /// \brief Sets lower and upper bounds of the box.
96  void setBounds(Vector const& lower, Vector const& upper){
97  SIZE_CHECK(lower.size() == upper.size());
98  m_lower = lower;
99  m_upper = upper;
100  }
101  /// \brief Sets lower and upper bounds of the box.
102  void setBounds(std::size_t dimension, double lower, double upper){
103  m_lower = Vector(dimension,lower);
104  m_upper = Vector(dimension,upper);
105  }
106  /// \brief Returns the lower bound of the box.
107  Vector const& lower()const{
108  return m_lower;
109  }
110  /// \brief Returns the upper bound of the box.
111  Vector const& upper()const{
112  return m_upper;
113  }
114 private:
115  /// \brief Represents the lower bound of the points in the box
116  Vector m_lower;
117  /// \brief Represents the upper bound of the points in the box
118  Vector m_upper;
119 };
120 }
121 #endif