ROL
ROL_CompositeConstraint.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_COMPOSITECONSTRAINT_H
42 #define ROL_COMPOSITECONSTRAINT_H
43 
45 #include "ROL_Objective.hpp"
47 
48 namespace ROL {
49 
56 template<class Real>
58 private:
59 
60  typedef Vector<Real> V;
62  typedef typename PV::size_type size_type;
63 
64  const static size_type OPT = 0;
65  const static size_type SLACK = 1;
66 
67  const static size_type INEQ = 0;
68  const static size_type EQUAL = 1;
69 
70  Teuchos::RCP<InequalityConstraint<Real> > incon_;
71  Teuchos::RCP<EqualityConstraint<Real> > eqcon_;
72 
73  Teuchos::RCP<Vector<Real> > scratch_;
74 
75  bool hasEquality_; // True if an equality constraint is present
76  int ncval_; // Number of constraint evaluations
77 
78 
79 public:
80 
81  // Constructor with inequality and equality constraints
82  CompositeConstraint( const Teuchos::RCP<InequalityConstraint<Real> > &incon,
83  const Teuchos::RCP<EqualityConstraint<Real> > &eqcon,
84  const Teuchos::RCP<Vector<Real> > &x = Teuchos::null ) :
85  incon_(incon), eqcon_(eqcon), scratch_(Teuchos::null),
86  hasEquality_(true), ncval_(0) {
87  if( x != Teuchos::null ) {
88  scratch_ = x->dual().clone();
89  }
90  }
91 
92  // Constructor with inequality constraint only
93  CompositeConstraint( const Teuchos::RCP<InequalityConstraint<Real> > &incon,
94  const Teuchos::RCP<Vector<Real> > &x = Teuchos::null ) :
95  incon_(incon), eqcon_(Teuchos::null), scratch_(Teuchos::null),
96  hasEquality_(false), ncval_(0) {
97  if( x != Teuchos::null ) {
98  scratch_ = x->dual().clone();
99  }
100  }
101 
102 
104  return ncval_;
105  }
106 
107  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
108 
109  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
110 
111  Teuchos::RCP<const V> xo = xpv.get(OPT);
112  Teuchos::RCP<const V> xs = xpv.get(SLACK);
113 
114  incon_->update(*xo,flag,iter);
115 
116  if( hasEquality_ ) {
117  eqcon_->update(*xo,flag,iter);
118  }
119 
120  }
121 
122  void value( Vector<Real> &c, const Vector<Real> &x, Real &tol ) {
123 
124  PV &cpv = Teuchos::dyn_cast<PV>(c);
125  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
126 
127  Teuchos::RCP<const V> xo = xpv.get(OPT);
128  Teuchos::RCP<const V> xs = xpv.get(SLACK);
129 
130  Teuchos::RCP<V> ci = cpv.get(INEQ);
131  Teuchos::RCP<V> ce;
132 
133  incon_->value(*ci, *xo, tol);
134  ci->axpy(-1.0,*xs);
135 
136  if(hasEquality_) {
137  ce = cpv.get(EQUAL);
138  eqcon_->value(*ce, *xo, tol);
139  }
140 
141  ++ncval_;
142 
143  }
144 
146  const Vector<Real> &v,
147  const Vector<Real> &x,
148  Real &tol ) {
149 
150  using Teuchos::RCP; using Teuchos::dyn_cast;
151 
152  // Partition vectors and extract subvectors
153  const PV &xpv = dyn_cast<const PV>(x);
154  const PV &vpv = dyn_cast<const PV>(v);
155 
156  RCP<const V> xo = xpv.get(OPT);
157  RCP<const V> xs = xpv.get(SLACK);
158 
159  RCP<const V> vo = vpv.get(OPT);
160  RCP<const V> vs = vpv.get(SLACK);
161 
162  PV &jvpv = dyn_cast<PV>(jv);
163 
164  RCP<V> jvi = jvpv.get(INEQ);
165  incon_->applyJacobian(*jvi, *vo, *xo, tol);
166  jvi->axpy(-1.0,*vs);
167 
168  if(hasEquality_) {
169  RCP<V> jve = jvpv.get(EQUAL);
170  eqcon_->applyJacobian(*jve, *vo, *xo, tol);
171  }
172 
173  }
174 
176  const Vector<Real> &v,
177  const Vector<Real> &x,
178  Real &tol ) {
179 
180  using Teuchos::RCP; using Teuchos::dyn_cast;
181 
182  // Partition vectors and extract subvectors
183  const PV &xpv = dyn_cast<const PV>(x);
184  PV &ajvpv = dyn_cast<PV>(ajv);
185 
186  RCP<const V> xo = xpv.get(OPT);
187  RCP<const V> xs = xpv.get(SLACK);
188 
189  RCP<V> ajvo = ajvpv.get(OPT);
190  RCP<V> ajvs = ajvpv.get(SLACK);
191 
192  const PV &vpv = dyn_cast<const PV>(v);
193 
194  RCP<const V> vi = vpv.get(INEQ);
195 
196  incon_->applyAdjointJacobian(*ajvo,*vi,*xo,tol);
197 
198  ajvs->set(*vi);
199  ajvs->scale(-1.0);
200 
201  if(hasEquality_) {
202 
203  RCP<const V> ve = vpv.get(EQUAL);
204 
205  if( scratch_ == Teuchos::null ) {
206  scratch_ = ajvo->clone();
207  }
208  eqcon_->applyAdjointJacobian(*scratch_,*ve,*xo,tol);
209  ajvo->plus(*scratch_);
210 
211  }
212 
213  }
214 
216  const Vector<Real> &u,
217  const Vector<Real> &v,
218  const Vector<Real> &x,
219  Real &tol ) {
220 
221  using Teuchos::RCP; using Teuchos::dyn_cast;
222 
223  const PV &xpv = dyn_cast<const PV>(x);
224  const PV &vpv = dyn_cast<const PV>(v);
225  PV &ahuvpv = dyn_cast<PV>(ahuv);
226 
227  RCP<const V> xo = xpv.get(OPT);
228  RCP<const V> xs = xpv.get(SLACK);
229 
230  RCP<const V> vo = vpv.get(OPT);
231 
232  RCP<V> ahuvo = ahuvpv.get(OPT);
233  RCP<V> ahuvs = ahuvpv.get(SLACK);
234 
235  if( scratch_ == Teuchos::null ) {
236  scratch_ = ahuvo->clone();
237  }
238 
239  const PV &upv = dyn_cast<const PV>(u);
240 
241  RCP<const V> ui = upv.get(INEQ);
242 
243  incon_->applyAdjointHessian(*ahuvo,*ui,*vo,*xo,tol);
244  ahuvs->zero();
245 
246  if(hasEquality_) {
247  RCP<const V> ue = upv.get(EQUAL);
248  eqcon_->applyAdjointHessian(*scratch_,*ue,*vo,*xo,tol);
249  ahuvo->plus(*scratch_);
250  }
251 
252  }
253 
254  Teuchos::RCP<EqualityConstraint<Real> > getEqualityConstraint( void ) {
255  return eqcon_;
256  }
257 
258  Teuchos::RCP<InequalityConstraint<Real> > getInequalityConstraint( void ) {
259  return incon_;
260  }
261 
262 
263 // Definitions for parametrized (stochastic) equality constraints
264 public:
265  void setParameter(const std::vector<Real> &param) {
267  incon_->setParameter(param);
268  if( hasEquality_ ) {
269  eqcon_->setParameter(param);
270  }
271  }
272 }; // class CompositeConstraint
273 
274 } // namespace ROL
275 
276 #endif
Defines the linear algebra of vector space on a generic partitioned vector.
void setParameter(const std::vector< Real > &param)
virtual void setParameter(const std::vector< Real > &param)
CompositeConstraint(const Teuchos::RCP< InequalityConstraint< Real > > &incon, const Teuchos::RCP< Vector< Real > > &x=Teuchos::null)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
Teuchos::RCP< EqualityConstraint< Real > > getEqualityConstraint(void)
Defines the equality constraint operator interface.
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
Evaluate the constraint operator at .
Teuchos::RCP< const Vector< Real > > get(size_type i) const
Teuchos::RCP< InequalityConstraint< Real > > getInequalityConstraint(void)
Has both inequality and equality constraints. Treat inequality constraint as equality with slack vari...
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 ...
Teuchos::RCP< Vector< Real > > scratch_
std::vector< PV >::size_type size_type
PartitionedVector< Real > PV
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 .
Provides a unique argument for inequality constraints, which otherwise behave exactly as equality con...
Teuchos::RCP< EqualityConstraint< Real > > eqcon_
CompositeConstraint(const Teuchos::RCP< InequalityConstraint< Real > > &incon, const Teuchos::RCP< EqualityConstraint< Real > > &eqcon, const Teuchos::RCP< Vector< Real > > &x=Teuchos::null)
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 ...
Teuchos::RCP< InequalityConstraint< Real > > incon_