| 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_SACADO_EQUALITYCONSTRAINT |
| 11 |
#define ROL_SACADO_EQUALITYCONSTRAINT |
| 12 |
|
| 13 |
#include "Sacado.hpp" |
| 14 |
#include "ROL_StdVector.hpp" |
| 15 |
#include "ROL_Constraint.hpp" |
| 16 |
|
| 17 |
namespace ROL { |
| 18 |
|
| 19 |
//! \brief ROL interface wrapper for Sacado Constraint |
| 20 |
template<class Real, template<class> class Constr> |
| 21 |
class Sacado_Constraint : public Constraint<Real> { |
| 22 |
|
| 23 |
protected: |
| 24 |
|
| 25 |
Constr<Real> constr_; |
| 26 |
|
| 27 |
int dim_; |
| 28 |
|
| 29 |
template<class ScalarT> |
| 30 |
void applyJacobianAD( Vector<ScalarT> &jv, const Vector<ScalarT> &v, |
| 31 |
const Vector<ScalarT> &x, Real &tol ); |
| 32 |
|
| 33 |
template<class ScalarT> |
| 34 |
void applyAdjointJacobianAD( Vector<ScalarT> &aju, const Vector<ScalarT> &u, |
| 35 |
const Vector<ScalarT> &x, Real &tol); |
| 36 |
|
| 37 |
template<class ScalarT> |
| 38 |
void applyAdjointHessianAD( Vector<ScalarT> &ahuv, const Vector<ScalarT> &u, |
| 39 |
const Vector<ScalarT> &v, const Vector<ScalarT> &x, Real &tol); |
| 40 |
|
| 41 |
|
| 42 |
public: |
| 43 |
|
| 44 |
Sacado_Constraint(int dim) : dim_(dim) {} |
| 45 |
Sacado_Constraint(const Constr<Real> & constr) : constr_(constr) {} |
| 46 |
|
| 47 |
virtual void value(Vector<Real> &c, const Vector<Real> &x, Real &tol) { |
| 48 |
constr_.value(c,x,tol); |
| 49 |
} |
| 50 |
|
| 51 |
virtual void applyJacobian(Vector<Real> &jv, const Vector<Real> &v, |
| 52 |
const Vector<Real> &x, Real &tol) { |
| 53 |
this->applyJacobianAD(jv,v,x,tol); |
| 54 |
} |
| 55 |
|
| 56 |
virtual void applyAdjointJacobian(Vector<Real> &aju, const Vector<Real> &u, |
| 57 |
const Vector<Real> &x, Real &tol) { |
| 58 |
this->applyAdjointJacobianAD(aju,u,x,tol); |
| 59 |
} |
| 60 |
|
| 61 |
void applyAdjointHessian(Vector<Real> &ahuv, const Vector<Real> &u, |
| 62 |
const Vector<Real> &v, const Vector<Real> &x, Real &tol){ |
| 63 |
this->applyAdjointHessianAD(ahuv,u,v,x,tol); |
| 64 |
} |
| 65 |
|
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
template<class Real, template<class> class Constr> |
| 70 |
template<class ScalarT> |
| 71 |
void Sacado_Constraint<Real,Constr>::applyJacobianAD(Vector<ScalarT> &jv, const Vector<ScalarT> &v, |
| 72 |
const Vector<ScalarT> &x, Real &tol) { |
| 73 |
|
| 74 |
// Data type which supports automatic differentiation |
| 75 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 76 |
typedef std::vector<FadType> Fadvector; |
| 77 |
typedef std::vector<ScalarT> vector; |
| 78 |
typedef StdVector<ScalarT> SV; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
ROL::Ptr<const vector> xp = dynamic_cast<const SV&>(x).getVector(); |
| 84 |
|
| 85 |
int n = xp->size(); |
| 86 |
|
| 87 |
// Get a pointer to the direction vector |
| 88 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 89 |
|
| 90 |
ROL::Ptr<vector> jvp = dynamic_cast<SV&>(jv).getVector(); |
| 91 |
|
| 92 |
// Create a vector of independent variables |
| 93 |
ROL::Ptr<Fadvector> x_fad_ptr = ROL::makePtr<Fadvector>(); |
| 94 |
x_fad_ptr->reserve(n); |
| 95 |
|
| 96 |
// Initialize constructor for each element |
| 97 |
for(int i=0; i<n; ++i) { |
| 98 |
x_fad_ptr->push_back(FadType(n,i,(*xp)[i])); |
| 99 |
} |
| 100 |
|
| 101 |
// Create a vector of independent variables |
| 102 |
ROL::Ptr<Fadvector> c_fad_ptr = ROL::makePtr<Fadvector>(); |
| 103 |
c_fad_ptr->reserve(dim_); |
| 104 |
|
| 105 |
for(int j=0; j<dim_; ++j) { |
| 106 |
c_fad_ptr->push_back(0); |
| 107 |
} |
| 108 |
|
| 109 |
StdVector<FadType> x_fad(x_fad_ptr); |
| 110 |
StdVector<FadType> c_fad(c_fad_ptr); |
| 111 |
|
| 112 |
// Evaluate constraint |
| 113 |
constr_.value(c_fad,x_fad,tol); |
| 114 |
|
| 115 |
for(int i=0; i<dim_; ++i) { |
| 116 |
(*jvp)[i] = 0; |
| 117 |
for(int j=0; j<n; ++j) { |
| 118 |
(*jvp)[i] += (*vp)[j]*(*c_fad_ptr)[i].dx(j); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
template<class Real, template<class> class Constr> |
| 126 |
template<class ScalarT> |
| 127 |
void Sacado_Constraint<Real,Constr>::applyAdjointJacobianAD(Vector<ScalarT> &aju, const Vector<ScalarT> &u, |
| 128 |
const Vector<ScalarT> &x, Real &tol) { |
| 129 |
|
| 130 |
// Data type which supports automatic differentiation |
| 131 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 132 |
typedef std::vector<FadType> Fadvector; |
| 133 |
typedef std::vector<ScalarT> vector; |
| 134 |
typedef StdVector<ScalarT> SV; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
// Get a pointer to the optimization vector |
| 140 |
ROL::Ptr<const vector> xp = dynamic_cast<const SV&>(x).getVector(); |
| 141 |
|
| 142 |
// Get a pointer to the direction vector |
| 143 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 144 |
|
| 145 |
ROL::Ptr<vector> ajup = dynamic_cast<SV&>(aju).getVector(); |
| 146 |
|
| 147 |
int n = xp->size(); |
| 148 |
|
| 149 |
// Create a vector of independent variables |
| 150 |
ROL::Ptr<Fadvector> x_fad_ptr = ROL::makePtr<Fadvector>(); |
| 151 |
x_fad_ptr->reserve(n); |
| 152 |
|
| 153 |
// Initialize constructor for each element |
| 154 |
for(int i=0; i<n; ++i) { |
| 155 |
x_fad_ptr->push_back(FadType(n,i,(*xp)[i])); |
| 156 |
} |
| 157 |
|
| 158 |
ROL::Ptr<Fadvector> c_fad_ptr = ROL::makePtr<Fadvector>(); |
| 159 |
c_fad_ptr->reserve(dim_); |
| 160 |
for(int j=0; j<dim_; ++j) { |
| 161 |
c_fad_ptr->push_back(0); |
| 162 |
} |
| 163 |
|
| 164 |
StdVector<FadType> x_fad(x_fad_ptr); |
| 165 |
StdVector<FadType> c_fad(c_fad_ptr); |
| 166 |
|
| 167 |
// Evaluate constraint |
| 168 |
constr_.value(c_fad,x_fad,tol); |
| 169 |
|
| 170 |
FadType udotc = 0; |
| 171 |
|
| 172 |
for(int j=0;j<dim_;++j){ |
| 173 |
udotc += (*c_fad_ptr)[j]*(*up)[j]; |
| 174 |
} |
| 175 |
|
| 176 |
for(int i=0;i<n;++i){ |
| 177 |
(*ajup)[i] = udotc.dx(i); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
template<class Real, template<class> class Constr> |
| 183 |
template<class ScalarT> |
| 184 |
void Sacado_Constraint<Real,Constr>::applyAdjointHessianAD(Vector<ScalarT> &ahuv, const Vector<ScalarT> &u, |
| 185 |
const Vector<ScalarT> &v, const Vector<ScalarT> &x, |
| 186 |
Real &tol){ |
| 187 |
|
| 188 |
// Data type which supports automatic differentiation |
| 189 |
typedef Sacado::Fad::SFad<ScalarT,1> FadType; |
| 190 |
typedef std::vector<FadType> Fadvector; |
| 191 |
typedef std::vector<ScalarT> vector; |
| 192 |
typedef StdVector<ScalarT> SV; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
// Get a pointer to the optimization vector |
| 198 |
ROL::Ptr<const vector> xp = dynamic_cast<const SV&>(x).getVector(); |
| 199 |
|
| 200 |
// Get a pointer to the dual constraint vector |
| 201 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 202 |
|
| 203 |
// Get a pointer to the direction vector |
| 204 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 205 |
|
| 206 |
// Get a pointer to the directional adjoint Hessian |
| 207 |
ROL::Ptr<vector> ahuvp = dynamic_cast<SV&>(ahuv).getVector(); |
| 208 |
|
| 209 |
// Number of optimization variables |
| 210 |
int n = xp->size(); |
| 211 |
|
| 212 |
// Create a vector of independent variables |
| 213 |
ROL::Ptr<Fadvector> x_fad_ptr = ROL::makePtr<Fadvector>(); |
| 214 |
x_fad_ptr->reserve(n); |
| 215 |
|
| 216 |
// Allocate for directional adjoint Jacobian |
| 217 |
ROL::Ptr<Fadvector> aju_fad_ptr = ROL::makePtr<Fadvector>(); |
| 218 |
aju_fad_ptr->reserve(n); |
| 219 |
|
| 220 |
for(int i=0; i<n; ++i) { |
| 221 |
x_fad_ptr->push_back(FadType(1,(*xp)[i])); |
| 222 |
|
| 223 |
// Set derivative direction |
| 224 |
(*x_fad_ptr)[i].fastAccessDx(0) = (*vp)[i]; |
| 225 |
|
| 226 |
aju_fad_ptr->push_back(0); |
| 227 |
} |
| 228 |
|
| 229 |
// Allocate for constraint vector |
| 230 |
ROL::Ptr<Fadvector> c_fad_ptr = ROL::makePtr<Fadvector>(); |
| 231 |
c_fad_ptr->reserve(dim_); |
| 232 |
|
| 233 |
// Allocate for dual constraint vector |
| 234 |
ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>(); |
| 235 |
u_fad_ptr->reserve(dim_); |
| 236 |
|
| 237 |
for(int j=0; j<dim_; ++j) { |
| 238 |
u_fad_ptr->push_back((*up)[j]); |
| 239 |
} |
| 240 |
|
| 241 |
StdVector<FadType> x_fad(x_fad_ptr); |
| 242 |
StdVector<FadType> u_fad(u_fad_ptr); |
| 243 |
StdVector<FadType> aju_fad(aju_fad_ptr); |
| 244 |
|
| 245 |
// Evaluate constraint adjoint Jacobian direction |
| 246 |
this->applyAdjointJacobianAD( aju_fad, u_fad, x_fad, tol); |
| 247 |
|
| 248 |
for(int i=0; i<n; ++i) { |
| 249 |
(*ahuvp)[i] = (*aju_fad_ptr)[i].dx(0); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
} |
| 255 |
#endif |