ROL
ROL_BoundConstraint.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_H
45 #define ROL_BOUND_CONSTRAINT_H
46 
47 #include "ROL_Vector.hpp"
48 #include "ROL_Types.hpp"
49 #include <iostream>
50 
69 namespace ROL {
70 
71 template <class Real>
73 private:
74  bool Lactivated_;
75  bool Uactivated_;
76  Ptr<Vector<Real> > lower_;
77  Ptr<Vector<Real> > upper_;
79 
80 public:
81 
82  virtual ~BoundConstraint() {}
83 
84  BoundConstraint(void) : Lactivated_(true), Uactivated_(true), hasSetScalar_(false) {}
85 
86  BoundConstraint(const Vector<Real> &x) : Lactivated_(false), Uactivated_(false), hasSetScalar_(false) {
87  try {
88  lower_ = x.clone(); lower_->setScalar(ROL_NINF<Real>());
89  upper_ = x.clone(); upper_->setScalar(ROL_INF<Real>());
90  hasSetScalar_ = true;
91  }
92  catch(std::exception &e) {
93  hasSetScalar_ = false;
94  // Do nothing. If someone calls getLowerBound or getUpperBound,
95  // an exception will be thrown.
96  }
97  }
98 
99 
100  // REQUIRED FUNCTIONS (VIRTUAL)
101 
109  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {}
110 
119  virtual void project( Vector<Real> &x ) {
120  if (isActivated()) {
121  throw Exception::NotImplemented(">>> ROL::BoundConstraint::project: Not Implemented!");
122  }
123  }
124 
135  virtual void projectInterior( Vector<Real> &x ) {
136  if (isActivated()) {
137  throw Exception::NotImplemented(">>> ROL::BoundConstraint::projectInterior: Not Implemented!");
138  }
139  }
140 
152  virtual void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0 ) {
153  if (isUpperActivated()) {
154  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneUpperActive: Not Implemented!");
155  }
156  }
157 
171  virtual void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0 ) {
172  if (isUpperActivated()) {
173  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneUpperActive: Not Implemented!");
174  }
175  }
176 
188  virtual void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0 ) {
189  if (isLowerActivated()) {
190  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneLowerActive: Not Implemented!");
191  }
192  }
193 
207  virtual void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0 ) {
208  if (isLowerActivated()) {
209  throw Exception::NotImplemented(">>> ROL::BoundConstraint::pruneLowerActive: Not Implemented!");
210  }
211  }
212 
213 
214  // QUERY FUNCTIONS (VIRTUAL AND NONVIRTUAL)
215 
217  virtual const ROL::Ptr<const Vector<Real> > getLowerBound( void ) const {
218  if (hasSetScalar_) {
219  return lower_;
220  }
221  throw Exception::NotImplemented(">>> ROL::BoundConstraint::getLowerBound: Not implemented!");
222  }
223 
225  virtual const ROL::Ptr<const Vector<Real> > getUpperBound( void ) const {
226  if (hasSetScalar_) {
227  return upper_;
228  }
229  throw Exception::NotImplemented(">>> ROL::BoundConstraint::getUpperBound: Not implemented!");
230  }
231 
237  virtual bool isFeasible( const Vector<Real> &v ) {
238  if (isActivated()) {
239  throw Exception::NotImplemented(">>> ROL::BoundConstraint::isFeasible: Not implemented!");
240  }
241  return true;
242  }
243 
248  void activateLower(void) {
249  Lactivated_ = true;
250  }
251 
256  void activateUpper(void) {
257  Uactivated_ = true;
258  }
259 
264  void activate(void) {
265  activateLower();
266  activateUpper();
267  }
268 
273  void deactivateLower(void) {
274  Lactivated_ = false;
275  }
276 
281  void deactivateUpper(void) {
282  Uactivated_ = false;
283  }
284 
289  void deactivate(void) {
290  deactivateLower();
291  deactivateUpper();
292  }
293 
298  bool isLowerActivated(void) const {
299  return Lactivated_;
300  }
301 
306  bool isUpperActivated(void) const {
307  return Uactivated_;
308  }
309 
314  bool isActivated(void) const {
315  return (isLowerActivated() || isUpperActivated());
316  }
317 
318 
319  // HELPER FUNCTIONS (NONVIRTUAL)
320 
332  void pruneActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0 ) {
333  if (isActivated()) {
334  pruneUpperActive(v,x,eps);
335  pruneLowerActive(v,x,eps);
336  }
337  }
338 
351  void pruneActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0 ) {
352  if (isActivated()) {
353  pruneUpperActive(v,g,x,eps);
354  pruneLowerActive(v,g,x,eps);
355  }
356  }
357 
365  void pruneLowerInactive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0 ) {
366  if (isLowerActivated()) {
367  const Real one(1);
368  ROL::Ptr<Vector<Real> > tmp = v.clone();
369  tmp->set(v);
370  pruneLowerActive(*tmp,x,eps);
371  v.axpy(-one,*tmp);
372  }
373  }
374 
382  void pruneUpperInactive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0 ) {
383  if (isUpperActivated()) {
384  const Real one(1);
385  ROL::Ptr<Vector<Real> > tmp = v.clone();
386  tmp->set(v);
387  pruneUpperActive(*tmp,x,eps);
388  v.axpy(-one,*tmp);
389  }
390  }
391 
400  void pruneLowerInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0 ) {
401  if (isLowerActivated()) {
402  const Real one(1);
403  ROL::Ptr<Vector<Real> > tmp = v.clone();
404  tmp->set(v);
405  pruneLowerActive(*tmp,g,x,eps);
406  v.axpy(-one,*tmp);
407  }
408  }
409 
418  void pruneUpperInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0 ) {
419  if (isUpperActivated()) {
420  const Real one(1);
421  ROL::Ptr<Vector<Real> > tmp = v.clone();
422  tmp->set(v);
423  pruneUpperActive(*tmp,g,x,eps);
424  v.axpy(-one,*tmp);
425  }
426  }
427 
435  void pruneInactive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0 ) {
436  if (isActivated()) {
437  const Real one(1);
438  ROL::Ptr<Vector<Real> > tmp = v.clone();
439  tmp->set(v);
440  pruneActive(*tmp,x,eps);
441  v.axpy(-one,*tmp);
442  }
443  }
444 
453  void pruneInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0 ) {
454  if (isActivated()) {
455  const Real one(1);
456  ROL::Ptr<Vector<Real> > tmp = v.clone();
457  tmp->set(v);
458  pruneActive(*tmp,g,x,eps);
459  v.axpy(-one,*tmp);
460  }
461  }
462 
470  if (isActivated()) {
471  ROL::Ptr<Vector<Real> > tmp = g.clone();
472  tmp->set(g);
473  pruneActive(g,*tmp,x);
474  }
475  }
476 
484  if (isActivated()) {
485  const Real one(1);
486  v.plus(x);
487  project(v);
488  v.axpy(-one,x);
489  }
490  }
491 
492 }; // class BoundConstraint
493 
494 } // namespace ROL
495 
496 #endif
bool Uactivated_
Flag that determines whether or not the upper bounds are being used.
Ptr< Vector< Real > > upper_
BoundConstraint(const Vector< Real > &x)
virtual const ROL::Ptr< const Vector< Real > > getUpperBound(void) const
Return the ref count pointer to the upper bound vector.
virtual void projectInterior(Vector< Real > &x)
Project optimization variables into the interior of the feasible set.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void plus(const Vector &x)=0
Compute , where .
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.
Contains definitions of custom data types in ROL.
virtual void pruneLowerActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -binding set.
void pruneInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -inactive set.
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update bounds.
void pruneUpperInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -inactive set.
void pruneActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -active set.
bool Lactivated_
Flag that determines whether or not the lower bounds are being used.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual void pruneUpperActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the upper -binding set.
virtual void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the lower -active set.
void pruneActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -binding set.
void activateUpper(void)
Turn on upper bound.
Ptr< Vector< Real > > lower_
void deactivateUpper(void)
Turn off upper bound.
virtual void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the upper -active set.
void pruneLowerInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -inactive set.
void computeProjectedStep(Vector< Real > &v, const Vector< Real > &x)
Compute projected step.
void deactivateLower(void)
Turn off lower bound.
void pruneLowerInactive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -nonbinding set.
Provides the interface to apply upper and lower bound constraints.
void computeProjectedGradient(Vector< Real > &g, const Vector< Real > &x)
Compute projected gradient.
virtual const ROL::Ptr< const Vector< Real > > getLowerBound(void) const
Return the ref count pointer to the lower bound vector.
void pruneUpperInactive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -nonbinding set.
void pruneInactive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -nonbinding set.
void deactivate(void)
Turn off bounds.
bool isLowerActivated(void) const
Check if lower bound are on.
virtual bool isFeasible(const Vector< Real > &v)
Check if the vector, v, is feasible.
virtual void project(Vector< Real > &x)
Project optimization variables onto the bounds.
bool isUpperActivated(void) const
Check if upper bound are on.