ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
function
function/test_07.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
#include "
ROL_CompositeObjective.hpp
"
16
17
typedef
double
RealT
;
18
19
template
<
class
Real>
20
class
ObjectiveFunctionTest07_1
:
public
ROL::StdObjective
<Real> {
21
public
:
22
Real
value
(
const
std::vector<Real> &x, Real &tol ) {
23
Real half(0.5), quad(0);
24
unsigned
size = x.size();
25
for
(
unsigned
i = 0; i < size; i++ ) {
26
quad += x[i]*x[i];
27
}
28
return
half*quad;
29
}
30
31
void
gradient
( std::vector<Real> &g,
const
std::vector<Real> &x, Real &tol ) {
32
g.assign(x.begin(),x.end());
33
}
34
35
void
hessVec
( std::vector<Real> &hv,
const
std::vector<Real> &v,
const
std::vector<Real> &x, Real &tol ) {
36
hv.assign(v.begin(),v.end());
37
}
38
};
39
40
template
<
class
Real>
41
class
ObjectiveFunctionTest07_2
:
public
ROL::StdObjective
<Real> {
42
public
:
43
Real
value
(
const
std::vector<Real> &x, Real &tol ) {
44
Real lin(0);
45
unsigned
size = x.size();
46
for
(
unsigned
i = 0; i < size; i++ ) {
47
lin += x[i];
48
}
49
return
lin;
50
}
51
52
void
gradient
( std::vector<Real> &g,
const
std::vector<Real> &x, Real &tol ) {
53
g.assign(x.size(),1);
54
}
55
56
void
hessVec
( std::vector<Real> &hv,
const
std::vector<Real> &v,
const
std::vector<Real> &x, Real &tol ) {
57
hv.assign(x.size(),0);
58
}
59
};
60
61
template
<
class
Real>
62
class
ObjectiveFunctionTest07_scalarize
:
public
ROL::StdObjective
<Real> {
63
public
:
64
Real
value
(
const
std::vector<Real> &x, Real &tol ) {
65
return
std::log(x[0]) * std::exp(x[1]);
66
}
67
68
void
gradient
( std::vector<Real> &g,
const
std::vector<Real> &x, Real &tol ) {
69
g[0] = std::exp(x[1])/x[0];
70
g[1] = std::exp(x[1]) * std::log(x[0]);
71
}
72
73
void
hessVec
( std::vector<Real> &hv,
const
std::vector<Real> &v,
const
std::vector<Real> &x, Real &tol ) {
74
Real H11 = -std::exp(x[1])/(x[0]*x[0]);
75
Real H12 = std::exp(x[1])/x[0];
76
Real H21 = std::exp(x[1])/x[0];
77
Real H22 = std::exp(x[1]) * std::log(x[0]);
78
hv[0] = H11*v[0] + H12*v[1];
79
hv[1] = H21*v[0] + H22*v[1];
80
}
81
};
82
83
void
setRandomVector
(std::vector<RealT> &x) {
84
unsigned
dim
= x.size();
85
for
(
unsigned
i = 0; i <
dim
; i++ ) {
86
x[i] = (
RealT
)rand()/(
RealT
)RAND_MAX;
87
}
88
}
89
90
int
main
(
int
argc,
char
* argv[]) {
91
92
ROL::GlobalMPISession mpiSession(&argc, &argv);
93
94
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
95
int
iprint = argc - 1;
96
ROL::Ptr<std::ostream> outStream;
97
ROL::nullstream
bhs;
// outputs nothing
98
if
(iprint > 0)
99
outStream = ROL::makePtrFromRef(std::cout);
100
else
101
outStream = ROL::makePtrFromRef(bhs);
102
103
int
errorFlag = 0;
104
105
try
{
106
/**********************************************************************************************/
107
/************************* CONSTRUCT SOL COMPONENTS *******************************************/
108
/**********************************************************************************************/
109
// Build vectors
110
unsigned
dim
= 4;
111
ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(
dim
,0.0);
112
ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::StdVector<RealT>>(x_ptr);
113
setRandomVector
(*x_ptr);
114
ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(
dim
,0.0);
115
ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::StdVector<RealT>>(d_ptr);
116
setRandomVector
(*d_ptr);
117
// Build objective function
118
std::vector<ROL::Ptr<ROL::Objective<RealT> > > vec_obj(2,ROL::nullPtr);
119
vec_obj[0] = ROL::makePtr<ObjectiveFunctionTest07_1<RealT>>();
120
vec_obj[1] = ROL::makePtr<ObjectiveFunctionTest07_2<RealT>>();
121
ROL::Ptr<ROL::StdObjective<RealT> > obj_scalarize
122
= ROL::makePtr<ObjectiveFunctionTest07_scalarize<RealT>>();
123
ROL::Ptr<ROL::Objective<RealT> > obj
124
= ROL::makePtr<ROL::CompositeObjective<RealT>>(vec_obj,obj_scalarize);
125
// Test parametrized objective functions
126
*outStream <<
"Check Derivatives of CompositeObjective\n"
;
127
obj->checkGradient(*x,*d,
true
,*outStream);
128
obj->checkHessVec(*x,*d,
true
,*outStream);
129
}
130
catch
(std::logic_error& err) {
131
*outStream << err.what() <<
"\n"
;
132
errorFlag = -1000;
133
};
// end try
134
135
if
(errorFlag != 0)
136
std::cout <<
"End Result: TEST FAILED\n"
;
137
else
138
std::cout <<
"End Result: TEST PASSED\n"
;
139
140
return
0;
141
}
ROL_CompositeObjective.hpp
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...
ObjectiveFunctionTest07_1
Definition
function/test_07.cpp:20
ObjectiveFunctionTest07_1::gradient
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Definition
function/test_07.cpp:31
ObjectiveFunctionTest07_1::hessVec
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition
function/test_07.cpp:35
ObjectiveFunctionTest07_1::value
Real value(const std::vector< Real > &x, Real &tol)
Definition
function/test_07.cpp:22
ObjectiveFunctionTest07_2
Definition
function/test_07.cpp:41
ObjectiveFunctionTest07_2::value
Real value(const std::vector< Real > &x, Real &tol)
Definition
function/test_07.cpp:43
ObjectiveFunctionTest07_2::hessVec
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition
function/test_07.cpp:56
ObjectiveFunctionTest07_2::gradient
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Definition
function/test_07.cpp:52
ObjectiveFunctionTest07_scalarize
Definition
function/test_07.cpp:62
ObjectiveFunctionTest07_scalarize::hessVec
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition
function/test_07.cpp:73
ObjectiveFunctionTest07_scalarize::value
Real value(const std::vector< Real > &x, Real &tol)
Definition
function/test_07.cpp:64
ObjectiveFunctionTest07_scalarize::gradient
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Definition
function/test_07.cpp:68
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_07.cpp:90
setRandomVector
void setRandomVector(std::vector< RealT > &x)
Definition
function/test_07.cpp:83
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