ROL
ROL_StdBoundConstraint_Def.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_DEF_HPP
50 #define ROL_STDBOUNDCONSTRAINT_DEF_HPP
51 
52 namespace ROL {
53 
54 template<class Real>
55 StdBoundConstraint<Real>::StdBoundConstraint(std::vector<Real> &x, bool isLower, Real scale)
56  : scale_(scale) {
57  dim_ = x.size();
58  x_lo_.clear(); x_up_.clear();
59  if (isLower) {
60  x_lo_.assign(x.begin(),x.end());
61  x_up_.resize(dim_,ROL_INF<Real>());
63  }
64  else {
65  x_lo_.resize(dim_,ROL_NINF<Real>());
66  x_up_.assign(x.begin(),x.end());
68  }
69  min_diff_ = ROL_INF<Real>();
70 
71  lower_ = makePtr<StdVector<Real>>(makePtrFromRef(x_lo_));
72  upper_ = makePtr<StdVector<Real>>(makePtrFromRef(x_up_));
73 }
74 
75 template<class Real>
76 StdBoundConstraint<Real>::StdBoundConstraint(std::vector<Real> &l, std::vector<Real> &u, Real scale)
77  : x_lo_(l), x_up_(u), scale_(scale) {
79  dim_ = x_lo_.size();
80  for ( int i = 0; i < dim_; i++ ) {
81  if ( i == 0 ) {
82  min_diff_ = x_up_[i] - x_lo_[i];
83  }
84  else {
85  min_diff_ = ( (min_diff_ < (x_up_[i] - x_lo_[i])) ? min_diff_ : (x_up_[i] - x_lo_[i]) );
86  }
87  }
88  min_diff_ *= 0.5;
89 
90  lower_ = makePtr<StdVector<Real>>(makePtrFromRef(x_lo_));
91  upper_ = makePtr<StdVector<Real>>(makePtrFromRef(x_up_));
92 }
93 
94 template<class Real>
96  bool lflag = true, uflag = true;
98  Ptr<const std::vector<Real>> ex =
99  dynamic_cast<const StdVector<Real>&>(x).getVector();
101  for ( int i = 0; i < dim_; ++i ) {
102  if ( (*ex)[i] < x_lo_[i] ) {
103  lflag = false;
104  break;
105  }
106  }
107  }
109  for ( int i = 0; i < dim_; ++i ) {
110  if ( (*ex)[i] > x_up_[i] ) {
111  uflag = false;
112  break;
113  }
114  }
115  }
116  }
117  return (lflag && uflag);
118 }
119 
120 template<class Real>
123  Ptr<std::vector<Real>> ex =
124  dynamic_cast<StdVector<Real>&>(x).getVector();
126  for ( int i = 0; i < dim_; ++i ) {
127  (*ex)[i] = std::max(x_lo_[i],(*ex)[i]);
128  }
129  }
131  for ( int i = 0; i < dim_; ++i ) {
132  (*ex)[i] = std::min(x_up_[i],(*ex)[i]);
133  }
134  }
135  }
136 }
137 
138 template<class Real>
141  Ptr<std::vector<Real>> ex =
142  dynamic_cast<StdVector<Real>&>(x).getVector();
143  const Real eps(1e-1), tol(100.0*ROL_EPSILON<Real>()), one(1);
145  for ( int i = 0; i < dim_; ++i ) {
146  Real val = ((x_lo_[i] < -tol) ? (one-eps)*x_lo_[i]
147  : ((x_lo_[i] > tol) ? (one+eps)*x_lo_[i]
148  : x_lo_[i]+eps));
149  val = std::min(x_lo_[i]+eps*min_diff_, val);
150  (*ex)[i] = ((*ex)[i] < x_lo_[i]+tol) ? val : (*ex)[i];
151  }
152  }
154  for ( int i = 0; i < dim_; ++i ) {
155  Real val = ((x_up_[i] < -tol) ? (one+eps)*x_up_[i]
156  : ((x_up_[i] > tol) ? (one-eps)*x_up_[i]
157  : x_up_[i]-eps));
158  val = std::max(x_up_[i]-eps*min_diff_, val);
159  (*ex)[i] = ((*ex)[i] > x_up_[i]-tol) ? val : (*ex)[i];
160  }
161  }
162  }
163 }
164 
165 template<class Real>
168  Ptr<const std::vector<Real>> ex =
169  dynamic_cast<const StdVector<Real>&>(x).getVector();
170  Ptr<std::vector<Real>> ev =
171  dynamic_cast<StdVector<Real>&>(v).getVector();
172  Real epsn = std::min(scale_*eps,min_diff_);
173  for ( int i = 0; i < dim_; ++i ) {
174  if ( ((*ex)[i] <= x_lo_[i]+epsn) ) {
175  (*ev)[i] = static_cast<Real>(0);
176  }
177  }
178  }
179 }
180 
181 template<class Real>
184  Ptr<const std::vector<Real>> ex =
185  dynamic_cast<const StdVector<Real>&>(x).getVector();
186  Ptr<std::vector<Real>> ev =
187  dynamic_cast<StdVector<Real>&>(v).getVector();
188  Real epsn = std::min(scale_*eps,min_diff_);
189  for ( int i = 0; i < dim_; ++i ) {
190  if ( ((*ex)[i] >= x_up_[i]-epsn) ) {
191  (*ev)[i] = static_cast<Real>(0);
192  }
193  }
194  }
195 }
196 
197 template<class Real>
198 void StdBoundConstraint<Real>::pruneLowerActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps) {
200  Ptr<const std::vector<Real>> ex =
201  dynamic_cast<const StdVector<Real>&>(x).getVector();
202  Ptr<const std::vector<Real>> eg =
203  dynamic_cast<const StdVector<Real>&>(g).getVector();
204  Ptr<std::vector<Real>> ev =
205  dynamic_cast<StdVector<Real>&>(v).getVector();
206  Real epsn = std::min(scale_*xeps,this->min_diff_);
207  for ( int i = 0; i < dim_; ++i ) {
208  if ( (*ex)[i] <= x_lo_[i]+epsn && (*eg)[i] > geps ) {
209  (*ev)[i] = static_cast<Real>(0);
210  }
211  }
212  }
213 }
214 
215 template<class Real>
216 void StdBoundConstraint<Real>::pruneUpperActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps) {
218  Ptr<const std::vector<Real>> ex =
219  dynamic_cast<const StdVector<Real>&>(x).getVector();
220  Ptr<const std::vector<Real>> eg =
221  dynamic_cast<const StdVector<Real>&>(g).getVector();
222  Ptr<std::vector<Real>> ev =
223  dynamic_cast<StdVector<Real>&>(v).getVector();
224  Real epsn = std::min(scale_*xeps,min_diff_);
225  for ( int i = 0; i < dim_; ++i ) {
226  if ( (*ex)[i] >= x_up_[i]-epsn && (*eg)[i] < -geps ) {
227  (*ev)[i] = static_cast<Real>(0);
228  }
229  }
230  }
231 }
232 
233 }// End ROL Namespace
234 
235 #endif
Provides the interface to apply upper and lower bound constraints.
Ptr< Vector< Real > > upper_
void activateLower(void)
Turn on lower bound.
Ptr< Vector< Real > > lower_
void activate(void)
Turn on bounds.
void activateUpper(void)
Turn on upper bound.
void projectInterior(Vector< Real > &x) override
Project optimization variables into the interior of the feasible set.
StdBoundConstraint(std::vector< Real > &x, bool isLower=false, Real scale=Real(1))
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0)) override
Set variables to zero if they correspond to the lower -active set.
void project(Vector< Real > &x) override
Project optimization variables onto the bounds.
bool isFeasible(const Vector< Real > &x) override
Check if the vector, v, is feasible.
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0)) override
Set variables to zero if they correspond to the upper -active set.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:84