ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
src
function
objective
ROL_CompositeObjective_Def.hpp
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
#ifndef ROL_COMPOSITE_OBJECTIVE_DEF_H
11
#define ROL_COMPOSITE_OBJECTIVE_DEF_H
12
13
namespace
ROL
{
14
15
template
<
typename
Real>
16
CompositeObjective<Real>::CompositeObjective
(
const
std::vector<Ptr<
Objective<Real>
>> &obj_vec,
17
const
Ptr<
StdObjective<Real>
> &std_obj)
18
:
obj_vec_
(obj_vec),
std_obj_
(std_obj),
isInitialized_
(false),
19
isValueComputed_
(false),
isGradientComputed_
(false) {
20
obj_value_
= makePtr<std::vector<Real>>(
obj_vec_
.size(),0);
21
obj_value_vec_
= makePtr<StdVector<Real>>(
obj_value_
);
22
obj_grad_
= makePtr<std::vector<Real>>(
obj_vec_
.size(),0);
23
obj_grad_vec_
= makePtr<StdVector<Real>>(
obj_grad_
);
24
obj_gv_
= makePtr<std::vector<Real>>(
obj_vec_
.size(),0);
25
obj_gv_vec_
= makePtr<StdVector<Real>>(
obj_gv_
);
26
obj_hess_
= makePtr<std::vector<Real>>(
obj_vec_
.size(),0);
27
obj_hess_vec_
= makePtr<StdVector<Real>>(
obj_hess_
);
28
}
29
30
template
<
typename
Real>
31
void
CompositeObjective<Real>::update
(
const
Vector<Real>
&x,
UpdateType
type,
int
iter ) {
32
int
size =
obj_vec_
.size();
33
for
(
int
i = 0; i < size; ++i) {
34
obj_vec_
[i]->update(x,type,iter);
35
}
36
isValueComputed_
=
false
;
37
isGradientComputed_
= (type==
UpdateType::Trial
|| type==
UpdateType::Revert
?
isGradientComputed_
:
false
);
38
}
39
40
template
<
typename
Real>
41
void
CompositeObjective<Real>::update
(
const
Vector<Real>
&x,
bool
flag,
int
iter ) {
42
int
size =
obj_vec_
.size();
43
for
(
int
i = 0; i < size; ++i) {
44
obj_vec_
[i]->update(x,flag,iter);
45
}
46
isValueComputed_
=
false
;
47
isGradientComputed_
= (flag ? false :
isGradientComputed_
);
48
}
49
50
template
<
typename
Real>
51
Real
CompositeObjective<Real>::value
(
const
Vector<Real>
&x, Real &tol ) {
52
computeValue
(x,tol);
53
return
std_obj_
->value(*
obj_value_vec_
,tol);
54
}
55
56
template
<
typename
Real>
57
void
CompositeObjective<Real>::gradient
(
Vector<Real>
&g,
const
Vector<Real>
&x, Real &tol ) {
58
g.
zero
();
59
computeGradient
(x,tol);
60
int
size =
obj_vec_
.size();
61
for
(
int
i = 0; i < size; ++i) {
62
g.
axpy
((*
obj_grad_
)[i],*(
vec_grad_
[i]));
63
}
64
}
65
66
template
<
typename
Real>
67
void
CompositeObjective<Real>::hessVec
(
Vector<Real>
&hv,
const
Vector<Real>
&v,
const
Vector<Real>
&x, Real &tol ) {
68
hv.
zero
();
69
computeHessVec
(v,x,tol);
70
int
size =
obj_vec_
.size();
71
for
(
int
i = 0; i < size; ++i) {
72
hv.
axpy
((*
obj_grad_
)[i],*(
vec_hess_
[i]));
73
hv.
axpy
((*
obj_hess_
)[i],*(
vec_grad_
[i]));
74
}
75
}
76
77
template
<
typename
Real>
78
void
CompositeObjective<Real>::setParameter
(
const
std::vector<Real> ¶m) {
79
Objective<Real>::setParameter
(param);
80
const
int
size =
obj_vec_
.size();
81
for
(
int
i = 0; i < size; ++i) {
82
obj_vec_
[i]->setParameter(param);
83
}
84
std_obj_
->setParameter(param);
85
isValueComputed_
=
false
;
// Recompute value every time
86
isGradientComputed_
=
false
;
// Recompute gradient every time
87
}
88
89
template
<
typename
Real>
90
void
CompositeObjective<Real>::initialize
(
const
Vector<Real>
&x) {
91
if
(!
isInitialized_
){
92
int
size =
obj_vec_
.size();
93
vec_grad_
.clear();
vec_grad_
.resize(size,nullPtr);
94
vec_hess_
.clear();
vec_hess_
.resize(size,nullPtr);
95
for
(
int
i = 0; i < size; ++i) {
96
vec_grad_
[i] = x.
dual
().
clone
();
97
vec_hess_
[i] = x.
dual
().
clone
();
98
}
99
isInitialized_
=
true
;
100
}
101
}
102
103
template
<
typename
Real>
104
void
CompositeObjective<Real>::computeValue
(
const
Vector<Real>
&x, Real &tol) {
105
initialize
(x);
106
if
(!
isValueComputed_
) {
107
int
size =
obj_vec_
.size();
108
for
(
int
i = 0; i < size; ++i) {
109
(*obj_value_)[i] =
obj_vec_
[i]->value(x,tol);
110
}
111
isValueComputed_
=
true
;
112
}
113
}
114
115
template
<
typename
Real>
116
void
CompositeObjective<Real>::computeGradient
(
const
Vector<Real>
&x, Real &tol) {
117
computeValue
(x,tol);
118
if
(!
isGradientComputed_
) {
119
std_obj_
->gradient(*(
obj_grad_vec_
),*(
obj_value_vec_
),tol);
120
int
size =
obj_vec_
.size();
121
for
(
int
i = 0; i < size; ++i) {
122
obj_vec_
[i]->gradient(*(
vec_grad_
[i]),x,tol);
123
}
124
isGradientComputed_
=
true
;
125
}
126
}
127
128
template
<
typename
Real>
129
void
CompositeObjective<Real>::computeHessVec
(
const
Vector<Real>
&v,
const
Vector<Real>
&x, Real &tol) {
130
computeGradient
(x,tol);
131
int
size =
obj_vec_
.size();
132
for
(
int
i = 0; i < size; ++i) {
133
//(*obj_gv_)[i] = vec_grad_[i]->dot(v.dual());
134
(*obj_gv_)[i] =
vec_grad_
[i]->apply(v);
135
obj_vec_
[i]->hessVec(*(
vec_hess_
[i]),v,x,tol);
136
}
137
std_obj_
->hessVec(*(
obj_hess_vec_
),*(
obj_gv_vec_
),*(
obj_value_vec_
),tol);
138
}
139
140
}
// namespace ROL
141
142
#endif
initialize
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
Definition
ROL_RandVarFunctional.hpp:219
ROL::CompositeObjective::initialize
void initialize(const Vector< Real > &x)
Definition
ROL_CompositeObjective_Def.hpp:90
ROL::CompositeObjective::setParameter
void setParameter(const std::vector< Real > ¶m) override
Definition
ROL_CompositeObjective_Def.hpp:78
ROL::CompositeObjective::obj_gv_
Ptr< std::vector< Real > > obj_gv_
Definition
ROL_CompositeObjective.hpp:29
ROL::CompositeObjective::obj_value_
Ptr< std::vector< Real > > obj_value_
Definition
ROL_CompositeObjective.hpp:29
ROL::CompositeObjective::obj_grad_
Ptr< std::vector< Real > > obj_grad_
Definition
ROL_CompositeObjective.hpp:29
ROL::CompositeObjective::obj_gv_vec_
Ptr< StdVector< Real > > obj_gv_vec_
Definition
ROL_CompositeObjective.hpp:30
ROL::CompositeObjective::gradient
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol) override
Definition
ROL_CompositeObjective_Def.hpp:57
ROL::CompositeObjective::obj_hess_
Ptr< std::vector< Real > > obj_hess_
Definition
ROL_CompositeObjective.hpp:29
ROL::CompositeObjective::obj_hess_vec_
Ptr< StdVector< Real > > obj_hess_vec_
Definition
ROL_CompositeObjective.hpp:30
ROL::CompositeObjective::vec_hess_
std::vector< Ptr< Vector< Real > > > vec_hess_
Definition
ROL_CompositeObjective.hpp:31
ROL::CompositeObjective::obj_grad_vec_
Ptr< StdVector< Real > > obj_grad_vec_
Definition
ROL_CompositeObjective.hpp:30
ROL::CompositeObjective::computeValue
void computeValue(const Vector< Real > &x, Real &tol)
Definition
ROL_CompositeObjective_Def.hpp:104
ROL::CompositeObjective::hessVec
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Definition
ROL_CompositeObjective_Def.hpp:67
ROL::CompositeObjective::update
void update(const Vector< Real > &x, UpdateType type, int iter=-1) override
Definition
ROL_CompositeObjective_Def.hpp:31
ROL::CompositeObjective::obj_vec_
const std::vector< Ptr< Objective< Real > > > obj_vec_
Definition
ROL_CompositeObjective.hpp:26
ROL::CompositeObjective::isInitialized_
bool isInitialized_
Definition
ROL_CompositeObjective.hpp:33
ROL::CompositeObjective::computeHessVec
void computeHessVec(const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Definition
ROL_CompositeObjective_Def.hpp:129
ROL::CompositeObjective::isGradientComputed_
bool isGradientComputed_
Definition
ROL_CompositeObjective.hpp:33
ROL::CompositeObjective::isValueComputed_
bool isValueComputed_
Definition
ROL_CompositeObjective.hpp:33
ROL::CompositeObjective::computeGradient
void computeGradient(const Vector< Real > &x, Real &tol)
Definition
ROL_CompositeObjective_Def.hpp:116
ROL::CompositeObjective::obj_value_vec_
Ptr< StdVector< Real > > obj_value_vec_
Definition
ROL_CompositeObjective.hpp:30
ROL::CompositeObjective::vec_grad_
std::vector< Ptr< Vector< Real > > > vec_grad_
Definition
ROL_CompositeObjective.hpp:31
ROL::CompositeObjective::std_obj_
const Ptr< StdObjective< Real > > std_obj_
Definition
ROL_CompositeObjective.hpp:27
ROL::CompositeObjective::value
Real value(const Vector< Real > &x, Real &tol) override
Definition
ROL_CompositeObjective_Def.hpp:51
ROL::CompositeObjective::CompositeObjective
CompositeObjective(const std::vector< Ptr< Objective< Real > > > &obj_vec, const Ptr< StdObjective< Real > > &std_obj)
Definition
ROL_CompositeObjective_Def.hpp:16
ROL::Objective::setParameter
virtual void setParameter(const std::vector< Real > ¶m)
Definition
ROL_Objective.hpp:509
ROL::ROL::Objective::Objective
Objective()
Definition
ROL_Constraint_SerialSimOpt.hpp:53
ROL::ROL::Vector::clone
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ROL::StdObjective
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector's.
Definition
ROL_StdObjective.hpp:26
ROL::Vector
Defines the linear algebra or vector space interface.
Definition
ROL_Vector.hpp:51
ROL::Vector::dual
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
Definition
ROL_Vector.hpp:193
ROL::Vector::zero
virtual void zero()
Set to zero vector.
Definition
ROL_Vector.hpp:134
ROL::Vector::axpy
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition
ROL_Vector.hpp:120
ROL
Definition
ROL_ElementwiseVector.hpp:27
ROL::UpdateType
UpdateType
Definition
ROL_UpdateType.hpp:18
ROL::UpdateType::Trial
@ Trial
Definition
ROL_UpdateType.hpp:22
ROL::UpdateType::Revert
@ Revert
Definition
ROL_UpdateType.hpp:21
Generated by
1.17.0