ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
example
rosenbrock
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
14
15
#define USE_HESSVEC 1
16
17
#include "
ROL_Rosenbrock.hpp
"
18
#include "
ROL_Algorithm.hpp
"
19
#include "
ROL_LineSearchStep.hpp
"
20
#include "
ROL_StatusTest.hpp
"
21
#include "
ROL_Stream.hpp
"
22
#include "ROL_GlobalMPISession.hpp"
23
24
#include <iostream>
25
26
typedef
double
RealT
;
27
28
int
main
(
int
argc,
char
*argv[]) {
29
30
ROL::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
47
ROL::ZOO::Objective_Rosenbrock<RealT>
obj;
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
ROL_Algorithm.hpp
ROL_LineSearchStep.hpp
ROL_Rosenbrock.hpp
Contains definitions for Rosenbrock's function.
ROL_StatusTest.hpp
ROL_Stream.hpp
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
ROL::Algorithm
Provides an interface to run unconstrained optimization algorithms.
Definition
ROL_Algorithm.hpp:29
ROL::StdVector
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Definition
ROL_StdVector.hpp:27
ROL::StdVector::axpy
void axpy(const Real alpha, const Vector< Real > &x)
Definition
ROL_StdVector.hpp:70
ROL::StdVector::norm
Real norm() const
Returns where .
Definition
ROL_StdVector.hpp:109
ROL::ZOO::Objective_Rosenbrock
Rosenbrock's function.
Definition
ROL_Rosenbrock.hpp:32
RealT
double RealT
Definition
function/constraint/test_01.cpp:29
ROL::details::nullstream
basic_nullstream< char, std::char_traits< char > > nullstream
Definition
ROL_Stream.hpp:36
ROL::ROL_EPSILON
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Definition
ROL_Types.hpp:57
main
int main(int argc, char *argv[])
Definition
rosenbrock/example_01.cpp:28
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0