ROL
function/test_05.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 
51 #include "ROL_StdVector.hpp"
53 #include "ROL_Algorithm.hpp"
54 #include "Teuchos_oblackholestream.hpp"
55 #include "Teuchos_GlobalMPISession.hpp"
56 
57 #include <iostream>
58 
59 typedef double RealT;
60 
61 
62 int main(int argc, char *argv[]) {
63 
64  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
65 
66  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
67  int iprint = argc - 1;
68  Teuchos::RCP<std::ostream> outStream;
69  Teuchos::oblackholestream bhs; // outputs nothing
70  if (iprint > 0)
71  outStream = Teuchos::rcp(&std::cout, false);
72  else
73  outStream = Teuchos::rcp(&bhs, false);
74 
75  int errorFlag = 0;
76 
77  // *** Example body.
78 
79  try {
80 
81  Teuchos::RCP<ROL::Objective<RealT> > obj;
82  Teuchos::RCP<ROL::EqualityConstraint<RealT> > constr;
83  Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp( new std::vector<RealT> (0, 0.0) );
84  Teuchos::RCP<std::vector<RealT> > sol_rcp = Teuchos::rcp( new std::vector<RealT> (0, 0.0) );
85  ROL::StdVector<RealT> x(x_rcp); // Iteration vector.
86  ROL::StdVector<RealT> sol(sol_rcp); // Reference solution vector.
87 
88  // Retrieve objective, constraint, iteration vector, solution vector.
89  ROL::ZOO::getSimpleEqConstrained <RealT, ROL::StdVector<RealT>, ROL::StdVector<RealT>, ROL::StdVector<RealT>, ROL::StdVector<RealT> > (obj, constr, x, sol);
90 
91  // Inititalize vectors
92  int dim = 5;
93  int nc = 3;
94  RealT left = -1e0, right = 1e0;
95  Teuchos::RCP<std::vector<RealT> > xtest_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
96  Teuchos::RCP<std::vector<RealT> > g_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
97  Teuchos::RCP<std::vector<RealT> > d_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
98  Teuchos::RCP<std::vector<RealT> > v_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
99  Teuchos::RCP<std::vector<RealT> > vc_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) );
100  Teuchos::RCP<std::vector<RealT> > vl_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) );
101  ROL::StdVector<RealT> xtest(xtest_rcp);
102  ROL::StdVector<RealT> g(g_rcp);
103  ROL::StdVector<RealT> d(d_rcp);
104  ROL::StdVector<RealT> v(v_rcp);
105  ROL::StdVector<RealT> vc(vc_rcp);
106  ROL::StdVector<RealT> vl(vl_rcp);
107  // set xtest, d, v
108  for (int i=0; i<dim; i++) {
109  (*xtest_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
110  (*d_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
111  (*v_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
112  }
113  // set vc, vl
114  for (int i=0; i<nc; i++) {
115  (*vc_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
116  (*vl_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
117  }
118 
119  xtest.set(x);
120 
121  // Initialize nonlinear least squares objectives
122  ROL::NonlinearLeastSquaresObjective<RealT> nlls(constr,x,vc,false);
123  ROL::NonlinearLeastSquaresObjective<RealT> gnnlls(constr,x,vc,true);
124 
125  // Check derivatives
126  constr->checkApplyJacobian(xtest, v, vc, true, *outStream); *outStream << "\n";
127  constr->checkApplyAdjointJacobian(xtest, vl, vc, xtest, true, *outStream); *outStream << "\n";
128  constr->checkApplyAdjointHessian(xtest, vl, d, xtest, true, *outStream); *outStream << "\n";
129  nlls.checkGradient(xtest, d, true, *outStream); *outStream << "\n";
130  nlls.checkHessVec(xtest, v, true, *outStream); *outStream << "\n";
131  nlls.checkHessSym(xtest, d, v, true, *outStream); *outStream << "\n";
132 
133  // Define algorithm.
134  Teuchos::ParameterList parlist;
135  std::string stepname = "Trust Region";
136  parlist.sublist("Step").sublist(stepname).set("Subproblem Solver","Truncated CG");
137  parlist.sublist("Status Test").set("Gradient Tolerance",1.e-10);
138  parlist.sublist("Status Test").set("Constraint Tolerance",1.e-10);
139  parlist.sublist("Status Test").set("Step Tolerance",1.e-18);
140  parlist.sublist("Status Test").set("Iteration Limit",100);
141  ROL::Algorithm<RealT> algo(stepname, parlist);
142 
143  // Run Algorithm
144  *outStream << "\nSOLVE USING FULL HESSIAN\n";
145  x.set(xtest);
146  algo.run(x, nlls, true, *outStream);
147  algo.reset();
148  *outStream << "\nSOLVE USING GAUSS-NEWTON HESSIAN\n";
149  x.set(xtest);
150  algo.run(x, gnnlls, true, *outStream);
151  }
152  catch (std::logic_error err) {
153  *outStream << err.what() << "\n";
154  errorFlag = -1000;
155  }; // end try
156 
157  if (errorFlag != 0)
158  std::cout << "End Result: TEST FAILED\n";
159  else
160  std::cout << "End Result: TEST PASSED\n";
161 
162  return 0;
163 
164 }
165 
void reset(void)
int main(int argc, char *argv[])
double RealT
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to run optimization algorithms.
Contains definitions for the equality constrained NLP from Nocedal/Wright, 2nd edition, page 574, example 18.2; note the typo in reversing the initial guess and the solution.
void set(const Vector< Real > &x)
Set where .
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
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.
Provides the interface to evaluate nonlinear least squares objective functions.
double RealT
virtual std::vector< Real > checkHessSym(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &w, const bool printToStream=true, std::ostream &outStream=std::cout)
Hessian symmetry check.