ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
step
step/test_15.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 1
15
16
#include "
ROL_GetTestProblems.hpp
"
17
#include "
ROL_OptimizationSolver.hpp
"
18
#include "
ROL_Stream.hpp
"
19
#include "ROL_GlobalMPISession.hpp"
20
21
#include <iostream>
22
//#include <fenv.h>
23
24
typedef
double
RealT
;
25
26
int
main
(
int
argc,
char
*argv[]) {
27
//feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
28
29
ROL::GlobalMPISession mpiSession(&argc, &argv);
30
31
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
32
int
iprint = argc - 1;
33
ROL::Ptr<std::ostream> outStream;
34
ROL::nullstream
bhs;
// outputs nothing
35
if
(iprint > 0)
36
outStream = ROL::makePtrFromRef(std::cout);
37
else
38
outStream = ROL::makePtrFromRef(bhs);
39
40
int
errorFlag = 0;
41
42
// *** Test body.
43
44
try
{
45
46
std::string filename =
"input.xml"
;
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"
,
"Trust Region"
);
53
54
for
(
ROL::ETestOptProblem
prob =
ROL::TESTOPTPROBLEM_ROSENBROCK
; prob <
ROL::TESTOPTPROBLEM_LAST
; prob++ ) {
55
// Get Objective Function
56
ROL::Ptr<ROL::Vector<RealT> > x0;
57
std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
58
ROL::Ptr<ROL::OptimizationProblem<RealT> > problem;
59
ROL::GetTestProblem<RealT>
(problem,x0,z,prob);
60
if
(problem->getProblemType() ==
ROL::TYPE_B
) {
61
if
( prob ==
ROL::TESTOPTPROBLEM_HS2
|| prob ==
ROL::TESTOPTPROBLEM_BVP
) {
62
parlist->sublist(
"Step"
).sublist(
"Line Search"
).set(
"Initial Step Size"
,1.e-4);
63
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Initial Radius"
,-1.e1);
64
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Safeguard Size"
,1.e-4);
65
parlist->sublist(
"Status Test"
).set(
"Gradient Tolerance"
,1.e-6);
66
}
67
else
if
( prob ==
ROL::TESTOPTPROBLEM_HS25
) {
68
parlist->sublist(
"Step"
).sublist(
"Line Search"
).set(
"Initial Step Size"
,1.0);
69
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Initial Radius"
,1.e3);
70
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Safeguard Size"
,1.e4);
71
parlist->sublist(
"Status Test"
).set(
"Gradient Tolerance"
,1.e-8);
72
}
73
else
{
74
parlist->sublist(
"Step"
).sublist(
"Line Search"
).set(
"Initial Step Size"
,1.0);
75
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Initial Radius"
,-1.e1);
76
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Safeguard Size"
,1.e4);
77
parlist->sublist(
"Status Test"
).set(
"Gradient Tolerance"
,1.e-6);
78
}
79
parlist->sublist(
"General"
).set(
"Scale for Epsilon Active Sets"
,1.0);
80
if
( prob ==
ROL::TESTOPTPROBLEM_HS4
) {
81
parlist->sublist(
"General"
).set(
"Scale for Epsilon Active Sets"
,1.e-2);
82
}
83
*outStream << std::endl << std::endl <<
ROL:: ETestOptProblemToString
(prob) << std::endl << std::endl;
84
85
// Get Dimension of Problem
86
int
dim
= x0->dimension();
87
parlist->sublist(
"General"
).sublist(
"Krylov"
).set(
"Iteration Limit"
, 2*
dim
);
88
89
// Error Vector
90
ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
91
e->zero();
92
93
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Subproblem Solver"
,
"Lin-More"
);
94
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Subproblem Model"
,
"Lin-More"
);
95
*outStream << std::endl << std::endl <<
"Lin-More"
<< std::endl << std::endl;
96
97
// Define Solver
98
ROL::OptimizationSolver<RealT>
solver(*problem,*parlist);
99
100
// Run Solver
101
solver.
solve
(*outStream);
102
103
// Compute Error
104
RealT
err(0);
105
for
(
int
i = 0; i < static_cast<int>(z.size()); ++i) {
106
e->set(*x0);
107
e->axpy(-1.0,*z[i]);
108
if
(i == 0) {
109
err = e->norm();
110
}
111
else
{
112
err = std::min(err,e->norm());
113
}
114
}
115
*outStream << std::endl <<
"Norm of Error: "
<< err << std::endl;
116
}
117
}
118
}
119
catch
(std::logic_error& err) {
120
*outStream << err.what() << std::endl;
121
errorFlag = -1000;
122
};
// end try
123
124
if
(errorFlag != 0)
125
std::cout <<
"End Result: TEST FAILED"
<< std::endl;
126
else
127
std::cout <<
"End Result: TEST PASSED"
<< std::endl;
128
129
return
0;
130
131
}
132
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_HS2
@ TESTOPTPROBLEM_HS2
Definition
ROL_GetTestProblems.hpp:130
ROL::TESTOPTPROBLEM_HS25
@ TESTOPTPROBLEM_HS25
Definition
ROL_GetTestProblems.hpp:138
ROL::TESTOPTPROBLEM_LAST
@ TESTOPTPROBLEM_LAST
Definition
ROL_GetTestProblems.hpp:166
ROL::TESTOPTPROBLEM_BVP
@ TESTOPTPROBLEM_BVP
Definition
ROL_GetTestProblems.hpp:155
ROL::TESTOPTPROBLEM_HS4
@ TESTOPTPROBLEM_HS4
Definition
ROL_GetTestProblems.hpp:132
ROL::TYPE_B
@ TYPE_B
Definition
ROL_Types.hpp:226
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_15.cpp:26
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0