ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
step
step/test_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
13
14
#define USE_HESSVEC 0
15
16
#include "
ROL_GetTestProblems.hpp
"
17
#include "
ROL_OptimizationSolver.hpp
"
18
#include "
ROL_Stream.hpp
"
19
#include "ROL_GlobalMPISession.hpp"
20
21
22
#include <iostream>
23
24
typedef
double
RealT
;
25
26
int
main
(
int
argc,
char
*argv[]) {
27
28
ROL::GlobalMPISession mpiSession(&argc, &argv);
29
30
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
31
int
iprint = argc - 1;
32
ROL::Ptr<std::ostream> outStream;
33
ROL::nullstream
bhs;
// outputs nothing
34
if
(iprint > 0)
35
outStream = ROL::makePtrFromRef(std::cout);
36
else
37
outStream = ROL::makePtrFromRef(bhs);
38
39
int
errorFlag = 0;
40
41
// *** Test body.
42
43
try
{
44
45
std::string filename =
"input.xml"
;
46
47
auto
parlist = ROL::getParametersFromXmlFile( filename );
48
parlist->sublist(
"General"
).set(
"Inexact Hessian-Times-A-Vector"
,
true
);
49
#if USE_HESSVEC
50
parlist->sublist(
"General"
).set(
"Inexact Hessian-Times-A-Vector"
,
false
);
51
#endif
52
parlist->sublist(
"Step"
).set(
"Type"
,
"Line Search"
);
53
54
for
(
ROL::ETestOptProblem
objFunc =
ROL::TESTOPTPROBLEM_ROSENBROCK
; objFunc <
ROL::TESTOPTPROBLEM_LAST
; objFunc++ ) {
55
for
(
ROL::EDescent
desc =
ROL::DESCENT_STEEPEST
; desc <
ROL::DESCENT_LAST
; desc++ ) {
56
// Set up optimization problem
57
ROL::Ptr<ROL::Vector<RealT> > x0;
58
std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
59
ROL::Ptr<ROL::OptimizationProblem<RealT> > problem;
60
ROL::GetTestProblem<RealT>
(problem,x0,z,objFunc);
61
if
(problem->getProblemType() ==
ROL::TYPE_U
62
&& objFunc !=
ROL::TESTOPTPROBLEM_MINIMAX1
63
&& objFunc !=
ROL::TESTOPTPROBLEM_MINIMAX2
64
&& objFunc !=
ROL::TESTOPTPROBLEM_MINIMAX3
) {
65
*outStream << std::endl << std::endl <<
ROL::ETestOptProblemToString
(objFunc) << 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
parlist->sublist(
"Step"
).sublist(
"Line Search"
).sublist(
"Descent Method"
).set(
"Type"
,
ROL::EDescentToString
(desc));
76
if
( desc ==
ROL::DESCENT_NEWTON
&&
77
((objFunc ==
ROL::TESTOPTPROBLEM_LEASTSQUARES
) ||
78
(objFunc ==
ROL::TESTOPTPROBLEM_POISSONCONTROL
) ||
79
(objFunc ==
ROL::TESTOPTPROBLEM_POISSONINVERSION
)) ) {
80
parlist->sublist(
"Step"
).sublist(
"Line Search"
).sublist(
"Descent Method"
).set(
"Type"
,
ROL::EDescentToString
(
ROL::DESCENT_NEWTONKRYLOV
));
81
}
82
*outStream << std::endl << std::endl <<
ROL::EDescentToString
(desc) << std::endl << std::endl;
83
84
// Define Solver
85
ROL::OptimizationSolver<RealT>
solver(*problem,*parlist);
86
87
// Run Solver
88
solver.
solve
(*outStream);
89
90
// Compute Error
91
RealT
err(0);
92
for
(
int
i = 0; i < static_cast<int>(z.size()); ++i) {
93
e->set(*x0);
94
e->axpy(-1.0,*z[i]);
95
if
(i == 0) {
96
err = e->norm();
97
}
98
else
{
99
err = std::min(err,e->norm());
100
}
101
}
102
*outStream << std::endl <<
"Norm of Error: "
<< err << std::endl;
103
//errorFlag += (int)(e.norm() < std::sqrt(ROL::ROL_EPSILON<RealT>()));
104
}
105
}
106
}
107
}
108
catch
(std::logic_error& err) {
109
*outStream << err.what() << std::endl;
110
errorFlag = -1000;
111
};
// end try
112
113
if
(errorFlag != 0)
114
std::cout <<
"End Result: TEST FAILED"
<< std::endl;
115
else
116
std::cout <<
"End Result: TEST PASSED"
<< std::endl;
117
118
return
0;
119
120
}
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::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_MINIMAX3
@ TESTOPTPROBLEM_MINIMAX3
Definition
ROL_GetTestProblems.hpp:165
ROL::TESTOPTPROBLEM_MINIMAX1
@ TESTOPTPROBLEM_MINIMAX1
Definition
ROL_GetTestProblems.hpp:163
ROL::TESTOPTPROBLEM_LEASTSQUARES
@ TESTOPTPROBLEM_LEASTSQUARES
Definition
ROL_GetTestProblems.hpp:125
ROL::TESTOPTPROBLEM_LAST
@ TESTOPTPROBLEM_LAST
Definition
ROL_GetTestProblems.hpp:166
ROL::TESTOPTPROBLEM_MINIMAX2
@ TESTOPTPROBLEM_MINIMAX2
Definition
ROL_GetTestProblems.hpp:164
ROL::TESTOPTPROBLEM_POISSONINVERSION
@ TESTOPTPROBLEM_POISSONINVERSION
Definition
ROL_GetTestProblems.hpp:127
ROL::TESTOPTPROBLEM_POISSONCONTROL
@ TESTOPTPROBLEM_POISSONCONTROL
Definition
ROL_GetTestProblems.hpp:126
ROL::EDescent
EDescent
Definition
ROL_Types.hpp:381
ROL::DESCENT_NEWTONKRYLOV
@ DESCENT_NEWTONKRYLOV
Definition
ROL_Types.hpp:386
ROL::DESCENT_LAST
@ DESCENT_LAST
Definition
ROL_Types.hpp:387
ROL::DESCENT_STEEPEST
@ DESCENT_STEEPEST
Definition
ROL_Types.hpp:382
ROL::DESCENT_NEWTON
@ DESCENT_NEWTON
Definition
ROL_Types.hpp:385
ROL::EDescentToString
std::string EDescentToString(EDescent tr)
Definition
ROL_Types.hpp:390
ROL::TYPE_U
@ TYPE_U
Definition
ROL_Types.hpp:224
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/test_01.cpp:26
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0