ROL
ROL_MeanVarianceFromTarget.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_MEANVARIANCEFROMTARGET_HPP
45 #define ROL_MEANVARIANCEFROMTARGET_HPP
46 
47 #include "ROL_RiskMeasure.hpp"
48 #include "ROL_PositiveFunction.hpp"
49 #include "ROL_PlusFunction.hpp"
50 #include "ROL_AbsoluteValue.hpp"
51 
52 #include "Teuchos_ParameterList.hpp"
53 #include "Teuchos_Array.hpp"
54 
55 namespace ROL {
56 
57 template<class Real>
58 class MeanVarianceFromTarget : public RiskMeasure<Real> {
59  typedef typename std::vector<Real>::size_type uint;
60 private:
61 
62  Teuchos::RCP<PositiveFunction<Real> > positiveFunction_;
63 
64  std::vector<Real> target_;
65  std::vector<Real> order_;
66  std::vector<Real> coeff_;
68 
69 public:
70 
71  MeanVarianceFromTarget( Real target, Real order, Real coeff,
72  Teuchos::RCP<PositiveFunction<Real> > &pf )
73  : RiskMeasure<Real>(), positiveFunction_(pf) {
74  target_.clear(); order_.clear(); coeff_.clear();
75  target_.push_back(target);
76  order_.push_back((order < 2.0) ? 2.0 : order);
77  coeff_.push_back((coeff < 0.0) ? 1.0 : coeff);
78  NumMoments_ = order_.size();
79  }
80 
81  MeanVarianceFromTarget( std::vector<Real> &target, std::vector<Real> &order, std::vector<Real> &coeff,
82  Teuchos::RCP<PositiveFunction<Real> > &pf )
83  : RiskMeasure<Real>(), positiveFunction_(pf) {
84  NumMoments_ = order.size();
85  target_.clear(); order_.clear(); coeff_.clear();
86  if ( NumMoments_ != target.size() ) {
87  target.resize(NumMoments_,0.0);
88  }
89  if ( NumMoments_ != coeff.size() ) {
90  coeff.resize(NumMoments_,1.0);
91  }
92  for ( uint i = 0; i < NumMoments_; i++ ) {
93  target_.push_back(target[i]);
94  order_.push_back((order[i] < 2.0) ? 2.0 : order[i]);
95  coeff_.push_back((coeff[i] < 0.0) ? 1.0 : coeff[i]);
96  }
97  }
98 
99  MeanVarianceFromTarget( Teuchos::ParameterList &parlist )
100  : RiskMeasure<Real>() {
101  Teuchos::ParameterList &list
102  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Variance From Target");
103  // Get data from parameter list
104  Teuchos::Array<Real> target
105  = Teuchos::getArrayFromStringParameter<double>(list,"Targets");
106  Teuchos::Array<Real> order
107  = Teuchos::getArrayFromStringParameter<double>(list,"Orders");
108  Teuchos::Array<Real> coeff
109  = Teuchos::getArrayFromStringParameter<double>(list,"Coefficients");
110  // Check inputs
111  NumMoments_ = order.size();
112  target_.clear(); order_.clear(); coeff_.clear();
113  if ( NumMoments_ != static_cast<uint>(target.size()) ) {
114  target.resize(NumMoments_,0.0);
115  }
116  if ( NumMoments_ != static_cast<uint>(coeff.size()) ) {
117  coeff.resize(NumMoments_,1.0);
118  }
119  for ( uint i = 0; i < NumMoments_; i++ ) {
120  target_.push_back(target[i]);
121  order_.push_back((order[i] < 2.0) ? 2.0 : order[i]);
122  coeff_.push_back((coeff[i] < 0.0) ? 1.0 : coeff[i]);
123  }
124  // Build (approximate) positive function
125  if ( list.get("Deviation Type","Upper") == "Upper" ) {
126  positiveFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
127  }
128  else {
129  positiveFunction_ = Teuchos::rcp(new AbsoluteValue<Real>(list));
130  }
131  }
132 
133  void update(const Real val, const Real weight) {
134  Real diff = 0.0, pf0 = 0.0;
135  RiskMeasure<Real>::val_ += weight * val;
136  for ( uint p = 0; p < NumMoments_; p++ ) {
137  diff = val-target_[p];
138  pf0 = positiveFunction_->evaluate(diff,0);
139  RiskMeasure<Real>::val_ += weight * coeff_[p] * std::pow(pf0,order_[p]);
140  }
141  }
142 
143  void update(const Real val, const Vector<Real> &g, const Real weight) {
144  Real diff = 0.0, pf0 = 0.0, pf1 = 0.0, c = 1.0;
145  for ( uint p = 0; p < NumMoments_; p++ ) {
146  diff = val-target_[p];
147  pf0 = positiveFunction_->evaluate(diff,0);
148  pf1 = positiveFunction_->evaluate(diff,1);
149  c += order_[p]*coeff_[p]*std::pow(pf0,order_[p]-1.0)*pf1;
150  }
151  (RiskMeasure<Real>::g_)->axpy(weight * c,g);
152  }
153 
154  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
155  const Real weight) {
156  Real diff = 0.0, pf0 = 0.0, pf1 = 0.0, pf2 = 0.0, p1 = 0.0, p2 = 0.0, ch = 1.0, cg = 0.0;
157  for ( uint p = 0; p < NumMoments_; p++ ) {
158  diff = val - target_[p];
159  pf0 = positiveFunction_->evaluate(diff,0);
160  pf1 = positiveFunction_->evaluate(diff,1);
161  pf2 = positiveFunction_->evaluate(diff,2);
162  //p0 = std::pow(pf0,order_[p]);
163  p1 = std::pow(pf0,order_[p]-1.0);
164  p2 = std::pow(pf0,order_[p]-2.0);
165  cg += order_[p]*coeff_[p]*gv*( (order_[p]-1.0)*p2*pf1*pf1 + p1*pf2 );
166  ch += order_[p]*coeff_[p]*p1*pf1;
167  }
168  RiskMeasure<Real>::hv_->axpy(weight*cg,g);
169  RiskMeasure<Real>::hv_->axpy(weight*ch,hv);
170  }
171 };
172 
173 }
174 
175 #endif
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
void update(const Real val, const Vector< Real > &g, const Real weight)
MeanVarianceFromTarget(Teuchos::ParameterList &parlist)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
MeanVarianceFromTarget(Real target, Real order, Real coeff, Teuchos::RCP< PositiveFunction< Real > > &pf)
MeanVarianceFromTarget(std::vector< Real > &target, std::vector< Real > &order, std::vector< Real > &coeff, Teuchos::RCP< PositiveFunction< Real > > &pf)
Teuchos::RCP< PositiveFunction< Real > > positiveFunction_
void update(const Real val, const Real weight)
std::vector< Real >::size_type uint