ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
function
function/test_06.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
10
#include "
ROL_Stream.hpp
"
11
#include "ROL_GlobalMPISession.hpp"
12
13
#include "
ROL_StdVector.hpp
"
14
#include "
ROL_StdObjective.hpp
"
15
16
typedef
double
RealT
;
17
18
template
<
class
Real>
19
class
ObjectiveFunctionTest06
:
public
ROL::StdObjective
<Real> {
20
public
:
21
Real
value
(
const
std::vector<Real> &x, Real &tol ) {
22
Real quad(0), lin(0);
23
unsigned
size = x.size();
24
std::vector<Real> p(size+2,1);
25
for
(
unsigned
i = 0; i < size; i++ ) {
26
quad += x[i]*x[i];
27
lin += x[i]*p[i+1];
28
}
29
return
std::exp(p[0])*quad + lin + p[size+1];
30
}
31
32
void
gradient
( std::vector<Real> &g,
const
std::vector<Real> &x, Real &tol ) {
33
unsigned
size = x.size();
34
std::vector<Real> p(size+2,1);
35
const
Real two(2);
36
for
(
unsigned
i = 0; i < size; i++ ) {
37
g[i] = two*std::exp(p[0])*x[i] + p[i+1];
38
}
39
}
40
41
void
hessVec
( std::vector<Real> &hv,
const
std::vector<Real> &v,
const
std::vector<Real> &x, Real &tol ) {
42
unsigned
size = x.size();
43
std::vector<Real> p(size+2,1);
44
const
Real two(2);
45
for
(
unsigned
i = 0; i < size; i++ ) {
46
hv[i] = two*std::exp(p[0])*v[i];
47
}
48
}
49
};
50
51
void
setRandomVector
(std::vector<RealT> &x) {
52
unsigned
dim
= x.size();
53
for
(
unsigned
i = 0; i <
dim
; i++ ) {
54
x[i] = (
RealT
)rand()/(
RealT
)RAND_MAX;
55
}
56
}
57
58
int
main
(
int
argc,
char
* argv[]) {
59
60
ROL::GlobalMPISession mpiSession(&argc, &argv);
61
62
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
63
int
iprint = argc - 1;
64
ROL::Ptr<std::ostream> outStream;
65
ROL::nullstream
bhs;
// outputs nothing
66
if
(iprint > 0)
67
outStream = ROL::makePtrFromRef(std::cout);
68
else
69
outStream = ROL::makePtrFromRef(bhs);
70
71
int
errorFlag = 0;
72
73
try
{
74
/**********************************************************************************************/
75
/************************* CONSTRUCT SOL COMPONENTS *******************************************/
76
/**********************************************************************************************/
77
// Build vectors
78
unsigned
dim
= 4;
79
ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(
dim
,0.0);
80
ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::StdVector<RealT>>(x_ptr);
81
setRandomVector
(*x_ptr);
82
ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(
dim
,0.0);
83
ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::StdVector<RealT>>(d_ptr);
84
setRandomVector
(*d_ptr);
85
// Build objective function
86
ROL::Ptr<ROL::StdObjective<RealT> > obj =
87
ROL::makePtr<ObjectiveFunctionTest06<RealT>>();
88
// Test parametrized objective functions
89
*outStream <<
"Check Derivatives of StdObjective\n"
;
90
obj->checkGradient(*x,*d,
true
,*outStream);
91
obj->checkHessVec(*x,*d,
true
,*outStream);
92
}
93
catch
(std::logic_error& err) {
94
*outStream << err.what() <<
"\n"
;
95
errorFlag = -1000;
96
};
// end try
97
98
if
(errorFlag != 0)
99
std::cout <<
"End Result: TEST FAILED\n"
;
100
else
101
std::cout <<
"End Result: TEST PASSED\n"
;
102
103
return
0;
104
}
ROL_StdObjective.hpp
ROL_StdVector.hpp
ROL_Stream.hpp
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
ObjectiveFunctionTest06
Definition
function/test_06.cpp:19
ObjectiveFunctionTest06::gradient
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Definition
function/test_06.cpp:32
ObjectiveFunctionTest06::value
Real value(const std::vector< Real > &x, Real &tol)
Definition
function/test_06.cpp:21
ObjectiveFunctionTest06::hessVec
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition
function/test_06.cpp:41
ROL::StdObjective
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector's.
Definition
ROL_StdObjective.hpp:26
RealT
double RealT
Definition
function/constraint/test_01.cpp:29
main
int main(int argc, char *argv[])
Definition
function/test_06.cpp:58
setRandomVector
void setRandomVector(std::vector< RealT > &x)
Definition
function/test_06.cpp:51
ROL::details::nullstream
basic_nullstream< char, std::char_traits< char > > nullstream
Definition
ROL_Stream.hpp:36
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0