| 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_TEUCHOS_OBJECTIVE_H |
| 11 |
#define ROL_TEUCHOS_OBJECTIVE_H |
| 12 |
|
| 13 |
#include "ROL_Objective.hpp" |
| 14 |
#include "ROL_TeuchosVector.hpp" |
| 15 |
|
| 16 |
/** @ingroup func_group |
| 17 |
\class TeuchosObjective |
| 18 |
\brief Specializes the Objective interface for objective functions |
| 19 |
that operate on TeuchosVector |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
namespace ROL { |
| 24 |
|
| 25 |
template <class Ordinal, class Real> |
| 26 |
class TeuchosObjective : public Objective<Real> { |
| 27 |
|
| 28 |
template<class T> using RCP = Teuchos::RCP<T>; |
| 29 |
|
| 30 |
using SerialDenseVector = Teuchos::SerialDenseVector<Ordinal,Real>; |
| 31 |
|
| 32 |
RCP<const SerialDenseVector> getVector( const Vector<Real>& x ) { |
| 33 |
return dynamic_cast<const TeuchosVector<Ordinal,Real>&>(x).getVector(); |
| 34 |
} |
| 35 |
|
| 36 |
RCP<SerialDenseVector> getVector( Vector<Real>& x ) { |
| 37 |
return dynamic_cast<TeuchosVector<Ordinal,Real>&>(x).getVector(); |
| 38 |
} |
| 39 |
|
| 40 |
public: |
| 41 |
|
| 42 |
virtual ~TeuchosObjective() {} |
| 43 |
|
| 44 |
virtual void update( const SerialDenseVector &x, bool flag = true, int iter = -1 ) {} |
| 45 |
|
| 46 |
using Objective<Real>::update; |
| 47 |
void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) { |
| 48 |
auto xp = getVector(x); |
| 49 |
update(*xp,flag,true); |
| 50 |
} |
| 51 |
|
| 52 |
virtual Real value( const SerialDenseVector &x, Real &tol ) = 0; |
| 53 |
|
| 54 |
using Objective<Real>::value; |
| 55 |
Real value( const Vector<Real> &x, Real &tol ) { |
| 56 |
auto xp = getVector(x); |
| 57 |
return value(*xp,tol); |
| 58 |
} |
| 59 |
|
| 60 |
virtual void gradient( SerialDenseVector &g, const SerialDenseVector &x, Real &tol ) { |
| 61 |
ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument, |
| 62 |
">>> ERROR (TeuchosObjective): gradient not implemented!"); |
| 63 |
} |
| 64 |
|
| 65 |
using Objective<Real>::gradient; |
| 66 |
void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) { |
| 67 |
auto gp = getVector(g); |
| 68 |
auto xp = getVector(x); |
| 69 |
|
| 70 |
try { |
| 71 |
gradient(*gp,*xp,tol); |
| 72 |
} |
| 73 |
catch (std::exception &e) { |
| 74 |
Objective<Real>::gradient(g,x,tol); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
virtual Real dirDeriv( const SerialDenseVector &x, const SerialDenseVector &d, Real &tol ) { |
| 79 |
ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument, |
| 80 |
">>> ERROR (TeuchosObjective): dirDeriv not implemented!"); |
| 81 |
} |
| 82 |
|
| 83 |
using Objective<Real>::dirDeriv; |
| 84 |
Real dirDeriv( const Vector<Real> &x, const Vector<Real> &d, Real &tol ) { |
| 85 |
auto xp = getVector(x); |
| 86 |
auto dp = getVector(d); |
| 87 |
try { |
| 88 |
return dirDeriv(*xp,*dp,tol); |
| 89 |
} |
| 90 |
catch (std::exception &e) { |
| 91 |
return Objective<Real>::dirDeriv(x,d,tol); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
virtual void hessVec( SerialDenseVector &hv, const SerialDenseVector &v, |
| 96 |
const SerialDenseVector &x, Real &tol ) { |
| 97 |
ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument, |
| 98 |
">>> ERROR (TeuchosObjective): hessVec not implemented!"); |
| 99 |
} |
| 100 |
|
| 101 |
using Objective<Real>::hessVec; |
| 102 |
void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) { |
| 103 |
auto hvp = getVector(hv); |
| 104 |
auto vp = getVector(v); |
| 105 |
auto xp = getVector(x); |
| 106 |
try { |
| 107 |
hessVec(*hvp,*vp,*xp,tol); |
| 108 |
} |
| 109 |
catch (std::exception &e) { |
| 110 |
Objective<Real>::hessVec(hv,v,x,tol); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
virtual void invHessVec( SerialDenseVector &hv, const SerialDenseVector &v, |
| 115 |
const SerialDenseVector &x, Real &tol ) { |
| 116 |
ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument, |
| 117 |
">>> ERROR (TeuchosObjective): invHessVec not implemented!"); |
| 118 |
} |
| 119 |
|
| 120 |
using Objective<Real>::invHessVec; |
| 121 |
void invHessVec( Vector<Real> &hv, const Vector<Real> &v, |
| 122 |
const Vector<Real> &x, Real &tol ) { |
| 123 |
auto hvp = getVector(hv); |
| 124 |
auto vp = getVector(v); |
| 125 |
auto xp = getVector(x); |
| 126 |
invHessVec(*hvp,*vp,*xp,tol); |
| 127 |
} |
| 128 |
|
| 129 |
virtual void precond( SerialDenseVector &Pv, const SerialDenseVector &v, |
| 130 |
const SerialDenseVector &x, Real &tol ) { |
| 131 |
Pv.assign(v); |
| 132 |
} |
| 133 |
|
| 134 |
using Objective<Real>::precond; |
| 135 |
void precond( Vector<Real> &Pv, const Vector<Real> &v, |
| 136 |
const Vector<Real> &x, Real &tol ) { |
| 137 |
auto Pvp = getVector(Pv); |
| 138 |
auto vp = getVector(v); |
| 139 |
auto xp = getVector(x); |
| 140 |
|
| 141 |
precond(*Pvp,*vp,*xp,tol); |
| 142 |
} |
| 143 |
}; // class TeuchosObjective |
| 144 |
|
| 145 |
} // namespace ROL |
| 146 |
|
| 147 |
#endif // ROL_TEUCHOS_OBJECTIVE_H |