ROL
ROL_BoundConstraint_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_BOUND_CONSTRAINT_DEF_H
11 #define ROL_BOUND_CONSTRAINT_DEF_H
12 
13 namespace ROL {
14 
15 template<typename Real>
17  int dim = x.dimension();
18  Real denom = (dim > 0 ? static_cast<Real>(dim) : 1e15);
19  return std::sqrt(ROL_INF<Real>() / denom);
20 }
21 
22 template<typename Real>
24  : Lactivated_(true), Uactivated_(true) {}
25 
26 template<typename Real>
28  : Lactivated_(false), Uactivated_(false) {
29  try {
30  lower_ = x.clone(); lower_->setScalar(-computeInf(x));
31  upper_ = x.clone(); upper_->setScalar( computeInf(x));
32  }
33  catch(std::exception &e) {
34  // Do nothing. If someone calls getLowerBound or getUpperBound,
35  // an exception will be thrown.
36  }
37 }
38 
39 template<typename Real>
41  if (isActivated()) {
42  throw Exception::NotImplemented(">>> ROL::BoundConstraint::project: Not Implemented!");
43  }
44 }
45 
46 template<typename Real>
48  if (isActivated()) {
49  throw Exception::NotImplemented(">>> ROL::BoundConstraint::projectInterior: Not Implemented!");
50  }
51 }
52 
53 template<typename Real>
55  if (isUpperActivated()) {
56  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneUpperActive: Not Implemented!");
57  }
58 }
59 
60 template<typename Real>
61 void BoundConstraint<Real>::pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
62  if (isUpperActivated()) {
63  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneUpperActive: Not Implemented!");
64  }
65 }
66 
67 template<typename Real>
69  if (isLowerActivated()) {
70  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneLowerActive: Not Implemented!");
71  }
72 }
73 
74 template<typename Real>
75 void BoundConstraint<Real>::pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
76  if (isLowerActivated()) {
77  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneLowerActive: Not Implemented!");
78  }
79 }
80 
81 template<typename Real>
82 const Ptr<const Vector<Real>> BoundConstraint<Real>::getLowerBound( void ) const {
83  if (lower_ != nullPtr) {
84  return lower_;
85  }
86  throw Exception::NotImplemented(">>> ROL::BoundConstraint::getLowerBound: Lower bound not provided!");
87 }
88 
89 template<typename Real>
90 const Ptr<const Vector<Real>> BoundConstraint<Real>::getUpperBound( void ) const {
91  if (upper_ != nullPtr) {
92  return upper_;
93  }
94  throw Exception::NotImplemented(">>> ROL::BoundConstraint::getUpperBound: Upper bound not provided!");
95 }
96 
97 template<typename Real>
99  if (isActivated()) {
100  const Real tol(static_cast<Real>(1e-2)*std::sqrt(ROL_EPSILON<Real>()));
101  Ptr<Vector<Real>> Pv = v.clone();
102  Pv->set(v);
103  project(*Pv);
104  Pv->axpy(static_cast<Real>(-1),v);
105  Real diff = Pv->norm();
106  return (diff <= tol);
107  }
108  return true;
109 }
110 
111 template<typename Real>
113  throw Exception::NotImplemented(">>> BoundConstraint::applyInverseScalingFunction : This function has not been implemeted!");
114 }
115 
116 template<typename Real>
118  throw Exception::NotImplemented(">>> BoundConstraint::applyScalingFunctionJacobian : This function has not been implemeted!");
119 }
120 
121 template<typename Real>
123  Lactivated_ = true;
124 }
125 
126 template<typename Real>
128  Uactivated_ = true;
129 }
130 
131 template<typename Real>
133  activateLower();
134  activateUpper();
135 }
136 
137 template<typename Real>
139  Lactivated_ = false;
140 }
141 
142 template<typename Real>
144  Uactivated_ = false;
145 }
146 
147 template<typename Real>
149  deactivateLower();
150  deactivateUpper();
151 }
152 
153 template<typename Real>
155  return Lactivated_;
156 }
157 
158 template<typename Real>
160  return Uactivated_;
161 }
162 
163 template<typename Real>
165  return (isLowerActivated() || isUpperActivated());
166 }
167 
168 template<typename Real>
170  if (isActivated()) {
171  pruneUpperActive(v,x,eps);
172  pruneLowerActive(v,x,eps);
173  }
174 }
175 
176 template<typename Real>
177 void BoundConstraint<Real>::pruneActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
178  if (isActivated()) {
179  pruneUpperActive(v,g,x,xeps,geps);
180  pruneLowerActive(v,g,x,xeps,geps);
181  }
182 }
183 
184 template<typename Real>
186  if (isLowerActivated()) {
187  const Real one(1);
188  Ptr<Vector<Real>> tmp = v.clone();
189  tmp->set(v);
190  pruneLowerActive(*tmp,x,eps);
191  v.axpy(-one,*tmp);
192  }
193 }
194 
195 template<typename Real>
197  if (isUpperActivated()) {
198  const Real one(1);
199  Ptr<Vector<Real>> tmp = v.clone();
200  tmp->set(v);
201  pruneUpperActive(*tmp,x,eps);
202  v.axpy(-one,*tmp);
203  }
204 }
205 
206 template<typename Real>
207 void BoundConstraint<Real>::pruneLowerInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
208  if (isLowerActivated()) {
209  const Real one(1);
210  Ptr<Vector<Real>> tmp = v.clone();
211  tmp->set(v);
212  pruneLowerActive(*tmp,g,x,xeps,geps);
213  v.axpy(-one,*tmp);
214  }
215 }
216 
217 template<typename Real>
218 void BoundConstraint<Real>::pruneUpperInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
219  if (isUpperActivated()) {
220  const Real one(1);
221  Ptr<Vector<Real>> tmp = v.clone();
222  tmp->set(v);
223  pruneUpperActive(*tmp,g,x,xeps,geps);
224  v.axpy(-one,*tmp);
225  }
226 }
227 
228 template<typename Real>
230  if (isActivated()) {
231  const Real one(1);
232  Ptr<Vector<Real>> tmp = v.clone();
233  tmp->set(v);
234  pruneActive(*tmp,x,eps);
235  v.axpy(-one,*tmp);
236  }
237 }
238 
239 template<typename Real>
240 void BoundConstraint<Real>::pruneInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps ) {
241  if (isActivated()) {
242  const Real one(1);
243  Ptr<Vector<Real>> tmp = v.clone();
244  tmp->set(v);
245  pruneActive(*tmp,g,x,xeps,geps);
246  v.axpy(-one,*tmp);
247  }
248 }
249 
250 template<typename Real>
252  if (isActivated()) {
253  Ptr<Vector<Real>> tmp = g.clone();
254  tmp->set(g);
255  pruneActive(g,*tmp,x);
256  }
257 }
258 
259 template<typename Real>
261  if (isActivated()) {
262  const Real one(1);
263  v.plus(x);
264  project(v);
265  v.axpy(-one,x);
266  }
267 }
268 
269 } // namespace ROL
270 
271 #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:162
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:119
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:46
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.