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 
75 namespace ROL {
76 
77 template<class Real>
78 class MeanVarianceFromTarget : public RiskMeasure<Real> {
79  typedef typename std::vector<Real>::size_type uint;
80 private:
81 
82  Teuchos::RCP<PositiveFunction<Real> > positiveFunction_;
83 
84  std::vector<Real> target_;
85  std::vector<Real> order_;
86  std::vector<Real> coeff_;
88 
89  void checkInputs(void) const {
90  int oSize = order_.size(), cSize = coeff_.size();
91  TEUCHOS_TEST_FOR_EXCEPTION((oSize!=cSize),std::invalid_argument,
92  ">>> ERROR (ROL::MeanVarianceFromTarget): Order and coefficient arrays have different sizes!");
93  Real zero(0), two(2);
94  for (int i = 0; i < oSize; i++) {
95  TEUCHOS_TEST_FOR_EXCEPTION((order_[i] < two), std::invalid_argument,
96  ">>> ERROR (ROL::MeanVarianceFromTarget): Element of order array out of range!");
97  TEUCHOS_TEST_FOR_EXCEPTION((coeff_[i] < zero), std::invalid_argument,
98  ">>> ERROR (ROL::MeanVarianceFromTarget): Element of coefficient array out of range!");
99  }
100  TEUCHOS_TEST_FOR_EXCEPTION(positiveFunction_ == Teuchos::null, std::invalid_argument,
101  ">>> ERROR (ROL::MeanVarianceFromTarget): PositiveFunction pointer is null!");
102  }
103 
104 public:
115  MeanVarianceFromTarget( const Real target, const Real order, const Real coeff,
116  const Teuchos::RCP<PositiveFunction<Real> > &pf )
117  : RiskMeasure<Real>(), positiveFunction_(pf) {
118  target_.clear(); target_.push_back(target);
119  order_.clear(); order_.push_back(order);
120  coeff_.clear(); coeff_.push_back(coeff);
121  checkInputs();
122  NumMoments_ = order_.size();
123  }
124 
135  MeanVarianceFromTarget( const std::vector<Real> &target,
136  const std::vector<Real> &order,
137  const std::vector<Real> &coeff,
138  const Teuchos::RCP<PositiveFunction<Real> > &pf )
139  : RiskMeasure<Real>(), positiveFunction_(pf) {
140  target_.clear(); order_.clear(); coeff_.clear();
141  for ( uint i = 0; i < target.size(); i++ ) {
142  target_.push_back(target[i]);
143  }
144  for ( uint i = 0; i < order.size(); i++ ) {
145  order_.push_back(order[i]);
146  }
147  for ( uint i = 0; i < coeff.size(); i++ ) {
148  coeff_.push_back(coeff[i]);
149  }
150  checkInputs();
151  NumMoments_ = order_.size();
152  }
153 
166  MeanVarianceFromTarget( Teuchos::ParameterList &parlist )
167  : RiskMeasure<Real>() {
168  Teuchos::ParameterList &list
169  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Variance From Target");
170  // Get data from parameter list
171  Teuchos::Array<Real> target
172  = Teuchos::getArrayFromStringParameter<double>(list,"Targets");
173  target_ = target.toVector();
174  Teuchos::Array<Real> order
175  = Teuchos::getArrayFromStringParameter<double>(list,"Orders");
176  order_ = order.toVector();
177  Teuchos::Array<Real> coeff
178  = Teuchos::getArrayFromStringParameter<double>(list,"Coefficients");
179  coeff_ = coeff.toVector();
180  // Build (approximate) positive function
181  std::string type = list.get<std::string>("Deviation Type");
182  if ( type == "Upper" ) {
183  positiveFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
184  }
185  else if ( type == "Absolute" ) {
186  positiveFunction_ = Teuchos::rcp(new AbsoluteValue<Real>(list));
187  }
188  else {
189  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
190  ">>> (ROL::MeanDeviation): Deviation type is not recoginized!");
191  }
192  // Check inputs
193  checkInputs();
194  NumMoments_ = order.size();
195  }
196 
197  void update(const Real val, const Real weight) {
198  Real diff(0), pf0(0);
199  RiskMeasure<Real>::val_ += weight * val;
200  for ( uint p = 0; p < NumMoments_; p++ ) {
201  diff = val-target_[p];
202  pf0 = positiveFunction_->evaluate(diff,0);
203  RiskMeasure<Real>::val_ += weight * coeff_[p] * std::pow(pf0,order_[p]);
204  }
205  }
206 
207  void update(const Real val, const Vector<Real> &g, const Real weight) {
208  Real diff(0), pf0(0), pf1(0), c(1), one(1);
209  for ( uint p = 0; p < NumMoments_; p++ ) {
210  diff = val-target_[p];
211  pf0 = positiveFunction_->evaluate(diff,0);
212  pf1 = positiveFunction_->evaluate(diff,1);
213  c += order_[p]*coeff_[p]*std::pow(pf0,order_[p]-one)*pf1;
214  }
215  (RiskMeasure<Real>::g_)->axpy(weight * c,g);
216  }
217 
218  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
219  const Real weight) {
220  Real diff(0), pf0(0), pf1(0), pf2(0), p1(0), p2(0), ch(1), cg(0), one(1), two(2);
221  for ( uint p = 0; p < NumMoments_; p++ ) {
222  diff = val - target_[p];
223  pf0 = positiveFunction_->evaluate(diff,0);
224  pf1 = positiveFunction_->evaluate(diff,1);
225  pf2 = positiveFunction_->evaluate(diff,2);
226  //p0 = std::pow(pf0,order_[p]);
227  p1 = std::pow(pf0,order_[p]-one);
228  p2 = std::pow(pf0,order_[p]-two);
229  cg += order_[p]*coeff_[p]*gv*( (order_[p]-one)*p2*pf1*pf1 + p1*pf2 );
230  ch += order_[p]*coeff_[p]*p1*pf1;
231  }
232  RiskMeasure<Real>::hv_->axpy(weight*cg,g);
233  RiskMeasure<Real>::hv_->axpy(weight*ch,hv);
234  }
235 };
236 
237 }
238 
239 #endif
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
Update internal risk measure storage for Hessian-time-a-vector computation.
void update(const Real val, const Vector< Real > &g, const Real weight)
Update internal risk measure storage for gradient computation.
MeanVarianceFromTarget(Teuchos::ParameterList &parlist)
Constructor.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
MeanVarianceFromTarget(const std::vector< Real > &target, const std::vector< Real > &order, const std::vector< Real > &coeff, const Teuchos::RCP< PositiveFunction< Real > > &pf)
Constructor.
Teuchos::RCP< PositiveFunction< Real > > positiveFunction_
Provides an interface for the mean plus a sum of arbitrary order variances from targets.
void update(const Real val, const Real weight)
Update internal risk measure storage for value computation.
std::vector< Real >::size_type uint
Provides the interface to implement risk measures.
MeanVarianceFromTarget(const Real target, const Real order, const Real coeff, const Teuchos::RCP< PositiveFunction< Real > > &pf)
Constructor.