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 
47 #include "ROL_BoundConstraint.hpp"
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  bool augmented_;
58  Real lower_;
59  Real upper_;
60 
61 public:
62 
63  RiskBoundConstraint(Teuchos::ParameterList &parlist,
64  const Teuchos::RCP<BoundConstraint<Real> > &bc = Teuchos::null)
65  : BoundConstraint<Real>(), bc_(bc), augmented_(false), lower_(ROL_NINF), upper_(ROL_INF) {
66  std::string type = parlist.sublist("SOL").sublist("Risk Measure").get("Name","CVaR");
67  if ( type == "CVaR" || type == "HMCR" ||
68  type == "Log-Exponential Quadrangle" ||
69  type == "Quantile-Based Quadrangle" ||
70  type == "Truncated Mean Quadrangle" ) {
71  augmented_ = true;
72  }
73  if ( !(bc_->isActivated()) ) {
75  }
76  }
77 
78  RiskBoundConstraint(const Teuchos::RCP<BoundConstraint<Real> > &bc = Teuchos::null,
79  const bool augmented = false,
80  const Real lower = ROL_NINF,
81  const Real upper = ROL_INF)
82  : bc_(bc), augmented_(augmented) {
83  lower_ = std::min(lower,upper);
84  upper_ = std::max(lower,upper);
85  if (!augmented_ && !(bc_->isActivated()) ) {
87  }
88  }
89 
90  RiskBoundConstraint(const std::string name,
91  const Teuchos::RCP<BoundConstraint<Real> > &bc = Teuchos::null)
92  : bc_(bc), augmented_(true), lower_(ROL_NINF), upper_(ROL_INF) {
93  if ( name == "BPOE" ) { lower_ = 0.; }
94  }
95 
96  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
97  if ( bc_ != Teuchos::null ) {
98  Teuchos::RCP<const Vector<Real> > xv
99  = (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
100  bc_->update(*xv,flag,iter);
101  }
102  }
103 
104  void project( Vector<Real> &x ) {
105  if ( augmented_ ) {
106  Real xvar = Teuchos::dyn_cast<RiskVector<Real> >(x).getStatistic();
107  xvar = std::min(upper_,std::max(lower_,xvar));
108  (Teuchos::dyn_cast<RiskVector<Real> >(x)).setStatistic(xvar);
109  }
110  if ( bc_ != Teuchos::null ) {
111  Teuchos::RCP<Vector<Real> > xvec = Teuchos::rcp_const_cast<Vector<Real> >(
112  (Teuchos::dyn_cast<RiskVector<Real> >(x)).getVector());
113  bc_->project(*xvec);
114  (Teuchos::dyn_cast<RiskVector<Real> >(x)).setVector(*xvec);
115  }
116  }
117 
118  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
119  if ( augmented_ ) {
120  Real xvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x)).getStatistic();
121  if ( xvar >= upper_ - eps ) {
122  (Teuchos::dyn_cast<RiskVector<Real> >(v)).setStatistic(0.0);
123  }
124  }
125  if ( bc_ != Teuchos::null ) {
126  Teuchos::RCP<Vector<Real> > vvec = Teuchos::rcp_const_cast<Vector<Real> >(
127  (Teuchos::dyn_cast<RiskVector<Real> >(v)).getVector());
128  Teuchos::RCP<const Vector<Real> > xvec =
129  (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
130  bc_->pruneUpperActive(*vvec,*xvec,eps);
131  }
132  }
133 
134  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
135  if ( augmented_ ) {
136  Real gvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(g)).getStatistic();
137  Real xvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x)).getStatistic();
138  if ( (xvar >= upper_ - eps) && gvar < 0.0 ) {
139  (Teuchos::dyn_cast<RiskVector<Real> >(v)).setStatistic(0.0);
140  }
141  }
142  if ( bc_ != Teuchos::null ) {
143  Teuchos::RCP<Vector<Real> > vvec = Teuchos::rcp_const_cast<Vector<Real> >(
144  (Teuchos::dyn_cast<RiskVector<Real> >(v)).getVector());
145  Teuchos::RCP<const Vector<Real> > gvec =
146  (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(g))).getVector();
147  Teuchos::RCP<const Vector<Real> > xvec =
148  (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
149  bc_->pruneUpperActive(*vvec,*gvec,*xvec,eps);
150  }
151  }
152 
153  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
154  if ( augmented_ ) {
155  Real xvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x)).getStatistic();
156  if ( xvar <= lower_ + eps ) {
157  (Teuchos::dyn_cast<RiskVector<Real> >(v)).setStatistic(0.0);
158  }
159  }
160  if ( bc_ != Teuchos::null ) {
161  Teuchos::RCP<Vector<Real> > vvec = Teuchos::rcp_const_cast<Vector<Real> >(
162  (Teuchos::dyn_cast<RiskVector<Real> >(v)).getVector());
163  Teuchos::RCP<const Vector<Real> > xvec
164  = (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
165  bc_->pruneLowerActive(*vvec,*xvec,eps);
166  }
167  }
168 
169  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
170  if ( augmented_ ) {
171  Real gvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(g)).getStatistic();
172  Real xvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x)).getStatistic();
173  if ( (xvar <= lower_ + eps) && gvar > 0.0 ) {
174  (Teuchos::dyn_cast<RiskVector<Real> >(v)).setStatistic(0.0);
175  }
176  }
177  if ( bc_ != Teuchos::null ) {
178  Teuchos::RCP<Vector<Real> > vvec = Teuchos::rcp_const_cast<Vector<Real> >(
179  (Teuchos::dyn_cast<RiskVector<Real> >(v)).getVector());
180  Teuchos::RCP<const Vector<Real> > gvec
181  = (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(g))).getVector();
182  Teuchos::RCP<const Vector<Real> > xvec
183  = (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
184  bc_->pruneLowerActive(*vvec,*gvec,*xvec,eps);
185  }
186  }
187 
189  if ( augmented_ ) {
190  (Teuchos::dyn_cast<RiskVector<Real> >(u)).setStatistic(upper_);
191  }
192  if ( bc_ != Teuchos::null ) {
193  Teuchos::RCP<Vector<Real> > uvec = Teuchos::rcp_const_cast<Vector<Real> >(
194  (Teuchos::dyn_cast<RiskVector<Real> >(u)).getVector());
195  bc_->setVectorToUpperBound(*uvec);
196  }
197  }
198 
200  if ( augmented_ ) {
201  (Teuchos::dyn_cast<RiskVector<Real> >(l)).setStatistic(lower_);
202  }
203  if ( bc_ != Teuchos::null ) {
204  Teuchos::RCP<Vector<Real> > lvec = Teuchos::rcp_const_cast<Vector<Real> >(
205  (Teuchos::dyn_cast<RiskVector<Real> >(l)).getVector());
206  bc_->setVectorToLowerBound(*lvec);
207  }
208  }
209 
210  void pruneActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
211  if ( augmented_ ) {
212  Real xvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x)).getStatistic();
213  if ( (xvar <= lower_ + eps) || (xvar >= upper_ - eps) ) {
214  (Teuchos::dyn_cast<RiskVector<Real> >(v)).setStatistic(0.0);
215  }
216  }
217  if ( bc_ != Teuchos::null ) {
218  Teuchos::RCP<Vector<Real> > vvec = Teuchos::rcp_const_cast<Vector<Real> >(
219  (Teuchos::dyn_cast<RiskVector<Real> >(v)).getVector());
220  Teuchos::RCP<const Vector<Real> > xvec =
221  (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
222  bc_->pruneActive(*vvec,*xvec,eps);
223  }
224  }
225 
226  void pruneActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
227  if (augmented_) {
228  Real gvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(g)).getStatistic();
229  Real xvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x)).getStatistic();
230  if ( ((xvar <= lower_ + eps) && gvar > 0.0) ||
231  ((xvar >= upper_ - eps) && gvar < 0.0) ) {
232  (Teuchos::dyn_cast<RiskVector<Real> >(v)).setStatistic(0.0);
233  }
234  }
235  if ( bc_ != Teuchos::null ) {
236  Teuchos::RCP<Vector<Real> > vvec = Teuchos::rcp_const_cast<Vector<Real> >(
237  (Teuchos::dyn_cast<RiskVector<Real> >(v)).getVector());
238  Teuchos::RCP<const Vector<Real> > gvec =
239  (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(g))).getVector();
240  Teuchos::RCP<const Vector<Real> > xvec =
241  (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
242  bc_->pruneActive(*vvec,*gvec,*xvec,eps);
243  }
244  }
245 
246  bool isFeasible( const Vector<Real> &v ) {
247  bool flagstat = true, flagvec = true;
248  if ( augmented_ ) {
249  Real vvar = Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(v)).getStatistic();
250  flagstat = ((vvar >= lower_ && vvar <= upper_) ? true : false);
251  }
252  if ( bc_ != Teuchos::null ) {
253  Teuchos::RCP<const Vector<Real> > vvec
254  = (Teuchos::dyn_cast<RiskVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
255  if ( bc_->isActivated() ) {
256  flagvec = bc_->isFeasible(*vvec);
257  }
258  }
259  return (flagstat && flagvec);
260  }
261 
262 }; // class RiskBoundConstraint
263 
264 } // namespace ROL
265 
266 #endif
RiskBoundConstraint(const Teuchos::RCP< BoundConstraint< Real > > &bc=Teuchos::null, const bool augmented=false, const Real lower=ROL_NINF, const Real upper=ROL_INF)
void setVectorToUpperBound(Vector< Real > &u)
Set the input vector to the upper bound.
Contains definitions of custom data types in ROL.
void setVectorToLowerBound(Vector< Real > &l)
Set the input vector to the lower bound.
const Real getStatistic(const int i=0) const
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 > &x, Real eps=0.0)
Set variables to zero if they correspond to the -active set.
void pruneActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the -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.
static const double ROL_INF
Definition: ROL_Types.hpp:128
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the lower -binding set.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the lower -active set.
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the upper -binding set.
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the upper -active set.
RiskBoundConstraint(const std::string name, const Teuchos::RCP< BoundConstraint< Real > > &bc=Teuchos::null)
static const double ROL_NINF
Definition: ROL_Types.hpp:129
void deactivate(void)
Turn off bounds.
void project(Vector< Real > &x)
Project optimization variables onto the bounds.
Teuchos::RCP< BoundConstraint< Real > > bc_
RiskBoundConstraint(Teuchos::ParameterList &parlist, const Teuchos::RCP< BoundConstraint< Real > > &bc=Teuchos::null)