// @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.hpp" using namespace ROL; /** \brief Generic objective wrapper class for class that uses Sacado */ template class Obj> class Sacado_Objective : public Objective { protected: Obj obj_; /* Evaluate the gradient at x */ template void gradientAD( Vector &g, const Vector &x, Real &tol ); /* Compute the action of the Hessian evaluated at x on a vector v */ template void hessVecAD( Vector &hv, const Vector &v, const Vector &x, Real &tol ); public: Sacado_Objective() : obj_(Obj()) {} Sacado_Objective(const Obj &obj) : obj_(obj) {} /* Evaluate the objective function at x */ Real value( const Vector &x, Real &tol ) { return obj_.value(x,tol); } /* Evaluate the gradient at x */ void gradient( Vector &g, const Vector &x, Real &tol ) { this->gradientAD(g,x,tol); } /* Compute the action of the Hessian evaluated at x on a vector v */ void hessVec( Vector &hv, const Vector &v, const Vector &x, Real &tol ) { this->hessVecAD(hv,v,x,tol); } }; template class Obj> template void Sacado_Objective::gradientAD(Vector &g, const Vector &x, Real &tol) { // Data type which supports automatic differentiation typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; // Get a pointer to the optimization vector ROL::Ptr xp = dynamic_cast(x).getVector(); // Get a pointer to the gradient vector ROL::Ptr gp = dynamic_cast(g).getVector(); int n = xp->size(); // Create a vector of independent variables ROL::Ptr x_fad_ptr = ROL::makePtr(); x_fad_ptr->reserve(n); // Initialize constructor for each element for(int i=0; ipush_back(FadType(n,i,(*xp)[i])); } StdVector x_fad(x_fad_ptr); // AD access to objective function FadType J_fad = obj_.value(x_fad,tol); // Evaluate gradient for(int i=0; i class Obj> template void Sacado_Objective::hessVecAD( Vector &hv, const Vector &v, const Vector &x, Real &tol ) { // Data type which supports automatic differentiation typedef Sacado::Fad::SFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; // Get a pointer to the optimization vector ROL::Ptr xp = dynamic_cast(x).getVector(); // Get a pointer to the direction vector ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr hvp = dynamic_cast(hv).getVector(); int n = xp->size(); // Create a vector of independent variables ROL::Ptr x_fad_ptr = ROL::makePtr(); x_fad_ptr->reserve(n); // Allocate for gradient ROL::Ptr g_fad_ptr = ROL::makePtr(); g_fad_ptr->resize(n); for(int i=0; ipush_back(FadType(1,(*xp)[i])); } // Set directional derivative for(int i=0; i x_fad(x_fad_ptr); StdVector g_fad(g_fad_ptr); this->gradientAD(g_fad,x_fad,tol); for(int i=0; i