ROL
function/test_09.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
15 #include "ROL_HS39.hpp"
16 
17 #include "ROL_RandomVector.hpp"
19 #include "ROL_StdVector.hpp"
20 #include "ROL_Algorithm.hpp"
22 #include "ROL_CompositeStep.hpp"
23 #include "ROL_Stream.hpp"
24 #include "Teuchos_GlobalMPISession.hpp"
25 
26 #include <iostream>
27 
28 typedef double RealT;
29 
30 
31 int main(int argc, char *argv[]) {
32 
33  typedef std::vector<RealT> vector;
34  typedef ROL::Vector<RealT> V;
35  typedef ROL::StdVector<RealT> SV;
36  typedef ROL::Objective<RealT> OBJ;
37  typedef ROL::Constraint<RealT> EC;
38 
39  typedef typename vector::size_type uint;
40 
41  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
42 
43  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
44  int iprint = argc - 1;
45  ROL::Ptr<std::ostream> outStream;
46  ROL::nullstream bhs; // outputs nothing
47  if (iprint > 0)
48  outStream = ROL::makePtrFromRef(std::cout);
49  else
50  outStream = ROL::makePtrFromRef(bhs);
51 
52  int errorFlag = 0;
53 
54  // *** Example body.
55 
56  try {
57 
58  uint xdim = 4;
59  uint cdim = 1;
60 
61  ROL::Ptr<vector> x_exact_ptr = ROL::makePtr<vector>(xdim);
62  (*x_exact_ptr)[0] = 1.0;
63  (*x_exact_ptr)[1] = 1.0;
64 
65  ROL::Ptr<V> x = ROL::makePtr<SV>( ROL::makePtr<vector>(xdim, 0.0) );
66  ROL::Ptr<V> d = ROL::makePtr<SV>( ROL::makePtr<vector>(xdim, 0.0) );
67  ROL::Ptr<V> xtest = ROL::makePtr<SV>( ROL::makePtr<vector>(xdim, 0.0) );
68 
69  ROL::Ptr<V> c1 = ROL::makePtr<SV>( ROL::makePtr<vector>(cdim, 1.0) );
70  ROL::Ptr<V> c2 = ROL::makePtr<SV>( ROL::makePtr<vector>(cdim, 1.0) );
71  ROL::Ptr<V> l1 = ROL::makePtr<SV>( ROL::makePtr<vector>(cdim, 1.0) );
72  ROL::Ptr<V> l2 = ROL::makePtr<SV>( ROL::makePtr<vector>(cdim, 1.0) );
73 
74  ROL::Ptr<V> c = ROL::CreatePartitionedVector( c1, c2 );
75  ROL::Ptr<V> l = ROL::CreatePartitionedVector( l1, l2 );
76 
77 
78 
79  SV x_exact( x_exact_ptr );
80 
81  // Initial guess from H&S 39
82  x->applyUnary(ROL::Elementwise::Fill<RealT>(2.0));
83 
84  ROL::RandomizeVector(*d, -1.0, 1.0 );
85  ROL::RandomizeVector(*xtest, -1.0, 1.0 );
86 
87  ROL::Ptr<OBJ> obj = ROL::makePtr<ROL::ZOO::Objective_HS39<RealT>>();
88  ROL::Ptr<EC> con1 = ROL::makePtr<ROL::ZOO::Constraint_HS39a<RealT>>();
89  ROL::Ptr<EC> con2 = ROL::makePtr<ROL::ZOO::Constraint_HS39b<RealT>>();
90  std::vector<ROL::Ptr<EC> > cvec(2); cvec[0] = con1; cvec[1] = con2;
91 
92  ROL::Ptr<EC> con = ROL::makePtr<ROL::Constraint_Partitioned<RealT>>(cvec);
93 
94  *outStream << "Checking objective" << std::endl;
95  obj->checkGradient(*x,*d,true,*outStream);
96 
97  *outStream << "\nChecking first equality constraint" << std::endl;
98  con1->checkApplyJacobian( *xtest, *d, *c1 , true, *outStream );
99  con1->checkApplyAdjointJacobian( *xtest, *l1, *c1, *d, true, *outStream );
100  con1->checkApplyAdjointHessian( *xtest, *l1, *d, *xtest, true, *outStream );
101 
102  *outStream << "\nChecking second equality constraint" << std::endl;
103  con2->checkApplyJacobian( *xtest, *d, *c2, true, *outStream );
104  con2->checkApplyAdjointJacobian( *xtest, *l2, *c2, *d, true, *outStream );
105  con2->checkApplyAdjointHessian( *xtest, *l2, *d, *xtest, true, *outStream );
106 
107  *outStream << "\nChecking partitioned equality constraint" << std::endl;
108  con->checkApplyJacobian( *xtest, *d, *c, true, *outStream );
109  con->checkApplyAdjointJacobian( *xtest, *l, *c, *d, true, *outStream );
110  con->checkApplyAdjointHessian( *xtest, *l, *d, *xtest, true, *outStream );
111 
112  // Define algorithm.
113  ROL::ParameterList parlist;
114  std::string stepname = "Composite Step";
115  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1.e-4);
116  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true);
117  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
118  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
119  parlist.sublist("Step").sublist(stepname).set("Output Level",0);
120  parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
121  parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12);
122  parlist.sublist("Status Test").set("Step Tolerance",1.e-18);
123  parlist.sublist("Status Test").set("Iteration Limit",100);
124  ROL::Ptr<ROL::StatusTest<RealT>>
125  status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(parlist);
126  ROL::Ptr<ROL::Step<RealT>>
127  step = ROL::makePtr<ROL::CompositeStep<RealT>>(parlist);
128  ROL::Algorithm<RealT> algo(step,status,false);
129 
130  algo.run(*x,x->dual(),*l,*c,*obj,*con,true,*outStream);
131 
132  x->axpy(-1.0,x_exact);
133 
134  if( x->norm() > 1e-6 ) {
135  ++errorFlag;
136  }
137 
138  }
139  catch (std::logic_error& err) {
140  *outStream << err.what() << "\n";
141  errorFlag = -1000;
142  }; // end try
143 
144  if (errorFlag != 0)
145  std::cout << "End Result: TEST FAILED\n";
146  else
147  std::cout << "End Result: TEST PASSED\n";
148 
149  return 0;
150 
151 }
152 
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:46
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:38
int main(int argc, char *argv[])