ROL
ROL_RiskBoundConstraint.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_RISK_BOUND_CONSTRAINT_H
45 #define ROL_RISK_BOUND_CONSTRAINT_H
46 
48 #include "ROL_RiskVector.hpp"
49 #include "ROL_Types.hpp"
50 
51 namespace ROL {
52 
53 template <class Real>
54 class RiskBoundConstraint : public BoundConstraint<Real> {
55 private:
56  Teuchos::RCP<BoundConstraint<Real> > bc_;
57  Teuchos::RCP<StdBoundConstraint<Real> > stat_bc_;
58  std::vector<Real> lower_, upper_;
59 
61  int nStat_;
62 
64  mutable Teuchos::RCP<RiskVector<Real> > lo_, hi_;
65 
66 public:
67 
68  RiskBoundConstraint(Teuchos::ParameterList &parlist,
69  const Teuchos::RCP<BoundConstraint<Real> > &bc = Teuchos::null)
70  : BoundConstraint<Real>(), bc_(bc), stat_bc_(Teuchos::null),
71  augmented_(false), activated_(false), nStat_(0),
72  isLOinitialized_(false), isHIinitialized_(false) {
73  lower_.clear(); upper_.clear();
74  // Get stochastic optimization information
75  std::string optType = parlist.sublist("SOL").get("Stochastic Optimization Type","Risk Averse");
76  if ( optType == "BPOE" ) {
77  augmented_ = true;
78  activated_ = true;
79  nStat_ = 1;
80  lower_.resize(nStat_,ROL_NINF<Real>());
81  upper_.resize(nStat_,ROL_INF<Real>());
82  lower_[0] = static_cast<Real>(0);
83  }
84  else if ( optType == "Risk Averse" ) {
85  std::string name;
86  RiskMeasureInfo<Real>(parlist,name,nStat_,lower_,upper_,activated_);
87  augmented_ = (nStat_ > 0) ? true : false;
88  }
89  else if ( optType == "Risk Neutral" || optType == "Mean Value" ) {
90  augmented_ = false;
91  activated_ = false;
92  nStat_ = 0;
93  }
94  else {
95  TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
96  ">>> (ROL::RiskBoundConstraint): Invalid stochastic optimization type!" << optType);
97  }
98  // Build statistic bound constraint
99  if ( augmented_ ) {
100  stat_bc_ = Teuchos::rcp(new StdBoundConstraint<Real>(lower_,upper_));
101  }
102  // Determine whether or not bound constraint is activated
104  if ( !activated_ ) {
105  if ( stat_bc_ != Teuchos::null ) {
106  stat_bc_->deactivate();
107  }
108  if ( bc == Teuchos::null || (bc != Teuchos::null && !bc->isActivated()) ) {
110  }
111  }
112  }
113 
114  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
115  if ( augmented_ && activated_ ) {
116  Teuchos::RCP<const StdVector<Real> > xs = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic();
117  stat_bc_->update(*xs,flag,iter);
118  }
119  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
120  Teuchos::RCP<const Vector<Real> > xv = Teuchos::dyn_cast<const RiskVector<Real> >(x).getVector();
121  bc_->update(*xv,flag,iter);
122  }
123  }
124 
125  void project( Vector<Real> &x ) {
126  if ( augmented_ && activated_ ) {
127  Teuchos::RCP<StdVector<Real> > xs = Teuchos::dyn_cast<RiskVector<Real> >(x).getStatistic();
128  stat_bc_->project(*xs);
129  }
130  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
131  Teuchos::RCP<Vector<Real> > xvec = Teuchos::dyn_cast<RiskVector<Real> >(x).getVector();
132  bc_->project(*xvec);
133  }
134  }
135 
136  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0 ) {
137  if ( augmented_ && activated_ ) {
138  Teuchos::RCP<StdVector<Real> > vs = Teuchos::dyn_cast<RiskVector<Real> >(v).getStatistic();
139  Teuchos::RCP<const StdVector<Real> > xs = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic();
140  stat_bc_->pruneUpperActive(*vs,*xs,eps);
141  }
142  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
143  Teuchos::RCP<Vector<Real> > vv = Teuchos::dyn_cast<RiskVector<Real> >(v).getVector();
144  Teuchos::RCP<const Vector<Real> > xv = Teuchos::dyn_cast<const RiskVector<Real> >(x).getVector();
145  bc_->pruneUpperActive(*vv,*xv,eps);
146  }
147  }
148 
149  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0 ) {
150  if ( augmented_ && activated_ ) {
151  Teuchos::RCP<StdVector<Real> > vs = Teuchos::dyn_cast<RiskVector<Real> >(v).getStatistic();
152  Teuchos::RCP<const StdVector<Real> > gs = Teuchos::dyn_cast<const RiskVector<Real> >(g).getStatistic();
153  Teuchos::RCP<const StdVector<Real> > xs = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic();
154  stat_bc_->pruneUpperActive(*vs,*gs,*xs,eps);
155  }
156  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
157  Teuchos::RCP<Vector<Real> > vv = Teuchos::dyn_cast<RiskVector<Real> >(v).getVector();
158  Teuchos::RCP<const Vector<Real> > gv = Teuchos::dyn_cast<const RiskVector<Real> >(g).getVector();
159  Teuchos::RCP<const Vector<Real> > xv = Teuchos::dyn_cast<const RiskVector<Real> >(x).getVector();
160  bc_->pruneUpperActive(*vv,*gv,*xv,eps);
161  }
162  }
163 
164  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0 ) {
165  if ( augmented_ && activated_ ) {
166  Teuchos::RCP<StdVector<Real> > vs = Teuchos::dyn_cast<RiskVector<Real> >(v).getStatistic();
167  Teuchos::RCP<const StdVector<Real> > xs = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic();
168  stat_bc_->pruneLowerActive(*vs,*xs,eps);
169  }
170  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
171  Teuchos::RCP<Vector<Real> > vv = Teuchos::dyn_cast<RiskVector<Real> >(v).getVector();
172  Teuchos::RCP<const Vector<Real> > xv = Teuchos::dyn_cast<const RiskVector<Real> >(x).getVector();
173  bc_->pruneLowerActive(*vv,*xv,eps);
174  }
175  }
176 
177  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0 ) {
178  if ( augmented_ && activated_ ) {
179  Teuchos::RCP<StdVector<Real> > vs = Teuchos::dyn_cast<RiskVector<Real> >(v).getStatistic();
180  Teuchos::RCP<const StdVector<Real> > gs = Teuchos::dyn_cast<const RiskVector<Real> >(g).getStatistic();
181  Teuchos::RCP<const StdVector<Real> > xs = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic();
182  stat_bc_->pruneLowerActive(*vs,*gs,*xs,eps);
183  }
184  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
185  Teuchos::RCP<Vector<Real> > vv = Teuchos::dyn_cast<RiskVector<Real> >(v).getVector();
186  Teuchos::RCP<const Vector<Real> > gv = Teuchos::dyn_cast<const RiskVector<Real> >(g).getVector();
187  Teuchos::RCP<const Vector<Real> > xv = Teuchos::dyn_cast<const RiskVector<Real> >(x).getVector();
188  bc_->pruneLowerActive(*vv,*gv,*xv,eps);
189  }
190  }
191 
193  if ( augmented_ && activated_ ) {
194  Teuchos::RCP<StdVector<Real> > us = Teuchos::dyn_cast<RiskVector<Real> >(u).getStatistic();
195  stat_bc_->setVectorToUpperBound(*us);
196  }
197  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
198  Teuchos::RCP<Vector<Real> > uv = Teuchos::dyn_cast<RiskVector<Real> >(u).getVector();
199  bc_->setVectorToUpperBound(*uv);
200  }
201  }
202 
203  const Teuchos::RCP<const Vector<Real> > getLowerVectorRCP(void) const {
204  if (!isLOinitialized_) {
205  const Teuchos::RCP<const Vector<Real> > vlo = bc_->getLowerVectorRCP();
206  lo_ = Teuchos::rcp(new RiskVector<Real>(Teuchos::rcp_const_cast<Vector<Real> >(vlo),
207  lower_, augmented_));
208  isLOinitialized_ = true;
209  }
210  return lo_;
211  }
212 
213  const Teuchos::RCP<const Vector<Real> > getUpperVectorRCP(void) const {
214  if (!isHIinitialized_) {
215  const Teuchos::RCP<const Vector<Real> > vhi = bc_->getUpperVectorRCP();
216  hi_ = Teuchos::rcp(new RiskVector<Real>(Teuchos::rcp_const_cast<Vector<Real> >(vhi),
217  upper_, augmented_));
218  isHIinitialized_ = true;
219  }
220  return hi_;
221  }
222 
224  if ( augmented_ && activated_ ) {
225  Teuchos::RCP<StdVector<Real> > ls = Teuchos::dyn_cast<RiskVector<Real> >(l).getStatistic();
226  stat_bc_->setVectorToLowerBound(*ls);
227  }
228  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
229  Teuchos::RCP<Vector<Real> > lvec = Teuchos::dyn_cast<RiskVector<Real> >(l).getVector();
230  bc_->setVectorToLowerBound(*lvec);
231  }
232  }
233 
234  void pruneActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0 ) {
235  if ( augmented_ && activated_ ) {
236  Teuchos::RCP<StdVector<Real> > vs = Teuchos::dyn_cast<RiskVector<Real> >(v).getStatistic();
237  Teuchos::RCP<const StdVector<Real> > xs = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic();
238  stat_bc_->pruneActive(*vs,*xs,eps);
239  }
240  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
241  Teuchos::RCP<Vector<Real> > vv = Teuchos::dyn_cast<RiskVector<Real> >(v).getVector();
242  Teuchos::RCP<const Vector<Real> > xv = Teuchos::dyn_cast<const RiskVector<Real> >(x).getVector();
243  bc_->pruneActive(*vv,*xv,eps);
244  }
245  }
246 
247  void pruneActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0 ) {
248  if ( augmented_ && activated_ ) {
249  Teuchos::RCP<StdVector<Real> > vs = Teuchos::dyn_cast<RiskVector<Real> >(v).getStatistic();
250  Teuchos::RCP<const StdVector<Real> > gs = Teuchos::dyn_cast<const RiskVector<Real> >(g).getStatistic();
251  Teuchos::RCP<const StdVector<Real> > xs = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic();
252  stat_bc_->pruneActive(*vs,*gs,*xs,eps);
253  }
254  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
255  Teuchos::RCP<Vector<Real> > vv = Teuchos::dyn_cast<RiskVector<Real> >(v).getVector();
256  Teuchos::RCP<const Vector<Real> > gv = Teuchos::dyn_cast<const RiskVector<Real> >(g).getVector();
257  Teuchos::RCP<const Vector<Real> > xv = Teuchos::dyn_cast<const RiskVector<Real> >(x).getVector();
258  bc_->pruneActive(*vv,*gv,*xv,eps);
259  }
260  }
261 
262  bool isFeasible( const Vector<Real> &v ) {
263  bool flagstat = true, flagvec = true;
264  if ( augmented_ && activated_ ) {
265  Teuchos::RCP<const StdVector<Real> > vs = Teuchos::dyn_cast<const RiskVector<Real> >(v).getStatistic();
266  flagstat = stat_bc_->isFeasible(*vs);
267  }
268  if ( bc_ != Teuchos::null && bc_->isActivated() ) {
269  Teuchos::RCP<const Vector<Real> > vv = Teuchos::dyn_cast<const RiskVector<Real> >(v).getVector();
270  flagvec = bc_->isFeasible(*vv);
271  }
272  return (flagstat && flagvec);
273  }
274 
275 }; // class RiskBoundConstraint
276 
277 } // namespace ROL
278 
279 #endif
Contains definitions for std::vector bound constraints.
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 activate(void)
Turn on bounds.
void setVectorToUpperBound(Vector< Real > &u)
Set the input vector to the upper bound.
Teuchos::RCP< RiskVector< Real > > hi_
Contains definitions of custom data types in ROL.
Teuchos::RCP< RiskVector< Real > > lo_
void setVectorToLowerBound(Vector< Real > &l)
Set the input vector to the lower bound.
Teuchos::RCP< StdBoundConstraint< Real > > stat_bc_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update bounds.
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.
const Teuchos::RCP< const Vector< Real > > getLowerVectorRCP(void) const
Return the ref count pointer to the lower bound vector.
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 pruneActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0)
Set variables to zero if they correspond to the -active set.
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.
bool isFeasible(const Vector< Real > &v)
Check if the vector, v, is feasible.
Provides the interface to apply upper and lower bound constraints.
void deactivate(void)
Turn off bounds.
void project(Vector< Real > &x)
Project optimization variables onto the bounds.
const Teuchos::RCP< const Vector< Real > > getUpperVectorRCP(void) const
Return the ref count pointer to the upper bound vector.
Teuchos::RCP< BoundConstraint< Real > > bc_
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 lower -binding set.
RiskBoundConstraint(Teuchos::ParameterList &parlist, const Teuchos::RCP< BoundConstraint< Real > > &bc=Teuchos::null)