ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
example
sacado
example_01b.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
17
18
#include <iostream>
19
20
#include "
ROL_Algorithm.hpp
"
21
#include "
ROL_LineSearchStep.hpp
"
22
#include "
ROL_StatusTest.hpp
"
23
#include "ROL_ParameterList.hpp"
24
25
#include "
ROL_Stream.hpp
"
26
#include "ROL_GlobalMPISession.hpp"
27
28
#include "
example_01b.hpp
"
29
30
using namespace
ROL
;
31
32
typedef
double
RealT
;
33
34
int
main
(
int
argc,
char
**argv)
35
{
36
ROL::GlobalMPISession mpiSession(&argc, &argv);
37
38
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
39
int
iprint = argc - 1;
40
ROL::Ptr<std::ostream> outStream;
41
ROL::nullstream
bhs;
// outputs nothing
42
if
(iprint > 0)
43
outStream = ROL::makePtrFromRef(std::cout);
44
else
45
outStream = ROL::makePtrFromRef(bhs);
46
47
int
errorFlag = 0;
48
49
// *** Example body.
50
51
try
{
52
53
Zakharov_Sacado_Objective<RealT>
obj;
54
55
int
dim
= 10;
// Set problem dimension.
56
57
// Load optimizer parameters form XML file
58
std::string paramfile =
"parameters.xml"
;
59
auto
parlist = ROL::getParametersFromXmlFile(paramfile);
60
61
// Define algorithm.
62
ROL::Ptr<ROL::Step<RealT>>
63
step = ROL::makePtr<ROL::LineSearchStep<RealT>>(*parlist);
64
ROL::Ptr<ROL::StatusTest<RealT>>
65
status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
66
ROL::Algorithm<RealT>
algo(step,status,
false
);
67
68
// Iteration Vector
69
ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(
dim
, 0.0);
70
// Set Initial Guess
71
for
(
int
i=0; i<
dim
; i++) {
72
(*x_ptr)[i] = 2;
73
}
74
75
StdVector<RealT>
x(x_ptr);
76
77
// Run Algorithm
78
algo.
run
(x, obj,
true
, *outStream);
79
80
// Get True Solution
81
ROL::Ptr<std::vector<RealT> > xtrue_ptr = ROL::makePtr<std::vector<RealT>>(
dim
, 0.0);
82
StdVector<RealT>
xtrue(xtrue_ptr);
83
84
85
// Compute Error
86
x.
axpy
(-1.0, xtrue);
87
RealT
abserr = x.
norm
();
88
*outStream << std::scientific <<
"\n Absolute Error: "
<< abserr;
89
if
( abserr > sqrt(
ROL_EPSILON<RealT>
()) ) {
90
errorFlag += 1;
91
}
92
}
93
catch
(std::logic_error& err) {
94
*outStream << err.what() <<
"\n"
;
95
errorFlag = -1000;
96
};
// end try
97
98
if
(errorFlag != 0)
99
std::cout <<
"End Result: TEST FAILED\n"
;
100
else
101
std::cout <<
"End Result: TEST PASSED\n"
;
102
103
return
0;
104
105
}
ROL_Algorithm.hpp
ROL_LineSearchStep.hpp
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::Algorithm::run
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
Definition
ROL_Algorithm.hpp:68
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
Zakharov_Sacado_Objective
Definition
example_01b.hpp:69
main
int main(int argc, char **argv)
Definition
example_01b.cpp:34
example_01b.hpp
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
Definition
ROL_ElementwiseVector.hpp:27
ROL::ROL_EPSILON
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Definition
ROL_Types.hpp:57
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0