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, Real feasTol)
56  : scale_(scale), feasTol_(feasTol), min_diff_(ROL_INF<Real>()){
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 
70  lower_ = makePtr<StdVector<Real>>(makePtrFromRef(x_lo_));
71  upper_ = makePtr<StdVector<Real>>(makePtrFromRef(x_up_));
72 }
73 
74 template<class Real>
75 StdBoundConstraint<Real>::StdBoundConstraint(std::vector<Real> &l, std::vector<Real> &u, Real scale, Real feasTol)
76  : x_lo_(l), x_up_(u), scale_(scale), feasTol_(feasTol) {
78  dim_ = x_lo_.size();
79  for ( int i = 0; i < dim_; i++ ) {
80  if ( i == 0 ) {
81  min_diff_ = x_up_[i] - x_lo_[i];
82  }
83  else {
84  min_diff_ = ( (min_diff_ < (x_up_[i] - x_lo_[i])) ? min_diff_ : (x_up_[i] - x_lo_[i]) );
85  }
86  }
87  min_diff_ *= 0.5;
88 
89  lower_ = makePtr<StdVector<Real>>(makePtrFromRef(x_lo_));
90  upper_ = makePtr<StdVector<Real>>(makePtrFromRef(x_up_));
91 }
92 
93 template<class Real>
96  Ptr<std::vector<Real>> ex =
97  dynamic_cast<StdVector<Real>&>(x).getVector();
99  for ( int i = 0; i < dim_; ++i ) {
100  (*ex)[i] = std::max(x_lo_[i],(*ex)[i]);
101  }
102  }
104  for ( int i = 0; i < dim_; ++i ) {
105  (*ex)[i] = std::min(x_up_[i],(*ex)[i]);
106  }
107  }
108  }
109 }
110 
111 template<class Real>
114  Ptr<std::vector<Real>> ex =
115  dynamic_cast<StdVector<Real>&>(x).getVector();
116  const Real eps = feasTol_;
117  const Real tol = 100.0*ROL_EPSILON<Real>();
118  const Real one(1);
120  for ( int i = 0; i < dim_; ++i ) {
121  Real val = ((x_lo_[i] < -tol) ? (one-eps)*x_lo_[i]
122  : ((x_lo_[i] > tol) ? (one+eps)*x_lo_[i]
123  : x_lo_[i]+eps));
124  val = std::min(x_lo_[i]+eps*min_diff_, val);
125  (*ex)[i] = ((*ex)[i] < val) ? val : (*ex)[i];
126  }
127  }
129  for ( int i = 0; i < dim_; ++i ) {
130  Real val = ((x_up_[i] < -tol) ? (one+eps)*x_up_[i]
131  : ((x_up_[i] > tol) ? (one-eps)*x_up_[i]
132  : x_up_[i]-eps));
133  val = std::max(x_up_[i]-eps*min_diff_, val);
134  (*ex)[i] = ((*ex)[i] > val) ? val : (*ex)[i];
135  }
136  }
137  }
138 }
139 
140 template<class Real>
143  Ptr<const std::vector<Real>> ex =
144  dynamic_cast<const StdVector<Real>&>(x).getVector();
145  Ptr<std::vector<Real>> ev =
146  dynamic_cast<StdVector<Real>&>(v).getVector();
147  Real epsn = std::min(scale_*eps,min_diff_);
148  for ( int i = 0; i < dim_; ++i ) {
149  if ( ((*ex)[i] >= x_up_[i]-epsn) ) {
150  (*ev)[i] = static_cast<Real>(0);
151  }
152  }
153  }
154 }
155 
156 template<class Real>
157 void StdBoundConstraint<Real>::pruneUpperActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps) {
159  Ptr<const std::vector<Real>> ex =
160  dynamic_cast<const StdVector<Real>&>(x).getVector();
161  Ptr<const std::vector<Real>> eg =
162  dynamic_cast<const StdVector<Real>&>(g).getVector();
163  Ptr<std::vector<Real>> ev =
164  dynamic_cast<StdVector<Real>&>(v).getVector();
165  Real epsn = std::min(scale_*xeps,min_diff_);
166  for ( int i = 0; i < dim_; ++i ) {
167  if ( (*ex)[i] >= x_up_[i]-epsn && (*eg)[i] < -geps ) {
168  (*ev)[i] = static_cast<Real>(0);
169  }
170  }
171  }
172 }
173 
174 template<class Real>
177  Ptr<const std::vector<Real>> ex =
178  dynamic_cast<const StdVector<Real>&>(x).getVector();
179  Ptr<std::vector<Real>> ev =
180  dynamic_cast<StdVector<Real>&>(v).getVector();
181  Real epsn = std::min(scale_*eps,min_diff_);
182  for ( int i = 0; i < dim_; ++i ) {
183  if ( ((*ex)[i] <= x_lo_[i]+epsn) ) {
184  (*ev)[i] = static_cast<Real>(0);
185  }
186  }
187  }
188 }
189 
190 template<class Real>
191 void StdBoundConstraint<Real>::pruneLowerActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps) {
193  Ptr<const std::vector<Real>> ex =
194  dynamic_cast<const StdVector<Real>&>(x).getVector();
195  Ptr<const std::vector<Real>> eg =
196  dynamic_cast<const StdVector<Real>&>(g).getVector();
197  Ptr<std::vector<Real>> ev =
198  dynamic_cast<StdVector<Real>&>(v).getVector();
199  Real epsn = std::min(scale_*xeps,this->min_diff_);
200  for ( int i = 0; i < dim_; ++i ) {
201  if ( (*ex)[i] <= x_lo_[i]+epsn && (*eg)[i] > geps ) {
202  (*ev)[i] = static_cast<Real>(0);
203  }
204  }
205  }
206 }
207 
208 template<class Real>
210  bool lflag = true, uflag = true;
212  Ptr<const std::vector<Real>> ex =
213  dynamic_cast<const StdVector<Real>&>(x).getVector();
215  for ( int i = 0; i < dim_; ++i ) {
216  if ( (*ex)[i] < x_lo_[i] ) {
217  lflag = false;
218  break;
219  }
220  }
221  }
223  for ( int i = 0; i < dim_; ++i ) {
224  if ( (*ex)[i] > x_up_[i] ) {
225  uflag = false;
226  break;
227  }
228  }
229  }
230  }
231  return (lflag && uflag);
232 }
233 
234 template<class Real>
236  Ptr<std::vector<Real>> ed =
237  dynamic_cast<StdVector<Real>&>(d).getVector();
238  Ptr<const std::vector<Real>> ex =
239  dynamic_cast<const StdVector<Real>&>(x).getVector();
240  Ptr<const std::vector<Real>> eg =
241  dynamic_cast<const StdVector<Real>&>(g).getVector();
242 
243  Real grad, lodiff, updiff, c;
244 
245  for ( int i = 0; i < dim_; ++i ) {
246  grad = (*eg)[i];
247  lodiff = (*ex)[i] - x_lo_[i];
248  updiff = x_up_[i] - (*ex)[i];
249  c = buildC(i);
250  if (-grad > lodiff) {
251  if (lodiff <= updiff) {
252  (*ed)[i] = std::min(std::abs(grad), c);
253  continue;
254  }
255  }
256  if (+grad > updiff) {
257  if (updiff <= lodiff) {
258  (*ed)[i] = std::min(std::abs(grad), c);
259  continue;
260  }
261  }
262  (*ed)[i] = std::min({lodiff, updiff, c});
263  }
264 }
265 
266 template<class Real>
268  buildScalingFunction(dv, x, g);
269 
270  Ptr<std::vector<Real>> edv =
271  dynamic_cast<StdVector<Real>&>(dv).getVector();
272  Ptr<const std::vector<Real>> ev =
273  dynamic_cast<const StdVector<Real>&>(v).getVector();
274 
275  for ( int i = 0; i < dim_; ++i ) {
276  (*edv)[i] = (*ev)[i]/(*edv)[i];
277  }
278 }
279 
280 template<class Real>
282  buildScalingFunction(dv, x, g);
283 
284  Ptr<std::vector<Real>> edv =
285  dynamic_cast<StdVector<Real>&>(dv).getVector();
286  Ptr<const std::vector<Real>> ev =
287  dynamic_cast<const StdVector<Real>&>(v).getVector();
288  Ptr<const std::vector<Real>> ex =
289  dynamic_cast<const StdVector<Real>&>(x).getVector();
290  Ptr<const std::vector<Real>> eg =
291  dynamic_cast<const StdVector<Real>&>(g).getVector();
292 
293  Real zero(0), one(1), indicator, d1prime;
294 
295  for ( int i = 0; i < dim_; ++i ) {
296  indicator = (*edv)[i] < buildC(i) ? one : zero;
297 
298  if (indicator == zero) {
299  (*edv)[i] = zero;
300  continue;
301  }
302 
303  // When indicator is not zero...
304 
305  d1prime = sgn((*eg)[i]);
306  if (d1prime == zero) {
307  d1prime = one;
308  if (x_up_[i] - (*ex)[i] < (*ex)[i] - x_lo_[i])
309  d1prime = -one;
310  }
311  (*edv)[i] = d1prime*(*eg)[i]*(*ev)[i];
312  }
313 }
314 
315 }// End ROL Namespace
316 
317 #endif
Ptr< Vector< Real > > upper_
void projectInterior(Vector< Real > &x) override
Project optimization variables into the interior of the feasible set.
void activateLower(void)
Turn on lower bound.
void activate(void)
Turn on bounds.
Real ROL_INF(void)
Definition: ROL_Types.hpp:105
void buildScalingFunction(Vector< Real > &d, const Vector< Real > &x, const Vector< Real > &g) const
bool isFeasible(const Vector< Real > &v) override
Check if the vector, v, is feasible.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
void activateUpper(void)
Turn on upper bound.
Ptr< Vector< Real > > lower_
void applyInverseScalingFunction(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const override
Apply inverse scaling function.
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.
void project(Vector< Real > &x) override
Project optimization variables onto the bounds.
StdBoundConstraint(std::vector< Real > &x, bool isLower=false, Real scale=Real(1), const Real feasTol=std::sqrt(ROL_EPSILON< Real >()))
Provides the interface to apply upper and lower bound constraints.
void applyScalingFunctionJacobian(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const override
Apply scaling function Jacobian.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real xeps=Real(0), Real geps=Real(0)) override
Set variables to zero if they correspond to the -binding set.