ROL
ROL_Constraints.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_CONSTRAINTS_H
45 #define ROL_CONSTRAINTS_H
46 
47 #include "ROL_Vector.hpp"
48 #include "ROL_BoundConstraint.hpp"
50 #include "ROL_Types.hpp"
51 #include <iostream>
52 
53 namespace ROL {
54 
55 template <class Real>
56 class Constraints {
57 private:
58  bool activated_;
59 
60  std::vector<Teuchos::RCP<BoundConstraint<Real> > > ic_;
61  std::vector<Teuchos::RCP<EqualityConstraint<Real> > > ec_;
62 
63 public:
64 
65  virtual ~Constraints() {}
66 
67  Constraints(void) : activated_(true) {}
68 
69  Constraints(std::vector<Teuchos::RCP<BoundConstraint<Real> > > & ic) : activated_(true), ic_(ic) {}
70 
71  Constraints(Teuchos::RCP<BoundConstraint<Real> > & ic) : activated_(true) {
72  ic_.clear();
73  ic_.push_back(ic);
74  }
75 
76  Constraints(std::vector<Teuchos::RCP<EqualityConstraint<Real> > > & ec) : activated_(true), ec_(ec) {}
77 
78  Constraints(Teuchos::RCP<EqualityConstraint<Real> > & ec) : activated_(true) {
79  ec_.clear();
80  ec_.push_back(ec);
81  }
82 
83  Constraints(std::vector<Teuchos::RCP<BoundConstraint<Real> > > & ic, std::vector<Teuchos::RCP<EqualityConstraint<Real> > > & ec) : activated_(true), ic_(ic), ec_(ec) {}
84 
85  Constraints(Teuchos::RCP<BoundConstraint<Real> > &ic, Teuchos::RCP<EqualityConstraint<Real> > & ec) : activated_(true) {
86  ic_.clear();
87  ic_.push_back(ic);
88  ec_.clear();
89  ec_.push_back(ec);
90  }
91 
92  Constraints(Teuchos::RCP<BoundConstraint<Real> > &ic, std::vector<Teuchos::RCP<EqualityConstraint<Real> > > & ec) : activated_(true), ec_(ec) {
93  ic_.clear();
94  ic_.push_back(ic);
95  }
96 
97  Constraints(std::vector<Teuchos::RCP<BoundConstraint<Real> > > &ic, Teuchos::RCP<EqualityConstraint<Real> > & ec) : activated_(true), ic_(ic) {
98  ec_.clear();
99  ec_.push_back(ec);
100  }
101 
107  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
108  for (unsigned i=0; i<this->ic_.size(); i++) {
109  if (this->ic_[i]->isActivated()) {
110  this->ic_[i]->update(x, flag, iter);
111  }
112  }
113  }
114 
118  void project( Vector<Real> &x ) {
119  for (unsigned i=0; i<this->ic_.size(); i++) {
120  if (this->ic_[i]->isActivated()) {
121  this->ic_[i]->project(x);
122  }
123  }
124  }
125  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
126  for (unsigned i=0; i<this->ic_.size(); i++) {
127  if (this->ic_[i]->isActivated()) {
128  this->ic_[i]->pruneUpperActive(v, g, x, eps);
129  }
130  }
131  }
132  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
133  for (unsigned i=0; i<this->ic_.size(); i++) {
134  if (this->ic_[i]->isActivated()) {
135  this->ic_[i]->pruneLowerActive(v, g, x, eps);
136  }
137  }
138  }
139  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
140  for (unsigned i=0; i<this->ic_.size(); i++) {
141  if (this->ic_[i]->isActivated()) {
142  this->ic_[i]->pruneUpperActive(v, x, eps);
143  }
144  }
145  }
146  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
147  for (unsigned i=0; i<this->ic_.size(); i++) {
148  if (this->ic_[i]->isActivated()) {
149  this->ic_[i]->pruneLowerActive(v, x, eps);
150  }
151  }
152  }
154  if (this->ic_[0]->isActivated()) {
155  this->ic_[0]->setVectorToUpperBound(u);
156  }
157  }
159  if (this->ic_[0]->isActivated()) {
160  this->ic_[0]->setVectorToLowerBound(l);
161  }
162  }
163 
170  void pruneActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
171  for (unsigned i=0; i<this->ic_.size(); i++) {
172  if (this->ic_[i]->isActivated()) {
173  this->ic_[i]->pruneActive(v, g, x, eps);
174  }
175  }
176  }
177 
183  void pruneActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
184  for (unsigned i=0; i<this->ic_.size(); i++) {
185  if (this->ic_[i]->isActivated()) {
186  this->ic_[i]->pruneActive(v, x, eps);
187  }
188  }
189  }
190 
193  bool isFeasible( const Vector<Real> &v ) {
194  bool iFeas = true;
195  for (unsigned i=0; i<this->ic_.size(); i++) {
196  if (this->ic_[i]->isActivated()) {
197  iFeas = iFeas && (this->ic_[i]->isFeasible(v));
198  }
199  }
200  return iFeas;
201  }
202 
203 
208  void value(std::vector<Teuchos::RCP<Vector<Real> > > &c, const Vector<Real> &x, const EConstraint & ctype, std::vector<Real> & tol) {
209  if (ctype == CONSTRAINT_EQUALITY) {
210  for (unsigned i=0; i<size(this->ec_.size()); i++) {
211  if (this->ec_[i]->isActivated()) {
212  this->ec_[i]->value(*(c[i]), x, tol[i]);
213  }
214  }
215  }
216  else if (ctype == CONSTRAINT_INEQUALITY) {
217  for (unsigned i=0; i<size(this->ic_.size()); i++) {
218  if (this->ic_[i]->isActivated()) {
219  this->ic_[i]->value(*(c[i]), x, tol[i]);
220  }
221  }
222  }
223  }
224 
225 
230  void applyJacobian(std::vector<Teuchos::RCP<Vector<Real> > > &jv,
231  std::vector<Teuchos::RCP<const Vector<Real> > > &v,
232  const Vector<Real> &x,
233  const bool &adj,
234  const EConstraint &ctype,
235  std::vector<Real> &tol) {
236  if (ctype == CONSTRAINT_EQUALITY) {
237  for (unsigned i=0; i<size(this->ec_.size()); i++) {
238  if (this->ec_[i]->isActivated()) {
239  this->ec_[i]->applyJacobian(*(jv[i]), *(v[i]), x, adj, tol[i]);
240  }
241  }
242  }
243  else if (ctype == CONSTRAINT_INEQUALITY) {
244  for (unsigned i=0; i<size(this->ic_.size()); i++) {
245  if (this->ic_[i]->isActivated()) {
246  this->ic_[i]->applyJacobian(*(jv[i]), *(v[i]), x, adj, tol[i]);
247  }
248  }
249  }
250  }
251 
252 
258  void maptoJacobianKernel(std::vector<Teuchos::RCP<Vector<Real> > > &wv,
259  std::vector<Teuchos::RCP<const Vector<Real> > > &v,
260  const Vector<Real> &x,
261  const EConstraint &ctype,
262  std::vector<Real> &tol) {
263  if (ctype == CONSTRAINT_EQUALITY) {
264  for (unsigned i=0; i<size(this->ec_.size()); i++) {
265  if (this->ec_[i]->isActivated()) {
266  this->ec_[i]->maptoJacobianKernel(*(wv[i]), *(v[i]), x, tol[i]);
267  }
268  }
269  }
270  else if (ctype == CONSTRAINT_INEQUALITY) {
271  for (unsigned i=0; i<size(this->ic_.size()); i++) {
272  if (this->ic_[i]->isActivated()) {
273  this->ic_[i]->maptoJacobianKernel(*(wv[i]), *(v[i]), x, tol[i]);
274  }
275  }
276  }
277  }
278 
279 
284  void applyHessian(std::vector<Teuchos::RCP<Vector<Real> > > &huv,
285  std::vector<Teuchos::RCP<const Vector<Real> > > &u,
286  std::vector<Teuchos::RCP<const Vector<Real> > > &v,
287  const Vector<Real> &x,
288  const EConstraint &ctype,
289  std::vector<Real> &tol) {
290  if (ctype == CONSTRAINT_EQUALITY) {
291  for (unsigned i=0; i<size(this->ec_.size()); i++) {
292  if (this->ec_[i]->isActivated()) {
293  this->ec_[i]->applyHessian(*(huv[i]), *(u[i]), *(v[i]), x, tol[i]);
294  }
295  }
296  }
297  else if (ctype == CONSTRAINT_INEQUALITY) {
298  for (unsigned i=0; i<size(this->ic_.size()); i++) {
299  if (this->ic_[i]->isActivated()) {
300  this->ic_[i]->applyHessian(*(huv[i]), *(u[i]), *(v[i]), x, tol[i]);
301  }
302  }
303  }
304  }
305 
306 
313  void pruneInactive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
314  Teuchos::RCP<Vector<Real> > tmp = x.clone();
315  tmp->set(v);
316  this->pruneActive(*tmp,g,x,eps);
317  v.axpy(-1.0,*tmp);
318  }
319 
325  void pruneInactive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
326  Teuchos::RCP<Vector<Real> > tmp = x.clone();
327  tmp->set(v);
328  this->pruneActive(*tmp,x,eps);
329  v.axpy(-1.0,*tmp);
330  }
331 
337  Teuchos::RCP<Vector<Real> > tmp = g.clone();
338  tmp->set(g);
339  this->pruneActive(g,*tmp,x);
340  }
341 
347  v.plus(x);
348  this->project(v);
349  v.axpy(-1.0,x);
350  }
351 
354  void activate(void) { this->activated_ = true; }
355 
358  void deactivate(void) { this->activated_ = false; }
359 
362  bool isActivated(void) { return this->activated_; }
363 
364 }; // class Constraints
365 
366 } // namespace ROL
367 
368 #endif
void value(std::vector< Teuchos::RCP< Vector< Real > > > &c, const Vector< Real > &x, const EConstraint &ctype, std::vector< Real > &tol)
Evaluate constraints: either for all activated equality constraints or for all activated inequality c...
std::vector< Teuchos::RCP< EqualityConstraint< Real > > > ec_
void pruneActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Remove active set variables that are also in the binding set. v is the vector to be pruned g is the g...
virtual void plus(const Vector &x)=0
Compute , where .
void computeProjectedStep(Vector< Real > &v, const Vector< Real > &x)
Compute projected step P(x+v)-x. v is the step vector x is the optimization variables.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:143
Constraints(std::vector< Teuchos::RCP< BoundConstraint< Real > > > &ic)
void deactivate(void)
Turn off constraints.
Contains definitions of custom data types in ROL.
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Constraints(Teuchos::RCP< BoundConstraint< Real > > &ic, std::vector< Teuchos::RCP< EqualityConstraint< Real > > > &ec)
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void computeProjectedGradient(Vector< Real > &g, const Vector< Real > &x)
Compute projected gradient. g is the gradient of the objective function at x x is the optimization va...
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
std::vector< Teuchos::RCP< BoundConstraint< Real > > > ic_
void setVectorToLowerBound(Vector< Real > &l)
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0.0)
Constraints(std::vector< Teuchos::RCP< EqualityConstraint< Real > > > &ec)
Constraints(std::vector< Teuchos::RCP< BoundConstraint< Real > > > &ic, std::vector< Teuchos::RCP< EqualityConstraint< Real > > > &ec)
Defines the equality constraint operator interface.
void pruneActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0.0)
Remove active set variables. v is the vector to be pruned x is the optimization variable eps is the a...
void pruneInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=0.0)
Remove the inactive set variables. v is the vector to be pruned x is the optimization variable eps is...
void project(Vector< Real > &x)
Project optimization variables onto constraint set. x is the optimization variable.
void applyJacobian(std::vector< Teuchos::RCP< Vector< Real > > > &jv, std::vector< Teuchos::RCP< const Vector< Real > > > &v, const Vector< Real > &x, const bool &adj, const EConstraint &ctype, std::vector< Real > &tol)
Apply constraint Jacobians or their adjoints: either for all activated equality constraints or for al...
bool isActivated(void)
Check if constraints are on.
Constraints(Teuchos::RCP< BoundConstraint< Real > > &ic, Teuchos::RCP< EqualityConstraint< Real > > &ec)
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0.0)
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Provides the interface to apply upper and lower bound constraints.
Constraints(Teuchos::RCP< BoundConstraint< Real > > &ic)
Constraints(Teuchos::RCP< EqualityConstraint< Real > > &ec)
void pruneInactive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Remove the inactive set variables that are not in the binding set. v is the vector to be pruned g is ...
void setVectorToUpperBound(Vector< Real > &u)
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update constraint functions. x is the optimization variable, flag = true if x is changed...
Constraints(std::vector< Teuchos::RCP< BoundConstraint< Real > > > &ic, Teuchos::RCP< EqualityConstraint< Real > > &ec)
void maptoJacobianKernel(std::vector< Teuchos::RCP< Vector< Real > > > &wv, std::vector< Teuchos::RCP< const Vector< Real > > > &v, const Vector< Real > &x, const EConstraint &ctype, std::vector< Real > &tol)
Compute the action of the operators W that are onto the null space (kernel) of the contraint Jacobian...
void activate(void)
Turn on constraints.
EConstraint
Enumeration of constraint types.
Definition: ROL_Types.hpp:1037
bool isFeasible(const Vector< Real > &v)
Check if the vector, v, is feasible.
void applyHessian(std::vector< Teuchos::RCP< Vector< Real > > > &huv, std::vector< Teuchos::RCP< const Vector< Real > > > &u, std::vector< Teuchos::RCP< const Vector< Real > > > &v, const Vector< Real > &x, const EConstraint &ctype, std::vector< Real > &tol)
Apply constraint Hessians to (v,u), c&#39;&#39;(x)(v,u) = (c&#39;&#39;(x)u)v: either for all activated equality const...