ROL
example_01.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 
48 #define USE_HESSVEC 1
49 
50 #include "ROL_Rosenbrock.hpp"
51 #include "ROL_LineSearchStep.hpp"
52 #include "ROL_Algorithm.hpp"
53 #include "Teuchos_oblackholestream.hpp"
54 #include "Teuchos_GlobalMPISession.hpp"
55 
56 #include <iostream>
57 
58 typedef double RealT;
59 
60 int main(int argc, char *argv[]) {
61 
62  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
63 
64  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
65  int iprint = argc - 1;
66  Teuchos::RCP<std::ostream> outStream;
67  Teuchos::oblackholestream bhs; // outputs nothing
68  if (iprint > 0)
69  outStream = Teuchos::rcp(&std::cout, false);
70  else
71  outStream = Teuchos::rcp(&bhs, false);
72 
73  int errorFlag = 0;
74 
75  // *** Example body.
76 
77  try {
78 
80  int dim = 100; // Set problem dimension. Must be even.
81 
82  Teuchos::ParameterList parlist;
83  // Enumerations
84  parlist.set("Descent Type", "Newton Krylov");
85  parlist.set("Linesearch Type", "Cubic Interpolation");
86  parlist.set("Linesearch Curvature Condition", "Wolfe");
87  // Linesearch Parameters
88  parlist.set("Maximum Number of Function Evaluations", 20);
89  parlist.set("Sufficient Decrease Parameter", 1.e-4);
90  parlist.set("Curvature Conditions Parameter", 0.9);
91  parlist.set("Backtracking Rate", 0.5);
92  parlist.set("Initial Linesearch Parameter", 1.0);
93  parlist.set("User Defined Linesearch Parameter", false);
94  // Krylov Parameters
95  parlist.set("Absolute Krylov Tolerance", 1.e-4);
96  parlist.set("Relative Krylov Tolerance", 1.e-2);
97  parlist.set("Maximum Number of Krylov Iterations", 10);
98  // Define Step
99  ROL::LineSearchStep<RealT> step(parlist);
100 
101  // Define Status Test
102  RealT gtol = 1e-12; // norm of gradient tolerance
103  RealT stol = 1e-14; // norm of step tolerance
104  int maxit = 100; // maximum number of iterations
105  ROL::StatusTest<RealT> status(gtol, stol, maxit);
106 
107  // Define Algorithm
108  ROL::DefaultAlgorithm<RealT> algo(step,status,false);
109 
110  // Iteration Vector
111  Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
112  // Set Initial Guess
113  for (int i=0; i<dim/2; i++) {
114  (*x_rcp)[2*i] = -1.2;
115  (*x_rcp)[2*i+1] = 1.0;
116  }
117  ROL::StdVector<RealT> x(x_rcp);
118 
119  // Run Algorithm
120  std::vector<std::string> output = algo.run(x, obj, false);
121  for ( unsigned i = 0; i < output.size(); i++ ) {
122  std::cout << output[i];
123  }
124 
125  // Get True Solution
126  Teuchos::RCP<std::vector<RealT> > xtrue_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 1.0) );
127  ROL::StdVector<RealT> xtrue(xtrue_rcp);
128 
129  // Compute Error
130  x.axpy(-1.0, xtrue);
131  RealT abserr = x.norm();
132  RealT relerr = abserr/xtrue.norm();
133  *outStream << std::scientific << "\n Absolute Error: " << abserr;
134  *outStream << std::scientific << "\n Relative Error: " << relerr << "\n";
135  if ( relerr > sqrt(ROL::ROL_EPSILON) ) {
136  errorFlag += 1;
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 
Rosenbrock's function.
Contains definitions for Rosenbrock's function.
Provides the interface to compute optimization steps with line search.
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to check status of optimization algorithms.