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"
55 #include "Teuchos_oblackholestream.hpp"
56 #include "Teuchos_GlobalMPISession.hpp"
57 
58 #include <iostream>
59 
60 typedef double RealT;
61 
62 
63 int main(int argc, char *argv[]) {
64 
65  typedef std::vector<RealT> vector;
66  typedef ROL::Vector<RealT> V;
67  typedef ROL::StdVector<RealT> SV;
68  typedef ROL::Objective<RealT> OBJ;
70 
71  typedef typename vector::size_type uint;
72 
73 
74  using Teuchos::RCP; using Teuchos::rcp;
75 
76  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
77 
78  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
79  int iprint = argc - 1;
80  Teuchos::RCP<std::ostream> outStream;
81  Teuchos::oblackholestream bhs; // outputs nothing
82  if (iprint > 0)
83  outStream = Teuchos::rcp(&std::cout, false);
84  else
85  outStream = Teuchos::rcp(&bhs, false);
86 
87  int errorFlag = 0;
88 
89  // *** Example body.
90 
91  try {
92 
93  uint xdim = 4;
94  uint cdim = 1;
95 
96  RCP<vector> x_exact_rcp = rcp( new vector(xdim), 0.0 );
97  (*x_exact_rcp)[0] = 1.0;
98  (*x_exact_rcp)[1] = 1.0;
99 
100  RCP<V> x = rcp( new SV( rcp( new vector(xdim, 0.0) ) ) );
101  RCP<V> d = rcp( new SV( rcp( new vector(xdim, 0.0) ) ) );
102  RCP<V> xtest = rcp( new SV( rcp( new vector(xdim, 0.0) ) ) );
103 
104  RCP<V> c1 = rcp( new SV( rcp( new vector(cdim, 1.0) ) ) );
105  RCP<V> c2 = rcp( new SV( rcp( new vector(cdim, 1.0) ) ) );
106  RCP<V> l1 = rcp( new SV( rcp( new vector(cdim, 1.0) ) ) );
107  RCP<V> l2 = rcp( new SV( rcp( new vector(cdim, 1.0) ) ) );
108 
109  RCP<V> c = ROL::CreatePartitionedVector( c1, c2 );
110  RCP<V> l = ROL::CreatePartitionedVector( l1, l2 );
111 
112 
113 
114  SV x_exact( x_exact_rcp );
115 
116  // Initial guess from H&S 39
117  x->applyUnary(ROL::Elementwise::Fill<RealT>(2.0));
118 
119  ROL::RandomizeVector(*d, -1.0, 1.0 );
120  ROL::RandomizeVector(*xtest, -1.0, 1.0 );
121 
122  RCP<OBJ> obj = rcp( new ROL::ZOO::Objective_HS39<RealT>() );
123  RCP<EC> con1 = rcp( new ROL::ZOO::EqualityConstraint_HS39a<RealT>() );
124  RCP<EC> con2 = rcp( new ROL::ZOO::EqualityConstraint_HS39b<RealT>() );
125 
126  RCP<EC> con = CreateEqualityConstraintPartitioned(con1, con2);
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  Teuchos::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::Algorithm<RealT> algo(stepname, parlist);
159 
160  algo.run(*x,x->dual(),*l,*c,*obj,*con,true,*outStream);
161 
162  x->axpy(-1.0,x_exact);
163 
164  if( x->norm() > 1e-6 ) {
165  ++errorFlag;
166  }
167 
168  }
169  catch (std::logic_error err) {
170  *outStream << err.what() << "\n";
171  errorFlag = -1000;
172  }; // end try
173 
174  if (errorFlag != 0)
175  std::cout << "End Result: TEST FAILED\n";
176  else
177  std::cout << "End Result: TEST PASSED\n";
178 
179  return 0;
180 
181 }
182 
Provides the interface to evaluate objective functions.
double RealT
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].
Teuchos::RCP< Vector< Real > > CreatePartitionedVector(const Teuchos::RCP< Vector< Real > > &a)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< EqualityConstraint< Real > > CreateEqualityConstraintPartitioned(const Teuchos::RCP< EqualityConstraint< Real > > &con1, const Teuchos::RCP< EqualityConstraint< Real > > &con2)
Contains definitions for W. Hock and K. Schittkowski 39th test function.
Defines the equality constraint operator interface.
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to run optimization algorithms.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
int main(int argc, char *argv[])
W. Hock and K. Schittkowski 39th test function.
Definition: ROL_HS39.hpp:69