ROL
ROL_PD_HMCR2.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_PD_HMCR2_HPP
45#define ROL_PD_HMCR2_HPP
46
48#include "ROL_Types.hpp"
49
50namespace ROL {
51
52template<class Real>
53class PD_HMCR2 : public PD_RandVarFunctional<Real> {
54private:
55 Real beta_;
57
58 Ptr<ScalarController<Real>> values_;
59 Ptr<ScalarController<Real>> gradvecs_;
60 Ptr<VectorController<Real>> gradients_;
61 Ptr<VectorController<Real>> hessvecs_;
62
63 using RandVarFunctional<Real>::val_;
64 using RandVarFunctional<Real>::g_;
65 using RandVarFunctional<Real>::gv_;
66 using RandVarFunctional<Real>::hv_;
68
69 using RandVarFunctional<Real>::point_;
71
76
81
82 void initializeStorage(void) {
83 values_ = makePtr<ScalarController<Real>>();
84 gradvecs_ = makePtr<ScalarController<Real>>();
85 gradients_ = makePtr<VectorController<Real>>();
86 hessvecs_ = makePtr<VectorController<Real>>();
87
90 }
91
92 void clear(void) {
93 gradvecs_->reset();
94 hessvecs_->reset();
95 }
96
97 void checkInputs(void) {
98 Real zero(0), one(1);
99 ROL_TEST_FOR_EXCEPTION((beta_ < zero) || (beta_ >= one), std::invalid_argument,
100 ">>> ERROR (ROL::PD_HMCR2): Confidence parameter beta is out of range!");
102 }
103
104public:
105 PD_HMCR2(const Real beta)
106 : PD_RandVarFunctional<Real>(), beta_(beta) {
107 checkInputs();
108 }
109
110 void setStorage(const Ptr<ScalarController<Real>> &value_storage,
111 const Ptr<VectorController<Real>> &gradient_storage) {
112 values_ = value_storage;
113 gradients_ = gradient_storage;
115 }
116
117 void setHessVecStorage(const Ptr<ScalarController<Real>> &gradvec_storage,
118 const Ptr<VectorController<Real>> &hessvec_storage) {
119 gradvecs_ = gradvec_storage;
120 hessvecs_ = hessvec_storage;
122 }
123
125 const Real zero(0), two(2);
126 Real val(0), lold(0), lnew(0), mdiff(0), gdiff(0), sum(0), gsum(0);
127 for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
128 values_->get(val, sampler.getMyPoint(i));
129 getMultiplier(lold, sampler.getMyPoint(i));
130 lnew = std::max(zero, getPenaltyParameter()*val+lold);
131 sum += sampler.getMyWeight(i) * std::pow(lnew,two);
132 }
133 sampler.sumAll(&sum,&gsum,1);
134 gsum = std::sqrt(gsum);
135 for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
136 values_->get(val, sampler.getMyPoint(i));
137 getMultiplier(lold, sampler.getMyPoint(i));
138 lnew = std::max(zero, getPenaltyParameter()*val+lold)/gsum;
139 mdiff += sampler.getMyWeight(i) * std::pow(lnew-lold,2);
140 setMultiplier(lnew, sampler.getMyPoint(i));
141 }
142 sampler.sumAll(&mdiff,&gdiff,1);
143 gdiff = std::sqrt(gdiff);
144 return gdiff;
145 }
146
147 void initialize(const Vector<Real> &x) {
149 mScalar1_ = static_cast<Real>(0);
150 mScalar2_ = static_cast<Real>(0);
151 clear();
152 }
153
155 const Vector<Real> &x,
156 const std::vector<Real> &xstat,
157 Real &tol) {
158 const Real zero(0), two(2);
159 Real lam(0);
160 getMultiplier(lam, point_);
161 Real val = computeValue(obj, x, tol);
162 Real arg = val - xstat[0];
163 Real pf = std::max(zero, arg + lam/getPenaltyParameter());
164 val_ += weight_ * std::pow(pf,two);
165 setValue(arg, point_);
166 }
167
168 Real getValue(const Vector<Real> &x,
169 const std::vector<Real> &xstat,
170 SampleGenerator<Real> &sampler) {
171 const Real half(0.5), one(1);
172 Real ev(0);
173 sampler.sumAll(&val_, &ev, 1);
174 Real norm = std::sqrt(ev);
175 Real sig = one/(one-beta_);
176 Real val = (norm <= sig/getPenaltyParameter()
177 ? half * getPenaltyParameter() * ev
178 : sig * (norm - sig*half/getPenaltyParameter()));
179 return xstat[0] + val;
180 }
181
183 const Vector<Real> &x,
184 const std::vector<Real> &xstat,
185 Real &tol) {
186 const Real zero(0), two(2);
187 Real lam(0);
188 getMultiplier(lam, point_);
189 Real val = computeValue(obj, x, tol);
190 Real arg = val - xstat[0];
191 Real pf = std::max(zero, arg + lam/getPenaltyParameter());
192 if ( pf > zero ) {
193 val_ += weight_ * pf;
194 gv_ += weight_ * std::pow(pf,two);
195 computeGradient(*dualVector_, obj, x, tol);
196 g_->axpy(weight_ * pf, *dualVector_);
197 }
198 }
199
201 std::vector<Real> &gstat,
202 const Vector<Real> &x,
203 const std::vector<Real> &xstat,
204 SampleGenerator<Real> &sampler) {
205 const Real one(1);
206 std::vector<Real> mv = {val_, gv_};
207 std::vector<Real> ev(2,0);
208 sampler.sumAll(&mv[0], &ev[0], 2);
209 Real norm = std::sqrt(ev[1]);
210 Real sig = one/(one-beta_);
211 Real scal = (norm <= sig/getPenaltyParameter()
213 : sig/norm);
214 gstat[0] = one - scal * ev[0];
215 sampler.sumAll(*g_, g);
216 g.scale(scal);
217 }
218
220 const Vector<Real> &v,
221 const std::vector<Real> &vstat,
222 const Vector<Real> &x,
223 const std::vector<Real> &xstat,
224 Real &tol) {
225 const Real zero(0), two(2);
226 Real lam(0);
227 getMultiplier(lam, point_);
228 Real val = computeValue(obj, x, tol);
229 Real arg = val - xstat[0];
230 Real pf = std::max(zero, arg + lam/getPenaltyParameter());
231 if ( pf > zero ) {
232 val_ += weight_ * std::pow(pf,two);
233 mScalar1_ += weight_ * pf;
234
235 Real gv = computeGradVec(*dualVector_, obj, v, x, tol);
236 mScalar2_ += weight_ * pf * gv;
237 gv_ += weight_ * (vstat[0] - gv);
238 g_->axpy(weight_ * pf, *dualVector_);
239 hv_->axpy(weight_ * (gv - vstat[0]), *dualVector_);
240 computeHessVec(*dualVector_, obj, v, x, tol);
241 hv_->axpy(weight_ * pf, *dualVector_);
242 }
243 }
244
246 std::vector<Real> &hvstat,
247 const Vector<Real> &v,
248 const std::vector<Real> &vstat,
249 const Vector<Real> &x,
250 const std::vector<Real> &xstat,
251 SampleGenerator<Real> &sampler) {
252 const Real one(1);
253 std::vector<Real> mv = {val_, gv_, mScalar1_, mScalar2_};
254 std::vector<Real> ev(4,0);
255 sampler.sumAll(&mv[0],&ev[0],4);
256 Real norm = std::sqrt(ev[0]);
257 Real sig = one/(one-beta_);
258 Real scal = (norm <= sig/getPenaltyParameter()
260 : sig/norm);
261 hvstat[0] = scal * ev[1];
262 sampler.sumAll(*hv_,hv);
263 hv.scale(scal);
264 if (norm > sig/getPenaltyParameter()) {
265 Real norm3 = ev[0]*norm;
266 hvstat[0] += sig/norm3 * (ev[3] - ev[2]*vstat[0]) * ev[2];
267 dualVector_->zero();
268 sampler.sumAll(*g_,*dualVector_);
269 hv.axpy(sig/norm3 * (ev[2]*vstat[0] - ev[3]),*dualVector_);
270 }
271 }
272};
273
274}
275
276#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0 zero)()
Contains definitions of custom data types in ROL.
Provides the interface to evaluate objective functions.
void clear(void)
void setHessVecStorage(const Ptr< ScalarController< Real > > &gradvec_storage, const Ptr< VectorController< Real > > &hessvec_storage)
Ptr< VectorController< Real > > hessvecs_
Ptr< VectorController< Real > > gradients_
Ptr< ScalarController< Real > > values_
Real computeDual(SampleGenerator< Real > &sampler)
void initializeStorage(void)
void getGradient(Vector< Real > &g, std::vector< Real > &gstat, const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
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.
void initialize(const Vector< Real > &x)
Initialize temporary variables.
void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
void getHessVec(Vector< Real > &hv, std::vector< Real > &hvstat, const Vector< Real > &v, const std::vector< Real > &vstat, const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
void checkInputs(void)
PD_HMCR2(const Real beta)
void setStorage(const Ptr< ScalarController< Real > > &value_storage, const Ptr< VectorController< Real > > &gradient_storage)
void updateGradient(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for gradient computation.
Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
Ptr< ScalarController< Real > > gradvecs_
void getMultiplier(Real &lam, const std::vector< Real > &pt) const
void setMultiplier(Real &lam, const std::vector< Real > &pt)
virtual void setHessVecStorage(const Ptr< ScalarController< Real > > &gradvec_storage, const Ptr< VectorController< Real > > &hessvec_storage)
void setValue(const Real val, const std::vector< Real > &pt)
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
virtual void setStorage(const Ptr< ScalarController< Real > > &value_storage, const Ptr< VectorController< Real > > &gradient_storage)
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)
virtual void setStorage(const Ptr< ScalarController< Real > > &value_storage, const Ptr< VectorController< Real > > &gradient_storage)
void computeGradient(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Ptr< Vector< Real > > dualVector_
virtual void setHessVecStorage(const Ptr< ScalarController< Real > > &gradvec_storage, const Ptr< VectorController< Real > > &hessvec_storage)
Real computeGradVec(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
virtual int numMySamples(void) const
virtual std::vector< Real > getMyPoint(const int i) const
void sumAll(Real *input, Real *output, int dim) const
virtual Real getMyWeight(const int i) const
Defines the linear algebra or vector space interface.
virtual void scale(const Real alpha)=0
Compute where .
virtual void axpy(const Real alpha, const Vector &x)
Compute where .