ROL
ROL_BinaryConstraint.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_BINARY_CONSTRAINT_H
45 #define ROL_BINARY_CONSTRAINT_H
46 
47 #include "ROL_BoundConstraint.hpp"
49 
58 namespace ROL {
59 
60 template<class Real>
61 class BinaryConstraint : public EqualityConstraint<Real> {
62 
63  template <typename T> using RCP = Teuchos::RCP<T>;
64 
65  using V = Vector<Real>;
66 
67 
68 private:
69 
70  const RCP<const V> lo_; // Lower Bound Vector
71  const RCP<const V> up_; // Upper Bound Vector
72 
73  RCP<V> d_; // Scratch Vector
74 
75 // RCP<V> dl_; // Scratch Vectors
76 // RCP<V> du_; // Scratch Vectors
77 
78  Real gamma_; // Penality parameter
79 
80 
81  class BoundsCheck : public Elementwise::BinaryFunction<Real> {
82 
83  private:
84 
85  int opt_;
86 
87  public:
88 
89  BoundsCheck( int option ) : opt_(option) {}
90 
91  Real apply( const Real &dl, const Real &du ) const {
92 
93  if( dl < ROL_INF<Real>() ) {
94  if( du < ROL_INF<Real>() ) {
95  switch(opt_) {
96  case 0: return dl*du; break;
97  case 1: return du-dl; break;
98  case 2: return -2.0; break;
99  default: return 0.0; break; // Should never be called
100  }
101  }
102  else { // dl finite, du infinite
103  switch(opt_) {
104  case 0: return dl; break;
105  case 1: return 1.0; break;
106  case 2: return 0.0; break;
107  default: return 0.0; break; // Should never be called
108  }
109  }
110  }
111  else { // dl infinite, du finite
112  if( du <ROL_INF<Real>() ) { // dl and du infinite
113  switch(opt_) {
114  case 0: return du; break;
115  case 1: return -1.0; break;
116  case 2: return 0.0; break;
117  default: return 0.0; break; // Should never be called
118  }
119  }
120  else {
121  return 0.0;
122  }
123  }
124  } // apply
125  }; // class BoundsCheck
126 
127 
128 public:
129 
130  BinaryConstraint( const RCP<const V> &lo, const RCP<const V> &up, Real gamma ) :
131  lo_( lo ), up_( up ), d_( lo_->clone() ), gamma_( gamma ) {}
132 
133  BinaryConstraint( const BoundConstraint<Real> &bnd, Real gamma ) :
134  BinaryConstraint( bnd.getLowerVectorRCP(), bnd.getUpperVectorRCP(), gamma ) {}
135 
136  BinaryConstraint( const RCP<const BoundConstraint<Real>> &bnd, Real gamma ) :
137  BinaryConstraint( bnd->getLowerVectorRCP(), bnd->getUpperVectorRCP(), gamma ) {}
138 
139 
149  void value(V &c, const V &x, Real &tol) {
150 
151  c.set( x );
152  c.axpy( -1.0, *lo_ ); // c = x-l
153 
154  d_->set( *up_ ); // d = u-x
155  d_->axpy( -1.0, x );
156 
157  BoundsCheck bc(0);
158  c.applyBinary( bc, *d_ );
159 
160  c.scale( gamma_ );
161 
162  }
163 
164 
174  void applyJacobian(V &jv, const V &v, const V &x, Real &tol) {
175 
176  Elementwise::Multiply<Real> mult;
177 
178  jv.set( x );
179  jv.axpy( -1.0, *lo_ );
180  d_->set( *up_ );
181  d_->axpy( -1.0, x );
182 
183  BoundsCheck bc(1);
184  jv.applyBinary( bc, *d_ );
185  jv.applyBinary( mult, v );
186  jv.scale( gamma_ );
187  }
188 
189 
190  void applyAdjointJacobian(V &ajv, const V &v, const V &x, Real &tol) {
191  applyJacobian(ajv,v,x,tol);
192  }
193 
194 
201  void applyAdjointHessian(V &ahuv, const V &u, const V &v, const V &x, Real &tol) {
202 
203  Elementwise::Multiply<Real> mult;
204 
205  ahuv.set( x );
206  ahuv.axpy( -1.0, *lo_ );
207  d_->set( *up_ );
208  d_->axpy( -1.0, x );
209 
210  BoundsCheck bc(2);
211  ahuv.applyBinary( bc, *d_ );
212  ahuv.applyBinary( mult, v );
213  ahuv.applyBinary( mult, u );
214 
215  ahuv.scale( gamma_ );
216 
217  }
218 
219  void setPenalty( Real gamma ) {
220  gamma_ = gamma;
221  }
222 };
223 
224 
225 } // namespace ROL
226 
227 
228 #endif // ROL_BINARY_CONSTRAINT_H
virtual void scale(const Real alpha)=0
Compute where .
Implements an equality constraint function that evaluates to zero on the surface of a bounded paralle...
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:145
virtual void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector &x)
Definition: ROL_Vector.hpp:224
const RCP< const V > lo_
void applyAdjointJacobian(V &ajv, const V &v, const V &x, Real &tol)
Apply the adjoint of the the constraint Jacobian at , , to vector .
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
const RCP< const V > up_
BinaryConstraint(const RCP< const BoundConstraint< Real >> &bnd, Real gamma)
Defines the equality constraint operator interface.
void value(V &c, const V &x, Real &tol)
Evaluate constraint .
Provides the interface to apply upper and lower bound constraints.
void applyAdjointHessian(V &ahuv, const V &u, const V &v, const V &x, Real &tol)
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:198
void applyJacobian(V &jv, const V &v, const V &x, Real &tol)
BinaryConstraint(const BoundConstraint< Real > &bnd, Real gamma)
Real apply(const Real &dl, const Real &du) const
BinaryConstraint(const RCP< const V > &lo, const RCP< const V > &up, Real gamma)