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
ROL::BoundConstraint::activateUpper
void activateUpper(void)
Turn on upper bound.
Definition: ROL_BoundConstraint.hpp:256
ROL::Vector::clone
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ROL::BoundConstraint::getUpperBound
virtual const ROL::Ptr< const Vector< Real > > getUpperBound(void) const
Return the ref count pointer to the upper bound vector.
Definition: ROL_BoundConstraint.hpp:225
ROL::Vector::axpy
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
ROL::BoundConstraint::isActivated
bool isActivated(void) const
Check if bounds are on.
Definition: ROL_BoundConstraint.hpp:314
ROL::BoundConstraint::pruneUpperInactive
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.
Definition: ROL_BoundConstraint.hpp:418
ROL::BoundConstraint::isUpperActivated
bool isUpperActivated(void) const
Check if upper bound are on.
Definition: ROL_BoundConstraint.hpp:306
ROL::BoundConstraint::Lactivated_
bool Lactivated_
Flag that determines whether or not the lower bounds are being used.
Definition: ROL_BoundConstraint.hpp:74
ROL::BoundConstraint::deactivateUpper
void deactivateUpper(void)
Turn off upper bound.
Definition: ROL_BoundConstraint.hpp:281
ROL::BoundConstraint::deactivateLower
void deactivateLower(void)
Turn off lower bound.
Definition: ROL_BoundConstraint.hpp:273
ROL::BoundConstraint::~BoundConstraint
virtual ~BoundConstraint()
Definition: ROL_BoundConstraint.hpp:82
ROL::BoundConstraint::lower_
Ptr< Vector< Real > > lower_
Definition: ROL_BoundConstraint.hpp:76
ROL::BoundConstraint::hasSetScalar_
bool hasSetScalar_
Definition: ROL_BoundConstraint.hpp:78
ROL::BoundConstraint::isFeasible
virtual bool isFeasible(const Vector< Real > &v)
Check if the vector, v, is feasible.
Definition: ROL_BoundConstraint.hpp:237
ROL::BoundConstraint::pruneUpperActive
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.
Definition: ROL_BoundConstraint.hpp:152
ROL::BoundConstraint::deactivate
void deactivate(void)
Turn off bounds.
Definition: ROL_BoundConstraint.hpp:289
ROL::BoundConstraint::computeProjectedStep
void computeProjectedStep(Vector< Real > &v, const Vector< Real > &x)
Compute projected step.
Definition: ROL_BoundConstraint.hpp:483
ROL::BoundConstraint::pruneInactive
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.
Definition: ROL_BoundConstraint.hpp:453
ROL::BoundConstraint::project
virtual void project(Vector< Real > &x)
Project optimization variables onto the bounds.
Definition: ROL_BoundConstraint.hpp:119
ROL::Vector::plus
virtual void plus(const Vector &x)=0
Compute , where .
ROL::BoundConstraint::pruneActive
void pruneActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -active set.
Definition: ROL_BoundConstraint.hpp:332
ROL::BoundConstraint::getLowerBound
virtual const ROL::Ptr< const Vector< Real > > getLowerBound(void) const
Return the ref count pointer to the lower bound vector.
Definition: ROL_BoundConstraint.hpp:217
ROL::BoundConstraint::pruneUpperActive
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.
Definition: ROL_BoundConstraint.hpp:171
ROL::BoundConstraint::computeProjectedGradient
void computeProjectedGradient(Vector< Real > &g, const Vector< Real > &x)
Compute projected gradient.
Definition: ROL_BoundConstraint.hpp:469
ROL::Vector
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
ROL_Types.hpp
Contains definitions of custom data types in ROL.
ROL::BoundConstraint::pruneLowerActive
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.
Definition: ROL_BoundConstraint.hpp:188
ROL::BoundConstraint::activate
void activate(void)
Turn on bounds.
Definition: ROL_BoundConstraint.hpp:264
ROL
Definition: ROL_ElementwiseVector.hpp:61
ROL::BoundConstraint::Uactivated_
bool Uactivated_
Flag that determines whether or not the upper bounds are being used.
Definition: ROL_BoundConstraint.hpp:75
ROL::BoundConstraint::BoundConstraint
BoundConstraint(const Vector< Real > &x)
Definition: ROL_BoundConstraint.hpp:86
ROL::BoundConstraint::activateLower
void activateLower(void)
Turn on lower bound.
Definition: ROL_BoundConstraint.hpp:248
ROL::BoundConstraint::update
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update bounds.
Definition: ROL_BoundConstraint.hpp:109
ROL::BoundConstraint::projectInterior
virtual void projectInterior(Vector< Real > &x)
Project optimization variables into the interior of the feasible set.
Definition: ROL_BoundConstraint.hpp:135
ROL::Exception::NotImplemented
Definition: ROL_Types.hpp:983
ROL::BoundConstraint::pruneUpperInactive
void pruneUpperInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -inactive set.
Definition: ROL_BoundConstraint.hpp:382
ROL::BoundConstraint
Provides the interface to apply upper and lower bound constraints.
Definition: ROL_BoundConstraint.hpp:72
ROL::BoundConstraint::upper_
Ptr< Vector< Real > > upper_
Definition: ROL_BoundConstraint.hpp:77
ROL::BoundConstraint::pruneLowerInactive
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.
Definition: ROL_BoundConstraint.hpp:400
ROL::BoundConstraint::pruneInactive
void pruneInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -inactive set.
Definition: ROL_BoundConstraint.hpp:435
ROL::BoundConstraint::BoundConstraint
BoundConstraint(void)
Definition: ROL_BoundConstraint.hpp:84
ROL_Vector.hpp
ROL::BoundConstraint::pruneLowerInactive
void pruneLowerInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -inactive set.
Definition: ROL_BoundConstraint.hpp:365
ROL::BoundConstraint::pruneActive
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.
Definition: ROL_BoundConstraint.hpp:351
ROL::BoundConstraint::pruneLowerActive
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.
Definition: ROL_BoundConstraint.hpp:207
ROL::BoundConstraint::isLowerActivated
bool isLowerActivated(void) const
Check if lower bound are on.
Definition: ROL_BoundConstraint.hpp:298