ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
step
step/test_05.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
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
53
// Krylov parameters.
54
parlist->sublist(
"General"
).sublist(
"Krylov"
).set(
"Type"
,
"Conjugate Residuals"
);
55
parlist->sublist(
"General"
).sublist(
"Krylov"
).set(
"Absolute Tolerance"
, 1.e-8);
56
parlist->sublist(
"General"
).sublist(
"Krylov"
).set(
"Relative Tolerance"
, 1.e-4);
57
parlist->sublist(
"General"
).sublist(
"Krylov"
).set(
"Iteration Limit"
, 50);
58
parlist->sublist(
"Step"
).set(
"Type"
,
"Primal Dual Active Set"
);
59
60
for
(
ROL::ETestOptProblem
prob =
ROL::TESTOPTPROBLEM_ROSENBROCK
; prob <
ROL::TESTOPTPROBLEM_LAST
; prob++ ) {
61
// Get Objective Function
62
ROL::Ptr<ROL::Vector<RealT> > x0;
63
std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
64
ROL::Ptr<ROL::OptimizationProblem<RealT> > problem;
65
ROL::GetTestProblem<RealT>
(problem,x0,z,prob);
66
67
if
(problem->getProblemType() ==
ROL::TYPE_B
) {
68
if
( prob !=
ROL::TESTOPTPROBLEM_HS5
) {
69
// PDAS parameters.
70
if
(prob ==
ROL::TESTOPTPROBLEM_HS1
||
71
prob ==
ROL::TESTOPTPROBLEM_HS2
||
72
prob ==
ROL::TESTOPTPROBLEM_HS3
||
73
prob ==
ROL::TESTOPTPROBLEM_HS4
||
74
prob ==
ROL::TESTOPTPROBLEM_HS45
) {
75
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Step Tolerance"
,1.e-10);
76
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Gradient Tolerance"
,1.e-8);
77
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Iteration Limit"
,1);
78
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Dual Scaling"
,1.e8);
79
}
80
else
if
(prob ==
ROL::TESTOPTPROBLEM_HS5
) {
81
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Step Tolerance"
,1.e-10);
82
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Gradient Tolerance"
,1.e-8);
83
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Iteration Limit"
,10);
84
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Dual Scaling"
,1.e-2);
85
}
86
else
if
(prob ==
ROL::TESTOPTPROBLEM_HS25
) {
87
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Step Tolerance"
,1.e-10);
88
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Gradient Tolerance"
,1.e-8);
89
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Iteration Limit"
,10);
90
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Dual Scaling"
,1.e10);
91
}
92
else
if
(prob ==
ROL::TESTOPTPROBLEM_HS38
) {
93
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Step Tolerance"
,1.e-10);
94
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Gradient Tolerance"
,1.e-8);
95
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Iteration Limit"
,1);
96
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Dual Scaling"
,1.e-3);
97
}
98
else
if
(prob ==
ROL::TESTOPTPROBLEM_BVP
) {
99
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Step Tolerance"
,1.e-10);
100
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Relative Gradient Tolerance"
,1.e-8);
101
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Iteration Limit"
,1);
102
parlist->sublist(
"Step"
).sublist(
"Primal Dual Active Set"
).set(
"Dual Scaling"
,1.e0);
103
}
104
*outStream << std::endl << std::endl <<
ROL:: ETestOptProblemToString
(prob) << std::endl << std::endl;
105
106
// Get Dimension of Problem
107
int
dim
= x0->dimension();
108
parlist->sublist(
"General"
).sublist(
"Krylov"
).set(
"Iteration Limit"
, 2*
dim
);
109
110
// Error Vector
111
ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
112
e->zero();
113
114
// Define Solver
115
ROL::OptimizationSolver<RealT>
solver(*problem,*parlist);
116
117
// Run Solver
118
solver.
solve
(*outStream);
119
120
// Compute Error
121
RealT
err(0);
122
for
(
int
i = 0; i < static_cast<int>(z.size()); ++i) {
123
e->set(*x0);
124
e->axpy(-1.0,*z[i]);
125
if
(i == 0) {
126
err = e->norm();
127
}
128
else
{
129
err = std::min(err,e->norm());
130
}
131
}
132
*outStream << std::endl <<
"Norm of Error: "
<< err << std::endl;
133
134
// Update error flag
135
ROL::Ptr<const ROL::AlgorithmState<RealT> > state = solver.
getAlgorithmState
();
136
errorFlag += ((err < std::max(1.e-6*z[0]->norm(),1.e-8) || (state->gnorm < 1.e-6)) ? 0 : 1);
137
}
138
}
139
}
140
}
141
catch
(std::logic_error& err) {
142
*outStream << err.what() << std::endl;
143
errorFlag = -1000;
144
};
// end try
145
146
if
(errorFlag != 0)
147
std::cout <<
"End Result: TEST FAILED"
<< std::endl;
148
else
149
std::cout <<
"End Result: TEST PASSED"
<< std::endl;
150
151
return
0;
152
153
}
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_HS1
@ TESTOPTPROBLEM_HS1
Definition
ROL_GetTestProblems.hpp:129
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_HS5
@ TESTOPTPROBLEM_HS5
Definition
ROL_GetTestProblems.hpp:133
ROL::TESTOPTPROBLEM_HS3
@ TESTOPTPROBLEM_HS3
Definition
ROL_GetTestProblems.hpp:131
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::TESTOPTPROBLEM_HS38
@ TESTOPTPROBLEM_HS38
Definition
ROL_GetTestProblems.hpp:142
ROL::TESTOPTPROBLEM_HS45
@ TESTOPTPROBLEM_HS45
Definition
ROL_GetTestProblems.hpp:146
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_05.cpp:26
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0