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 * <https://shark-ml.github.io/Shark/>
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
39namespace shark{
40
41/// \brief Handler for box-constraints
42///
43/// Handles box-constraints i.e. function values in \f$ l_i < x_i < u_i \f$.
44/// \ingroup constraint_handling
45template<class Vector>
47public:
48 BoxConstraintHandler(Vector const& lower, Vector const& upper)
49 :m_lower(lower),m_upper(upper){
50 SIZE_CHECK(lower.size() == upper.size());
51 typedef AbstractConstraintHandler<Vector> base_type;
52 this->m_features |= base_type::CAN_PROVIDE_CLOSEST_FEASIBLE;
53 this->m_features |= base_type::IS_BOX_CONSTRAINED;
54 this->m_features |= base_type::CAN_GENERATE_RANDOM_POINT;
55 }
56 BoxConstraintHandler(std::size_t dim, double lower, double upper)
57 :m_lower(Vector(dim,lower)),m_upper(Vector(dim,upper)){
58 typedef AbstractConstraintHandler<Vector> base_type;
59 this->m_features |= base_type::CAN_PROVIDE_CLOSEST_FEASIBLE;
60 this->m_features |= base_type::IS_BOX_CONSTRAINED;
61 this->m_features |= base_type::CAN_GENERATE_RANDOM_POINT;
62 }
63
65 typedef AbstractConstraintHandler<Vector> base_type;
66 this->m_features |= base_type::CAN_PROVIDE_CLOSEST_FEASIBLE;
67 this->m_features |= base_type::IS_BOX_CONSTRAINED;
68 this->m_features |= base_type::CAN_GENERATE_RANDOM_POINT;
69 }
70
71 std::size_t dimensions()const{
72 return m_lower.size();
73 }
74
75 bool isFeasible(Vector const& point)const{
76 SIZE_CHECK(point.size() == dimensions());
77 for(std::size_t i = 0; i != dimensions();++i){
78 if(point(i) + 1.e-13 < m_lower(i)||point(i) - 1.e-13 > m_upper(i))
79 return false;
80 }
81 return true;
82 }
83 void closestFeasible(Vector& point )const{
84 SIZE_CHECK(point.size() == dimensions());
85 for(std::size_t i = 0; i != dimensions();++i){
86 point(i) = std::max(point(i),m_lower(i));
87 point(i) = std::min(point(i),m_upper(i));
88 }
89 }
90
91 virtual void generateRandomPoint(random::rng_type& rng, Vector & startingPoint )const {
92 startingPoint.resize(dimensions());
93 for(std::size_t i = 0; i != dimensions(); ++i){
94 startingPoint(i) = random::uni(rng, m_lower(i),m_upper(i));
95 }
96 }
97
98
99 /// \brief Sets lower and upper bounds of the box.
100 void setBounds(Vector const& lower, Vector const& upper){
101 SIZE_CHECK(lower.size() == upper.size());
102 m_lower = lower;
103 m_upper = upper;
104 }
105 /// \brief Sets lower and upper bounds of the box.
106 void setBounds(std::size_t dimension, double lower, double upper){
107 m_lower = Vector(dimension,lower);
108 m_upper = Vector(dimension,upper);
109 }
110 /// \brief Returns the lower bound of the box.
111 Vector const& lower()const{
112 return m_lower;
113 }
114 /// \brief Returns the upper bound of the box.
115 Vector const& upper()const{
116 return m_upper;
117 }
118private:
119 /// \brief Represents the lower bound of the points in the box
120 Vector m_lower;
121 /// \brief Represents the upper bound of the points in the box
122 Vector m_upper;
123};
124}
125#endif