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_StdVector.hpp"
48 #include "ROL_RiskMeasureInfo.hpp"
49 #include "Teuchos_ParameterList.hpp"
50 
51 namespace ROL {
52 
53 template<class Real>
54 class RiskVector : public Vector<Real> {
55 private:
56  Teuchos::RCP<std::vector<Real> > stat_;
57  Teuchos::RCP<StdVector<Real> > stat_vec_;
58  Teuchos::RCP<Vector<Real> > vec_;
59 
60  bool augmented_;
61  int nStat_;
62 
63  mutable bool isDualInitialized_;
64  mutable Teuchos::RCP<Vector<Real> > dual_vec1_;
65  mutable Teuchos::RCP<RiskVector<Real> > dual_vec_;
66 
67 public:
68  RiskVector( Teuchos::ParameterList &parlist,
69  const Teuchos::RCP<Vector<Real> > &vec,
70  const Real stat = 1 )
71  : stat_(Teuchos::null), stat_vec_(Teuchos::null), vec_(vec),
72  augmented_(false), nStat_(0), isDualInitialized_(false) {
73  // Get risk measure information
74  std::string name;
75  std::vector<Real> lower, upper;
76  bool activated(false);
77  int nStat(0);
78  RiskMeasureInfo<Real>(parlist,name,nStat,lower,upper,activated);
79  augmented_ = (nStat > 0) ? true : false;
80  nStat_ = nStat;
81  // Initialize statistic vector
82  if (augmented_) {
83  stat_ = Teuchos::rcp(new std::vector<Real>(nStat_,stat));
84  stat_vec_ = Teuchos::rcp(new StdVector<Real>(stat_));
85  }
86  }
87 
88  RiskVector( const Teuchos::RCP<Vector<Real> > &vec,
89  const bool augmented = false )
90  : stat_(Teuchos::null), stat_vec_(Teuchos::null), vec_(vec),
91  augmented_(augmented), nStat_((augmented ? 1 : 0)), isDualInitialized_(false) {
92  if (augmented) {
93  stat_ = Teuchos::rcp(new std::vector<Real>(nStat_,0));
94  stat_vec_ = Teuchos::rcp(new StdVector<Real>(stat_));
95  }
96  }
97 
98  RiskVector( const Teuchos::RCP<Vector<Real> > &vec,
99  const std::vector<Real> &stat,
100  const bool augmented = true )
101  : stat_(Teuchos::null), stat_vec_(Teuchos::null), vec_(vec),
102  augmented_(augmented), nStat_(stat.size()), isDualInitialized_(false) {
103  if (augmented) {
104  stat_ = Teuchos::rcp(new std::vector<Real>(stat));
105  stat_vec_ = Teuchos::rcp(new StdVector<Real>(stat_));
106  }
107  }
108 
109  void set( const Vector<Real> &x ) {
110  const RiskVector<Real> &xs = Teuchos::dyn_cast<const RiskVector<Real> >(x);
111  vec_->set(*(xs.getVector()));
112  if (augmented_) {
113  stat_vec_->set(*(xs.getStatistic()));
114  }
115  }
116 
117  void plus( const Vector<Real> &x ) {
118  const RiskVector<Real> &xs = Teuchos::dyn_cast<const RiskVector<Real> >(x);
119  vec_->plus(*(xs.getVector()));
120  if (augmented_) {
121  stat_vec_->plus(*(xs.getStatistic()));
122  }
123  }
124 
125  void scale( const Real alpha ) {
126  vec_->scale(alpha);
127  if (augmented_) {
128  stat_vec_->scale(alpha);
129  }
130  }
131 
132  void axpy( const Real alpha, const Vector<Real> &x ) {
133  const RiskVector<Real> &xs = Teuchos::dyn_cast<const RiskVector<Real> >(x);
134  vec_->axpy(alpha,*(xs.getVector()));
135  if (augmented_) {
136  stat_vec_->axpy(alpha,*(xs.getStatistic()));
137  }
138  }
139 
140  Real dot( const Vector<Real> &x ) const {
141  const RiskVector<Real> &xs = Teuchos::dyn_cast<const RiskVector<Real> >(x);
142  Real val = vec_->dot(*(xs.getVector()));
143  if (augmented_) {
144  val += stat_vec_->dot(*(xs.getStatistic()));
145  }
146  return val;
147  }
148 
149  Real norm(void) const {
150  return sqrt( dot(*this) );
151  }
152 
153  Teuchos::RCP<Vector<Real> > clone(void) const {
154  std::vector<Real> stat(nStat_,static_cast<Real>(0));
155  return Teuchos::rcp(new RiskVector(vec_->clone(),stat,augmented_));
156  }
157 
158  const Vector<Real> &dual(void) const {
159  // Initialize dual vectors if not already initialized
160  if ( !isDualInitialized_ ) {
161  dual_vec1_ = vec_->dual().clone();
162  std::vector<Real> stat(nStat_,static_cast<Real>(0));
163  dual_vec_ = Teuchos::rcp(new RiskVector<Real>(dual_vec1_,stat,augmented_));
164  isDualInitialized_ = true;
165  }
166  // Set vector component
167  dual_vec1_->set(vec_->dual());
168  // Set statistic component
169  if ( augmented_ ) {
170  Teuchos::rcp_dynamic_cast<RiskVector<Real> >(dual_vec_)->setStatistic(*stat_);
171  }
172  // Return dual vector
173  return *dual_vec_;
174  }
175 
176  Teuchos::RCP<Vector<Real> > basis( const int i ) const {
177  Teuchos::RCP<Vector<Real> > e1;
178  std::vector<Real> e2(nStat_,static_cast<Real>(0));
179  int n1 = vec_->dimension(), n2 = stat_vec_->dimension();
180  if ( i < n1 ) {
181  e1 = vec_->basis(i);
182  }
183  else if (i >= n1 && i < n1+n2) {
184  e1 = vec_->clone(); e1->zero();
185  e2[i-n1] = static_cast<Real>(1);
186  }
187  else {
188  TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
189  ">>> ERROR (ROL::RiskVector::Basis): index is out of bounds.");
190  }
191  return Teuchos::rcp(new RiskVector<Real>(e1,e2,augmented_));
192  }
193 
194  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
195  vec_->applyUnary(f);
196  if (augmented_) {
197  stat_vec_->applyUnary(f);
198  }
199  }
200 
201  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
202  const RiskVector<Real> &xs = Teuchos::dyn_cast<const RiskVector<Real> >(x);
203  vec_->applyBinary(f,*xs.getVector());
204  if (augmented_) {
205  stat_vec_->applyBinary(f,*xs.getStatistic());
206  }
207  }
208 
209  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
210  Real result = r.initialValue();
211  r.reduce(vec_->reduce(r),result);
212  if (augmented_) {
213  r.reduce(stat_vec_->reduce(r),result);
214  }
215  return result;
216  }
217 
218  int dimension(void) const {
219  int dim = vec_->dimension();
220  if (augmented_) {
221  dim += stat_vec_->dimension();
222  }
223  return dim;
224  }
225 
226  /***************************************************************************/
227  /************ ROL VECTOR ACCESSOR FUNCTIONS ********************************/
228  /***************************************************************************/
229  Teuchos::RCP<const StdVector<Real> > getStatistic(void) const {
230  return stat_vec_;
231  }
232 
233  Teuchos::RCP<StdVector<Real> > getStatistic(void) {
234  return stat_vec_;
235  }
236 
237  Teuchos::RCP<const Vector<Real> > getVector(void) const {
238  return vec_;
239  }
240 
241  Teuchos::RCP<Vector<Real> > getVector(void) {
242  return vec_;
243  }
244 
245  /***************************************************************************/
246  /************ COMPONENT ACCESSOR FUNCTIONS *********************************/
247  /***************************************************************************/
248  const Real getStatistic(const int i) const {
249  TEUCHOS_TEST_FOR_EXCEPTION((i < 0 || i > (int)nStat_-1),std::invalid_argument,
250  ">>> ERROR (ROL::RiskVector::getStatistic): index out-of-bounds.");
251  TEUCHOS_TEST_FOR_EXCEPTION(!augmented_,std::invalid_argument,
252  ">>> ERROR (ROL::RiskVector::getStatistic): vector is not augmented.");
253  return (*stat_)[i];
254  }
255 
256  void getStatistic(std::vector<Real> &stat) const {
257  stat.clear();
258  if ( augmented_ ) {
259  stat.assign(stat_->begin(),stat_->end());
260  }
261  }
262 
263  void setStatistic(const Real stat) {
264  if ( augmented_ ) {
265  stat_->assign(nStat_,stat);
266  }
267  }
268 
269  void setStatistic(const std::vector<Real> &stat) {
270  if ( augmented_ ) {
271  stat_->assign(stat.begin(),stat.end());
272  }
273  }
274 
275  void setVector(const Vector<Real>& vec) {
276  vec_->set(vec);
277  }
278 };
279 
280 }
281 
282 #endif
Real norm(void) const
Returns where .
void scale(const Real alpha)
Compute where .
int dimension(void) const
Return dimension of the vector space.
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)
const Real getStatistic(const int i) const
Teuchos::RCP< const Vector< Real > > getVector(void) const
Teuchos::RCP< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
void setVector(const Vector< Real > &vec)
void plus(const Vector< Real > &x)
Compute , where .
Teuchos::RCP< Vector< Real > > getVector(void)
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)
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
Real dot(const Vector< Real > &x) const
Compute where .
Real reduce(const Elementwise::ReductionOp< Real > &r) const
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
RiskVector(Teuchos::ParameterList &parlist, const Teuchos::RCP< Vector< Real > > &vec, const Real stat=1)
Teuchos::RCP< Vector< Real > > dual_vec1_
Teuchos::RCP< std::vector< Real > > stat_
Teuchos::RCP< StdVector< Real > > stat_vec_
Teuchos::RCP< const StdVector< Real > > getStatistic(void) const
Teuchos::RCP< Vector< Real > > vec_
void getStatistic(std::vector< Real > &stat) const
RiskVector(const Teuchos::RCP< Vector< Real > > &vec, const bool augmented=false)
Teuchos::RCP< Vector< Real > > basis(const int i) const
Return i-th basis vector.
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Teuchos::RCP< StdVector< Real > > getStatistic(void)