ROL
ROL_RiskVector.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_RISKVECTOR_HPP
11 #define ROL_RISKVECTOR_HPP
12 
13 #include "ROL_StdVector.hpp"
15 #include "ROL_ParameterList.hpp"
16 
17 namespace ROL {
18 
19 template<class Real>
20 class RiskVector : public Vector<Real> {
21 private:
22  Ptr<std::vector<Real> > statObj_;
23  Ptr<StdVector<Real> > statObj_vec_;
25  int nStatObj_;
26 
27  std::vector<Ptr<std::vector<Real> > > statCon_;
28  std::vector<Ptr<StdVector<Real> > > statCon_vec_;
30  std::vector<int> nStatCon_;
31 
32  Ptr<Vector<Real> > vec_;
33 
34  mutable bool isDualInitialized_;
35  mutable Ptr<std::vector<Real> > dualObj_;
36  mutable std::vector<Ptr<std::vector<Real> > > dualCon_;
37  mutable Ptr<Vector<Real> > dual_vec1_;
38  mutable Ptr<RiskVector<Real> > dual_vec_;
39 
40  void initializeObj(Ptr<ParameterList> &parlist,
41  const Real stat = 1) {
42  // Get risk measure information
43  if (parlist != nullPtr) {
44  std::string name;
45  std::vector<Real> lower, upper;
46  bool activated(false);
47  RandVarFunctionalInfo<Real>(*parlist,name,nStatObj_,lower,upper,activated);
48  augmentedObj_ = (nStatObj_ > 0) ? true : false;
49  // Initialize statistic vector
50  if (augmentedObj_) {
51  statObj_ = makePtr<std::vector<Real>>(nStatObj_,stat);
52  statObj_vec_ = makePtr<StdVector<Real>>(statObj_);
53  }
54  }
55  else {
56  augmentedObj_ = false;
57  nStatObj_ = 0;
58  }
59  }
60 
61  void initializeCon(std::vector<Ptr<ParameterList> > &parlist,
62  const Real stat = 1) {
63  int size = parlist.size();
64  statCon_.resize(size); statCon_vec_.resize(size); nStatCon_.resize(size);
65  for (int i = 0; i < size; ++i) {
66  if (parlist[i] != nullPtr) {
67  // Get risk measure information
68  std::string name;
69  std::vector<Real> lower, upper;
70  bool activated(false);
71  RandVarFunctionalInfo<Real>(*parlist[i],name,nStatCon_[i],lower,upper,activated);
72  augmentedCon_ = (nStatCon_[i] > 0) ? true : augmentedCon_;
73  // Initialize statistic vector
74  if (nStatCon_[i] > 0) {
75  statCon_[i] = makePtr<std::vector<Real>>(nStatCon_[i],stat);
76  statCon_vec_[i] = makePtr<StdVector<Real>>(statCon_[i]);
77  }
78  else {
79  statCon_[i] = nullPtr;
80  statCon_vec_[i] = nullPtr;
81  }
82  }
83  else {
84  statCon_[i] = nullPtr;
85  statCon_vec_[i] = nullPtr;
86  }
87  }
88  }
89 
90 public:
91 
92  // Objective risk only
93  RiskVector( Ptr<ParameterList> &parlist,
94  const Ptr<Vector<Real> > &vec,
95  const Real stat = 0 )
96  : statObj_(nullPtr), statObj_vec_(nullPtr),
97  augmentedObj_(false), nStatObj_(0),
98  augmentedCon_(false),
99  vec_(vec), isDualInitialized_(false) {
100  initializeObj(parlist,stat);
101  }
102 
103  // Inequality constraint risk only
104  RiskVector( std::vector<Ptr<ParameterList> > &parlist,
105  const Ptr<Vector<Real> > &vec,
106  const Real stat = 0 )
107  : statObj_(nullPtr), statObj_vec_(nullPtr),
108  augmentedObj_(false), nStatObj_(0),
109  augmentedCon_(false),
110  vec_(vec), isDualInitialized_(false) {
111  initializeCon(parlist,stat);
112  }
113 
114  // Objective and inequality constraint risk
115  RiskVector( Ptr<ParameterList> & parlistObj,
116  std::vector<Ptr<ParameterList> > &parlistCon,
117  const Ptr<Vector<Real> > &vec,
118  const Real stat = 0 )
119  : statObj_(nullPtr), statObj_vec_(nullPtr),
120  augmentedObj_(false), nStatObj_(0),
121  augmentedCon_(false),
122  vec_(vec), isDualInitialized_(false) {
123  initializeObj(parlistObj,stat);
124  initializeCon(parlistCon,stat);
125  }
126 
127  // Build from components
128  RiskVector( const Ptr<Vector<Real> > &vec,
129  const Ptr<std::vector<Real> > &statObj,
130  const std::vector<Ptr<std::vector<Real> > > &statCon )
131  : statObj_(nullPtr), statObj_vec_(nullPtr),
132  augmentedObj_(false), nStatObj_(0), augmentedCon_(false),
133  vec_(vec), isDualInitialized_(false) {
134  if (statObj != nullPtr) {
135  statObj_ = statObj;
136  statObj_vec_ = makePtr<StdVector<Real>>(statObj_);
137  augmentedObj_ = true;
138  nStatObj_ = statObj->size();
139  }
140  int size = statCon.size();
141  statCon_.clear(); statCon_vec_.clear(); nStatCon_.clear();
142  statCon_.resize(size,nullPtr);
143  statCon_vec_.resize(size,nullPtr);
144  nStatCon_.resize(size,0);
145  for (int i = 0; i < size; ++i) {
146  if (statCon[i] != nullPtr) {
147  statCon_[i] = statCon[i];
148  statCon_vec_[i] = makePtr<StdVector<Real>>(statCon_[i]);
149  augmentedCon_ = true;
150  nStatCon_[i] = statCon[i]->size();
151  }
152  }
153  }
154 
155  // Build from components -- Objective only...no statistic
156  RiskVector( const Ptr<Vector<Real> > &vec )
157  : statObj_(nullPtr), statObj_vec_(nullPtr),
158  augmentedObj_(false), nStatObj_(0), augmentedCon_(false),
159  vec_(vec), isDualInitialized_(false) {}
160 
161  void set( const Vector<Real> &x ) {
162  const RiskVector<Real> &xs = dynamic_cast<const RiskVector<Real>&>(x);
163  vec_->set(*(xs.getVector()));
164  if (augmentedObj_ && statObj_vec_ != nullPtr) {
165  statObj_vec_->set(*(xs.getStatisticVector(0)));
166  }
167  if (augmentedCon_) {
168  int size = statCon_vec_.size();
169  for (int i = 0; i < size; ++i) {
170  if (statCon_vec_[i] != nullPtr) {
171  statCon_vec_[i]->set(*(xs.getStatisticVector(1,i)));
172  }
173  }
174  }
175  }
176 
177  void plus( const Vector<Real> &x ) {
178  const RiskVector<Real> &xs = dynamic_cast<const RiskVector<Real>&>(x);
179  vec_->plus(*(xs.getVector()));
180  if (augmentedObj_ && statObj_vec_ != nullPtr) {
181  statObj_vec_->plus(*(xs.getStatisticVector(0)));
182  }
183  if (augmentedCon_) {
184  int size = statCon_vec_.size();
185  for (int i = 0; i < size; ++i) {
186  if (statCon_vec_[i] != nullPtr) {
187  statCon_vec_[i]->plus(*(xs.getStatisticVector(1,i)));
188  }
189  }
190  }
191  }
192 
193  void scale( const Real alpha ) {
194  vec_->scale(alpha);
195  if (augmentedObj_ && statObj_vec_ != nullPtr) {
196  statObj_vec_->scale(alpha);
197  }
198  if (augmentedCon_) {
199  int size = statCon_vec_.size();
200  for (int i = 0; i < size; ++i) {
201  if (statCon_vec_[i] != nullPtr) {
202  statCon_vec_[i]->scale(alpha);
203  }
204  }
205  }
206  }
207 
208  void axpy( const Real alpha, const Vector<Real> &x ) {
209  const RiskVector<Real> &xs = dynamic_cast<const RiskVector<Real>&>(x);
210  vec_->axpy(alpha,*(xs.getVector()));
211  if (augmentedObj_ && statObj_vec_ != nullPtr) {
212  statObj_vec_->axpy(alpha,*(xs.getStatisticVector(0)));
213  }
214  if (augmentedCon_) {
215  int size = statCon_vec_.size();
216  for (int i = 0; i < size; ++i) {
217  if (statCon_vec_[i] != nullPtr) {
218  statCon_vec_[i]->axpy(alpha,*(xs.getStatisticVector(1,i)));
219  }
220  }
221  }
222  }
223 
224  Real dot( const Vector<Real> &x ) const {
225  const RiskVector<Real> &xs = dynamic_cast<const RiskVector<Real>&>(x);
226  Real val = vec_->dot(*(xs.getVector()));
227  if (augmentedObj_ && statObj_vec_ != nullPtr) {
228  val += statObj_vec_->dot(*(xs.getStatisticVector(0)));
229  }
230  if (augmentedCon_) {
231  int size = statCon_vec_.size();
232  for (int i = 0; i < size; ++i) {
233  if (statCon_vec_[i] != nullPtr) {
234  val += statCon_vec_[i]->dot(*(xs.getStatisticVector(1,i)));
235  }
236  }
237  }
238  return val;
239  }
240 
241  Real norm(void) const {
242  return sqrt( dot(*this) );
243  }
244 
245  Ptr<Vector<Real> > clone(void) const {
246  Ptr<std::vector<Real> > e2 = nullPtr;
247  if (augmentedObj_ && statObj_vec_ != nullPtr) {
248  e2 = makePtr<std::vector<Real>>(nStatObj_,static_cast<Real>(0));
249  }
250  int size = statCon_vec_.size();
251  std::vector<Ptr<std::vector<Real> > > e3(size, nullPtr);
252  for (int j = 0; j < size; ++j) {
253  if (statCon_vec_[j] != nullPtr) {
254  e3[j] = makePtr<std::vector<Real>>(nStatCon_[j],static_cast<Real>(0));
255  }
256  }
257  return makePtr<RiskVector>(vec_->clone(),e2,e3);
258  }
259 
260  const Vector<Real> &dual(void) const {
261  // Initialize dual vectors if not already initialized
262  if ( !isDualInitialized_ ) {
263  dual_vec1_ = vec_->dual().clone();
264  dualObj_ = nullPtr;
265  if (statObj_ != nullPtr) {
266  dualObj_ = makePtr<std::vector<Real>>(statObj_->size());
267  }
268  int size = statCon_.size();
269  dualCon_.clear(); dualCon_.resize(size,nullPtr);
270  for (int i = 0; i < size; ++i) {
271  if (statCon_[i] != nullPtr) {
272  dualCon_[i] = makePtr<std::vector<Real>>(statCon_[i]->size());
273  }
274  }
275  dual_vec_ = makePtr<RiskVector<Real>>(dual_vec1_,dualObj_,dualCon_);
276  isDualInitialized_ = true;
277  }
278  // Set vector component
279  dual_vec1_->set(vec_->dual());
280  // Set statistic component
281  if ( augmentedObj_ && statObj_vec_ != nullPtr ) {
282  dynamicPtrCast<RiskVector<Real> >(dual_vec_)->setStatistic(*statObj_,0);
283  }
284  if ( augmentedCon_ ) {
285  int size = statCon_.size();
286  for (int i = 0; i < size; ++i) {
287  if (statCon_[i] != nullPtr) {
288  dynamicPtrCast<RiskVector<Real> >(dual_vec_)->setStatistic(*statCon_[i],1,i);
289  }
290  }
291  }
292  // Return dual vector
293  return *dual_vec_;
294  }
295 
296  Ptr<Vector<Real> > basis( const int i ) const {
297  Ptr<Vector<Real> > e1;
298  Ptr<std::vector<Real> > e2 = nullPtr;
299  if (augmentedObj_ && statObj_vec_ != nullPtr) {
300  e2 = makePtr<std::vector<Real>>(nStatObj_,static_cast<Real>(0));
301  }
302  int size = statCon_vec_.size();
303  std::vector<Ptr<std::vector<Real> > > e3(size);
304  for (int j = 0; j < size; ++j) {
305  if (statCon_vec_[j] != nullPtr) {
306  e3[j] = makePtr<std::vector<Real>>(nStatCon_[j],static_cast<Real>(0));
307  }
308  }
309  int n1 = vec_->dimension(), n2 = 0;
310  if (statObj_vec_ != nullPtr) {
311  n2 = statObj_vec_->dimension();
312  }
313  if ( i < n1 ) {
314  e1 = vec_->basis(i);
315  }
316  else if (i >= n1 && i < n1+n2) {
317  e1 = vec_->clone(); e1->zero();
318  (*e2)[i-n1] = static_cast<Real>(1);
319  }
320  else if (i >= n1+n2) {
321  e1 = vec_->clone(); e1->zero();
322  int sum = n1+n2, sum0 = sum;
323  for (int j = 0; j < size; ++j) {
324  if (statCon_vec_[j] != nullPtr) {
325  sum += nStatCon_[j];
326  if (i < sum) {
327  (*e3[j])[i-sum0] = static_cast<Real>(1);
328  break;
329  }
330  sum0 = sum;
331  }
332  }
333  if (i >= sum) {
334  throw Exception::NotImplemented(">>> ROL::RiskVector::Basis: index out of bounds!");
335  }
336  }
337  return makePtr<RiskVector<Real>>(e1,e2,e3);
338  }
339 
340  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
341  vec_->applyUnary(f);
342  if (augmentedObj_ && statObj_vec_ != nullPtr) {
343  statObj_vec_->applyUnary(f);
344  }
345  if (augmentedCon_) {
346  int size = statCon_vec_.size();
347  for (int i = 0; i < size; ++i) {
348  if (statCon_vec_[i] != nullPtr) {
349  statCon_vec_[i]->applyUnary(f);
350  }
351  }
352  }
353  }
354 
355  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
356  const RiskVector<Real> &xs = dynamic_cast<const RiskVector<Real>&>(x);
357  vec_->applyBinary(f,*xs.getVector());
358  if (augmentedObj_ && statObj_vec_ != nullPtr) {
359  statObj_vec_->applyBinary(f,*xs.getStatisticVector(0));
360  }
361  if (augmentedCon_) {
362  int size = statCon_vec_.size();
363  for (int i = 0; i < size; ++i) {
364  if (statCon_vec_[i] != nullPtr) {
365  statCon_vec_[i]->applyBinary(f,*xs.getStatisticVector(1,i));
366  }
367  }
368  }
369  }
370 
371  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
372  Real result = r.initialValue();
373  r.reduce(vec_->reduce(r),result);
374  if (augmentedObj_ && statObj_vec_ != nullPtr) {
375  r.reduce(statObj_vec_->reduce(r),result);
376  }
377  if (augmentedCon_) {
378  int size = statCon_vec_.size();
379  for (int i = 0; i < size; ++i) {
380  if (statCon_vec_[i] != nullPtr) {
381  r.reduce(statCon_vec_[i]->reduce(r),result);
382  }
383  }
384  }
385  return result;
386  }
387 
388  void setScalar( const Real C ) {
389  vec_->setScalar(C);
390  if (augmentedObj_ && statObj_vec_ != nullPtr) {
391  statObj_vec_->setScalar(C);
392  }
393  if (augmentedCon_) {
394  int size = statCon_vec_.size();
395  for (int i = 0; i < size; ++i) {
396  if (statCon_vec_[i] != nullPtr) {
397  statCon_vec_[i]->setScalar(C);
398  }
399  }
400  }
401  }
402 
403  void randomize( const Real l=0.0, const Real u=1.0 ) {
404  vec_->randomize(l,u);
405  if (augmentedObj_ && statObj_vec_ != nullPtr) {
406  statObj_vec_->randomize(l,u);
407  }
408  if (augmentedCon_) {
409  int size = statCon_vec_.size();
410  for (int i = 0; i < size; ++i) {
411  if (statCon_vec_[i] != nullPtr) {
412  statCon_vec_[i]->randomize(l,u);
413  }
414  }
415  }
416  }
417 
418  int dimension(void) const {
419  int dim = vec_->dimension();
420  if (augmentedObj_) {
421  dim += statObj_vec_->dimension();
422  }
423  if (augmentedCon_) {
424  int size = statCon_vec_.size();
425  for (int i = 0; i < size; ++i) {
426  if (statCon_vec_[i] != nullPtr) {
427  dim += statCon_vec_[i]->dimension();
428  }
429  }
430  }
431  return dim;
432  }
433 
434  /***************************************************************************/
435  /************ ROL VECTOR ACCESSOR FUNCTIONS ********************************/
436  /***************************************************************************/
437  Ptr<const StdVector<Real>>
438  getStatisticVector(const int comp, const int index = 0) const {
439  if (comp == 0) {
440  return statObj_vec_;
441  }
442  else if (comp == 1) {
443  return statCon_vec_[index];
444  }
445  else {
446  throw Exception::NotImplemented(">>> ROL::RiskVector::getStatisticVector: Component must be 0 or 1!");
447  }
448  }
449 
450  Ptr<StdVector<Real>>
451  getStatisticVector(const int comp, const int index = 0) {
452  if (comp == 0) {
453  return statObj_vec_;
454  }
455  else if (comp == 1) {
456  return statCon_vec_[index];
457  }
458  else {
459  throw Exception::NotImplemented(">>> ROL::RiskVector::getStatistic: Component must be 0 or 1!");
460  }
461  }
462 
463  Ptr<const Vector<Real> > getVector(void) const {
464  return vec_;
465  }
466 
467  Ptr<Vector<Real> > getVector(void) {
468  return vec_;
469  }
470 
471  /***************************************************************************/
472  /************ COMPONENT ACCESSOR FUNCTIONS *********************************/
473  /***************************************************************************/
474  Ptr<std::vector<Real>>
475  getStatistic(const int comp = 0, const int index = 0) {
476  if (comp == 0) {
477  if (augmentedObj_) {
478  return statObj_;
479  }
480  }
481  else if (comp == 1) {
482  if (augmentedCon_) {
483  return statCon_[index];
484  }
485  }
486  else {
487  throw Exception::NotImplemented(">>> ROL::RiskVector::getStatistic: Component must be 0 or 1!");
488  }
489  return nullPtr;
490  }
491 
492  Ptr<const std::vector<Real>>
493  getStatistic(const int comp = 0, const int index = 0) const {
494  if (comp == 0) {
495  if (augmentedObj_) {
496  return statObj_;
497  }
498  }
499  else if (comp == 1) {
500  if (augmentedCon_) {
501  return statCon_[index];
502  }
503  }
504  else {
505  throw Exception::NotImplemented(">>> ROL::RiskVector::getStatistic: Component must be 0 or 1!");
506  }
507  return nullPtr;
508  }
509 
510  void setStatistic(const Real stat, const int comp = 0, const int index = 0) {
511  if ( comp == 0 ) {
512  if ( augmentedObj_ ) {
513  statObj_->assign(nStatObj_,stat);
514  }
515  }
516  else if ( comp == 1 ) {
517  if ( augmentedCon_ ) {
518  statCon_[index]->assign(nStatCon_[index],stat);
519  }
520  }
521  else {
522  throw Exception::NotImplemented(">>> ROL::RiskVector::setStatistic: Component must be 0 or 1!");
523  }
524  }
525 
526  void setStatistic(const std::vector<Real> &stat, const int comp = 0, const int index = 0) {
527  if ( comp == 0 ) {
528  if ( augmentedObj_ ) {
529  if ( nStatObj_ != static_cast<int>(stat.size()) ) {
530  throw Exception::NotImplemented(">>> ROL::RiskVector::setStatistic: Dimension mismatch!");
531  }
532  statObj_->assign(stat.begin(),stat.end());
533  }
534  }
535  else if ( comp == 1) {
536  if ( augmentedCon_ ) {
537  if ( nStatCon_[index] != static_cast<int>(stat.size()) ) {
538  throw Exception::NotImplemented(">>> ROL::RiskVector::setStatistic: Dimension mismatch!");
539  }
540  statCon_[index]->assign(stat.begin(),stat.end());
541  }
542  }
543  else {
544  throw Exception::NotImplemented(">>> ROL::RiskVector::setStatistic: Component must be 0 or 1!");
545  }
546  }
547 
548  void setVector(const Vector<Real>& vec) {
549  vec_->set(vec);
550  }
551 };
552 
553 }
554 
555 #endif
void set(const Vector< Real > &x)
Set where .
std::vector< int > nStatCon_
void scale(const Real alpha)
Compute where .
Ptr< Vector< Real > > vec_
void setStatistic(const std::vector< Real > &stat, const int comp=0, const int index=0)
void setScalar(const Real C)
Set where .
Ptr< Vector< Real > > getVector(void)
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
void setStatistic(const Real stat, const int comp=0, const int index=0)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Ptr< Vector< Real > > dual_vec1_
void setVector(const Vector< Real > &vec)
void plus(const Vector< Real > &x)
Compute , where .
std::vector< Ptr< std::vector< Real > > > dualCon_
Ptr< const StdVector< Real > > getStatisticVector(const int comp, const int index=0) const
int dimension(void) const
Return dimension of the vector space.
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
Ptr< std::vector< Real > > getStatistic(const int comp=0, const int index=0)
RiskVector(const Ptr< Vector< Real > > &vec, const Ptr< std::vector< Real > > &statObj, const std::vector< Ptr< std::vector< Real > > > &statCon)
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
RiskVector(Ptr< ParameterList > &parlist, const Ptr< Vector< Real > > &vec, const Real stat=0)
Ptr< StdVector< Real > > getStatisticVector(const int comp, const int index=0)
RiskVector(const Ptr< Vector< Real > > &vec)
RiskVector(Ptr< ParameterList > &parlistObj, std::vector< Ptr< ParameterList > > &parlistCon, const Ptr< Vector< Real > > &vec, const Real stat=0)
Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
void initializeCon(std::vector< Ptr< ParameterList > > &parlist, const Real stat=1)
Ptr< StdVector< Real > > statObj_vec_
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
std::vector< Ptr< StdVector< Real > > > statCon_vec_
Real norm(void) const
Returns where .
RiskVector(std::vector< Ptr< ParameterList > > &parlist, const Ptr< Vector< Real > > &vec, const Real stat=0)
Ptr< RiskVector< Real > > dual_vec_
Ptr< const Vector< Real > > getVector(void) const
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Real dot(const Vector< Real > &x) const
Compute where .
Ptr< std::vector< Real > > statObj_
std::vector< Ptr< std::vector< Real > > > statCon_
void initializeObj(Ptr< ParameterList > &parlist, const Real stat=1)
Ptr< std::vector< Real > > dualObj_
constexpr auto dim
Ptr< const std::vector< Real > > getStatistic(const int comp=0, const int index=0) const