ROL
function/constraint/test_03.cpp
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 
48 #include <cmath>
49 
50 #include "ROL_RandomVector.hpp"
51 #include "ROL_StdVector.hpp"
53 #include "ROL_StdConstraint.hpp"
54 
55 #include "ROL_Stream.hpp"
56 #include "Teuchos_GlobalMPISession.hpp"
57 
58 using RealT = double;
59 using VectorT = std::vector<RealT>;
60 
61 
62 /* constraint function with (range) = 1 + dim(domain)
63  */
64 class InnerConstraint : public ROL::StdConstraint<RealT> {
65 public:
66  InnerConstraint( int n ) : y_(n+1), sin_(n+1), cos_(n+1), n_(n), m_(n+1) {}
67 
68  void update( const VectorT& x,
69  ROL::UpdateType type,
70  int iter ) override {
71  for( int k=0; k<m_; ++k ) {
72  y_[k] = 0;
73  if( k<n_ ) y_[k] += x[k]*x[k];
74  if( k>0 ) y_[k] -= 2*x[k-1];
75  sin_[k] = std::sin(y_[k]);
76  cos_[k] = std::cos(y_[k]);
77  }
78  }
79 
80  void value( VectorT& c,
81  const VectorT& x,
82  RealT& tol ) override {
83  for( int k=0; k<m_; ++k ) c[k] = std::cos(y_[k]);
84  }
85 
86  void applyJacobian( VectorT& jv,
87  const VectorT& v,
88  const VectorT& x,
89  RealT& tol ) override {
90  for( int k=0; k<m_; ++k ) {
91  jv[k] = 0;
92  if( k<n_ ) jv[k] -= 2*v[k]*x[k]*sin_[k];
93  if( k>0 ) jv[k] += 2*v[k-1]*sin_[k];
94  }
95  }
96 
98  const VectorT& l,
99  const VectorT& x,
100  RealT& tol ) override {
101  for( int i=0; i<n_; ++i ) {
102  ajl[i] = 2*(l[i+1]*sin_[i+1] - l[i]*x[i]*sin_[i]);
103  }
104  }
105 
107  const VectorT& l,
108  const VectorT& v,
109  const VectorT& x,
110  RealT& tol ) override {
111  ahlv[0] = -4*cos_[0]*l[0]*v[0]*x[0]*x[0] - 4*cos_[1]*l[1]*v[0] + 4*cos_[1]*l[1]*v[1]*x[1] - 2*l[0]*sin_[0]*v[0];
112  for(int i=0; i<n_-1; ++i) {
113  ahlv[i] = 4*cos_[i]*l[i]*v[i-1]*x[i] - 4*cos_[i]*l[i]*v[i]*x[i]*x[i] - 4*cos_[i+1]*l[i+1]*v[i] + 4*cos_[i+1]*l[i+1]*v[i+1]*x[i+1] - 2*l[i]*sin_[i]*v[i];
114  }
115  ahlv[n_-1] = 4*cos_[n_-1]*l[n_-1]*v[n_-2]*x[n_-1] - 4*cos_[n_-1]*l[n_-1]*v[n_-1]*x[n_-1]*x[n_-1] - 4*cos_[n_]*l[n_]*v[n_-1] - 2*l[n_-1]*sin_[n_-1]*v[n_-1];
116  }
117 private:
119  int n_, m_;
120 
121 };
122 
123 /* constraint function with dim(range) = dim(domain) - 2
124  */
125 class OuterConstraint : public ROL::StdConstraint<RealT> {
126 public:
127  OuterConstraint( int n ): d_(n-2), dv_(n-2), n_(n), m_(n-2) {}
128 
129  void update( const VectorT& x,
130  ROL::UpdateType type,
131  int iter ) override {
132  for(int i=0; i<m_; ++i)
133  d_[i] = -x[i] + 2*x[i+1] - x[i+2];
134  }
135 
136  void value( VectorT& c,
137  const VectorT& x,
138  RealT& tol ) override {
139  for(int i=0; i<m_; ++i)
140  c[i] = d_[i]*d_[i];
141  }
142 
143  void applyJacobian( VectorT& jv,
144  const VectorT& v,
145  const VectorT& x,
146  RealT& tol ) override {
147  for(int i=0; i<m_; ++i)
148  jv[i] = 2*d_[i]*(-v[i]+2*v[i+1]-v[i+2]);
149  }
150 
152  const VectorT& l,
153  const VectorT& x,
154  RealT& tol ) override {
155  for(int i=0; i<n_; ++i) {
156  ajl[i] = 0;
157  for( int j=0; j<m_; ++j ) {
158  if( j == i-1 ) ajl[i] += 4*d_[j]*l[j];
159  else if( std::abs(i-j-1) == 1) ajl[i] -= 2*d_[j]*l[j];
160  }
161  }
162  }
163 
165  const VectorT& l,
166  const VectorT& v,
167  const VectorT& x,
168  RealT& tol ) override {
169  for(int i=0; i<m_; ++i)
170  dv_[i] = -v[i] + 2*v[i+1] - v[i+2];
171  for(int i=0; i<n_; ++i) {
172  ahlv[i] = 0;
173  for( int j=0; j<m_; ++j ) {
174  if( j == i-1 ) ahlv[i] += 4*dv_[j]*l[j];
175  else if( std::abs(i-j-1) == 1) ahlv[i] -= 2*dv_[j]*l[j];
176  }
177  }
178  }
179 
180 private:
182  int n_, m_;
183 
184 };
185 
186 
187 
188 
189 int main(int argc, char *argv[]) {
190 
191  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
192 
193  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
194  int iprint = argc - 1;
195  ROL::Ptr<std::ostream> outStream;
196  ROL::nullstream bhs; // outputs nothing
197  if (iprint > 0)
198  outStream = ROL::makePtrFromRef(std::cout);
199  else
200  outStream = ROL::makePtrFromRef(bhs);
201 
202  // Save the format state of the original std::cout.
203  ROL::nullstream oldFormatState;
204  oldFormatState.copyfmt(std::cout);
205 
206 // RealT errtol = std::sqrt(ROL::ROL_THRESHOLD<RealT>());
207 
208  int errorFlag = 0;
209 
210  // *** Test body.
211 
212  try {
213 
214  int x_dim = 7; // Inner constraint domain space dimension
215  int ci_dim = x_dim + 1; // Inner constraint range space dimension (Outer constraint domain space dimension)
216  int co_dim = x_dim - 1; // Outer constraint range space dimension
217 
218  auto inner_con = ROL::makePtr<InnerConstraint>(x_dim);
219  auto outer_con = ROL::makePtr<OuterConstraint>(ci_dim);
220 
221  auto x = ROL::makePtr<ROL::StdVector<RealT>>(x_dim);
222  auto v = ROL::makePtr<ROL::StdVector<RealT>>(x_dim);
223  auto y = ROL::makePtr<ROL::StdVector<RealT>>(ci_dim);
224  auto w = ROL::makePtr<ROL::StdVector<RealT>>(ci_dim);
225  auto ci = ROL::makePtr<ROL::StdVector<RealT>>(ci_dim);
226  auto co = ROL::makePtr<ROL::StdVector<RealT>>(co_dim);
227 
228  auto cr_con = ROL::makePtr<ROL::ChainRuleConstraint<RealT>>(outer_con,inner_con,*x,*ci);
229 
236 
237  *outStream << "\n\nInner Constraint Check:\n\n";
238 
239  inner_con->checkApplyJacobian(*x,*v,*ci,true,*outStream,7,4);
240  inner_con->checkAdjointConsistencyJacobian(*ci,*v,*x,true,*outStream);
241  inner_con->checkApplyAdjointHessian(*x,*ci,*v,*x,true,*outStream,7,4);
242 
243  *outStream << "\n\nOuter Constraint Check:\n\n";
244  outer_con->checkApplyJacobian(*y,*w,*co,true,*outStream,7,4);
245  outer_con->checkAdjointConsistencyJacobian(*co,*w,*y,true,*outStream);
246  outer_con->checkApplyAdjointHessian(*y,*co,*w,*y,true,*outStream,7,4);
247 
248  *outStream << "\n\nChain Rule Constraint Check:\n\n";
249  cr_con->checkApplyJacobian(*x,*v,*co,true,*outStream,7,4);
250  cr_con->checkAdjointConsistencyJacobian(*co,*v,*x,true,*outStream);
251  cr_con->checkApplyAdjointHessian(*x,*co,*v,*x,true,*outStream,7,4);
252 
253  }
254  catch (std::logic_error& err) {
255  *outStream << err.what() << "\n";
256  errorFlag = -1000;
257  }; // end try
258 
259  if (errorFlag != 0)
260  std::cout << "End Result: TEST FAILED\n";
261  else
262  std::cout << "End Result: TEST PASSED\n";
263 
264  return 0;
265 
266 
267 }
268 
void applyAdjointJacobian(VectorT &ajl, const VectorT &l, const VectorT &x, RealT &tol) override
void applyJacobian(VectorT &jv, const VectorT &v, const VectorT &x, RealT &tol) override
void applyAdjointHessian(VectorT &ahlv, const VectorT &l, const VectorT &v, const VectorT &x, RealT &tol) override
void update(const VectorT &x, ROL::UpdateType type, int iter) override
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,upper].
Defines the equality constraint operator interface for StdVectors.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
void value(VectorT &c, const VectorT &x, RealT &tol) override
std::vector< RealT > VectorT
void value(VectorT &c, const VectorT &x, RealT &tol) override
void update(const VectorT &x, ROL::UpdateType type, int iter) override
void applyJacobian(VectorT &jv, const VectorT &v, const VectorT &x, RealT &tol) override
void applyAdjointHessian(VectorT &ahlv, const VectorT &l, const VectorT &v, const VectorT &x, RealT &tol) override
void applyAdjointJacobian(VectorT &ajl, const VectorT &l, const VectorT &x, RealT &tol) override
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])