ROL
ROL_BoundConstraint_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 
44 #ifndef ROL_BOUND_CONSTRAINT_DEF_H
45 #define ROL_BOUND_CONSTRAINT_DEF_H
46 
47 namespace ROL {
48 
49 template<typename Real>
51  int dim = x.dimension();
52  Real denom = (dim > 0 ? static_cast<Real>(dim) : 1e15);
53  return std::sqrt(ROL_INF<Real>() / denom);
54 }
55 
56 template<typename Real>
58  : Lactivated_(true), Uactivated_(true) {}
59 
60 template<typename Real>
62  : Lactivated_(false), Uactivated_(false) {
63  try {
64  lower_ = x.clone(); lower_->setScalar(-computeInf(x));
65  upper_ = x.clone(); upper_->setScalar( computeInf(x));
66  }
67  catch(std::exception &e) {
68  // Do nothing. If someone calls getLowerBound or getUpperBound,
69  // an exception will be thrown.
70  }
71 }
72 
73 template<typename Real>
75  if (isActivated()) {
76  throw Exception::NotImplemented(">>> ROL::BoundConstraint::project: Not Implemented!");
77  }
78 }
79 
80 template<typename Real>
82  if (isActivated()) {
83  throw Exception::NotImplemented(">>> ROL::BoundConstraint::projectInterior: Not Implemented!");
84  }
85 }
86 
87 template<typename Real>
89  if (isUpperActivated()) {
90  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneUpperActive: Not Implemented!");
91  }
92 }
93 
94 template<typename Real>
95 void BoundConstraint<Real>::pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
96  if (isUpperActivated()) {
97  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneUpperActive: Not Implemented!");
98  }
99 }
100 
101 template<typename Real>
103  if (isLowerActivated()) {
104  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneLowerActive: Not Implemented!");
105  }
106 }
107 
108 template<typename Real>
109 void BoundConstraint<Real>::pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
110  if (isLowerActivated()) {
111  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneLowerActive: Not Implemented!");
112  }
113 }
114 
115 template<typename Real>
116 const Ptr<const Vector<Real>> BoundConstraint<Real>::getLowerBound( void ) const {
117  if (lower_ != nullPtr) {
118  return lower_;
119  }
120  throw Exception::NotImplemented(">>> ROL::BoundConstraint::getLowerBound: Lower bound not provided!");
121 }
122 
123 template<typename Real>
124 const Ptr<const Vector<Real>> BoundConstraint<Real>::getUpperBound( void ) const {
125  if (upper_ != nullPtr) {
126  return upper_;
127  }
128  throw Exception::NotImplemented(">>> ROL::BoundConstraint::getUpperBound: Upper bound not provided!");
129 }
130 
131 template<typename Real>
133  if (isActivated()) {
134  const Real tol(static_cast<Real>(1e-2)*std::sqrt(ROL_EPSILON<Real>()));
135  Ptr<Vector<Real>> Pv = v.clone();
136  Pv->set(v);
137  project(*Pv);
138  Pv->axpy(static_cast<Real>(-1),v);
139  Real diff = Pv->norm();
140  return (diff <= tol);
141  }
142  return true;
143 }
144 
145 template<typename Real>
147  throw Exception::NotImplemented(">>> BoundConstraint::applyInverseScalingFunction : This function has not been implemeted!");
148 }
149 
150 template<typename Real>
152  throw Exception::NotImplemented(">>> BoundConstraint::applyScalingFunctionJacobian : This function has not been implemeted!");
153 }
154 
155 template<typename Real>
157  Lactivated_ = true;
158 }
159 
160 template<typename Real>
162  Uactivated_ = true;
163 }
164 
165 template<typename Real>
167  activateLower();
168  activateUpper();
169 }
170 
171 template<typename Real>
173  Lactivated_ = false;
174 }
175 
176 template<typename Real>
178  Uactivated_ = false;
179 }
180 
181 template<typename Real>
183  deactivateLower();
184  deactivateUpper();
185 }
186 
187 template<typename Real>
189  return Lactivated_;
190 }
191 
192 template<typename Real>
194  return Uactivated_;
195 }
196 
197 template<typename Real>
199  return (isLowerActivated() || isUpperActivated());
200 }
201 
202 template<typename Real>
204  if (isActivated()) {
205  pruneUpperActive(v,x,eps);
206  pruneLowerActive(v,x,eps);
207  }
208 }
209 
210 template<typename Real>
211 void BoundConstraint<Real>::pruneActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
212  if (isActivated()) {
213  pruneUpperActive(v,g,x,xeps,geps);
214  pruneLowerActive(v,g,x,xeps,geps);
215  }
216 }
217 
218 template<typename Real>
220  if (isLowerActivated()) {
221  const Real one(1);
222  Ptr<Vector<Real>> tmp = v.clone();
223  tmp->set(v);
224  pruneLowerActive(*tmp,x,eps);
225  v.axpy(-one,*tmp);
226  }
227 }
228 
229 template<typename Real>
231  if (isUpperActivated()) {
232  const Real one(1);
233  Ptr<Vector<Real>> tmp = v.clone();
234  tmp->set(v);
235  pruneUpperActive(*tmp,x,eps);
236  v.axpy(-one,*tmp);
237  }
238 }
239 
240 template<typename Real>
241 void BoundConstraint<Real>::pruneLowerInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
242  if (isLowerActivated()) {
243  const Real one(1);
244  Ptr<Vector<Real>> tmp = v.clone();
245  tmp->set(v);
246  pruneLowerActive(*tmp,g,x,xeps,geps);
247  v.axpy(-one,*tmp);
248  }
249 }
250 
251 template<typename Real>
252 void BoundConstraint<Real>::pruneUpperInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
253  if (isUpperActivated()) {
254  const Real one(1);
255  Ptr<Vector<Real>> tmp = v.clone();
256  tmp->set(v);
257  pruneUpperActive(*tmp,g,x,xeps,geps);
258  v.axpy(-one,*tmp);
259  }
260 }
261 
262 template<typename Real>
264  if (isActivated()) {
265  const Real one(1);
266  Ptr<Vector<Real>> tmp = v.clone();
267  tmp->set(v);
268  pruneActive(*tmp,x,eps);
269  v.axpy(-one,*tmp);
270  }
271 }
272 
273 template<typename Real>
274 void BoundConstraint<Real>::pruneInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
275  if (isActivated()) {
276  const Real one(1);
277  Ptr<Vector<Real>> tmp = v.clone();
278  tmp->set(v);
279  pruneActive(*tmp,g,x,xeps,geps);
280  v.axpy(-one,*tmp);
281  }
282 }
283 
284 template<typename Real>
286  if (isActivated()) {
287  Ptr<Vector<Real>> tmp = g.clone();
288  tmp->set(g);
289  pruneActive(g,*tmp,x);
290  }
291 }
292 
293 template<typename Real>
295  if (isActivated()) {
296  const Real one(1);
297  v.plus(x);
298  project(v);
299  v.axpy(-one,x);
300  }
301 }
302 
303 } // namespace ROL
304 
305 #endif
virtual bool isFeasible(const Vector< Real > &v)
Check if the vector, v, is feasible.
Ptr< Vector< Real > > upper_
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual int dimension() const
Return dimension of the vector space.
Definition: ROL_Vector.hpp:196
virtual void plus(const Vector &x)=0
Compute , where .
virtual void projectInterior(Vector< Real > &x)
Project optimization variables into the interior of the feasible set.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
void activateLower(void)
Turn on lower bound.
void activate(void)
Turn on bounds.
bool isActivated(void) const
Check if bounds are on.
virtual void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the upper -active set.
void pruneUpperInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the -inactive set.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual const Ptr< const Vector< Real > > getLowerBound(void) const
Return the ref count pointer to the lower bound vector.
virtual void applyInverseScalingFunction(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const
Apply inverse scaling function.
void activateUpper(void)
Turn on upper bound.
Ptr< Vector< Real > > lower_
void pruneLowerInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the -inactive set.
void deactivateUpper(void)
Turn off upper bound.
virtual void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the lower -active set.
void computeProjectedStep(Vector< Real > &v, const Vector< Real > &x)
Compute projected step.
void deactivateLower(void)
Turn off lower bound.
virtual void project(Vector< Real > &x)
Project optimization variables onto the bounds.
virtual const Ptr< const Vector< Real > > getUpperBound(void) const
Return the ref count pointer to the upper bound vector.
void pruneInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the -inactive set.
void pruneActive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the -active set.
Real computeInf(const Vector< Real > &x) const
void computeProjectedGradient(Vector< Real > &g, const Vector< Real > &x)
Compute projected gradient.
virtual void applyScalingFunctionJacobian(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const
Apply scaling function Jacobian.
void deactivate(void)
Turn off bounds.
bool isLowerActivated(void) const
Check if lower bound are on.
constexpr auto dim
bool isUpperActivated(void) const
Check if upper bound are on.