ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
src
function
constraint
ROL_Constraint_Partitioned_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_CONSTRAINT_PARTITIONED_DEF_H
11
#define ROL_CONSTRAINT_PARTITIONED_DEF_H
12
13
namespace
ROL
{
14
15
template
<
typename
Real>
16
Constraint_Partitioned<Real>::Constraint_Partitioned
(
const
std::vector<Ptr<
Constraint<Real>
>> &cvec,
17
bool
isInequality,
18
int
offset)
19
:
cvec_
(cvec),
offset_
(offset),
20
scratch_
(nullPtr),
ncval_
(0),
initialized_
(false) {
21
isInequality_
.clear();
isInequality_
.resize(cvec.size(),isInequality);
22
}
23
24
template
<
typename
Real>
25
Constraint_Partitioned<Real>::Constraint_Partitioned
(
const
std::vector<Ptr<
Constraint<Real>
>> &cvec,
26
std::vector<bool> isInequality,
27
int
offset)
28
:
cvec_
(cvec),
isInequality_
(isInequality),
offset_
(offset),
29
scratch_
(nullPtr),
ncval_
(0),
initialized_
(false) {}
30
31
template
<
typename
Real>
32
int
Constraint_Partitioned<Real>::getNumberConstraintEvaluations
(
void
)
const
{
33
return
ncval_
;
34
}
35
36
template
<
typename
Real>
37
Ptr<Constraint<Real>>
Constraint_Partitioned<Real>::get
(
int
ind)
const
{
38
if
(ind < 0 || ind >=
static_cast<
int
>
(
cvec_
.size())) {
39
throw
Exception::NotImplemented
(
">>> Constraint_Partitioned::get : Index out of bounds!"
);
40
}
41
return
cvec_
[ind];
42
}
43
44
template
<
typename
Real>
45
void
Constraint_Partitioned<Real>::update
(
const
Vector<Real>
&x,
UpdateType
type,
int
iter ) {
46
const
int
ncon =
static_cast<
int
>
(
cvec_
.size());
47
for
(
int
i = 0; i < ncon; ++i) {
48
cvec_
[i]->update(
getOpt
(x),type,iter);
49
}
50
}
51
52
template
<
typename
Real>
53
void
Constraint_Partitioned<Real>::update
(
const
Vector<Real>
&x,
bool
flag,
int
iter ) {
54
const
int
ncon =
static_cast<
int
>
(
cvec_
.size());
55
for
(
int
i = 0; i < ncon; ++i) {
56
cvec_
[i]->update(
getOpt
(x),flag,iter);
57
}
58
}
59
60
template
<
typename
Real>
61
void
Constraint_Partitioned<Real>::value
(
Vector<Real>
&c,
const
Vector<Real>
&x, Real &tol ) {
62
PartitionedVector<Real>
&cpv
63
=
dynamic_cast<
PartitionedVector<Real>
&
>
(c);
64
65
const
int
ncon =
static_cast<
int
>
(
cvec_
.size());
66
int
cnt =
offset_
+1;
67
for
(
int
i = 0; i < ncon; ++i) {
68
cvec_
[i]->value(*cpv.
get
(i),
getOpt
(x), tol);
69
if
(
isInequality_
[i]) {
70
cpv.
get
(i)->axpy(
static_cast<
Real
>
(-1),
getSlack
(x,cnt));
71
cnt++;
72
}
73
}
74
++
ncval_
;
75
}
76
77
template
<
typename
Real>
78
void
Constraint_Partitioned<Real>::applyJacobian
(
Vector<Real>
&jv,
79
const
Vector<Real>
&v,
80
const
Vector<Real>
&x,
81
Real &tol ) {
82
PartitionedVector<Real>
&jvpv
83
=
dynamic_cast<
PartitionedVector<Real>
&
>
(jv);
84
85
const
int
ncon =
static_cast<
int
>
(
cvec_
.size());
86
int
cnt =
offset_
+1;
87
for
(
int
i = 0; i < ncon; ++i) {
88
cvec_
[i]->applyJacobian(*jvpv.
get
(i),
getOpt
(v),
getOpt
(x), tol);
89
if
(
isInequality_
[i]) {
90
jvpv.
get
(i)->axpy(
static_cast<
Real
>
(-1),
getSlack
(v,cnt));
91
cnt++;
92
}
93
}
94
}
95
96
template
<
typename
Real>
97
void
Constraint_Partitioned<Real>::applyAdjointJacobian
(
Vector<Real>
&ajv,
98
const
Vector<Real>
&v,
99
const
Vector<Real>
&x,
100
Real &tol ) {
101
if
(!
initialized_
) {
102
scratch_
=
getOpt
(ajv).clone();
103
initialized_
=
true
;
104
}
105
106
const
PartitionedVector<Real>
&vpv
107
=
dynamic_cast<
const
PartitionedVector<Real>
&
>
(v);
108
109
const
int
ncon =
static_cast<
int
>
(
cvec_
.size());
110
int
cnt =
offset_
+1;
111
getOpt
(ajv).zero();
112
for
(
int
i = 0; i < ncon; ++i) {
113
scratch_
->zero();
114
cvec_
[i]->applyAdjointJacobian(*
scratch_
, *vpv.
get
(i),
getOpt
(x), tol);
115
getOpt
(ajv).plus(*
scratch_
);
116
if
(
isInequality_
[i]) {
117
getSlack
(ajv,cnt).set(*vpv.
get
(i));
118
getSlack
(ajv,cnt).scale(
static_cast<
Real
>
(-1));
119
cnt++;
120
}
121
}
122
}
123
124
template
<
typename
Real>
125
void
Constraint_Partitioned<Real>::applyAdjointHessian
(
Vector<Real>
&ahuv,
126
const
Vector<Real>
&u,
127
const
Vector<Real>
&v,
128
const
Vector<Real>
&x,
129
Real &tol ) {
130
if
(!
initialized_
) {
131
scratch_
=
getOpt
(ahuv).clone();
132
initialized_
=
true
;
133
}
134
135
const
PartitionedVector<Real>
&upv
136
=
dynamic_cast<
const
PartitionedVector<Real>
&
>
(u);
137
138
const
int
ncon =
static_cast<
int
>
(
cvec_
.size());
139
int
cnt =
offset_
+1;
140
getOpt
(ahuv).zero();
141
for
(
int
i = 0; i < ncon; ++i) {
142
Ptr<const Vector<Real>> ui = upv.
get
(i);
143
scratch_
->zero();
144
cvec_
[i]->applyAdjointHessian(*
scratch_
, *ui,
getOpt
(v),
getOpt
(x), tol);
145
getOpt
(ahuv).plus(*
scratch_
);
146
if
(
isInequality_
[i]) {
147
getSlack
(ahuv,cnt).zero();
148
cnt++;
149
}
150
}
151
}
152
153
template
<
typename
Real>
154
void
Constraint_Partitioned<Real>::applyPreconditioner
(
Vector<Real>
&pv,
155
const
Vector<Real>
&v,
156
const
Vector<Real>
&x,
157
const
Vector<Real>
&g,
158
Real &tol) {
159
PartitionedVector<Real>
&pvpv
160
=
dynamic_cast<
PartitionedVector<Real>
&
>
(pv);
161
const
PartitionedVector<Real>
&vpv
162
=
dynamic_cast<
const
PartitionedVector<Real>
&
>
(v);
163
164
const
int
ncon =
static_cast<
int
>
(
cvec_
.size());
165
for
(
int
i = 0; i < ncon; ++i) {
166
cvec_
[i]->applyPreconditioner(*pvpv.
get
(i), *vpv.
get
(i),
getOpt
(x),
getOpt
(g), tol);
167
}
168
}
169
170
template
<
typename
Real>
171
void
Constraint_Partitioned<Real>::setParameter
(
const
std::vector<Real> ¶m) {
172
Constraint<Real>::setParameter
(param);
173
const
int
ncon =
static_cast<
int
>
(
cvec_
.size());
174
for
(
int
i = 0; i < ncon; ++i) {
175
cvec_
[i]->setParameter(param);
176
}
177
}
178
179
template
<
typename
Real>
180
Vector<Real>
&
Constraint_Partitioned<Real>::getOpt
(
Vector<Real>
&xs )
const
{
181
try
{
182
return
*
dynamic_cast<
PartitionedVector<Real>
&
>
(xs).
get
(0);
183
}
184
catch
(std::exception &e) {
185
return
xs;
186
}
187
}
188
189
template
<
typename
Real>
190
const
Vector<Real>
&
Constraint_Partitioned<Real>::getOpt
(
const
Vector<Real>
&xs )
const
{
191
try
{
192
return
*
dynamic_cast<
const
PartitionedVector<Real>
&
>
(xs).
get
(0);
193
}
194
catch
(std::exception &e) {
195
return
xs;
196
}
197
}
198
199
template
<
typename
Real>
200
Vector<Real>
&
Constraint_Partitioned<Real>::getSlack
(
Vector<Real>
&xs,
int
ind )
const
{
201
return
*
dynamic_cast<
PartitionedVector<Real>
&
>
(xs).
get
(ind);
202
}
203
204
template
<
typename
Real>
205
const
Vector<Real>
&
Constraint_Partitioned<Real>::getSlack
(
const
Vector<Real>
&xs,
int
ind )
const
{
206
return
*
dynamic_cast<
const
PartitionedVector<Real>
&
>
(xs).
get
(ind);
207
}
208
209
}
// namespace ROL
210
211
#endif
ROL::Constraint_Partitioned::setParameter
void setParameter(const std::vector< Real > ¶m) override
Definition
ROL_Constraint_Partitioned_Def.hpp:171
ROL::Constraint_Partitioned::applyAdjointJacobian
void applyAdjointJacobian(Vector< Real > &ajv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Definition
ROL_Constraint_Partitioned_Def.hpp:97
ROL::Constraint_Partitioned::getSlack
Vector< Real > & getSlack(Vector< Real > &xs, int ind) const
Definition
ROL_Constraint_Partitioned_Def.hpp:200
ROL::Constraint_Partitioned::getOpt
Vector< Real > & getOpt(Vector< Real > &xs) const
Definition
ROL_Constraint_Partitioned_Def.hpp:180
ROL::Constraint_Partitioned::offset_
const int offset_
Definition
ROL_Constraint_Partitioned.hpp:29
ROL::Constraint_Partitioned::Constraint_Partitioned
Constraint_Partitioned(const std::vector< Ptr< Constraint< Real > > > &cvec, bool isInequality=false, int offset=0)
Definition
ROL_Constraint_Partitioned_Def.hpp:16
ROL::Constraint_Partitioned::value
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol) override
Definition
ROL_Constraint_Partitioned_Def.hpp:61
ROL::Constraint_Partitioned::get
Ptr< Constraint< Real > > get(int ind=0) const
Definition
ROL_Constraint_Partitioned_Def.hpp:37
ROL::Constraint_Partitioned::getNumberConstraintEvaluations
int getNumberConstraintEvaluations(void) const
Definition
ROL_Constraint_Partitioned_Def.hpp:32
ROL::Constraint_Partitioned::initialized_
bool initialized_
Definition
ROL_Constraint_Partitioned.hpp:32
ROL::Constraint_Partitioned::cvec_
std::vector< Ptr< Constraint< Real > > > cvec_
Definition
ROL_Constraint_Partitioned.hpp:27
ROL::Constraint_Partitioned::applyAdjointHessian
void applyAdjointHessian(Vector< Real > &ahuv, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Definition
ROL_Constraint_Partitioned_Def.hpp:125
ROL::Constraint_Partitioned::applyJacobian
void applyJacobian(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Definition
ROL_Constraint_Partitioned_Def.hpp:78
ROL::Constraint_Partitioned::isInequality_
std::vector< bool > isInequality_
Definition
ROL_Constraint_Partitioned.hpp:28
ROL::Constraint_Partitioned::applyPreconditioner
virtual void applyPreconditioner(Vector< Real > &pv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g, Real &tol) override
Definition
ROL_Constraint_Partitioned_Def.hpp:154
ROL::Constraint_Partitioned::ncval_
int ncval_
Definition
ROL_Constraint_Partitioned.hpp:31
ROL::Constraint_Partitioned::scratch_
Ptr< Vector< Real > > scratch_
Definition
ROL_Constraint_Partitioned.hpp:30
ROL::Constraint_Partitioned::update
void update(const Vector< Real > &x, UpdateType type, int iter=-1) override
Definition
ROL_Constraint_Partitioned_Def.hpp:45
ROL::Constraint::setParameter
virtual void setParameter(const std::vector< Real > ¶m)
Definition
ROL_Constraint.hpp:374
ROL::Exception::NotImplemented
Definition
ROL_Types.hpp:884
ROL::PartitionedVector
Defines the linear algebra of vector space on a generic partitioned vector.
Definition
ROL_PartitionedVector.hpp:26
ROL::PartitionedVector::get
ROL::Ptr< const Vector< Real > > get(size_type i) const
Definition
ROL_PartitionedVector.hpp:221
ROL::ROL::Constraint::Constraint
Constraint(void)
Definition
ROL_Constraint_SerialSimOpt.hpp:59
ROL::Vector
Defines the linear algebra or vector space interface.
Definition
ROL_Vector.hpp:51
ROL
Definition
ROL_ElementwiseVector.hpp:27
ROL::UpdateType
UpdateType
Definition
ROL_UpdateType.hpp:18
Generated by
1.17.0