ROL
ROL_RiskNeutralObjective.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_RISKNEUTRALOBJECTIVE_HPP
45 #define ROL_RISKNEUTRALOBJECTIVE_HPP
46 
47 #include "Teuchos_RefCountPtr.hpp"
48 #include "ROL_Vector.hpp"
49 #include "ROL_Objective.hpp"
51 #include "ROL_SampleGenerator.hpp"
52 
53 namespace ROL {
54 
55 template<class Real>
56 class RiskNeutralObjective : public Objective<Real> {
57 private:
58  Teuchos::RCP<ParametrizedObjective<Real> > pObj_;
59  Teuchos::RCP<SampleGenerator<Real> > vsampler_;
60  Teuchos::RCP<SampleGenerator<Real> > gsampler_;
61 
62  Real value_;
63  Teuchos::RCP<Vector<Real> > gradient_;
64 
66 
67 public:
68  virtual ~RiskNeutralObjective() {}
69 
71  SampleGenerator<Real> &vsampler,
72  SampleGenerator<Real> &gsampler ) {
73  pObj_ = Teuchos::rcp(&pObj,false); // Parametrized Objective Function Object
74  vsampler_ = Teuchos::rcp(&vsampler,false); // Objective Function Value Sampler Object
75  gsampler_ = Teuchos::rcp(&gsampler,false); // Gradient Sampler Object
76  firstUpdate_ = true;
77  }
78 
79  // Delegating constructors require C++11.
80  //RiskNeutralObjective( ParametrizedObjective<Real> &pObj, SampleGenerator<Real> &sampler )
81  // : RiskNeutralObjective(pObj,sampler,sampler) {}
83  pObj_ = Teuchos::rcp(&pObj,false); // Parametrized Objective Function Object
84  vsampler_ = Teuchos::rcp(&sampler,false); // Objective Function Value Sampler Object
85  gsampler_ = Teuchos::rcp(&sampler,false); // Gradient Sampler Object
86  firstUpdate_ = true;
87  }
88 
89  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
90  if ( firstUpdate_ ) {
91  gradient_ = (x.dual()).clone();
92  }
93  pObj_->update(x,flag,iter);
94  vsampler_->update(x);
95  value_ = 0.0;
96  if ( flag ) {
97  gsampler_->update(x);
98  gradient_->zero();
99  }
100  }
101 
102  virtual Real value( const Vector<Real> &x, Real &tol ) {
103 // std::cout << " Initial value = " << value_ << "\n";
104  Real myval = 0.0, ptval = 0.0, val = 0.0, error = 2.0*tol + 1.0;
105  std::vector<Real> ptvals;
106  while ( error > tol ) {
107  vsampler_->refine();
108  for ( int i = vsampler_->start(); i < vsampler_->numMySamples(); i++ ) {
109  pObj_->setParameter(vsampler_->getMyPoint(i));
110  ptval = pObj_->value(x,tol);
111  myval += vsampler_->getMyWeight(i)*ptval;
112  ptvals.push_back(ptval);
113 // std::cout << " ptval-" << i << " = " << ptval
114 // << " weight-" << i << " = " << vsampler_->getMyWeight(i) << "\n";
115  }
116  error = vsampler_->computeError(ptvals);
117 // std::cout << " v error = " << error
118 // << " tol = " << tol
119 // << " myval = " << myval
120 // << " num points = " << vsampler_->numMySamples()
121 // << " start = " << vsampler_->start() << "\n";
122  ptvals.clear();
123  }
124  vsampler_->sumAll(&myval,&val,1);
125  value_ += val;
126 // std::cout << " Final value = " << value_ << "\n";
127  vsampler_->setSamples();
128  return value_;
129  }
130 
131  virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
132 // std::cout << " Initial norm(gradient) = " << gradient_->norm() << "\n";
133  g.zero();
134  Teuchos::RCP<Vector<Real> > ptg = g.clone(); ptg->zero();
135  Teuchos::RCP<Vector<Real> > myg = g.clone(); myg->zero();
136  std::vector<Teuchos::RCP<Vector<Real> > > ptgs;
137  Real error = 2.0*tol + 1.0;
138  while ( error > tol ) {
139  gsampler_->refine();
140  for ( int i = gsampler_->start(); i < gsampler_->numMySamples(); i++ ) {
141  pObj_->setParameter(gsampler_->getMyPoint(i));
142  pObj_->gradient(*ptg,x,tol);
143  myg->axpy(gsampler_->getMyWeight(i),*ptg);
144  ptgs.push_back(x.clone());
145  (ptgs.back())->set(*ptg);
146  }
147  error = gsampler_->computeError(ptgs,x);
148 // std::cout << " g error = " << error
149 // << " tol = " << tol
150 // << " norm(g) = " << myg->norm()
151 // << " num points = " << gsampler_->numMySamples()
152 // << " start = " << gsampler_->start()
153 // << "\n";
154  ptgs.clear();
155  }
156  gsampler_->sumAll(*myg,g);
157  gradient_->axpy(1.0,g);
158  g.set(*(gradient_));
159 // std::cout << " Final norm(gradient) = " << gradient_->norm() << "\n";
160  gsampler_->setSamples();
161  }
162 
163  virtual void hessVec( Vector<Real> &hv, const Vector<Real> &v,
164  const Vector<Real> &x, Real &tol ) {
165  hv.zero();
166  Teuchos::RCP<Vector<Real> > pth = hv.clone(); pth->zero();
167  Teuchos::RCP<Vector<Real> > myh = hv.clone(); myh->zero();
168  for ( int i = 0; i < gsampler_->numMySamples(); i++ ) {
169  pObj_->setParameter(gsampler_->getMyPoint(i));
170  pObj_->hessVec(*pth,v,x,tol);
171  myh->axpy(gsampler_->getMyWeight(i),*pth);
172  }
173  gsampler_->sumAll(*myh,hv);
174  }
175 
176  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
177  Pv.set(v.dual());
178  }
179 };
180 
181 }
182 
183 #endif
184 
185 // virtual Real value( const Vector<Real> &x, Real &tol ) {
186 // Real myval = 0.0, ptval = 0.0, val = 0.0;
187 // for ( unsigned i = 0; i < vsampler_->numMySamples(); i++ ) {
188 // pObj_->setParameter(vsampler_->getMyPoint(i));
189 // ptval = pObj_->value(x,tol);
190 // myval += vsampler_->getMyWeight(i)*ptval;
191 // }
192 // vsampler_->sumAll(&myval,&val,1);
193 // return val;
194 // }
195 
196 // virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
197 // g.zero();
198 // Teuchos::RCP<Vector<Real> > ptg = x.clone(); ptg->zero();
199 // Teuchos::RCP<Vector<Real> > myg = x.clone(); myg->zero();
200 // for ( unsigned i = 0; i < gsampler_->numMySamples(); i++ ) {
201 // pObj_->setParameter(gsampler_->getMyPoint(i));
202 // pObj_->gradient(*ptg,x,tol);
203 // myg->axpy(gsampler_->getMyWeight(i),*ptg);
204 // }
205 // gsampler_->sumAll(*myg,g);
206 // }
Provides the interface to evaluate objective functions.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:211
RiskNeutralObjective(ParametrizedObjective< Real > &pObj, SampleGenerator< Real > &vsampler, SampleGenerator< Real > &gsampler)
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:155
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
Teuchos::RCP< Vector< Real > > gradient_
RiskNeutralObjective(ParametrizedObjective< Real > &pObj, SampleGenerator< Real > &sampler)
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
virtual void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply preconditioner to vector.
Teuchos::RCP< SampleGenerator< Real > > gsampler_
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
virtual Real value(const Vector< Real > &x, Real &tol)
Compute value.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:194
Teuchos::RCP< SampleGenerator< Real > > vsampler_
Teuchos::RCP< ParametrizedObjective< Real > > pObj_