// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef ROL_SACADO_STDOBJECTIVE_HPP #define ROL_SACADO_STDOBJECTIVE_HPP #include "Sacado.hpp" #include "ROL_Objective.hpp" #include "ROL_StdVector.hpp" namespace ROL { /** \brief Generic StdObjective wrapper class for class that uses Sacado */ template class Obj> class Sacado_StdObjective : public Objective { template using vector = std::vector; typedef Vector V; typedef StdVector SV; protected: // Template Object should inherit from ROL::StdObjective 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: /* Evaluate the objective function at x */ using Objective::value; Real value( const V &x, Real &tol ) { const SV xs = dynamic_cast(x); return value(*(xs.getVector()), tol); } Real value( const vector &x, Real &tol ) { return obj_.value(x,tol); } /* Evaluate the gradient at x */ using Objective::gradient; void gradient( V &g, const V &x, Real &tol ) { SV gs = dynamic_cast(g); const SV xs = dynamic_cast(x); this->gradient(*(gs.getVector()),*(xs.getVector()),tol); } 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 */ using Objective::hessVec; void hessVec( V &hv, const V &v, const V &x, Real &tol ) { SV hvs = dynamic_cast(hv); const SV vs = dynamic_cast(v); const SV xs = dynamic_cast(x); hessVec(*(hvs.getVector()),*(vs.getVector()),*(xs.getVector()),tol); } void hessVec( vector &hv, const vector &v, const vector &x, Real &tol ) { this->hessVecAD(hv,v,x,tol); } }; template class Obj> template void Sacado_StdObjective::gradientAD(vector &g, const vector &x, Real &tol) { // Data type which supports automatic differentiation typedef Sacado::Fad::DFad FadType; typedef vector Fadvector; // Get a pointer to the gradient vector int n = x.size(); // Create a vector of independent variables ROL::Ptr x_fad = ROL::makePtr(); x_fad->reserve(n); // Initialize constructor for each element for(int i=0; ipush_back(FadType(n,i,x[i])); } // 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_StdObjective::hessVecAD( vector &hv, const vector &v, const vector &x, Real &tol ) { // Data type which supports automatic differentiation typedef Sacado::Fad::SFad FadType; typedef vector Fadvector; int n = x.size(); // Create a vector of independent variables ROL::Ptr x_fad = ROL::makePtr(); x_fad->reserve(n); // Allocate for gradient Fadvector g_fad(n); for(int i=0; ipush_back( FadType(1,x[i]) ); } // Set directional derivative for(int i=0; igradientAD(g_fad,*x_fad,tol); for(int i=0; i