All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy > Class Template Reference

Compute automatically a gradient with finite differences. More...

#include <roboptim/core/fwd.hh>

Inheritance diagram for roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >:
roboptim::GenericDifferentiableFunction< T > roboptim::GenericFunction< T >

Public Member Functions

 ROBOPTIM_DIFFERENTIABLE_FUNCTION_FWD_TYPEDEFS_ (GenericDifferentiableFunction< T >)
 
 GenericFiniteDifferenceGradient (const boost::shared_ptr< const GenericFunction< T > > &f, value_type e=finiteDifferenceEpsilon)
 Instantiate a finite differences gradient. More...
 
 GenericFiniteDifferenceGradient (const GenericFunction< T > &f, value_type e=finiteDifferenceEpsilon)
 Instantiate a finite differences gradient. More...
 
 ~GenericFiniteDifferenceGradient ()
 
virtual std::ostream & print (std::ostream &o) const
 Display the function on the specified output stream. More...
 
- Public Member Functions inherited from roboptim::GenericDifferentiableFunction< T >
 ROBOPTIM_FUNCTION_FWD_TYPEDEFS_ (GenericFunction< T >)
 
 ROBOPTIM_ADD_FLAG (ROBOPTIM_IS_DIFFERENTIABLE)
 
 ROBOPTIM_GENERATE_TRAITS_REFS_ (gradient)
 Gradient type. More...
 
 ROBOPTIM_GENERATE_TRAITS_REFS_ (jacobian)
 Jacobian type. More...
 
size_type gradientSize () const
 Return the gradient size. More...
 
jacobianSize_t jacobianSize () const
 Return the jacobian size as a pair. More...
 
bool isValidGradient (const_gradient_ref gradient) const
 Check if the gradient is valid (check size). More...
 
bool isValidJacobian (const_jacobian_ref jacobian) const
 Check if the jacobian is valid (check sizes). More...
 
jacobian_t jacobian (const_argument_ref argument) const
 Computes the jacobian. More...
 
void jacobian (jacobian_ref jacobian, const_argument_ref argument) const
 Computes the jacobian. More...
 
gradient_t gradient (const_argument_ref argument, size_type functionId=0) const
 Computes the gradient. More...
 
void gradient (gradient_ref gradient, const_argument_ref argument, size_type functionId=0) const
 Computes the gradient. More...
 
- Public Member Functions inherited from roboptim::GenericFunction< T >
 ROBOPTIM_DEFINE_FLAG_TYPE ()
 
 ROBOPTIM_GENERATE_TRAITS_REFS_ (vector)
 Basic (column) vector type. More...
 
 ROBOPTIM_GENERATE_TRAITS_REFS_ (rowVector)
 Row vector type. More...
 
 ROBOPTIM_GENERATE_TRAITS_REFS_ (matrix)
 Basic matrix type. More...
 
 ROBOPTIM_GENERATE_TRAITS_REFS_ (result)
 Type of a function evaluation result. More...
 
 ROBOPTIM_GENERATE_TRAITS_REFS_ (argument)
 Type of a function evaluation argument. More...
 
bool isValidResult (const_result_ref result) const
 Check the given result size is valid. More...
 
GenericFunction< T >::size_type inputSize () const
 Return the input size (i.e. More...
 
GenericFunction< T >::size_type outputSize () const
 Return the output size (i.e. More...
 
virtual ~GenericFunction ()
 Trivial destructor. More...
 
result_t operator() (const_argument_ref argument) const
 Evaluate the function at a specified point. More...
 
void operator() (result_ref result, const_argument_ref argument) const
 Evaluate the function at a specified point. More...
 
const std::string & getName () const
 Get function name. More...
 
virtual flag_t getFlags () const
 Get the type-checking flag. More...
 
template<typename F >
void foreach (const discreteInterval_t interval, F functor)
 
template<typename F >
void foreach (const interval_t interval, const size_type n, F functor)
 
template<class ExpectedType >
ExpectedType * castInto (bool check=false)
 Cast function to ExpectedType. More...
 
template<class ExpectedType >
const ExpectedType * castInto (bool check=false) const
 Cast function to ExpectedType (const). More...
 
template<class ExpectedType >
bool asType () const
 Fonction type checking. More...
 

Protected Member Functions

virtual void impl_compute (result_ref, const_argument_ref) const
 Function evaluation. More...
 
virtual void impl_gradient (gradient_ref, const_argument_ref argument, size_type=0) const
 Gradient evaluation. More...
 
virtual void impl_jacobian (jacobian_ref jacobian, const_argument_ref argument) const
 Jacobian evaluation. More...
 
std::string generateName (const GenericFunction< T > &adaptee) const
 
- Protected Member Functions inherited from roboptim::GenericDifferentiableFunction< T >
 GenericDifferentiableFunction (size_type inputSize, size_type outputSize=1, std::string name=std::string())
 Concrete class constructor should call this constructor. More...
 
template<>
void impl_jacobian (jacobian_ref jacobian, const_argument_ref argument) const
 
- Protected Member Functions inherited from roboptim::GenericFunction< T >
 GenericFunction (size_type inputSize, size_type outputSize=1, std::string name=std::string())
 Concrete class constructor should call this constructor. More...
 

Protected Attributes

const boost::shared_ptr< const
GenericFunction< T > > 
adaptee_
 Shared pointer to the wrapped function. More...
 
const value_type epsilon_
 
argument_t xEps_
 

Additional Inherited Members

- Public Types inherited from roboptim::GenericDifferentiableFunction< T >
typedef std::pair< size_type,
size_type
jacobianSize_t
 Jacobian size type (pair of values). More...
 
- Static Public Member Functions inherited from roboptim::GenericFunction< T >
static value_type epsilon ()
 Get the value of the machine epsilon, useful for floating types comparison. More...
 
static value_type infinity ()
 Get the value that symbolizes positive infinity. More...
 
static interval_t makeInterval (value_type l, value_type u)
 Construct an interval from a lower and upper bound. More...
 
static interval_t makeInfiniteInterval ()
 Construct an infinite interval. More...
 
static interval_t makeLowerInterval (value_type l)
 Construct an interval from a lower bound. More...
 
static interval_t makeUpperInterval (value_type u)
 Construct an interval from an upper bound. More...
 
static value_type getLowerBound (const interval_t &interval)
 Get the lower bound of an interval. More...
 
static value_type getUpperBound (const interval_t &interval)
 Get the upper bound of an interval. More...
 
static discreteInterval_t makeDiscreteInterval (value_type min, value_type max, value_type step)
 Construct a discrete interval. More...
 
static discreteInterval_t makeDiscreteInterval (interval_t interval, value_type step)
 Construct a discrete interval. More...
 
static value_type getLowerBound (const discreteInterval_t &interval)
 Get the lower bound of a discrete interval. More...
 
static value_type getUpperBound (const discreteInterval_t &interval)
 Get the upper bound of a discrete interval. More...
 
static value_type getStep (const discreteInterval_t &interval)
 Get the upper step of a discrete interval. More...
 
template<typename F >
static void foreach (const discreteInterval_t interval, F functor)
 Iterate on an interval. More...
 
template<typename F >
static void foreach (const interval_t interval, const size_type n, F functor)
 Iterate on an interval. More...
 
- Static Public Attributes inherited from roboptim::GenericFunction< T >
static const flag_t flags = ROBOPTIM_IS_FUNCTION
 Flag representing the Roboptim Function type. More...
 

Detailed Description

template<typename T, typename FdgPolicy>
class roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >

Compute automatically a gradient with finite differences.

Finite difference gradient is a method to approximate a function's gradient. It is particularly useful in RobOptim to avoid the need to compute the analytical gradient manually.

This class takes a Function as its input and wraps it into a derivable function.

The one dimensional formula with the Simple policy is:

\[f'(x)\approx {f(x+\epsilon)-f(x)\over \epsilon}\]

where \(\epsilon\) is a constant given when calling the class constructor.

For sparse functions, the default behavior is to treat all values as nonzeros. That way, the function can be used in an optimization problem (sparse solvers expect the full sparse pattern during the initialization). The downside to this approach is the lower performance, as the Jacobian matrix will be a dense matrix treated as a sparse one.

Constructor & Destructor Documentation

template<typename T, typename FdgPolicy>
roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::GenericFiniteDifferenceGradient ( const boost::shared_ptr< const GenericFunction< T > > &  f,
value_type  e = finiteDifferenceEpsilon 
)

Instantiate a finite differences gradient.

Instantiate a derivable function that will wrap a non derivable function and compute automatically its gradient using finite differences.

Parameters
fshared pointer to the function that will be wrapped.
eepsilon used in finite difference computation
template<typename T, typename FdgPolicy>
roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::GenericFiniteDifferenceGradient ( const GenericFunction< T > &  f,
value_type  e = finiteDifferenceEpsilon 
)

Instantiate a finite differences gradient.

WARNING: prefer the shared_ptr alternative if possible.

Instantiate a derivable function that will wrap a non derivable function and compute automatically its gradient using finite differences.

Parameters
ffunction that will e wrapped
eepsilon used in finite difference computation
template<typename T , typename FdgPolicy >
roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::~GenericFiniteDifferenceGradient ( )

Member Function Documentation

template<typename T , typename FdgPolicy >
std::string roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::generateName ( const GenericFunction< T > &  adaptee) const
protected
template<typename T , typename FdgPolicy >
void roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::impl_compute ( result_ref  result,
const_argument_ref  argument 
) const
protectedvirtual

Function evaluation.

Evaluate the function, has to be implemented in concrete classes.

Warning
Do not call this function directly, call operator()(result_ref, const_argument_ref) const instead.
Parameters
resultresult will be stored in this vector
argumentpoint at which the function will be evaluated

Implements roboptim::GenericFunction< T >.

template<typename T , typename FdgPolicy >
void roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::impl_gradient ( gradient_ref  gradient,
const_argument_ref  argument,
size_type  functionId = 0 
) const
protectedvirtual

Gradient evaluation.

Compute the gradient, has to be implemented in concrete classes. The gradient is computed for a specific sub-function whose id is passed through the functionId argument.

Warning
Do not call this function directly, call gradient instead.
Parameters
gradientgradient will be stored in this vector
argumentpoint where the gradient will be computed
functionIdevaluated function id in the split representation

Implements roboptim::GenericDifferentiableFunction< T >.

template<typename T , typename FdgPolicy >
void roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::impl_jacobian ( jacobian_ref  jacobian,
const_argument_ref  arg 
) const
protectedvirtual

Jacobian evaluation.

Computes the jacobian, can be overridden by concrete classes. The default behavior is to compute the jacobian from the gradient.

Warning
Do not call this function directly, call jacobian instead.
Parameters
jacobianjacobian will be store in this argument
argpoint where the jacobian will be computed

Reimplemented from roboptim::GenericDifferentiableFunction< T >.

template<typename T , typename FdgPolicy >
std::ostream & roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::print ( std::ostream &  o) const
virtual

Display the function on the specified output stream.

Parameters
ooutput stream used for display
Returns
output stream

Reimplemented from roboptim::GenericDifferentiableFunction< T >.

References roboptim::decindent(), roboptim::iendl(), and roboptim::incindent().

template<typename T, typename FdgPolicy>
roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::ROBOPTIM_DIFFERENTIABLE_FUNCTION_FWD_TYPEDEFS_ ( GenericDifferentiableFunction< T >  )

Member Data Documentation

template<typename T, typename FdgPolicy>
const boost::shared_ptr<const GenericFunction<T> > roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::adaptee_
protected

Shared pointer to the wrapped function.

template<typename T, typename FdgPolicy>
const value_type roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::epsilon_
protected
template<typename T, typename FdgPolicy>
argument_t roboptim::GenericFiniteDifferenceGradient< T, FdgPolicy >::xEps_
mutableprotected