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