ROL
sacado/example_02.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 
55 #include <iostream>
56 
57 #include "ROL_Sacado_Objective.hpp"
58 #include "ROL_Sacado_EqualityConstraint.hpp"
59 
60 #include "ROL_LineSearchStep.hpp"
61 #include "ROL_Algorithm.hpp"
63 #include "ROL_CompositeStepSQP.hpp"
64 
65 #include "Teuchos_oblackholestream.hpp"
66 #include "Teuchos_GlobalMPISession.hpp"
67 #include "Teuchos_XMLParameterListHelpers.hpp"
68 
69 #include "example_02.hpp"
70 
71 using namespace ROL;
72 
73 typedef double RealT;
74 
75 int main(int argc, char **argv)
76 {
77 
78 
79  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
80 
81  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
82  int iprint = argc - 1;
83  Teuchos::RCP<std::ostream> outStream;
84  Teuchos::oblackholestream bhs; // outputs nothing
85  if (iprint > 0)
86  outStream = Teuchos::rcp(&std::cout, false);
87  else
88  outStream = Teuchos::rcp(&bhs, false);
89 
90  int errorFlag = 0;
91 
92  // *** Example body.
93 
94  try {
95 
96  // Run derivative checks, etc.
97  int dim = 5;
98  int nc = 3;
99 
100  Teuchos::RCP< Sacado_Objective<RealT,Example_Objective> > obj =
101  Teuchos::rcp( new Sacado_Objective<RealT,Example_Objective> ());
102 
103  Teuchos::RCP< Sacado_EqualityConstraint<RealT,Example_Constraint > > constr =
104  Teuchos::rcp( new Sacado_EqualityConstraint<RealT,Example_Constraint > (nc));
105 
106  Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
107 
108  Teuchos::RCP<std::vector<RealT> > sol_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
109  ROL::StdVector<RealT> x(x_rcp); // Iteration vector.
110  ROL::StdVector<RealT> sol(sol_rcp); // Reference solution vector.
111 
112  // Get initial guess
113  (*x_rcp)[0] = -1.8;
114  (*x_rcp)[1] = 1.7;
115  (*x_rcp)[2] = 1.9;
116  (*x_rcp)[3] = -0.8;
117  (*x_rcp)[4] = -0.8;
118 
119  // Get solution
120  (*sol_rcp)[0] = -1.717143570394391e+00;
121  (*sol_rcp)[1] = 1.595709690183565e+00;
122  (*sol_rcp)[2] = 1.827245752927178e+00;
123  (*sol_rcp)[3] = -7.636430781841294e-01;
124  (*sol_rcp)[4] = -7.636430781841294e-01;
125 
126  Teuchos::ParameterList parlist;
127 
128  // Define Step
129  parlist.set("Nominal SQP Optimality Solver Tolerance", 1.e-2);
130  ROL::CompositeStepSQP<RealT> step(parlist);
131 
132  RealT left = -1e0, right = 1e0;
133  Teuchos::RCP<std::vector<RealT> > xtest_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
134  Teuchos::RCP<std::vector<RealT> > g_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
135  Teuchos::RCP<std::vector<RealT> > d_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
136  Teuchos::RCP<std::vector<RealT> > v_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
137  Teuchos::RCP<std::vector<RealT> > vc_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) );
138  Teuchos::RCP<std::vector<RealT> > vl_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) );
139  ROL::StdVector<RealT> xtest(xtest_rcp);
140  ROL::StdVector<RealT> g(g_rcp);
141  ROL::StdVector<RealT> d(d_rcp);
142  ROL::StdVector<RealT> v(v_rcp);
143  ROL::StdVector<RealT> vc(vc_rcp);
144  ROL::StdVector<RealT> vl(vl_rcp);
145 
146  // set xtest, d, v
147  for (int i=0; i<dim; i++) {
148  (*xtest_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
149  (*d_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
150  (*v_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
151  }
152  // set vc, vl
153  for (int i=0; i<nc; i++) {
154  (*vc_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
155  (*vl_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
156  }
157 
158  obj->checkGradient(xtest, d, true, *outStream); *outStream << "\n";
159  obj->checkHessVec(xtest, v, true, *outStream); *outStream << "\n";
160  obj->checkHessSym(xtest, d, v, true, *outStream); *outStream << "\n";
161  constr->checkApplyJacobian(xtest, v, vc, true, *outStream); *outStream << "\n";
162  constr->checkApplyAdjointJacobian(xtest, vl, vc, xtest, true, *outStream); *outStream << "\n";
163  constr->checkApplyAdjointHessian(xtest, vl, d, xtest, true, *outStream); *outStream << "\n";
164 
165  Teuchos::RCP<std::vector<RealT> > v1_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
166  Teuchos::RCP<std::vector<RealT> > v2_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) );
167  ROL::StdVector<RealT> v1(v1_rcp);
168  ROL::StdVector<RealT> v2(v2_rcp);
169  RealT augtol = 1e-8;
170  constr->solveAugmentedSystem(v1, v2, d, vc, xtest, augtol);
171 
172  // Define Status Test
173  RealT gtol = 1e-12; // norm of gradient tolerance
174  RealT ctol = 1e-12; // norm of constraint tolerance
175  RealT stol = 1e-18; // norm of step tolerance
176  int maxit = 1000; // maximum number of iterations
177  ROL::StatusTestSQP<RealT> status(gtol, ctol, stol, maxit);
178 
179  // Define Algorithm
180  ROL::DefaultAlgorithm<RealT> algo(step, status, false);
181 
182  // Run Algorithm
183  vl.zero();
184 
185  std::vector<std::string> output = algo.run(x, g, vl, vc, *obj, *constr, false);
186  for ( unsigned i = 0; i < output.size(); i++ ) {
187  *outStream << output[i];
188  }
189 
190  // Compute Error
191  *outStream << "\nReference solution x_r =\n";
192  *outStream << std::scientific << " " << (*sol_rcp)[0] << "\n";
193  *outStream << std::scientific << " " << (*sol_rcp)[1] << "\n";
194  *outStream << std::scientific << " " << (*sol_rcp)[2] << "\n";
195  *outStream << std::scientific << " " << (*sol_rcp)[3] << "\n";
196  *outStream << std::scientific << " " << (*sol_rcp)[4] << "\n";
197  *outStream << "\nOptimal solution x =\n";
198  *outStream << std::scientific << " " << (*x_rcp)[0] << "\n";
199  *outStream << std::scientific << " " << (*x_rcp)[1] << "\n";
200  *outStream << std::scientific << " " << (*x_rcp)[2] << "\n";
201  *outStream << std::scientific << " " << (*x_rcp)[3] << "\n";
202  *outStream << std::scientific << " " << (*x_rcp)[4] << "\n";
203  x.axpy(-1.0, sol);
204  RealT abserr = x.norm();
205  RealT relerr = abserr/sol.norm();
206  *outStream << std::scientific << "\n Absolute Error: " << abserr;
207  *outStream << std::scientific << "\n Relative Error: " << relerr << "\n";
208  if ( relerr > sqrt(ROL::ROL_EPSILON) ) {
209  errorFlag += 1;
210  }
211  }
212  catch (std::logic_error err) {
213  *outStream << err.what() << "\n";
214  errorFlag = -1000;
215  }; // end try
216 
217  if (errorFlag != 0)
218  std::cout << "End Result: TEST FAILED\n";
219  else
220  std::cout << "End Result: TEST PASSED\n";
221 
222  return 0;
223 
224 
225 }
double RealT
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:141
int main(int argc, char **argv)
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:155
Real norm() const
Returns where .
Provides the std::vector implementation of the ROL::Vector interface.
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.
Implements the computation of optimization steps with composite-step trust-region SQP methods...
double RealT
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:115