ROL
rosenbrock/example_01.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#define USE_HESSVEC 1
16
17#include "ROL_Rosenbrock.hpp"
18#include "ROL_Algorithm.hpp"
20#include "ROL_StatusTest.hpp"
21#include "ROL_Stream.hpp"
22#include "Teuchos_GlobalMPISession.hpp"
23
24#include <iostream>
25
26typedef double RealT;
27
28int main(int argc, char *argv[]) {
29
30 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
31
32 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
33 int iprint = argc - 1;
34 ROL::Ptr<std::ostream> outStream;
35 ROL::nullstream bhs; // outputs nothing
36 if (iprint > 0)
37 outStream = ROL::makePtrFromRef(std::cout);
38 else
39 outStream = ROL::makePtrFromRef(bhs);
40
41 int errorFlag = 0;
42
43 // *** Example body.
44
45 try {
46
48 int dim = 100; // Set problem dimension. Must be even.
49
50 // Set parameters.
51 ROL::ParameterList parlist;
52 parlist.sublist("Step").sublist("Line Search").sublist("Descent Method").set("Type", "Newton-Krylov");
53 parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
54 parlist.sublist("Status Test").set("Step Tolerance",1.e-14);
55 parlist.sublist("Status Test").set("Iteration Limit",100);
56
57 // Define algorithm.
58 ROL::Ptr<ROL::Step<RealT>>
59 step = ROL::makePtr<ROL::LineSearchStep<RealT>>(parlist);
60 ROL::Ptr<ROL::StatusTest<RealT>>
61 status = ROL::makePtr<ROL::StatusTest<RealT>>(parlist);
62 ROL::Algorithm<RealT> algo(step,status,false);
63
64 // Iteration Vector
65 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
66 // Set Initial Guess
67 for (int i=0; i<dim/2; i++) {
68 (*x_ptr)[2*i] = -1.2;
69 (*x_ptr)[2*i+1] = 1.0;
70 }
71 ROL::StdVector<RealT> x(x_ptr);
72
73 // Run Algorithm
74 algo.run(x, obj, true, *outStream);
75
76 // Get True Solution
77 ROL::Ptr<std::vector<RealT> > xtrue_ptr = ROL::makePtr<std::vector<RealT>>(dim, 1.0);
78 ROL::StdVector<RealT> xtrue(xtrue_ptr);
79
80 // Compute Error
81 x.axpy(-1.0, xtrue);
82 RealT abserr = x.norm();
83 RealT relerr = abserr/xtrue.norm();
84 *outStream << std::scientific << "\n Absolute Error: " << abserr;
85 *outStream << std::scientific << "\n Relative Error: " << relerr << "\n";
86 if ( relerr > sqrt(ROL::ROL_EPSILON<RealT>()) ) {
87 errorFlag += 1;
88 }
89 }
90 catch (std::logic_error& err) {
91 *outStream << err.what() << "\n";
92 errorFlag = -1000;
93 }; // end try
94
95 if (errorFlag != 0)
96 std::cout << "End Result: TEST FAILED\n";
97 else
98 std::cout << "End Result: TEST PASSED\n";
99
100 return 0;
101
102}
103
Contains definitions for Rosenbrock's function.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides an interface to run optimization algorithms.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
Real norm() const
Returns where .
int main(int argc, char *argv[])
double RealT
constexpr auto dim