ROL
ROL_MeanDeviationFromTarget.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_MEANDEVIATIONFROMTARGET_HPP
45 #define ROL_MEANDEVIATIONFROMTARGET_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 MeanDeviationFromTarget : public RiskMeasure<Real> {
79  typedef typename std::vector<Real>::size_type uint;
80 private:
81  Teuchos::RCP<PositiveFunction<Real> > positiveFunction_;
82 
83  Teuchos::RCP<Vector<Real> > dualVector1_;
84  Teuchos::RCP<Vector<Real> > dualVector2_;
85  Teuchos::RCP<Vector<Real> > dualVector3_;
86  Teuchos::RCP<Vector<Real> > dualVector4_;
87 
88  std::vector<Real> target_;
89  std::vector<Real> order_;
90  std::vector<Real> coeff_;
92 
93  std::vector<Real> pval_;
94  std::vector<Real> pgv_;
95 
96  std::vector<Teuchos::RCP<Vector<Real> > > pg0_;
97  std::vector<Teuchos::RCP<Vector<Real> > > pg_;
98  std::vector<Teuchos::RCP<Vector<Real> > > phv_;
99 
101 
102  void initialize(void) {
103  // Initialize additional storage
104  pg_.clear(); pg0_.clear(); phv_.clear(); pval_.clear(); pgv_.clear();
105  pg_.resize(NumMoments_);
106  pg0_.resize(NumMoments_);
107  phv_.resize(NumMoments_);
108  pval_.resize(NumMoments_);
109  pgv_.resize(NumMoments_);
110  }
111 
112  void checkInputs(void) const {
113  int oSize = order_.size(), cSize = coeff_.size(), tSize = target_.size();
114  TEUCHOS_TEST_FOR_EXCEPTION((oSize!=cSize),std::invalid_argument,
115  ">>> ERROR (ROL::MeanDeviationFromTarget): Order and coefficient arrays have different sizes!");
116  TEUCHOS_TEST_FOR_EXCEPTION((oSize!=tSize),std::invalid_argument,
117  ">>> ERROR (ROL::MeanDeviationFromTarget): Order and target arrays have different sizes!");
118  Real zero(0), two(2);
119  for (int i = 0; i < oSize; i++) {
120  TEUCHOS_TEST_FOR_EXCEPTION((order_[i] < two), std::invalid_argument,
121  ">>> ERROR (ROL::MeanDeviationFromTarget): Element of order array out of range!");
122  TEUCHOS_TEST_FOR_EXCEPTION((coeff_[i] < zero), std::invalid_argument,
123  ">>> ERROR (ROL::MeanDeviationFromTarget): Element of coefficient array out of range!");
124  }
125  TEUCHOS_TEST_FOR_EXCEPTION(positiveFunction_ == Teuchos::null, std::invalid_argument,
126  ">>> ERROR (ROL::MeanDeviationFromTarget): PositiveFunction pointer is null!");
127  }
128 
129 public:
140  MeanDeviationFromTarget( const Real target, const Real order, const Real coeff,
141  const Teuchos::RCP<PositiveFunction<Real> > &pf )
142  : RiskMeasure<Real>(), positiveFunction_(pf), firstReset_(true) {
143  order_.clear(); order_.push_back(order);
144  coeff_.clear(); coeff_.push_back(coeff);
145  target_.clear(); target_.push_back(target);
146  checkInputs();
147  NumMoments_ = order_.size();
148  initialize();
149  }
150 
161  MeanDeviationFromTarget( const std::vector<Real> &target,
162  const std::vector<Real> &order,
163  const std::vector<Real> &coeff,
164  const Teuchos::RCP<PositiveFunction<Real> > &pf )
165  : RiskMeasure<Real>(), positiveFunction_(pf), firstReset_(true) {
166  target_.clear(); order_.clear(); coeff_.clear();
167  for ( uint i = 0; i < target.size(); i++ ) {
168  target_.push_back(target[i]);
169  }
170  for ( uint i = 0; i < order.size(); i++ ) {
171  order_.push_back(order[i]);
172  }
173  for ( uint i = 0; i < coeff.size(); i++ ) {
174  coeff_.push_back(coeff[i]);
175  }
176  checkInputs();
177  NumMoments_ = order_.size();
178  initialize();
179  }
180 
193  MeanDeviationFromTarget( Teuchos::ParameterList &parlist )
194  : RiskMeasure<Real>(), firstReset_(true) {
195  Teuchos::ParameterList &list
196  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Deviation From Target");
197  // Get data from parameter list
198  Teuchos::Array<Real> target
199  = Teuchos::getArrayFromStringParameter<double>(list,"Targets");
200  target_ = target.toVector();
201  Teuchos::Array<Real> order
202  = Teuchos::getArrayFromStringParameter<double>(list,"Orders");
203  order_ = order.toVector();
204  Teuchos::Array<Real> coeff
205  = Teuchos::getArrayFromStringParameter<double>(list,"Coefficients");
206  coeff_ = coeff.toVector();
207  // Build (approximate) positive function
208  std::string type = list.get<std::string>("Deviation Type");
209  if ( type == "Upper" ) {
210  positiveFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
211  }
212  else if ( type == "Absolute" ) {
213  positiveFunction_ = Teuchos::rcp(new AbsoluteValue<Real>(list));
214  }
215  else {
216  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
217  ">>> (ROL::MeanDeviation): Deviation type is not recoginized!");
218  }
219  // Check inputs
220  checkInputs();
221  NumMoments_ = order.size();
222  initialize();
223  }
224 
225  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
226  Real zero(0);
228  if (firstReset_) {
229  for ( uint p = 0; p < NumMoments_; p++ ) {
230  pg0_[p] = (x0->dual()).clone();
231  pg_[p] = (x0->dual()).clone();
232  phv_[p] = (x0->dual()).clone();
233  }
234  dualVector1_ = (x0->dual()).clone();
235  dualVector2_ = (x0->dual()).clone();
236  dualVector3_ = (x0->dual()).clone();
237  dualVector4_ = (x0->dual()).clone();
238  firstReset_ = false;
239  }
240  for ( uint p = 0; p < NumMoments_; p++ ) {
241  pg0_[p]->zero(); pg_[p]->zero(); phv_[p]->zero();
242  pval_[p] = zero; pgv_[p] = zero;
243  }
244  dualVector1_->zero(); dualVector2_->zero();
245  dualVector3_->zero(); dualVector4_->zero();
246  }
247 
248  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
249  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
250  reset(x0,x);
251  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(
252  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
253  }
254 
255  void update(const Real val, const Real weight) {
256  Real diff(0), pf0(0);
257  RiskMeasure<Real>::val_ += weight * val;
258  for ( uint p = 0; p < NumMoments_; p++ ) {
259  diff = val-target_[p];
260  pf0 = positiveFunction_->evaluate(diff,0);
261  pval_[p] += weight * std::pow(pf0,order_[p]);
262  }
263  }
264 
265  void update(const Real val, const Vector<Real> &g, const Real weight) {
266  Real diff(0), pf0(0), pf1(0), c(0), one(1);
267  for ( uint p = 0; p < NumMoments_; p++ ) {
268  diff = val-target_[p];
269  pf0 = positiveFunction_->evaluate(diff,0);
270  pf1 = positiveFunction_->evaluate(diff,1);
271  c = std::pow(pf0,order_[p]-one) * pf1;
272  (pg_[p])->axpy(weight * c,g);
273  pval_[p] += weight * std::pow(pf0,order_[p]);
274  }
275  RiskMeasure<Real>::g_->axpy(weight,g);
276  }
277 
278  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
279  const Real weight) {
280  Real diff(0), pf0(0), pf1(0), pf2(0), p0(0), p1(0), p2(0), c(0), one(1), two(2);
281  for ( uint p = 0; p < NumMoments_; p++ ) {
282  diff = val - target_[p];
283  pf0 = positiveFunction_->evaluate(diff,0);
284  pf1 = positiveFunction_->evaluate(diff,1);
285  pf2 = positiveFunction_->evaluate(diff,2);
286  p0 = std::pow(pf0,order_[p]);
287  p1 = std::pow(pf0,order_[p]-one);
288  p2 = std::pow(pf0,order_[p]-two);
289  c = -(order_[p]-one)*p1*pf1;
290  pg0_[p]->axpy(weight*c,g);
291  c = gv*((order_[p]-one)*p2*pf1*pf1 + p1*pf2);
292  pg_[p]->axpy(weight*c,g);
293  c = p1*pf1;
294  phv_[p]->axpy(weight*c,hv);
295  pval_[p] += weight*p0;
296  pgv_[p] += weight*p1*pf1*gv;
297  }
298  RiskMeasure<Real>::hv_->axpy(weight,hv);
299  }
300 
302  Real val = RiskMeasure<Real>::val_, dev(0), one(1);
303  sampler.sumAll(&val,&dev,1);
304  std::vector<Real> pval_sum(NumMoments_);
305  sampler.sumAll(&(pval_)[0],&pval_sum[0],NumMoments_);
306  for ( uint p = 0; p < NumMoments_; p++ ) {
307  dev += coeff_[p] * std::pow(pval_sum[p],one/order_[p]);
308  }
309  return dev;
310  }
311 
313  Real zero(0), one(1);
315  std::vector<Real> pval_sum(NumMoments_);
316  sampler.sumAll(&(pval_)[0],&pval_sum[0],NumMoments_);
317  Teuchos::RCP<Vector<Real> > pg;
318  for ( uint p = 0; p < NumMoments_; p++ ) {
319  if ( pval_sum[p] > zero ) {
320  pg = (pg_[p])->clone();
321  sampler.sumAll(*(pg_[p]),*pg);
322  dualVector1_->axpy(coeff_[p]/std::pow(pval_sum[p],one-one/order_[p]),*pg);
323  }
324  }
325  // Set RiskVector
326  (Teuchos::dyn_cast<RiskVector<Real> >(g)).setVector(*dualVector1_);
327  }
328 
330  Real zero(0), one(1), two(2);
332  std::vector<Real> pval_sum(NumMoments_);
333  sampler.sumAll(&(pval_)[0],&pval_sum[0],NumMoments_);
334  std::vector<Real> pgv_sum(NumMoments_);
335  sampler.sumAll(&(pgv_)[0],&pgv_sum[0],NumMoments_);
336  Real c(0);
337  for ( uint p = 0; p < NumMoments_; p++ ) {
338  if ( pval_sum[p] > zero ) {
339  sampler.sumAll(*(pg_[p]),*dualVector2_);
340  sampler.sumAll(*(pg0_[p]),*dualVector3_);
341  sampler.sumAll(*(phv_[p]),*dualVector4_);
342  c = coeff_[p]*(pgv_sum[p]/std::pow(pval_sum[p],two-one/order_[p]));
343  dualVector1_->axpy(c,*dualVector3_);
344  c = coeff_[p]/std::pow(pval_sum[p],one-one/order_[p]);
345  dualVector1_->axpy(c,*dualVector2_);
346  dualVector1_->axpy(c,*dualVector4_);
347  }
348  }
349  // Set RiskVector
350  (Teuchos::dyn_cast<RiskVector<Real> >(hv)).setVector(*dualVector1_);
351  }
352 };
353 
354 }
355 
356 #endif
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
void sumAll(Real *input, Real *output, int dim) const
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.
Provides an interface for the mean plus a sum of arbitrary order deviations from targets.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< const Vector< Real > > getVector(void) const
std::vector< Real >::size_type uint
Teuchos::RCP< Vector< Real > > dualVector1_
MeanDeviationFromTarget(const Real target, const Real order, const Real coeff, const Teuchos::RCP< PositiveFunction< Real > > &pf)
Constructor.
Real getValue(SampleGenerator< Real > &sampler)
Return risk measure value.
std::vector< Teuchos::RCP< Vector< Real > > > phv_
void update(const Real val, const Real weight)
Update internal risk measure storage for value computation.
Teuchos::RCP< Vector< Real > > dualVector2_
Teuchos::RCP< PositiveFunction< Real > > positiveFunction_
Teuchos::RCP< Vector< Real > > dualVector4_
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
Reset internal risk measure storage. Called for Hessian-times-a-vector computation.
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
MeanDeviationFromTarget(Teuchos::ParameterList &parlist)
Constructor.
MeanDeviationFromTarget(const std::vector< Real > &target, const std::vector< Real > &order, const std::vector< Real > &coeff, const Teuchos::RCP< PositiveFunction< Real > > &pf)
Constructor.
Teuchos::RCP< Vector< Real > > dualVector3_
std::vector< Teuchos::RCP< Vector< Real > > > pg_
std::vector< Teuchos::RCP< Vector< Real > > > pg0_
Provides the interface to implement risk measures.
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
void update(const Real val, const Vector< Real > &g, const Real weight)
Update internal risk measure storage for gradient computation.