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 Ptr<SampleGenerator<Real>> sampler_;
58  const Ptr<Constraint<Real>> con_;
59 
60  Ptr<Vector<Real>> scratch1_;
61  Ptr<Vector<Real>> scratch2_;
63 
64 public:
65  virtual ~AlmostSureConstraint() {}
66 
68  const 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  con_->update(x,flag,iter);
73  }
74  void update( const Vector<Real> &x, UpdateType type, int iter = -1 ) {
75  con_->update(x,type,iter);
76  }
77 
79  const Vector<Real> &x,
80  Real &tol) {
81  c.zero();
82  SimulatedVector<Real> &pc = dynamic_cast<SimulatedVector<Real>&>(c);
83 
84  std::vector<Real> param;
85  for (int i = 0; i < sampler_->numMySamples(); ++i) {
86  param = sampler_->getMyPoint(i);
87  con_->setParameter(param);
88  con_->update(x);
89  con_->value(*(pc.get(i)), x, tol);
90  }
91  }
92 
94  const Vector<Real> &v,
95  const Vector<Real> &x,
96  Real &tol) {
97  jv.zero();
98  SimulatedVector<Real> &pjv = dynamic_cast<SimulatedVector<Real>&>(jv);
99 
100  std::vector<Real> param;
101  for (int i = 0; i < sampler_->numMySamples(); ++i) {
102  param = sampler_->getMyPoint(i);
103  con_->setParameter(param);
104  con_->update(x);
105  con_->applyJacobian(*(pjv.get(i)), v, x, tol);
106  }
107  }
108 
110  const Vector<Real> &v,
111  const Vector<Real> &x,
112  Real &tol) {
113  ajv.zero();
114  if (!isInitialized_) {
115  scratch1_ = ajv.clone();
116  scratch2_ = ajv.clone();
117  isInitialized_ = true;
118  }
119  const SimulatedVector<Real> &pv = dynamic_cast<const SimulatedVector<Real>&>(v);
120 
121  std::vector<Real> param;
122  scratch1_->zero(); scratch2_->zero();
123  for (int i = 0; i < sampler_->numMySamples(); ++i) {
124  param = sampler_->getMyPoint(i);
125  con_->setParameter(param);
126  con_->update(x);
127  con_->applyAdjointJacobian(*scratch1_, *(pv.get(i)), x, tol);
128  scratch2_->plus(*scratch1_);
129  }
130  sampler_->sumAll(*scratch2_, ajv);
131  }
132 
134  const Vector<Real> &u,
135  const Vector<Real> &v,
136  const Vector<Real> &x,
137  Real &tol) {
138  ahuv.zero();
139  if (!isInitialized_) {
140  scratch1_ = ahuv.clone();
141  scratch2_ = ahuv.clone();
142  isInitialized_ = true;
143  }
144  const SimulatedVector<Real> &pu = dynamic_cast<const SimulatedVector<Real>&>(u);
145 
146  std::vector<Real> param;
147  scratch1_->zero(); scratch2_->zero();
148  for (int i = 0; i < sampler_->numMySamples(); ++i) {
149  param = sampler_->getMyPoint(i);
150  con_->setParameter(param);
151  con_->update(x);
152  con_->applyAdjointHessian(*scratch1_, *(pu.get(i)), v, x, tol);
153  scratch2_->plus(*scratch1_);
154  }
155  sampler_->sumAll(*scratch2_, ahuv);
156  }
157 
159  const Vector<Real> &v,
160  const Vector<Real> &x,
161  const Vector<Real> &g,
162  Real &tol) {
163  Pv.zero();
164  SimulatedVector<Real> &ppv = dynamic_cast<SimulatedVector<Real>&>(Pv);
165  const SimulatedVector<Real> &pv = dynamic_cast<const SimulatedVector<Real>&>(v);
166 
167  std::vector<Real> param;
168  scratch1_->zero(); scratch2_->zero();
169  for (int i = 0; i < sampler_->numMySamples(); ++i) {
170  param = sampler_->getMyPoint(i);
171  con_->setParameter(param);
172  con_->update(x);
173  con_->applyPreconditioner(*(ppv.get(i)), *(pv.get(i)), x, g, tol);
174  }
175  }
176 
177 }; // class AlmostSureConstraint
178 
179 } // namespace ROL
180 
181 #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.
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 ...
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 .
const Ptr< SampleGenerator< Real > > sampler_
const 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 .
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, UpdateType type, int iter=-1)
Update constraint function.
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 ...
AlmostSureConstraint(const Ptr< SampleGenerator< Real >> &sampler, const Ptr< Constraint< Real >> &con)
Defines the general constraint operator interface.