ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
step
fletcher
step/fletcher/test_03.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
12
13
#define USE_HESSVEC 1
14
15
#include "
ROL_GetTestProblems.hpp
"
16
#include "
ROL_OptimizationSolver.hpp
"
17
#include "
ROL_Stream.hpp
"
18
#include "ROL_GlobalMPISession.hpp"
19
20
21
#include <iostream>
22
23
typedef
double
RealT
;
24
25
int
main
(
int
argc,
char
*argv[]) {
26
27
ROL::GlobalMPISession mpiSession(&argc, &argv);
28
29
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
30
int
iprint = argc - 1;
31
ROL::Ptr<std::ostream> outStream;
32
ROL::nullstream
bhs;
// outputs nothing
33
if
(iprint > 0)
34
outStream = ROL::makePtrFromRef(std::cout);
35
else
36
outStream = ROL::makePtrFromRef(bhs);
37
38
int
errorFlag = 0;
39
40
// *** Test body.
41
42
try
{
43
44
std::string filename =
"input_ex03.xml"
;
45
46
auto
parlist = ROL::getParametersFromXmlFile( filename );
47
parlist->sublist(
"General"
).set(
"Inexact Hessian-Times-A-Vector"
,
true
);
48
#if USE_HESSVEC
49
parlist->sublist(
"General"
).set(
"Inexact Hessian-Times-A-Vector"
,
false
);
50
#endif
51
52
// Krylov parameters.
53
parlist->sublist(
"Step"
).set(
"Type"
,
"Fletcher"
);
54
55
for
(
ROL::ETestOptProblem
prob =
ROL::TESTOPTPROBLEM_ROSENBROCK
; prob <
ROL::TESTOPTPROBLEM_LAST
; prob++ ) {
56
if
((prob !=
ROL::TESTOPTPROBLEM_HS55
) && (prob !=
ROL::TESTOPTPROBLEM_HS14
) && (prob !=
ROL::TESTOPTPROBLEM_HS63
) && (prob !=
ROL::TESTOPTPROBLEM_HS41
)) {
57
// Get Objective Function
58
ROL::Ptr<ROL::Vector<RealT> > x0;
59
std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
60
ROL::Ptr<ROL::OptimizationProblem<RealT> > problem;
61
ROL::GetTestProblem<RealT>
(problem,x0,z,prob);
62
63
if
((problem->getProblemType() ==
ROL::TYPE_E
|| problem->getProblemType() ==
ROL::TYPE_EB
)
64
&& (prob !=
ROL::TESTOPTPROBLEM_CANTILEVERBEAM
&& prob !=
ROL::TESTOPTPROBLEM_QUARTIC
&& prob !=
ROL::TESTOPTPROBLEM_CANTILEVER
&& prob !=
ROL::TESTOPTPROBLEM_CYLINDERHEAD
)) {
65
*outStream << std::endl << std::endl <<
ROL:: ETestOptProblemToString
(prob) << std::endl << std::endl;
66
67
// Get Dimension of Problem
68
// int dim = x0->dimension();
69
// parlist->sublist("General").sublist("Krylov").set("Iteration Limit", 2*dim);
70
71
// Error Vector
72
ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
73
e->zero();
74
75
// Define Solver
76
ROL::OptimizationSolver<RealT>
solver(*problem,*parlist);
77
78
// Run Solver
79
solver.
solve
(*outStream);
80
81
// Compute Error
82
RealT
err(0);
83
for
(
int
i = 0; i < static_cast<int>(z.size()); ++i) {
84
e->set(*x0);
85
//std::cout << "\n\n e dim =" << e->dimension() << " z[i] dim =" << z[i]->dimension() << "\n\n";
86
e->axpy(-1.0,*z[i]);
87
if
(i == 0) {
88
err = e->norm();
89
}
90
else
{
91
err = std::min(err,e->norm());
92
}
93
}
94
*outStream << std::endl <<
"Norm of Error: "
<< err << std::endl;
95
96
// Update error flag
97
ROL::Ptr<const ROL::AlgorithmState<RealT> > state = solver.
getAlgorithmState
();
98
errorFlag += ((err < std::max(1.e-6*z[0]->norm(),1.e-8) || (state->gnorm < 1.e-6)) ? 0 : 1);
99
}
100
}
101
}
102
}
103
catch
(std::logic_error& err) {
104
*outStream << err.what() << std::endl;
105
errorFlag = -1000;
106
};
// end try
107
108
if
(errorFlag != 0)
109
std::cout <<
"End Result: TEST FAILED"
<< std::endl;
110
else
111
std::cout <<
"End Result: TEST PASSED"
<< std::endl;
112
113
return
0;
114
115
}
ROL_GetTestProblems.hpp
Contains definitions of test objective functions.
ROL_OptimizationSolver.hpp
ROL_Stream.hpp
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
ROL::OptimizationSolver
Provides a simplified interface for solving a wide range of optimization problems.
Definition
ROL_OptimizationSolver.hpp:29
ROL::OptimizationSolver::getAlgorithmState
ROL::Ptr< const AlgorithmState< Real > > getAlgorithmState(void) const
Return the AlgorithmState.
Definition
ROL_OptimizationSolver.hpp:238
ROL::OptimizationSolver::solve
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
Definition
ROL_OptimizationSolver.hpp:180
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::ETestOptProblemToString
std::string ETestOptProblemToString(ETestOptProblem to)
Definition
ROL_GetTestProblems.hpp:169
ROL::ETestOptProblem
ETestOptProblem
Definition
ROL_GetTestProblems.hpp:119
ROL::TESTOPTPROBLEM_ROSENBROCK
@ TESTOPTPROBLEM_ROSENBROCK
Definition
ROL_GetTestProblems.hpp:120
ROL::TESTOPTPROBLEM_HS55
@ TESTOPTPROBLEM_HS55
Definition
ROL_GetTestProblems.hpp:153
ROL::TESTOPTPROBLEM_CANTILEVERBEAM
@ TESTOPTPROBLEM_CANTILEVERBEAM
Definition
ROL_GetTestProblems.hpp:158
ROL::TESTOPTPROBLEM_HS63
@ TESTOPTPROBLEM_HS63
Definition
ROL_GetTestProblems.hpp:154
ROL::TESTOPTPROBLEM_LAST
@ TESTOPTPROBLEM_LAST
Definition
ROL_GetTestProblems.hpp:166
ROL::TESTOPTPROBLEM_HS41
@ TESTOPTPROBLEM_HS41
Definition
ROL_GetTestProblems.hpp:144
ROL::TESTOPTPROBLEM_HS14
@ TESTOPTPROBLEM_HS14
Definition
ROL_GetTestProblems.hpp:135
ROL::TESTOPTPROBLEM_CYLINDERHEAD
@ TESTOPTPROBLEM_CYLINDERHEAD
Definition
ROL_GetTestProblems.hpp:161
ROL::TESTOPTPROBLEM_CANTILEVER
@ TESTOPTPROBLEM_CANTILEVER
Definition
ROL_GetTestProblems.hpp:162
ROL::TESTOPTPROBLEM_QUARTIC
@ TESTOPTPROBLEM_QUARTIC
Definition
ROL_GetTestProblems.hpp:160
ROL::TYPE_E
@ TYPE_E
Definition
ROL_Types.hpp:227
ROL::TYPE_EB
@ TYPE_EB
Definition
ROL_Types.hpp:228
ROL::GetTestProblem
void GetTestProblem(Ptr< OptimizationProblem< Real > > &problem, Ptr< Vector< Real > > &x0, std::vector< Ptr< Vector< Real > > > &x, const ETestOptProblem test)
Definition
ROL_GetTestProblems.hpp:309
main
int main(int argc, char *argv[])
Definition
step/fletcher/test_03.cpp:25
Generated by
1.17.0