ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
step
step/test_14.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
#include "
ROL_GetTestProblems.hpp
"
15
#include "
ROL_OptimizationSolver.hpp
"
16
#include "
ROL_Stream.hpp
"
17
#include "ROL_GlobalMPISession.hpp"
18
19
20
#include <iostream>
21
22
typedef
double
RealT
;
23
24
int
main
(
int
argc,
char
*argv[]) {
25
26
ROL::GlobalMPISession mpiSession(&argc, &argv);
27
28
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
29
int
iprint = argc - 1;
30
ROL::Ptr<std::ostream> outStream;
31
ROL::nullstream
bhs;
// outputs nothing
32
if
(iprint > 0)
33
outStream = ROL::makePtrFromRef(std::cout);
34
else
35
outStream = ROL::makePtrFromRef(bhs);
36
37
int
errorFlag = 0;
38
39
// *** Test body.
40
41
try
{
42
std::string filename =
"input.xml"
;
43
44
auto
parlist = ROL::getParametersFromXmlFile( filename );
45
46
// Setup optimization problem
47
ROL::Ptr<ROL::Vector<RealT> > x0;
48
std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
49
ROL::Ptr<ROL::OptimizationProblem<RealT> > optProblem;
50
ROL::Ptr<ROL::BoundConstraint<RealT> > bnd;
51
ROL::GetTestProblem<RealT>
(optProblem,x0,z,
ROL::TESTOPTPROBLEM_HS1
);
52
53
// Get Dimension of Problem
54
int
dim
= x0->dimension();
55
parlist->sublist(
"General"
).sublist(
"Krylov"
).set(
"Iteration Limit"
, 2*
dim
);
56
57
// Check Derivatives
58
optProblem->check(*outStream);
59
60
// Error Vector
61
ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
62
e->zero();
63
64
// Setup optimization solver
65
parlist->sublist(
"Status Test"
).set(
"Gradient Tolerance"
,
static_cast<
RealT
>
(1e-6));
66
parlist->sublist(
"Step"
).set(
"Type"
,
"Moreau-Yosida Penalty"
);
67
ROL::OptimizationSolver<RealT>
optSolver(*optProblem,*parlist);
68
optSolver.
solve
(*outStream);
69
70
// Compute Error
71
RealT
err(0);
72
for
(
int
i = 0; i < static_cast<int>(z.size()); ++i) {
73
e->set(*x0);
74
e->axpy(-1.0,*z[i]);
75
if
(i == 0) {
76
err = e->norm();
77
}
78
else
{
79
err = std::min(err,e->norm());
80
}
81
}
82
*outStream << std::endl <<
"Norm of Error: "
<< err << std::endl;
83
84
RealT
tol =
static_cast<
RealT
>
(1e-3)*std::max(z[0]->norm(),
static_cast<
RealT
>
(1));
85
errorFlag += ((err < tol) ? 0 : 1);
86
}
87
catch
(std::logic_error& err) {
88
*outStream << err.what() << std::endl;
89
errorFlag = -1000;
90
};
// end try
91
92
if
(errorFlag != 0)
93
std::cout <<
"End Result: TEST FAILED"
<< std::endl;
94
else
95
std::cout <<
"End Result: TEST PASSED"
<< std::endl;
96
97
return
0;
98
99
}
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::TESTOPTPROBLEM_HS1
@ TESTOPTPROBLEM_HS1
Definition
ROL_GetTestProblems.hpp:129
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_14.cpp:24
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0