ROL
ROL_RiskVector.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_RISKVECTOR_HPP
45 #define ROL_RISKVECTOR_HPP
46 
47 #include "ROL_Vector.hpp"
48 #include "Teuchos_ParameterList.hpp"
49 #include "Teuchos_Array.hpp"
50 
51 namespace ROL {
52 
53 template<class Real>
54 class RiskVector : public Vector<Real> {
55  typedef typename std::vector<Real>::size_type uint;
56 
57 private:
58  std::vector<Real> stat_;
59  Teuchos::RCP<Vector<Real> > vec_;
60  bool augmented_;
61  uint nStat_;
62 
63  mutable Teuchos::RCP<Vector<Real> > dual_vec1_;
64  mutable Teuchos::RCP<RiskVector<Real> > dual_vec_;
65 
66 public:
67  RiskVector( Teuchos::ParameterList &parlist,
68  const Teuchos::RCP<Vector<Real> > &vec,
69  const Real stat = 1. )
70  : vec_(vec), augmented_(false), nStat_(0) {
71  stat_.clear();
72  dual_vec1_ = vec->dual().clone();
73  std::string type = parlist.sublist("SOL").sublist("Risk Measure").get("Name","CVaR");
74  if ( type == "CVaR" ||
75  type == "HMCR" ||
76  type == "KL Divergence" ||
77  type == "Moreau-Yosida CVaR" ||
78  type == "Log-Exponential Quadrangle" ||
79  type == "Log-Quantile Quadrangle" ||
80  type == "Quantile-Based Quadrangle" ||
81  type == "Truncated Mean Quadrangle" ) {
82  augmented_ = true;
83  nStat_ = 1;
84  stat_.resize(nStat_,stat);
85  }
86  else if ( type == "Mixed-Quantile Quadrangle" ) {
87  Teuchos::ParameterList &list
88  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mixed-Quantile Quadrangle");
89  Teuchos::Array<Real> prob
90  = Teuchos::getArrayFromStringParameter<Real>(list,"Probability Array");
91  augmented_ = true;
92  nStat_ = prob.size();
93  stat_.resize(nStat_,stat);
94  }
95  else if ( type == "Quantile-Radius Quadrangle" ) {
96  augmented_ = true;
97  nStat_ = 2;
98  stat_.resize(nStat_,stat);
99  }
100  }
101 
102  RiskVector( const Teuchos::RCP<Vector<Real> > &vec,
103  const bool augmented = false )
104  : vec_(vec), augmented_(augmented), nStat_((augmented ? 1 : 0)) {
105  stat_.clear();
106  dual_vec1_ = vec->dual().clone();
107  if (augmented) {
108  stat_.resize(nStat_,0.);
109  }
110  }
111 
112  RiskVector( const Teuchos::RCP<Vector<Real> > &vec,
113  const std::vector<Real> &stat,
114  const bool augmented = true )
115  : stat_(stat), vec_(vec), augmented_(augmented), nStat_(stat.size()) {
116  dual_vec1_ = vec->dual().clone();
117  }
118 
119  void plus( const Vector<Real> &x ) {
120  const RiskVector<Real> &xs = Teuchos::dyn_cast<const RiskVector<Real> >(
121  Teuchos::dyn_cast<const Vector<Real> >(x));
122  if (augmented_) {
123  for ( uint i = 0; i < nStat_; i++ ) {
124  stat_[i] += xs.getStatistic(i);
125  }
126  }
127  vec_->plus(*(xs.getVector()));
128  }
129 
130  void scale( const Real alpha ) {
131  if (augmented_) {
132  for ( uint i = 0; i < nStat_; i++ ) {
133  stat_[i] *= alpha;
134  }
135  }
136  vec_->scale(alpha);
137  }
138 
139  void axpy( const Real alpha, const Vector<Real> &x ) {
140  const RiskVector<Real> &xs = Teuchos::dyn_cast<const RiskVector<Real> >(
141  Teuchos::dyn_cast<const Vector<Real> >(x));
142  if (augmented_) {
143  for ( uint i = 0; i < nStat_; i++ ) {
144  stat_[i] += alpha*xs.getStatistic(i);
145  }
146  }
147  vec_->axpy(alpha,*(xs.getVector()));
148  }
149 
150  Real dot( const Vector<Real> &x ) const {
151  const RiskVector<Real> &xs = Teuchos::dyn_cast<const RiskVector<Real> >(
152  Teuchos::dyn_cast<const Vector<Real> >(x));
153  Real xprod = vec_->dot(*(xs.getVector()));
154  if (augmented_) {
155  for ( uint i = 0; i < nStat_; i++ ) {
156  xprod += stat_[i]*xs.getStatistic(i);
157  }
158  }
159  return xprod;
160  }
161 
162  Real norm() const {
163  return sqrt( this->dot(*this) );
164  }
165 
166  const Real getStatistic(const int i = 0) const {
167  TEUCHOS_TEST_FOR_EXCEPTION((i < 0 || i > (int)nStat_-1),std::invalid_argument,
168  ">>> ERROR (ROL::RiskVector): index out-of-bounds in getStatistic!");
169  return stat_[i];
170  }
171 
172  Teuchos::RCP<const Vector<Real> > getVector() const {
173  return vec_;
174  }
175 
176  Teuchos::RCP<Vector<Real> > clone() const {
177  std::vector<Real> stat(nStat_,0.);
178  Teuchos::RCP<Vector<Real> > vec = Teuchos::rcp_dynamic_cast<Vector<Real> >(
179  Teuchos::rcp_const_cast<Vector<Real> >(vec_->clone()));
180  return Teuchos::rcp( new RiskVector( vec, stat, augmented_ ) );
181  }
182 
183  const Vector<Real> &dual(void) const {
184  dual_vec1_->set(vec_->dual());
185  dual_vec_ = Teuchos::rcp(new RiskVector<Real>(dual_vec1_,stat_,augmented_));
186  return *dual_vec_;
187  }
188 
189  void setStatistic(const Real stat) {
190  stat_.assign(nStat_,stat);
191  }
192 
193  void setStatistic(const std::vector<Real> &stat) {
194  stat_.assign(stat.begin(),stat.end());
195  }
196 
197  void setVector(const Vector<Real>& vec) {
198  vec_->set(vec);
199  }
200 };
201 
202 }
203 
204 #endif
void scale(const Real alpha)
Compute where .
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
const Real getStatistic(const int i=0) const
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void setStatistic(const std::vector< Real > &stat)
void setVector(const Vector< Real > &vec)
void plus(const Vector< Real > &x)
Compute , where .
Teuchos::RCP< const Vector< Real > > getVector() const
Teuchos::RCP< RiskVector< Real > > dual_vec_
RiskVector(const Teuchos::RCP< Vector< Real > > &vec, const std::vector< Real > &stat, const bool augmented=true)
void setStatistic(const Real stat)
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
std::vector< Real >::size_type uint
Teuchos::RCP< Vector< Real > > dual_vec1_
RiskVector(Teuchos::ParameterList &parlist, const Teuchos::RCP< Vector< Real > > &vec, const Real stat=1.)
std::vector< Real > stat_
Real dot(const Vector< Real > &x) const
Compute where .
Teuchos::RCP< Vector< Real > > vec_
RiskVector(const Teuchos::RCP< Vector< Real > > &vec, const bool augmented=false)
Real norm() const
Returns where .