ROL
ROL_StdBoundConstraint.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_HPP
50 #define ROL_STDBOUNDCONSTRAINT_HPP
51 
52 #include "ROL_StdVector.hpp"
53 #include "ROL_BoundConstraint.hpp"
54 
55 namespace ROL {
56 
57  template<class Real>
58  class StdBoundConstraint : public BoundConstraint<Real> {
59  private:
60  int dim_;
61  std::vector<Real> x_lo_;
62  std::vector<Real> x_up_;
63  Real min_diff_;
64  Real scale_;
65  public:
66  StdBoundConstraint(std::vector<Real> &l, std::vector<Real> &u, Real scale = 1.0)
67  : x_lo_(l), x_up_(u), scale_(scale) {
68  dim_ = x_lo_.size();
69  for ( int i = 0; i < dim_; i++ ) {
70  if ( i == 0 ) {
71  min_diff_ = x_up_[i] - x_lo_[i];
72  }
73  else {
74  min_diff_ = ( (min_diff_ < (x_up_[i] - x_lo_[i])) ? min_diff_ : (x_up_[i] - x_lo_[i]) );
75  }
76  }
77  min_diff_ *= 0.5;
78  }
79 
80  bool isFeasible( const Vector<Real> &x ) {
81  Teuchos::RCP<const std::vector<Real> > ex =
82  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
83  bool val = true;
84  int cnt = 1;
85  for ( int i = 0; i < this->dim_; i++ ) {
86  if ( (*ex)[i] >= this->x_lo_[i] && (*ex)[i] <= this->x_up_[i] ) { cnt *= 1; }
87  else { cnt *= 0; }
88  }
89  if ( cnt == 0 ) { val = false; }
90  return val;
91  }
92 
93  void project( Vector<Real> &x ) {
94  Teuchos::RCP<std::vector<Real> > ex =
95  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(x)).getVector());
96  for ( int i = 0; i < this->dim_; i++ ) {
97  (*ex)[i] = std::max(this->x_lo_[i],std::min(this->x_up_[i],(*ex)[i]));
98  }
99  }
100 
101  void pruneLowerActive(Vector<Real> &v, const Vector<Real> &x, Real eps) {
102  Teuchos::RCP<const std::vector<Real> > ex =
103  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
104  Teuchos::RCP<std::vector<Real> > ev =
105  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(v)).getVector());
106  Real epsn = std::min(scale_*eps,this->min_diff_);
107  for ( int i = 0; i < this->dim_; i++ ) {
108  if ( ((*ex)[i] <= this->x_lo_[i]+epsn) ) {
109  (*ev)[i] = 0.0;
110  }
111  }
112  }
113 
114  void pruneUpperActive(Vector<Real> &v, const Vector<Real> &x, Real eps) {
115  Teuchos::RCP<const std::vector<Real> > ex =
116  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
117  Teuchos::RCP<std::vector<Real> > ev =
118  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(v)).getVector());
119  Real epsn = std::min(scale_*eps,this->min_diff_);
120  for ( int i = 0; i < this->dim_; i++ ) {
121  if ( ((*ex)[i] >= this->x_up_[i]-epsn) ) {
122  (*ev)[i] = 0.0;
123  }
124  }
125  }
126 
127  void pruneActive(Vector<Real> &v, const Vector<Real> &x, Real eps) {
128  Teuchos::RCP<const std::vector<Real> > ex =
129  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
130  Teuchos::RCP<std::vector<Real> > ev =
131  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(v)).getVector());
132  Real epsn = std::min(scale_*eps,this->min_diff_);
133  for ( int i = 0; i < this->dim_; i++ ) {
134  if ( ((*ex)[i] <= this->x_lo_[i]+epsn) ||
135  ((*ex)[i] >= this->x_up_[i]-epsn) ) {
136  (*ev)[i] = 0.0;
137  }
138  }
139  }
140 
141  void pruneLowerActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps) {
142  Teuchos::RCP<const std::vector<Real> > ex =
143  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
144  Teuchos::RCP<const std::vector<Real> > eg =
145  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(g))).getVector();
146  Teuchos::RCP<std::vector<Real> > ev =
147  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(v)).getVector());
148  Real epsn = std::min(scale_*eps,this->min_diff_);
149  for ( int i = 0; i < this->dim_; i++ ) {
150  if ( ((*ex)[i] <= this->x_lo_[i]+epsn && (*eg)[i] > 0.0) ) {
151  (*ev)[i] = 0.0;
152  }
153  }
154  }
155 
156  void pruneUpperActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps) {
157  Teuchos::RCP<const std::vector<Real> > ex =
158  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
159  Teuchos::RCP<const std::vector<Real> > eg =
160  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(g))).getVector();
161  Teuchos::RCP<std::vector<Real> > ev =
162  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(v)).getVector());
163  Real epsn = std::min(scale_*eps,this->min_diff_);
164  for ( int i = 0; i < this->dim_; i++ ) {
165  if ( ((*ex)[i] >= this->x_up_[i]-epsn && (*eg)[i] < 0.0) ) {
166  (*ev)[i] = 0.0;
167  }
168  }
169  }
170 
171  void pruneActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps) {
172  Teuchos::RCP<const std::vector<Real> > ex =
173  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
174  Teuchos::RCP<const std::vector<Real> > eg =
175  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(g))).getVector();
176  Teuchos::RCP<std::vector<Real> > ev =
177  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(v)).getVector());
178  Real epsn = std::min(scale_*eps,this->min_diff_);
179  for ( int i = 0; i < this->dim_; i++ ) {
180  if ( ((*ex)[i] <= this->x_lo_[i]+epsn && (*eg)[i] > 0.0) ||
181  ((*ex)[i] >= this->x_up_[i]-epsn && (*eg)[i] < 0.0) ) {
182  (*ev)[i] = 0.0;
183  }
184  }
185  }
186 
188  Teuchos::RCP<std::vector<Real> > us = Teuchos::rcp( new std::vector<Real>(this->dim_,0.0) );
189  us->assign(this->x_up_.begin(),this->x_up_.end());
190  Teuchos::RCP<ROL::Vector<Real> > up = Teuchos::rcp( new ROL::StdVector<Real>(us) );
191  u.set(*up);
192  }
193 
195  Teuchos::RCP<std::vector<Real> > ls = Teuchos::rcp( new std::vector<Real>(this->dim_,0.0) );
196  ls->assign(this->x_lo_.begin(),this->x_lo_.end());
197  Teuchos::RCP<ROL::Vector<Real> > lp = Teuchos::rcp( new ROL::StdVector<Real>(ls) );
198  l.set(*lp);
199  }
200  };
201 
202 }// End ROL Namespace
203 
204 #endif
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the upper -binding set.
void pruneActive(Vector< Real > &v, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the -active set.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
StdBoundConstraint(std::vector< Real > &l, std::vector< Real > &u, Real scale=1.0)
void setVectorToLowerBound(ROL::Vector< Real > &l)
Set the input vector to the lower bound.
void project(Vector< Real > &x)
Project optimization variables onto the bounds.
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the upper -active set.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps)
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)
Set variables to zero if they correspond to the -binding set.
void setVectorToUpperBound(ROL::Vector< Real > &u)
Set the input vector to the upper bound.
Provides the interface to apply upper and lower bound constraints.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the lower -binding set.
bool isFeasible(const Vector< Real > &x)
Check if the vector, v, is feasible.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:196