ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
step
step/test_04.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
22
#include <iostream>
23
//#include <fenv.h>
24
25
typedef
double
RealT
;
26
27
int
main
(
int
argc,
char
*argv[]) {
28
//feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
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
// *** Test body.
44
45
try
{
46
47
std::string filename =
"input.xml"
;
48
49
auto
parlist = ROL::getParametersFromXmlFile( filename );
50
parlist->sublist(
"General"
).set(
"Inexact Hessian-Times-A-Vector"
,
true
);
51
#if USE_HESSVEC
52
parlist->sublist(
"General"
).set(
"Inexact Hessian-Times-A-Vector"
,
false
);
53
#endif
54
parlist->sublist(
"Step"
).set(
"Type"
,
"Trust Region"
);
55
56
for
(
ROL::ETestOptProblem
prob =
ROL::TESTOPTPROBLEM_ROSENBROCK
; prob <
ROL::TESTOPTPROBLEM_LAST
; prob++ ) {
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
if
(problem->getProblemType() ==
ROL::TYPE_B
) {
63
if
( prob ==
ROL::TESTOPTPROBLEM_HS2
|| prob ==
ROL::TESTOPTPROBLEM_BVP
) {
64
parlist->sublist(
"Step"
).sublist(
"Line Search"
).set(
"Initial Step Size"
,1.e-4);
65
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Initial Radius"
,-1.e1);
66
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Safeguard Size"
,1.e-4);
67
parlist->sublist(
"Status Test"
).set(
"Gradient Tolerance"
,1.e-6);
68
}
69
else
if
( prob ==
ROL::TESTOPTPROBLEM_HS25
) {
70
parlist->sublist(
"Step"
).sublist(
"Line Search"
).set(
"Initial Step Size"
,1.0);
71
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Initial Radius"
,1.e3);
72
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Safeguard Size"
,1.e4);
73
parlist->sublist(
"Status Test"
).set(
"Gradient Tolerance"
,1.e-8);
74
}
75
else
{
76
parlist->sublist(
"Step"
).sublist(
"Line Search"
).set(
"Initial Step Size"
,1.0);
77
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Initial Radius"
,-1.e1);
78
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Safeguard Size"
,1.e4);
79
parlist->sublist(
"Status Test"
).set(
"Gradient Tolerance"
,1.e-6);
80
}
81
parlist->sublist(
"General"
).set(
"Scale for Epsilon Active Sets"
,1.0);
82
if
( prob ==
ROL::TESTOPTPROBLEM_HS4
) {
83
parlist->sublist(
"General"
).set(
"Scale for Epsilon Active Sets"
,1.e-2);
84
}
85
*outStream << std::endl << std::endl <<
ROL:: ETestOptProblemToString
(prob) << std::endl << std::endl;
86
87
// Get Dimension of Problem
88
int
dim
= x0->dimension();
89
parlist->sublist(
"General"
).sublist(
"Krylov"
).set(
"Iteration Limit"
, 2*
dim
);
90
91
// Error Vector
92
ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
93
e->zero();
94
95
//ROL::ETrustRegion tr = ROL::TRUSTREGION_CAUCHYPOINT;
96
//ROL::ETrustRegion tr = ROL::TRUSTREGION_DOGLEG;
97
//ROL::ETrustRegion tr = ROL::TRUSTREGION_DOUBLEDOGLEG;
98
ROL::ETrustRegion
tr =
ROL::TRUSTREGION_TRUNCATEDCG
;
99
//ROL::ETrustRegion tr = ROL::TRUSTREGION_LINMORE;
100
parlist->sublist(
"Step"
).sublist(
"Trust Region"
).set(
"Subproblem Solver"
,
ROL::ETrustRegionToString
(tr));
101
*outStream << std::endl << std::endl <<
ROL::ETrustRegionToString
(tr) << std::endl << std::endl;
102
103
// Define Solver
104
ROL::OptimizationSolver<RealT>
solver(*problem,*parlist);
105
106
// Run Solver
107
solver.
solve
(*outStream);
108
109
// Compute Error
110
RealT
err(0);
111
for
(
int
i = 0; i < static_cast<int>(z.size()); ++i) {
112
e->set(*x0);
113
e->axpy(-1.0,*z[i]);
114
if
(i == 0) {
115
err = e->norm();
116
}
117
else
{
118
err = std::min(err,e->norm());
119
}
120
}
121
*outStream << std::endl <<
"Norm of Error: "
<< err << std::endl;
122
}
123
}
124
}
125
catch
(std::logic_error& err) {
126
*outStream << err.what() << std::endl;
127
errorFlag = -1000;
128
};
// end try
129
130
if
(errorFlag != 0)
131
std::cout <<
"End Result: TEST FAILED"
<< std::endl;
132
else
133
std::cout <<
"End Result: TEST PASSED"
<< std::endl;
134
135
return
0;
136
137
}
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::ETrustRegionToString
std::string ETrustRegionToString(ETrustRegion tr)
Definition
ROL_TrustRegionTypes.hpp:39
ROL::ETrustRegion
ETrustRegion
Definition
ROL_TrustRegionTypes.hpp:30
ROL::TRUSTREGION_TRUNCATEDCG
@ TRUSTREGION_TRUNCATEDCG
Definition
ROL_TrustRegionTypes.hpp:32
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_04.cpp:27
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0