ROL
ROL_PH_DeviationObjective.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 PH_DEVIATIONOBJECTIVE_H
45#define PH_DEVIATIONOBJECTIVE_H
46
47#include "ROL_Objective.hpp"
49
56namespace ROL {
57
58template <class Real>
59class PH_DeviationObjective : public Objective<Real> {
60private:
61 const Ptr<Objective<Real>> obj_;
62 Ptr<ExpectationQuad<Real>> quad_;
63
65 Real val_;
66
69 Ptr<Vector<Real>> g_;
70
71 void getValue(const Vector<Real> &x, Real &tol) {
72 if (!isValueComputed_) {
73 val_ = obj_->value(x,tol);
74 isValueComputed_ = true;
75 }
76 }
77
78 void getGradient(const Vector<Real> &x, Real &tol) {
80 g_ = x.dual().clone();
82 }
84 obj_->gradient(*g_,x,tol);
86 }
87 }
88
89 Ptr<const Vector<Real>> getConstVector(const Vector<Real> &x) const {
90 const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
91 return xrv.getVector();
92 }
93
94 Ptr<Vector<Real>> getVector(Vector<Real> &x) const {
95 RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
96 return xrv.getVector();
97 }
98
99 Ptr<const std::vector<Real>> getConstStat(const Vector<Real> &x) const {
100 const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
101 Ptr<const std::vector<Real>> xstat = xrv.getStatistic();
102 if (xstat == nullPtr) {
103 xstat = makePtr<const std::vector<Real>>(0);
104 }
105 return xstat;
106 }
107
108 Ptr<std::vector<Real>> getStat(Vector<Real> &x) const {
109 RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
110 Ptr<std::vector<Real>> xstat = xrv.getStatistic();
111 if (xstat == nullPtr) {
112 xstat = makePtr<std::vector<Real>>(0);
113 }
114 return xstat;
115 }
116
117public:
118
120 ParameterList &parlist)
121 : obj_(obj),
122 isValueComputed_(false),
124 isGradientComputed_(false) {
125 std::string risk = parlist.sublist("SOL").sublist("Deviation Measure").get("Name","Variance");
127 switch(ed) {
129 quad_ = makePtr<MeanVarianceQuadrangle<Real>>(parlist); break;
131 quad_ = makePtr<TruncatedMeanQuadrangle<Real>>(parlist); break;
133 quad_ = makePtr<QuantileQuadrangle<Real>>(parlist); break;
135 quad_ = makePtr<MoreauYosidaCVaR<Real>>(parlist); break;
137 quad_ = makePtr<GenMoreauYosidaCVaR<Real>>(parlist); break;
139 quad_ = makePtr<LogExponentialQuadrangle<Real>>(parlist); break;
141 quad_ = makePtr<LogQuantileQuadrangle<Real>>(parlist); break;
143 quad_ = makePtr<SmoothedWorstCaseQuadrangle<Real>>(parlist); break;
144 default:
145 ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
146 "Invalid deviation measure type " << risk << "!");
147 }
148 }
149
150 void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
151 Ptr<const Vector<Real>> xvec = getConstVector(x);
152 obj_->update(*xvec,flag,iter);
153 isValueComputed_ = false;
154 isGradientComputed_ = false;
155 }
156
157 Real value( const Vector<Real> &x, Real &tol ) {
158 Ptr<const Vector<Real>> xvec = getConstVector(x);
159 Ptr<const std::vector<Real>> xstat = getConstStat(x);
160 getValue(*xvec,tol);
161 Real err = quad_->error(val_-(*xstat)[0],0);
162 return err;
163 }
164
165 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
166 Ptr<Vector<Real>> gvec = getVector(g);
167 Ptr<std::vector<Real>> gstat = getStat(g);
168 Ptr<const Vector<Real>> xvec = getConstVector(x);
169 Ptr<const std::vector<Real>> xstat = getConstStat(x);
170 getValue(*xvec,tol);
171 Real err = quad_->error(val_-(*xstat)[0],1);
172 getGradient(*xvec,tol);
173 gvec->set(*g_); gvec->scale(err);
174 (*gstat)[0] = -err;
175 }
176
177 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
178 Ptr<Vector<Real>> hvec = getVector(hv);
179 Ptr<std::vector<Real>> hstat = getStat(hv);
180 Ptr<const Vector<Real>> vvec = getConstVector(v);
181 Ptr<const std::vector<Real>> vstat = getConstStat(v);
182 Ptr<const Vector<Real>> xvec = getConstVector(x);
183 Ptr<const std::vector<Real>> xstat = getConstStat(x);
184 getValue(*xvec,tol);
185 Real err1 = quad_->error(val_-(*xstat)[0],1);
186 Real err2 = quad_->error(val_-(*xstat)[0],2);
187 getGradient(*xvec,tol);
188 //Real gv = vvec->dot(g_->dual());
189 Real gv = vvec->apply(*g_);
190 obj_->hessVec(*hvec,*vvec,*xvec,tol);
191 hvec->scale(err1); hvec->axpy(err2*(gv-(*vstat)[0]),*g_);
192 (*hstat)[0] = err2*((*vstat)[0]-gv);
193 }
194
195 void setParameter(const std::vector<Real> &param) {
196 obj_->setParameter(param);
198 }
199
200};
201
202}
203#endif
Provides the interface to evaluate objective functions.
virtual void setParameter(const std::vector< Real > &param)
Provides the interface for the progressive hedging deviation objective.
void getGradient(const Vector< Real > &x, Real &tol)
Ptr< ExpectationQuad< Real > > quad_
void setParameter(const std::vector< Real > &param)
Ptr< const Vector< Real > > getConstVector(const Vector< Real > &x) const
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Ptr< Vector< Real > > getVector(Vector< Real > &x) const
void getValue(const Vector< Real > &x, Real &tol)
const Ptr< Objective< Real > > obj_
Ptr< const std::vector< Real > > getConstStat(const Vector< Real > &x) const
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Ptr< std::vector< Real > > getStat(Vector< Real > &x) const
PH_DeviationObjective(const Ptr< Objective< Real > > &obj, ParameterList &parlist)
Ptr< std::vector< Real > > getStatistic(const int comp=0, const int index=0)
Ptr< const Vector< Real > > getVector(void) const
Defines the linear algebra or vector space interface.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
@ DEVIATIONMEASURE_TRUNCATEDMEANQUADRANGLE
@ DEVIATIONMEASURE_LOGEXPONENTIALQUADRANGLE
@ DEVIATIONMEASURE_MEANVARIANCEQUADRANGLE
@ DEVIATIONMEASURE_SMOOTHEDWORSTCASEQUADRANGLE
EDeviationMeasure StringToEDeviationMeasure(std::string s)