ROL
ROL_MeanVarianceFromTarget.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_MEANVARIANCEFROMTARGET_HPP
11#define ROL_MEANVARIANCEFROMTARGET_HPP
12
15#include "ROL_PlusFunction.hpp"
16#include "ROL_AbsoluteValue.hpp"
17
18#include "ROL_ParameterList.hpp"
19
40namespace ROL {
41
42template<class Real>
44 typedef typename std::vector<Real>::size_type uint;
45private:
46 ROL::Ptr<PositiveFunction<Real> > positiveFunction_;
47 std::vector<Real> target_;
48 std::vector<Real> order_;
49 std::vector<Real> coeff_;
51
52 using RandVarFunctional<Real>::val_;
53 using RandVarFunctional<Real>::gv_;
54 using RandVarFunctional<Real>::g_;
55 using RandVarFunctional<Real>::hv_;
57
58 using RandVarFunctional<Real>::point_;
60
65
66 void checkInputs(void) const {
67 int oSize = order_.size(), cSize = coeff_.size();
68 ROL_TEST_FOR_EXCEPTION((oSize!=cSize),std::invalid_argument,
69 ">>> ERROR (ROL::MeanVarianceFromTarget): Order and coefficient arrays have different sizes!");
70 Real zero(0), two(2);
71 for (int i = 0; i < oSize; i++) {
72 ROL_TEST_FOR_EXCEPTION((order_[i] < two), std::invalid_argument,
73 ">>> ERROR (ROL::MeanVarianceFromTarget): Element of order array out of range!");
74 ROL_TEST_FOR_EXCEPTION((coeff_[i] < zero), std::invalid_argument,
75 ">>> ERROR (ROL::MeanVarianceFromTarget): Element of coefficient array out of range!");
76 }
77 ROL_TEST_FOR_EXCEPTION(positiveFunction_ == ROL::nullPtr, std::invalid_argument,
78 ">>> ERROR (ROL::MeanVarianceFromTarget): PositiveFunction pointer is null!");
79 }
80
81public:
92 MeanVarianceFromTarget( const Real target, const Real order, const Real coeff,
93 const ROL::Ptr<PositiveFunction<Real> > &pf )
95 target_.clear(); target_.push_back(target);
96 order_.clear(); order_.push_back(order);
97 coeff_.clear(); coeff_.push_back(coeff);
99 NumMoments_ = order_.size();
100 }
101
112 MeanVarianceFromTarget( const std::vector<Real> &target,
113 const std::vector<Real> &order,
114 const std::vector<Real> &coeff,
115 const ROL::Ptr<PositiveFunction<Real> > &pf )
116 : RandVarFunctional<Real>(), positiveFunction_(pf) {
117 target_.clear(); order_.clear(); coeff_.clear();
118 for ( uint i = 0; i < target.size(); i++ ) {
119 target_.push_back(target[i]);
120 }
121 for ( uint i = 0; i < order.size(); i++ ) {
122 order_.push_back(order[i]);
123 }
124 for ( uint i = 0; i < coeff.size(); i++ ) {
125 coeff_.push_back(coeff[i]);
126 }
127 checkInputs();
128 NumMoments_ = order_.size();
129 }
130
143 MeanVarianceFromTarget( ROL::ParameterList &parlist )
144 : RandVarFunctional<Real>() {
145 ROL::ParameterList &list
146 = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Variance From Target");
147 // Get data from parameter list
148 target_ = ROL::getArrayFromStringParameter<double>(list,"Targets");
149 order_ = ROL::getArrayFromStringParameter<double>(list,"Orders");
150 coeff_ = ROL::getArrayFromStringParameter<double>(list,"Coefficients");
151
152 // Build (approximate) positive function
153 std::string type = list.get<std::string>("Deviation Type");
154 if ( type == "Upper" ) {
155 positiveFunction_ = ROL::makePtr<PlusFunction<Real>>(list);
156 }
157 else if ( type == "Absolute" ) {
158 positiveFunction_ = ROL::makePtr<AbsoluteValue<Real>>(list);
159 }
160 else {
161 ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
162 ">>> (ROL::MeanDeviation): Deviation type is not recoginized!");
163 }
164 // Check inputs
165 checkInputs();
166 NumMoments_ = order_.size();
167 }
168
170 const Vector<Real> &x,
171 const std::vector<Real> &xstat,
172 Real &tol) {
173 Real diff(0), pf0(0);
174 Real val = computeValue(obj,x,tol);
175 val_ += weight_ * val;
176 for ( uint p = 0; p < NumMoments_; p++ ) {
177 diff = val-target_[p];
178 pf0 = positiveFunction_->evaluate(diff,0);
179 val_ += weight_ * coeff_[p] * std::pow(pf0,order_[p]);
180 }
181 }
182
184 const Vector<Real> &x,
185 const std::vector<Real> &xstat,
186 Real &tol) {
187 Real diff(0), pf0(0), pf1(0), c(1), one(1);
188 Real val = computeValue(obj,x,tol);
189 for ( uint p = 0; p < NumMoments_; p++ ) {
190 diff = val-target_[p];
191 pf0 = positiveFunction_->evaluate(diff,0);
192 pf1 = positiveFunction_->evaluate(diff,1);
193 c += order_[p]*coeff_[p]*std::pow(pf0,order_[p]-one)*pf1;
194 }
195 computeGradient(*dualVector_,obj,x,tol);
196 g_->axpy(weight_ * c,*dualVector_);
197 }
198
200 const Vector<Real> &v,
201 const std::vector<Real> &vstat,
202 const Vector<Real> &x,
203 const std::vector<Real> &xstat,
204 Real &tol) {
205 Real diff(0), pf0(0), pf1(0), pf2(0), p1(0), p2(0), ch(1), cg(0), one(1), two(2);
206 Real val = computeValue(obj,x,tol);
207 Real gv = computeGradVec(*dualVector_,obj,v,x,tol);
208 for ( uint p = 0; p < NumMoments_; p++ ) {
209 diff = val - target_[p];
210 pf0 = positiveFunction_->evaluate(diff,0);
211 pf1 = positiveFunction_->evaluate(diff,1);
212 pf2 = positiveFunction_->evaluate(diff,2);
213 //p0 = std::pow(pf0,order_[p]);
214 p1 = std::pow(pf0,order_[p]-one);
215 p2 = std::pow(pf0,order_[p]-two);
216 cg += order_[p]*coeff_[p]*gv*( (order_[p]-one)*p2*pf1*pf1 + p1*pf2 );
217 ch += order_[p]*coeff_[p]*p1*pf1;
218 }
219 hv_->axpy(weight_*cg,*dualVector_);
220 computeHessVec(*dualVector_,obj,v,x,tol);
221 hv_->axpy(weight_*ch,*dualVector_);
222 }
223};
224
225}
226
227#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Provides an interface for the mean plus a sum of arbitrary order variances from targets.
MeanVarianceFromTarget(ROL::ParameterList &parlist)
Constructor.
ROL::Ptr< PositiveFunction< Real > > positiveFunction_
void updateHessVec(Objective< Real > &obj, const Vector< Real > &v, const std::vector< Real > &vstat, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for Hessian-time-a-vector computation.
std::vector< Real >::size_type uint
void updateGradient(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for gradient computation.
MeanVarianceFromTarget(const Real target, const Real order, const Real coeff, const ROL::Ptr< PositiveFunction< Real > > &pf)
Constructor.
void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
MeanVarianceFromTarget(const std::vector< Real > &target, const std::vector< Real > &order, const std::vector< Real > &coeff, const ROL::Ptr< PositiveFunction< Real > > &pf)
Constructor.
Provides the interface to evaluate objective functions.
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
Real computeValue(Objective< Real > &obj, const Vector< Real > &x, Real &tol)
void computeHessVec(Vector< Real > &hv, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
void computeGradient(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Ptr< Vector< Real > > dualVector_
Real computeGradVec(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Defines the linear algebra or vector space interface.