// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef ROL_SACADO_OBJECTIVE #define ROL_SACADO_OBJECTIVE #include "Sacado.hpp" #include "ROL_StdVector.hpp" #include "ROL_Objective_SimOpt.hpp" using namespace ROL; template class Obj> class Sacado_Objective_SimOpt : public Objective_SimOpt { protected: Obj obj_; template void gradient_1AD(Vector &g, const Vector &u, const Vector &z, Real &tol); template void gradient_2AD(Vector &g, const Vector &u, const Vector &z, Real &tol); template void hessVec_11AD(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void hessVec_12AD(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void hessVec_21AD(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void hessVec_22AD(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol); public: Sacado_Objective_SimOpt() : obj_(Obj()) {} Sacado_Objective_SimOpt(const Obj &obj) : obj_(obj) {} Real value(const Vector &u, const Vector &z, Real &tol ) { return obj_.value(u,z,tol); } void gradient_1(Vector &g, const Vector &u, const Vector &z, Real &tol ) { this->gradient_1AD(g,u,z,tol); } void gradient_2(Vector &g, const Vector &u, const Vector &z, Real &tol ) { this->gradient_2AD(g,u,z,tol); } void hessVec_11(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { this->hessVec_11AD(hv,v,u,z,tol); } void hessVec_12(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { this->hessVec_12AD(hv,v,u,z,tol); } void hessVec_21(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { this->hessVec_21AD(hv,v,u,z,tol); } void hessVec_22(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { this->hessVec_22AD(hv,v,u,z,tol); } }; template class Obj> template void Sacado_Objective_SimOpt::gradient_1AD(Vector &g, const Vector &u, const Vector &z, Real &tol) { typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); // Get a pointer to the gradient vector ROL::Ptr gp = dynamic_cast(g).getVector(); int m = zp->size(); int n = up->size(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); // Initialize constructor for each element of u for(int i=0; ipush_back(FadType(n,i,(*up)[i])); } // Initialize constructor for each element of z for(int j=0; jpush_back((*zp)[j]); } StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); FadType J_fad = obj_.value(u_fad,z_fad,tol); // Evaluate gradient for(int i=0; i class Obj> template void Sacado_Objective_SimOpt::gradient_2AD(Vector &g, const Vector &u, const Vector &z, Real &tol) { typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); // Get a pointer to the gradient vector ROL::Ptr gp = dynamic_cast(g).getVector(); int m = zp->size(); int n = up->size(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); // Initialize constructor for each element of u for(int i=0; ipush_back((*up)[i]); } // Initialize constructor for each element of z for(int j=0; jpush_back(FadType(m,j,(*zp)[j])); } StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); FadType J_fad = obj_.value(u_fad,z_fad,tol); // Evaluate gradient for(int j=0; j class Obj> template void Sacado_Objective_SimOpt::hessVec_11AD(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { typedef Sacado::Fad::SFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr hvp = dynamic_cast(hv).getVector(); int n = up->size(); // vp and hvp have this size also int m = zp->size(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); ROL::Ptr g_fad_ptr = ROL::makePtr(); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); g_fad_ptr->reserve(n); // Initialize constructor for each element of u for(int i=0; ipush_back(FadType(1,(*up)[i])); (*u_fad_ptr)[i].fastAccessDx(0) = (*vp)[i]; g_fad_ptr->push_back(0); } // Initialize constructor for each element of z for(int j=0; jpush_back((*zp)[j]); } StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); StdVector g_fad(g_fad_ptr); this->gradient_1AD(g_fad,u_fad,z_fad,tol); for(int i=0; i class Obj> template void Sacado_Objective_SimOpt::hessVec_12AD(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr hvp = dynamic_cast(hv).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); ROL::Ptr g_fad_ptr = ROL::makePtr(); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); g_fad_ptr->reserve(n); // Initialize constructor for each element of u for(int i=0; ipush_back((*up)[i]); g_fad_ptr->push_back(0); } // Initialize constructor for each element of z for(int j=0; jpush_back(FadType(m,j,(*zp)[j])); } StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); StdVector g_fad(g_fad_ptr); this->gradient_1AD(g_fad,u_fad,z_fad,tol); FadType vdotg = 0; for(int i=0; i class Obj> template void Sacado_Objective_SimOpt::hessVec_21AD(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr hvp = dynamic_cast(hv).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); ROL::Ptr g_fad_ptr = ROL::makePtr(); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); g_fad_ptr->reserve(m); // Initialize constructor for each element of u for(int i=0; ipush_back(FadType(n,i,(*up)[i])); } // Initialize constructor for each element of z for(int j=0; jpush_back((*zp)[j]); g_fad_ptr->push_back(0); } StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); StdVector g_fad(g_fad_ptr); this->gradient_2AD(g_fad,u_fad,z_fad,tol); FadType vdotg = 0; for(int j=0; j class Obj> template void Sacado_Objective_SimOpt::hessVec_22AD(Vector &hv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { typedef Sacado::Fad::SFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr hvp = dynamic_cast(hv).getVector(); int n = up->size(); int m = zp->size(); // vp and hvp have this size also ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); ROL::Ptr g_fad_ptr = ROL::makePtr(); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); g_fad_ptr->reserve(m); // Initialize constructor for each element of u for(int i=0; ipush_back((*up)[i]); } // Initialize constructor for each element of z for(int j=0; jpush_back(FadType(1,(*zp)[j])); (*z_fad_ptr)[j].fastAccessDx(0) = (*vp)[j]; g_fad_ptr->push_back(0); } StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); StdVector g_fad(g_fad_ptr); this->gradient_2AD(g_fad,u_fad,z_fad,tol); for(int j=0; j