ROL
ROL_SROMBoundConstraint.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_SROMBOUNDCONSTRAINT_HPP
50 #define ROL_SROMBOUNDCONSTRAINT_HPP
51 
52 #include "ROL_SROMVector.hpp"
53 #include "ROL_BoundConstraint.hpp"
54 
55 namespace ROL {
56 
57 template<class Real>
58 class SROMBoundConstraint : public BoundConstraint<Real> {
59 private:
60  std::vector<Real> pt_lo_;
61  std::vector<Real> pt_up_;
62 
63  size_t dimension_;
64 
65  Real min_diff_;
66  Real scale_;
67 
68 public:
69 
70  SROMBoundConstraint(const std::vector<Real> &pt_lo,
71  const std::vector<Real> &pt_up,
72  const Real scale = 1.0)
73  : pt_lo_(pt_lo), pt_up_(pt_up), scale_(scale) {
74  dimension_ = pt_lo_.size();
75  min_diff_ = 1.0;
76  Real diff = 0.0;
77  for ( size_t i = 0; i < dimension_; i++ ) {
78  diff = pt_up[i] - pt_lo_[i];
79  min_diff_ = ( (min_diff_ < diff) ? min_diff_ : diff );
80  }
81  min_diff_ *= 0.5;
82  }
83 
84  SROMBoundConstraint(const size_t dimension, const Real scale = 1.0)
85  : dimension_(dimension), min_diff_(1.0), scale_(scale) {
86  pt_lo_.clear(); pt_lo_.assign(dimension_,-0.1*ROL_OVERFLOW);
87  pt_up_.clear(); pt_up_.assign(dimension_, 0.1*ROL_OVERFLOW);
88  }
89 
90  bool isFeasible( const Vector<Real> &x ) {
91  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
92  size_t cnt = 1;
93  for (size_t i = 0; i < ex.getNumSamples(); i++) {
94  for (size_t j = 0; j < dimension_; j++) {
95  cnt *= ( ( ((*ex.getPoint(i))[j] >= pt_lo_[j]) && ((*ex.getPoint(i))[j] <= pt_up_[j]) ) ? 1 : 0 );
96  }
97  cnt *= ( ((ex.getWeight(i) >= 0.0) && (ex.getWeight(i) <= 1.0)) ? 1 : 0 );
98  }
99  return ((cnt==0) ? false : true);
100  }
101 
102  void project( Vector<Real> &x ) {
103  SROMVector<Real> &ex = Teuchos::dyn_cast<SROMVector<Real> >(x);
104  std::vector<Real> pt(dimension_,0.0);
105  for (size_t i = 0; i < ex.getNumSamples(); i++) {
106  for (size_t j = 0; j < dimension_; j++) {
107  pt[j] = std::max(pt_lo_[j],std::min(pt_up_[j],(*ex.getPoint(i))[j]));
108  }
109  ex.setPoint(i,pt);
110  ex.setWeight(i,std::max(0.0,std::min(1.0,ex.getWeight(i))));
111  }
112  }
113 
114  void pruneLowerActive(Vector<Real> &v, const Vector<Real> &x, Real eps) {
115  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
116  SROMVector<Real> &ev = Teuchos::dyn_cast<SROMVector<Real> >(v);
117  Real epsn = std::min(scale_*eps,min_diff_);
118  std::vector<Real> pt(dimension_,0.0);
119  Teuchos::RCP<const std::vector<Real> > xpt, vpt;
120  for (size_t i = 0; i < ex.getNumSamples(); i++) {
121  xpt = ex.getPoint(i);
122  vpt = ev.getPoint(i);
123  for (size_t j = 0; j < dimension_; j++) {
124  pt[j] = ( ((*xpt)[j] <= pt_lo_[j]+epsn) ? 0.0 : (*vpt)[j] );
125  }
126  ev.setPoint(i,pt);
127  if ( ex.getWeight(i) <= 0.0+epsn ) {
128  ev.setWeight(i,0.0);
129  }
130  }
131  }
132 
133  void pruneUpperActive(Vector<Real> &v, const Vector<Real> &x, Real eps) {
134  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
135  SROMVector<Real> &ev = Teuchos::dyn_cast<SROMVector<Real> >(v);
136  Real epsn = std::min(scale_*eps,min_diff_);
137  std::vector<Real> pt(dimension_,0.0);
138  Teuchos::RCP<const std::vector<Real> > xpt, vpt;
139  for (size_t i = 0; i < ex.getNumSamples(); i++) {
140  xpt = ex.getPoint(i);
141  vpt = ev.getPoint(i);
142  for (size_t j = 0; j < dimension_; j++) {
143  pt[j] = ( ((*xpt)[j] >= pt_up_[j]-epsn) ? 0.0 : (*vpt)[j] );
144  }
145  ev.setPoint(i,pt);
146  if ( ex.getWeight(i) >= 1.0-epsn ) {
147  ev.setWeight(i,0.0);
148  }
149  }
150  }
151 
152  void pruneActive(Vector<Real> &v, const Vector<Real> &x, Real eps) {
153  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
154  SROMVector<Real> &ev = Teuchos::dyn_cast<SROMVector<Real> >(v);
155  Real epsn = std::min(scale_*eps,min_diff_);
156  std::vector<Real> pt(dimension_,0.0);
157  Teuchos::RCP<const std::vector<Real> > xpt, vpt;
158  for (size_t i = 0; i < ex.getNumSamples(); i++) {
159  xpt = ex.getPoint(i);
160  vpt = ev.getPoint(i);
161  for (size_t j = 0; j < dimension_; j++) {
162  pt[j] = ( ((*xpt)[j] >= pt_up_[j]-epsn || (*xpt)[j] <= pt_lo_[j]+epsn) ? 0.0 : (*vpt)[j] );
163  }
164  ev.setPoint(i,pt);
165  if ( ex.getWeight(i) >= 1.0-epsn || ex.getWeight(i) <= 0.0+epsn ) {
166  ev.setWeight(i,0.0);
167  }
168  }
169  }
170 
171  void pruneLowerActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps) {
172  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
173  const SROMVector<Real> &eg = Teuchos::dyn_cast<const SROMVector<Real> >(g);
174  SROMVector<Real> &ev = Teuchos::dyn_cast<SROMVector<Real> >(v);
175  Real epsn = std::min(scale_*eps,min_diff_);
176  std::vector<Real> pt(dimension_,0.0);
177  Teuchos::RCP<const std::vector<Real> > xpt, vpt, gpt;
178  for (size_t i = 0; i < ex.getNumSamples(); i++) {
179  xpt = ex.getPoint(i);
180  gpt = eg.getPoint(i);
181  vpt = ev.getPoint(i);
182  for (size_t j = 0; j < dimension_; j++) {
183  pt[j] = ( ((*xpt)[j] <= pt_lo_[j]+epsn && (*gpt)[i] > 0.0) ? 0.0 : (*vpt)[j] );
184  }
185  ev.setPoint(i,pt);
186  if ( ex.getWeight(i) <= 0.0+epsn && eg.getWeight(i) > 0.0 ) {
187  ev.setWeight(i,0.0);
188  }
189  }
190  }
191 
192  void pruneUpperActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps) {
193  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
194  const SROMVector<Real> &eg = Teuchos::dyn_cast<const SROMVector<Real> >(g);
195  SROMVector<Real> &ev = Teuchos::dyn_cast<SROMVector<Real> >(v);
196  Real epsn = std::min(scale_*eps,min_diff_);
197  std::vector<Real> pt(dimension_,0.0);
198  Teuchos::RCP<const std::vector<Real> > xpt, vpt, gpt;
199  for (size_t i = 0; i < ex.getNumSamples(); i++) {
200  xpt = ex.getPoint(i);
201  gpt = eg.getPoint(i);
202  vpt = ev.getPoint(i);
203  for (size_t j = 0; j < dimension_; j++) {
204  pt[j] = ( ((*xpt)[j] >= pt_up_[j]-epsn && (*gpt)[i] < 0.0) ? 0.0 : (*vpt)[j] );
205  }
206  ev.setPoint(i,pt);
207  if ( ex.getWeight(i) >= 1.0-epsn && eg.getWeight(i) < 0.0 ) {
208  ev.setWeight(i,0.0);
209  }
210  }
211  }
212 
213  void pruneActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps) {
214  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
215  const SROMVector<Real> &eg = Teuchos::dyn_cast<const SROMVector<Real> >(g);
216  SROMVector<Real> &ev = Teuchos::dyn_cast<SROMVector<Real> >(v);
217  Real epsn = std::min(scale_*eps,min_diff_);
218  std::vector<Real> pt(dimension_,0.0);
219  Teuchos::RCP<const std::vector<Real> > xpt, vpt, gpt;
220  for (size_t i = 0; i < ex.getNumSamples(); i++) {
221  xpt = ex.getPoint(i);
222  gpt = eg.getPoint(i);
223  vpt = ev.getPoint(i);
224  for (size_t j = 0; j < dimension_; j++) {
225  pt[j] = ( (((*xpt)[j] >= pt_up_[j]-epsn && (*gpt)[i] < 0.0) ||
226  ((*xpt)[j] <= pt_lo_[j]+epsn && (*gpt)[i] > 0.0)) ? 0.0 : (*vpt)[j] );
227  }
228  ev.setPoint(i,pt);
229  if ( (ex.getWeight(i) >= 1.0-epsn && eg.getWeight(i) < 0.0) ||
230  (ex.getWeight(i) <= 0.0+epsn && eg.getWeight(i) > 0.0) ) {
231  ev.setWeight(i,0.0);
232  }
233  }
234  }
235 
237  SROMVector<Real> &eu = Teuchos::dyn_cast<SROMVector<Real> >(u);
238  for (size_t i = 0; i < eu.getNumSamples(); i++) {
239  eu.setPoint(i,pt_up_);
240  eu.setWeight(i,1.0);
241  }
242  }
243 
245  SROMVector<Real> &el = Teuchos::dyn_cast<SROMVector<Real> >(l);
246  for (size_t i = 0; i < el.getNumSamples(); i++) {
247  el.setPoint(i,pt_lo_);
248  el.setWeight(i,0.0);
249  }
250  }
251 };
252 
253 }// End ROL Namespace
254 
255 #endif
Teuchos::RCP< const std::vector< Element > > getPoint(const size_t i) const
const Element getWeight(const size_t i) const
void setWeight(const size_t i, const Element wt)
bool isFeasible(const Vector< Real > &x)
Check if the vector, v, is feasible.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
const size_t getNumSamples(void) const
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 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.
Provides the std::vector implementation of the ROL::Vector interface.
SROMBoundConstraint(const size_t dimension, const Real scale=1.0)
Provides the interface to apply upper and lower bound constraints.
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 > &g, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the -binding set.
SROMBoundConstraint(const std::vector< Real > &pt_lo, const std::vector< Real > &pt_up, const Real scale=1.0)
void setVectorToUpperBound(ROL::Vector< Real > &u)
Set the input vector to the upper bound.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the lower -active set.
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the upper -active set.
static const double ROL_OVERFLOW
Platform-dependent maximum double.
Definition: ROL_Types.hpp:126
void pruneActive(Vector< Real > &v, const Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the -active set.
void setPoint(const size_t i, const std::vector< Element > &pt)