ROL
ROL_Constraint_Partitioned.hpp
Go to the documentation of this file.
1 // Rapid Optimization Library (ROL) Package
2 // Copyright (2014) Sandia Corporation
3 //
4 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
5 // license for use of this work by or on behalf of the U.S. Government.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // 1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 //
18 // 3. Neither the name of the Corporation nor the names of the
19 // contributors may be used to endorse or promote products derived from
20 // this software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
23 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
26 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34 // Questions? Contact lead developers:
35 // Drew Kouri (dpkouri@sandia.gov) and
36 // Denis Ridzal (dridzal@sandia.gov)
37 //
38 // ************************************************************************
39 // @HEADER
40 
41 #ifndef ROL_CONSTRAINT_PARTITIONED_H
42 #define ROL_CONSTRAINT_PARTITIONED_H
43 
45 #include "ROL_Constraint.hpp"
46 
47 namespace ROL {
48 
55 template<class Real>
56 class Constraint_Partitioned : public Constraint<Real> {
57 private:
58  std::vector<ROL::Ptr<Constraint<Real> > > cvec_;
59  std::vector<bool> isInequality_; // Label whether cvec_[i] is inequality
60  ROL::Ptr<Vector<Real> > scratch_; // Scratch vector for intermediate computation
61  int ncval_; // Number of constraint evaluations
62  bool initialized_; // Is scratch vector initialized?
63 
65  try {
66  return *dynamic_cast<PartitionedVector<Real>&>(xs).get(0);
67  }
68  catch (std::exception &e) {
69  return xs;
70  }
71  }
72 
73  const Vector<Real>& getOpt( const Vector<Real> &xs ) {
74  try {
75  return *dynamic_cast<const PartitionedVector<Real>&>(xs).get(0);
76  }
77  catch (std::exception &e) {
78  return xs;
79  }
80  }
81 
82  Vector<Real>& getSlack( Vector<Real> &xs, const int ind ) {
83  return *dynamic_cast<PartitionedVector<Real>&>(xs).get(ind);
84  }
85 
86  const Vector<Real>& getSlack( const Vector<Real> &xs, const int ind ) {
87  return *dynamic_cast<const PartitionedVector<Real>&>(xs).get(ind);
88  }
89 
90 
91 public:
92  Constraint_Partitioned(const std::vector<ROL::Ptr<Constraint<Real> > > &cvec,
93  bool isInequality = false)
94  : cvec_(cvec),
95  scratch_(ROL::nullPtr), ncval_(0), initialized_(false) {
96  isInequality_.clear(); isInequality_.resize(cvec.size(),isInequality);
97  }
98 
99  Constraint_Partitioned(const std::vector<ROL::Ptr<Constraint<Real> > > &cvec,
100  const std::vector<bool> &isInequality)
101  : cvec_(cvec), isInequality_(isInequality),
102  scratch_(ROL::nullPtr), ncval_(0), initialized_(false) {}
103 
105  return ncval_;
106  }
107 
108  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
109  const int ncon = static_cast<int>(cvec_.size());
110  for (int i = 0; i < ncon; ++i) {
111  cvec_[i]->update(getOpt(x),flag,iter);
112  }
113  }
114 
115  void value( Vector<Real> &c, const Vector<Real> &x, Real &tol ) {
117  = dynamic_cast<PartitionedVector<Real>&>(c);
118 
119  const int ncon = static_cast<int>(cvec_.size());
120  int cnt = 1;
121  for (int i = 0; i < ncon; ++i) {
122  cvec_[i]->value(*cpv.get(i), getOpt(x), tol);
123  if (isInequality_[i]) {
124  cpv.get(i)->axpy(static_cast<Real>(-1),getSlack(x,cnt));
125  cnt++;
126  }
127  }
128  ++ncval_;
129  }
130 
132  const Vector<Real> &v,
133  const Vector<Real> &x,
134  Real &tol ) {
136  = dynamic_cast<PartitionedVector<Real>&>(jv);
137 
138  const int ncon = static_cast<int>(cvec_.size());
139  int cnt = 1;
140  for (int i = 0; i < ncon; ++i) {
141  cvec_[i]->applyJacobian(*jvpv.get(i), getOpt(v), getOpt(x), tol);
142  if (isInequality_[i]) {
143  jvpv.get(i)->axpy(static_cast<Real>(-1),getSlack(v,cnt));
144  cnt++;
145  }
146  }
147  }
148 
150  const Vector<Real> &v,
151  const Vector<Real> &x,
152  Real &tol ) {
153  if (!initialized_) {
154  scratch_ = getOpt(ajv).clone();
155  initialized_ = true;
156  }
157 
158  const PartitionedVector<Real> &vpv
159  = dynamic_cast<const PartitionedVector<Real>&>(v);
160 
161  const int ncon = static_cast<int>(cvec_.size());
162  int cnt = 1;
163  getOpt(ajv).zero();
164  for (int i = 0; i < ncon; ++i) {
165  scratch_->zero();
166  cvec_[i]->applyAdjointJacobian(*scratch_, *vpv.get(i), getOpt(x), tol);
167  getOpt(ajv).plus(*scratch_);
168  if (isInequality_[i]) {
169  getSlack(ajv,cnt).set(*vpv.get(i));
170  getSlack(ajv,cnt).scale(static_cast<Real>(-1));
171  cnt++;
172  }
173  }
174  }
175 
177  const Vector<Real> &u,
178  const Vector<Real> &v,
179  const Vector<Real> &x,
180  Real &tol ) {
181  if (!initialized_) {
182  scratch_ = getOpt(ahuv).clone();
183  initialized_ = true;
184  }
185 
186  const PartitionedVector<Real> &upv
187  = dynamic_cast<const PartitionedVector<Real>&>(u);
188 
189  const int ncon = static_cast<int>(cvec_.size());
190  int cnt = 1;
191  getOpt(ahuv).zero();
192  for (int i = 0; i < ncon; ++i) {
193  ROL::Ptr<const Vector<Real> > ui = upv.get(i);
194  scratch_->zero();
195  cvec_[i]->applyAdjointHessian(*scratch_, *ui, getOpt(v), getOpt(x), tol);
196  getOpt(ahuv).plus(*scratch_);
197  if (isInequality_[i]) {
198  getSlack(ahuv,cnt).zero();
199  cnt++;
200  }
201  }
202  }
203 
205  const Vector<Real> &v,
206  const Vector<Real> &x,
207  const Vector<Real> &g,
208  Real &tol) {
210  = dynamic_cast<PartitionedVector<Real>&>(pv);
211  const PartitionedVector<Real> &vpv
212  = dynamic_cast<const PartitionedVector<Real>&>(v);
213 
214  const int ncon = static_cast<int>(cvec_.size());
215  for (int i = 0; i < ncon; ++i) {
216  cvec_[i]->applyPreconditioner(*pvpv.get(i), *vpv.get(i), getOpt(x), getOpt(g), tol);
217  }
218  }
219 
220 // Definitions for parametrized (stochastic) equality constraints
221 public:
222  void setParameter(const std::vector<Real> &param) {
224  const int ncon = static_cast<int>(cvec_.size());
225  for (int i = 0; i < ncon; ++i) {
226  cvec_[i]->setParameter(param);
227  }
228  }
229 }; // class Constraint_Partitioned
230 
231 } // namespace ROL
232 
233 #endif
ROL::Ptr< const Vector< Real > > get(size_type i) const
Vector< Real > & getSlack(Vector< Real > &xs, const int ind)
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 linear algebra of vector space on a generic partitioned vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Constraint_Partitioned(const std::vector< ROL::Ptr< Constraint< Real > > > &cvec, const std::vector< bool > &isInequality)
const Vector< Real > & getSlack(const Vector< Real > &xs, const int ind)
Has both inequality and equality constraints. Treat inequality constraint as equality with slack vari...
virtual void setParameter(const std::vector< Real > &param)
ROL::Ptr< Vector< Real > > scratch_
virtual 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...
Vector< Real > & getOpt(Vector< Real > &xs)
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 .
const Vector< Real > & getOpt(const Vector< Real > &xs)
Constraint_Partitioned(const std::vector< ROL::Ptr< Constraint< Real > > > &cvec, bool isInequality=false)
void applyJacobian(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the constraint Jacobian at , , to 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 ...
std::vector< ROL::Ptr< Constraint< Real > > > cvec_
void setParameter(const std::vector< Real > &param)
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
Evaluate the constraint operator at .
Defines the general constraint operator interface.