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