ROL
ROL_AlmostSureConstraint.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_ALMOST_SURE_CONSTRAINT_H
45 #define ROL_ALMOST_SURE_CONSTRAINT_H
46 
47 #include "ROL_RiskVector.hpp"
48 #include "ROL_Constraint.hpp"
49 #include "ROL_SampleGenerator.hpp"
50 #include "ROL_SimulatedVector.hpp"
51 
52 namespace ROL {
53 
54 template <class Real>
55 class AlmostSureConstraint : public Constraint<Real> {
56 private:
57  const ROL::Ptr<SampleGenerator<Real> > sampler_;
58  const ROL::Ptr<Constraint<Real> > con_;
59 
60  ROL::Ptr<Vector<Real> > scratch1_;
61  ROL::Ptr<Vector<Real> > scratch2_;
63 
64 public:
65  virtual ~AlmostSureConstraint() {}
66 
67  AlmostSureConstraint(const ROL::Ptr<SampleGenerator<Real> > &sampler,
68  const ROL::Ptr<Constraint<Real> > &con)
69  : sampler_(sampler), con_(con), isInitialized_(false) {}
70 
71  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {}
72 
74  const Vector<Real> &x,
75  Real &tol) {
76  c.zero();
77  SimulatedVector<Real> &pc = dynamic_cast<SimulatedVector<Real>&>(c);
78 
79  std::vector<Real> param;
80  for (int i = 0; i < sampler_->numMySamples(); ++i) {
81  param = sampler_->getMyPoint(i);
82  con_->setParameter(param);
83  con_->update(x);
84  con_->value(*(pc.get(i)), x, tol);
85  }
86  }
87 
89  const Vector<Real> &v,
90  const Vector<Real> &x,
91  Real &tol) {
92  jv.zero();
93  SimulatedVector<Real> &pjv = dynamic_cast<SimulatedVector<Real>&>(jv);
94 
95  std::vector<Real> param;
96  for (int i = 0; i < sampler_->numMySamples(); ++i) {
97  param = sampler_->getMyPoint(i);
98  con_->setParameter(param);
99  con_->update(x);
100  con_->applyJacobian(*(pjv.get(i)), v, x, tol);
101  }
102  }
103 
105  const Vector<Real> &v,
106  const Vector<Real> &x,
107  Real &tol) {
108  ajv.zero();
109  if (!isInitialized_) {
110  scratch1_ = ajv.clone();
111  scratch2_ = ajv.clone();
112  isInitialized_ = true;
113  }
114  const SimulatedVector<Real> &pv = dynamic_cast<const SimulatedVector<Real>&>(v);
115 
116  std::vector<Real> param;
117  scratch1_->zero(); scratch2_->zero();
118  for (int i = 0; i < sampler_->numMySamples(); ++i) {
119  param = sampler_->getMyPoint(i);
120  con_->setParameter(param);
121  con_->update(x);
122  con_->applyAdjointJacobian(*scratch1_, *(pv.get(i)), x, tol);
123  scratch2_->plus(*scratch1_);
124  }
125  sampler_->sumAll(*scratch2_, ajv);
126  }
127 
129  const Vector<Real> &u,
130  const Vector<Real> &v,
131  const Vector<Real> &x,
132  Real &tol) {
133  ahuv.zero();
134  if (!isInitialized_) {
135  scratch1_ = ahuv.clone();
136  scratch2_ = ahuv.clone();
137  isInitialized_ = true;
138  }
139  const SimulatedVector<Real> &pu = dynamic_cast<const SimulatedVector<Real>&>(u);
140 
141  std::vector<Real> param;
142  scratch1_->zero(); scratch2_->zero();
143  for (int i = 0; i < sampler_->numMySamples(); ++i) {
144  param = sampler_->getMyPoint(i);
145  con_->setParameter(param);
146  con_->update(x);
147  con_->applyAdjointHessian(*scratch1_, *(pu.get(i)), v, x, tol);
148  scratch2_->plus(*scratch1_);
149  }
150  sampler_->sumAll(*scratch2_, ahuv);
151  }
152 
154  const Vector<Real> &v,
155  const Vector<Real> &x,
156  const Vector<Real> &g,
157  Real &tol) {
158  Pv.zero();
159  SimulatedVector<Real> &ppv = dynamic_cast<SimulatedVector<Real>&>(Pv);
160  const SimulatedVector<Real> &pv = dynamic_cast<const SimulatedVector<Real>&>(v);
161 
162  std::vector<Real> param;
163  scratch1_->zero(); scratch2_->zero();
164  for (int i = 0; i < sampler_->numMySamples(); ++i) {
165  param = sampler_->getMyPoint(i);
166  con_->setParameter(param);
167  con_->update(x);
168  con_->applyPreconditioner(*(ppv.get(i)), *(pv.get(i)), x, g, tol);
169  }
170  }
171 
172 }; // class AlmostSureConstraint
173 
174 } // namespace ROL
175 
176 #endif
void applyAdjointJacobian(Vector< Real > &ajv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the adjoint of the the constraint Jacobian at , , to vector .
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
const ROL::Ptr< SampleGenerator< Real > > sampler_
ROL::Ptr< Vector< Real > > scratch1_
void applyAdjointHessian(Vector< Real > &ahuv, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the derivative of the adjoint of the constraint Jacobian at to vector in direction ...
ROL::Ptr< Vector< Real > > scratch2_
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Defines the linear algebra of a vector space on a generic partitioned vector where the individual vec...
ROL::Ptr< const Vector< Real > > get(size_type i) const
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
Evaluate the constraint operator at .
AlmostSureConstraint(const ROL::Ptr< SampleGenerator< Real > > &sampler, const ROL::Ptr< Constraint< Real > > &con)
void applyJacobian(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the constraint Jacobian at , , to vector .
const ROL::Ptr< Constraint< Real > > con_
void applyPreconditioner(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g, Real &tol)
Apply a constraint preconditioner at , , to vector . Ideally, this preconditioner satisfies the follo...
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update constraint functions. x is the optimization variable, flag = true if optimization variable is ...
Defines the general constraint operator interface.