ROL
ROL_StdBoundConstraint.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
49 #ifndef ROL_STDBOUNDCONSTRAINT_HPP
50 #define ROL_STDBOUNDCONSTRAINT_HPP
51 
52 #include "ROL_StdVector.hpp"
53 #include "ROL_BoundConstraint.hpp"
54 
55 namespace ROL {
56 
57  template<class Real>
58  class StdBoundConstraint : public BoundConstraint<Real> {
59  private:
60  int dim_;
61  std::vector<Real> x_lo_;
62  std::vector<Real> x_up_;
63  Real min_diff_;
64  Real scale_;
65 
66  ROL::Ptr<Vector<Real> > l_;
67  ROL::Ptr<Vector<Real> > u_;
68 
69  public:
70  StdBoundConstraint(std::vector<Real> &x, bool isLower = false, Real scale = 1.0)
71  : scale_(scale) {
72  dim_ = x.size();
73  x_lo_.clear(); x_up_.clear();
74  if (isLower) {
75  x_lo_.assign(x.begin(),x.end());
76  x_up_.resize(dim_,ROL_INF<Real>());
78  }
79  else {
80  x_lo_.resize(dim_,ROL_NINF<Real>());
81  x_up_.assign(x.begin(),x.end());
83  }
84  min_diff_ = ROL_INF<Real>();
85 
86  l_ = ROL::makePtr<StdVector<Real>>(ROL::makePtrFromRef(x_lo_));
87  u_ = ROL::makePtr<StdVector<Real>>(ROL::makePtrFromRef(x_up_));
88  }
89 
90  StdBoundConstraint(std::vector<Real> &l, std::vector<Real> &u, Real scale = 1.0)
91  : x_lo_(l), x_up_(u), scale_(scale) {
93  dim_ = x_lo_.size();
94  for ( int i = 0; i < dim_; i++ ) {
95  if ( i == 0 ) {
96  min_diff_ = x_up_[i] - x_lo_[i];
97  }
98  else {
99  min_diff_ = ( (min_diff_ < (x_up_[i] - x_lo_[i])) ? min_diff_ : (x_up_[i] - x_lo_[i]) );
100  }
101  }
102  min_diff_ *= 0.5;
103 
104  l_ = ROL::makePtr<StdVector<Real>>(ROL::makePtrFromRef(x_lo_));
105  u_ = ROL::makePtr<StdVector<Real>>(ROL::makePtrFromRef(x_up_));
106  }
107 
108  bool isFeasible( const Vector<Real> &x ) {
109  bool lflag = true, uflag = true;
111  ROL::Ptr<const std::vector<Real> > ex =
112  dynamic_cast<const StdVector<Real>&>(x).getVector();
114  for ( int i = 0; i < dim_; ++i ) {
115  if ( (*ex)[i] < x_lo_[i] ) {
116  lflag = false;
117  break;
118  }
119  }
120  }
122  for ( int i = 0; i < dim_; ++i ) {
123  if ( (*ex)[i] > x_up_[i] ) {
124  uflag = false;
125  break;
126  }
127  }
128  }
129  }
130  return (lflag && uflag);
131  }
132 
133  void project( Vector<Real> &x ) {
135  ROL::Ptr<std::vector<Real> > ex =
136  dynamic_cast<StdVector<Real>&>(x).getVector();
138  for ( int i = 0; i < dim_; ++i ) {
139  (*ex)[i] = std::max(x_lo_[i],(*ex)[i]);
140  }
141  }
143  for ( int i = 0; i < dim_; ++i ) {
144  (*ex)[i] = std::min(x_up_[i],(*ex)[i]);
145  }
146  }
147  }
148  }
149 
152  ROL::Ptr<std::vector<Real> > ex =
153  dynamic_cast<StdVector<Real>&>(x).getVector();
154  const Real eps(1e-1), tol(100.0*ROL_EPSILON<Real>()), one(1);
156  for ( int i = 0; i < dim_; ++i ) {
157  Real val = ((x_lo_[i] < -tol) ? (one-eps)*x_lo_[i]
158  : ((x_lo_[i] > tol) ? (one+eps)*x_lo_[i]
159  : x_lo_[i]+eps));
160  val = std::min(x_lo_[i]+eps*min_diff_, val);
161  (*ex)[i] = ((*ex)[i] < x_lo_[i]+tol) ? val : (*ex)[i];
162  }
163  }
165  for ( int i = 0; i < dim_; ++i ) {
166  Real val = ((x_up_[i] < -tol) ? (one+eps)*x_up_[i]
167  : ((x_up_[i] > tol) ? (one-eps)*x_up_[i]
168  : x_up_[i]-eps));
169  val = std::max(x_up_[i]-eps*min_diff_, val);
170  (*ex)[i] = ((*ex)[i] > x_up_[i]-tol) ? val : (*ex)[i];
171  }
172  }
173  }
174  }
175 
176  void pruneLowerActive(Vector<Real> &v, const Vector<Real> &x, Real eps) {
178  ROL::Ptr<const std::vector<Real> > ex =
179  dynamic_cast<const StdVector<Real>&>(x).getVector();
180  ROL::Ptr<std::vector<Real> > ev =
181  dynamic_cast<StdVector<Real>&>(v).getVector();
182  Real epsn = std::min(scale_*eps,min_diff_);
183  for ( int i = 0; i < dim_; ++i ) {
184  if ( ((*ex)[i] <= x_lo_[i]+epsn) ) {
185  (*ev)[i] = static_cast<Real>(0);
186  }
187  }
188  }
189  }
190 
191  void pruneUpperActive(Vector<Real> &v, const Vector<Real> &x, Real eps) {
193  ROL::Ptr<const std::vector<Real> > ex =
194  dynamic_cast<const StdVector<Real>&>(x).getVector();
195  ROL::Ptr<std::vector<Real> > ev =
196  dynamic_cast<StdVector<Real>&>(v).getVector();
197  Real epsn = std::min(scale_*eps,min_diff_);
198  for ( int i = 0; i < dim_; ++i ) {
199  if ( ((*ex)[i] >= x_up_[i]-epsn) ) {
200  (*ev)[i] = static_cast<Real>(0);
201  }
202  }
203  }
204  }
205 
206  void pruneLowerActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps) {
208  ROL::Ptr<const std::vector<Real> > ex =
209  dynamic_cast<const StdVector<Real>&>(x).getVector();
210  ROL::Ptr<const std::vector<Real> > eg =
211  dynamic_cast<const StdVector<Real>&>(g).getVector();
212  ROL::Ptr<std::vector<Real> > ev =
213  dynamic_cast<StdVector<Real>&>(v).getVector();
214  Real epsn = std::min(scale_*eps,this->min_diff_);
215  for ( int i = 0; i < dim_; ++i ) {
216  if ( ((*ex)[i] <= x_lo_[i]+epsn && (*eg)[i] > static_cast<Real>(0)) ) {
217  (*ev)[i] = static_cast<Real>(0);
218  }
219  }
220  }
221  }
222 
223  void pruneUpperActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps) {
225  ROL::Ptr<const std::vector<Real> > ex =
226  dynamic_cast<const StdVector<Real>&>(x).getVector();
227  ROL::Ptr<const std::vector<Real> > eg =
228  dynamic_cast<const StdVector<Real>&>(g).getVector();
229  ROL::Ptr<std::vector<Real> > ev =
230  dynamic_cast<StdVector<Real>&>(v).getVector();
231  Real epsn = std::min(scale_*eps,min_diff_);
232  for ( int i = 0; i < dim_; ++i ) {
233  if ( ((*ex)[i] >= x_up_[i]-epsn && (*eg)[i] < static_cast<Real>(0)) ) {
234  (*ev)[i] = static_cast<Real>(0);
235  }
236  }
237  }
238  }
239 
240  const ROL::Ptr<const Vector<Real> > getLowerBound( void ) const {
241  return l_;
242  }
243 
244  const ROL::Ptr<const Vector<Real> > getUpperBound( void ) const {
245  return u_;
246  }
247  };
248 
249 }// End ROL Namespace
250 
251 #endif
const ROL::Ptr< const Vector< Real > > getUpperBound(void) const
Return the ref count pointer to the upper bound vector.
ROL::Ptr< Vector< Real > > l_
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the upper -binding set.
void activateLower(void)
Turn on lower bound.
void activate(void)
Turn on bounds.
StdBoundConstraint(std::vector< Real > &x, bool isLower=false, Real scale=1.0)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
StdBoundConstraint(std::vector< Real > &l, std::vector< Real > &u, Real scale=1.0)
void project(Vector< Real > &x)
Project optimization variables onto the bounds.
ROL::Ptr< Vector< Real > > u_
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the upper -active set.
void activateUpper(void)
Turn on upper bound.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the lower -active set.
const ROL::Ptr< const Vector< Real > > getLowerBound(void) const
Return the ref count pointer to the lower bound vector.
Provides the interface to apply upper and lower bound constraints.
void projectInterior(Vector< Real > &x)
Project optimization variables into the interior of the feasible set.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the -binding set.
bool isFeasible(const Vector< Real > &x)
Check if the vector, v, is feasible.