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