ROL
function/test_09.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 
49 #include "ROL_HS39.hpp"
50 
51 #include "ROL_RandomVector.hpp"
53 #include "ROL_StdVector.hpp"
54 #include "ROL_Algorithm.hpp"
56 #include "ROL_CompositeStep.hpp"
57 #include "ROL_Stream.hpp"
58 #include "Teuchos_GlobalMPISession.hpp"
59 
60 #include <iostream>
61 
62 typedef double RealT;
63 
64 
65 int main(int argc, char *argv[]) {
66 
67  typedef std::vector<RealT> vector;
68  typedef ROL::Vector<RealT> V;
69  typedef ROL::StdVector<RealT> SV;
70  typedef ROL::Objective<RealT> OBJ;
71  typedef ROL::Constraint<RealT> EC;
72 
73  typedef typename vector::size_type uint;
74 
75  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
76 
77  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
78  int iprint = argc - 1;
79  ROL::Ptr<std::ostream> outStream;
80  ROL::nullstream bhs; // outputs nothing
81  if (iprint > 0)
82  outStream = ROL::makePtrFromRef(std::cout);
83  else
84  outStream = ROL::makePtrFromRef(bhs);
85 
86  int errorFlag = 0;
87 
88  // *** Example body.
89 
90  try {
91 
92  uint xdim = 4;
93  uint cdim = 1;
94 
95  ROL::Ptr<vector> x_exact_ptr = ROL::makePtr<vector>(xdim);
96  (*x_exact_ptr)[0] = 1.0;
97  (*x_exact_ptr)[1] = 1.0;
98 
99  ROL::Ptr<V> x = ROL::makePtr<SV>( ROL::makePtr<vector>(xdim, 0.0) );
100  ROL::Ptr<V> d = ROL::makePtr<SV>( ROL::makePtr<vector>(xdim, 0.0) );
101  ROL::Ptr<V> xtest = ROL::makePtr<SV>( ROL::makePtr<vector>(xdim, 0.0) );
102 
103  ROL::Ptr<V> c1 = ROL::makePtr<SV>( ROL::makePtr<vector>(cdim, 1.0) );
104  ROL::Ptr<V> c2 = ROL::makePtr<SV>( ROL::makePtr<vector>(cdim, 1.0) );
105  ROL::Ptr<V> l1 = ROL::makePtr<SV>( ROL::makePtr<vector>(cdim, 1.0) );
106  ROL::Ptr<V> l2 = ROL::makePtr<SV>( ROL::makePtr<vector>(cdim, 1.0) );
107 
108  ROL::Ptr<V> c = ROL::CreatePartitionedVector( c1, c2 );
109  ROL::Ptr<V> l = ROL::CreatePartitionedVector( l1, l2 );
110 
111 
112 
113  SV x_exact( x_exact_ptr );
114 
115  // Initial guess from H&S 39
116  x->applyUnary(ROL::Elementwise::Fill<RealT>(2.0));
117 
118  ROL::RandomizeVector(*d, -1.0, 1.0 );
119  ROL::RandomizeVector(*xtest, -1.0, 1.0 );
120 
121  ROL::Ptr<OBJ> obj = ROL::makePtr<ROL::ZOO::Objective_HS39<RealT>>();
122  ROL::Ptr<EC> con1 = ROL::makePtr<ROL::ZOO::Constraint_HS39a<RealT>>();
123  ROL::Ptr<EC> con2 = ROL::makePtr<ROL::ZOO::Constraint_HS39b<RealT>>();
124  std::vector<ROL::Ptr<EC> > cvec(2); cvec[0] = con1; cvec[1] = con2;
125 
126  ROL::Ptr<EC> con = ROL::makePtr<ROL::Constraint_Partitioned<RealT>>(cvec);
127 
128  *outStream << "Checking objective" << std::endl;
129  obj->checkGradient(*x,*d,true,*outStream);
130 
131  *outStream << "\nChecking first equality constraint" << std::endl;
132  con1->checkApplyJacobian( *xtest, *d, *c1 , true, *outStream );
133  con1->checkApplyAdjointJacobian( *xtest, *l1, *c1, *d, true, *outStream );
134  con1->checkApplyAdjointHessian( *xtest, *l1, *d, *xtest, true, *outStream );
135 
136  *outStream << "\nChecking second equality constraint" << std::endl;
137  con2->checkApplyJacobian( *xtest, *d, *c2, true, *outStream );
138  con2->checkApplyAdjointJacobian( *xtest, *l2, *c2, *d, true, *outStream );
139  con2->checkApplyAdjointHessian( *xtest, *l2, *d, *xtest, true, *outStream );
140 
141  *outStream << "\nChecking partitioned equality constraint" << std::endl;
142  con->checkApplyJacobian( *xtest, *d, *c, true, *outStream );
143  con->checkApplyAdjointJacobian( *xtest, *l, *c, *d, true, *outStream );
144  con->checkApplyAdjointHessian( *xtest, *l, *d, *xtest, true, *outStream );
145 
146  // Define algorithm.
147  ROL::ParameterList parlist;
148  std::string stepname = "Composite Step";
149  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1.e-4);
150  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true);
151  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
152  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
153  parlist.sublist("Step").sublist(stepname).set("Output Level",0);
154  parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
155  parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12);
156  parlist.sublist("Status Test").set("Step Tolerance",1.e-18);
157  parlist.sublist("Status Test").set("Iteration Limit",100);
158  ROL::Ptr<ROL::StatusTest<RealT>>
159  status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(parlist);
160  ROL::Ptr<ROL::Step<RealT>>
161  step = ROL::makePtr<ROL::CompositeStep<RealT>>(parlist);
162  ROL::Algorithm<RealT> algo(step,status,false);
163 
164  algo.run(*x,x->dual(),*l,*c,*obj,*con,true,*outStream);
165 
166  x->axpy(-1.0,x_exact);
167 
168  if( x->norm() > 1e-6 ) {
169  ++errorFlag;
170  }
171 
172  }
173  catch (std::logic_error& err) {
174  *outStream << err.what() << "\n";
175  errorFlag = -1000;
176  }; // end try
177 
178  if (errorFlag != 0)
179  std::cout << "End Result: TEST FAILED\n";
180  else
181  std::cout << "End Result: TEST PASSED\n";
182 
183  return 0;
184 
185 }
186 
Provides the interface to evaluate objective functions.
typename PV< Real >::size_type size_type
ROL::Ptr< Vector< Real > > CreatePartitionedVector(const ROL::Ptr< Vector< Real >> &a)
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 linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Vector< Real > V
Contains definitions for W. Hock and K. Schittkowski 39th test function.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides an interface to run optimization algorithms.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])
Defines the general constraint operator interface.