Public Types | Public Member Functions | Protected Attributes | List of all members
corbo::EdgeGenericScalarFun< VerticesT > Class Template Reference

Generic edge for functions $ \mathbb{R}^n \to \mathbb{R} $. More...

#include <generic_edge.h>

Inheritance diagram for corbo::EdgeGenericScalarFun< VerticesT >:
Inheritance graph
[legend]

Public Types

using ExtFunT = std::function< double(const VertexContainer &)>
 
using ExtJacobT = std::function< void(const VertexContainer &, int, Eigen::Ref< Eigen::MatrixXd >, const double *)>
 
using Ptr = std::shared_ptr< EdgeGenericScalarFun >
 
using UPtr = std::unique_ptr< EdgeGenericScalarFun >
 
- Public Types inherited from corbo::Edge< VerticesT... >
using ConstPtr = std::shared_ptr< const Edge >
 
using Ptr = std::shared_ptr< Edge >
 
using UPtr = std::unique_ptr< Edge >
 
using VertexContainer = std::array< VertexInterface *, numVerticesCompileTime >
 Typedef to represent the vertex container. More...
 
- Public Types inherited from corbo::BaseEdge
using Ptr = std::shared_ptr< BaseEdge >
 
using UPtr = std::unique_ptr< BaseEdge >
 
- Public Types inherited from corbo::EdgeInterface
using Ptr = std::shared_ptr< EdgeInterface >
 
using UPtr = std::unique_ptr< EdgeInterface >
 

Public Member Functions

void computeJacobian (int vtx_idx, Eigen::Ref< Eigen::MatrixXd > block_jacobian, const double *multiplier=nullptr) override
 Compute edge block jacobian for a given vertex. More...
 
void computeValues (Eigen::Ref< Eigen::VectorXd > values) override
 Compute function values. More...
 
 EdgeGenericScalarFun (const ExtFunT &fun, bool lsq_form, VerticesT &... vertices)
 Construct generic scalar function edge by passing the function object and all vertices. More...
 
 EdgeGenericScalarFun (const ExtFunT &fun, const ExtJacobT &jacobian, bool lsq_form, VerticesT &... vertices)
 Construct generic scalar function edge by passing the function object, a jacobian, and all vertices. More...
 
int getDimension () const override
 Get dimension of the edge (dimension of the cost-function/constraint value vector) More...
 
bool isLeastSquaresForm () const override
 Defines if the edge is formulated as Least-Squares form. More...
 
bool isLinear () const override
 Return true if the edge is linear (and hence its Hessian is always zero) More...
 
bool providesJacobian () const override
 Return true if a custom Jacobian is provided (e.g. computeJacobian() is overwritten) More...
 
void setLinear (bool linear)
 
- Public Member Functions inherited from corbo::Edge< VerticesT... >
 Edge ()=delete
 
 Edge (VerticesT &... args)
 Construct edge by providing connected vertices. More...
 
int getNumVertices () const override
 Return number of attached vertices. More...
 
const VertexInterfacegetVertex (int idx) const override
 
VertexInterfacegetVertexRaw (int idx) override
 Get access to vertex with index idx (0 <= idx < numVertices) More...
 
bool providesJacobian () const override
 Return true if a custom Jacobian is provided (e.g. computeJacobian() is overwritten) More...
 
int verticesDimension () const override
 Return the combined dimension of all attached vertices (excluding fixed vertex components) More...
 
- Public Member Functions inherited from corbo::BaseEdge
virtual void computeHessian (int vtx_idx_i, int vtx_idx_j, const Eigen::Ref< const Eigen::MatrixXd > &block_jacobian_i, Eigen::Ref< Eigen::MatrixXd > block_hessian_ij, const double *multipliers=nullptr, double weight=1.0)
 
virtual void computeHessianInc (int vtx_idx_i, int vtx_idx_j, const Eigen::Ref< const Eigen::MatrixXd > &block_jacobian_i, Eigen::Ref< Eigen::MatrixXd > block_hessian_ij, const double *multipliers=nullptr, double weight=1.0)
 Compute edge block Hessian for a given vertex pair. More...
 
virtual void computeHessianInc (int vtx_idx_i, int vtx_idx_j, Eigen::Ref< Eigen::MatrixXd > block_hessian_ij, const double *multipliers=nullptr, double weight=1.0)
 
int getEdgeIdx () const
 Retrieve current edge index (warning, this value is determined within the related HyperGraph) More...
 
virtual bool providesHessian () const
 Return true if a custom Hessian is provided (e.g. computeHessian() is overwritten) More...
 
void reserveCacheMemory (int num_value_vectors, int num_jacobians)
 
void reserveValuesCacheMemory (int num_value_vectors)
 
void reserveJacobiansCacheMemory (int num_jacobians)
 
void computeValuesCached ()
 Call computeValues() and store result to previously allocated internal cache (call allocateInteralValuesCache() first once) More...
 
void computeSquaredNormOfValuesCached ()
 compute the specialied squared-norm method for computing the values (note only the first element in the values cache is used) More...
 
EdgeCachegetCache ()
 Retreive values computed previously via computeValuesCached() More...
 
const EdgeCachegetCache () const
 
- Public Member Functions inherited from corbo::EdgeInterface
virtual double computeSquaredNormOfValues ()
 
virtual double computeSumOfValues ()
 
int getNumFiniteVerticesLowerBounds () const
 
int getNumFiniteVerticesUpperBounds () const
 
virtual ~EdgeInterface ()
 Virtual destructor. More...
 

Protected Attributes

ExtFunT _fun
 Store the external function or functor. More...
 
ExtJacobT _jacob
 
bool _linear = false
 
bool _lsq_form
 
- Protected Attributes inherited from corbo::Edge< VerticesT... >
const VertexContainer _vertices
 Vertex container. More...
 
- Protected Attributes inherited from corbo::BaseEdge
EdgeCache _cache
 
int _edge_idx = 0
 

Additional Inherited Members

- Static Public Attributes inherited from corbo::Edge< VerticesT... >
static constexpr const int numVerticesCompileTime
 Return number of vertices at compile-time. More...
 

Detailed Description

template<class... VerticesT>
class corbo::EdgeGenericScalarFun< VerticesT >

Generic edge for functions $ \mathbb{R}^n \to \mathbb{R} $.

This edge can be used for passing a generic function (e.g. a lambda) to the optimization problem. Jacobians and Hessians are calculated numerically if necessary.

This edge should be only used for Rapid-Prototyping puroposes, due to the computational overhead that rises from copying and calling the external function resp. functor.

You can pass a function that accepts a EdgeGenericScalarFun::VertexContainer as argument. Each vertex stored in that container corresponds to the vertex passed using the class constructor (in the same order). The corresponding Vertex types are specified as template arguments of this class as well.

Example usage for defining (x-1)^2 (Note, keep in mind the special definitions for squaring within different solvers):

VectorVertex<1> x;
using MyEdgeT = EdgeGenericScalarFun<StateVertex<1>>;
auto fun = [] (MyEdgeT::VertexContainer& vertices) {return vertices.at(0)->getData(0)-1;}; // Note, we define only (x-1) here
MyEdgeT* my_edge = new MyEdgeT(fun, false, x);
// now add to the hyper-graph
See also
EdgeGenericVectorFun BaseEdge
Author
Christoph Rösmann (chris.nosp@m.toph.nosp@m..roes.nosp@m.mann.nosp@m.@tu-d.nosp@m.ortm.nosp@m.und.d.nosp@m.e)

Definition at line 68 of file generic_edge.h.

Member Typedef Documentation

◆ ExtFunT

template<class... VerticesT>
using corbo::EdgeGenericScalarFun< VerticesT >::ExtFunT = std::function<double(const VertexContainer&)>

Definition at line 76 of file generic_edge.h.

◆ ExtJacobT

template<class... VerticesT>
using corbo::EdgeGenericScalarFun< VerticesT >::ExtJacobT = std::function<void(const VertexContainer&, int, Eigen::Ref<Eigen::MatrixXd>, const double*)>

Definition at line 77 of file generic_edge.h.

◆ Ptr

template<class... VerticesT>
using corbo::EdgeGenericScalarFun< VerticesT >::Ptr = std::shared_ptr<EdgeGenericScalarFun>

Definition at line 79 of file generic_edge.h.

◆ UPtr

template<class... VerticesT>
using corbo::EdgeGenericScalarFun< VerticesT >::UPtr = std::unique_ptr<EdgeGenericScalarFun>

Definition at line 80 of file generic_edge.h.

Constructor & Destructor Documentation

◆ EdgeGenericScalarFun() [1/2]

template<class... VerticesT>
corbo::EdgeGenericScalarFun< VerticesT >::EdgeGenericScalarFun ( const ExtFunT fun,
bool  lsq_form,
VerticesT &...  vertices 
)
inline

Construct generic scalar function edge by passing the function object and all vertices.

Definition at line 83 of file generic_edge.h.

◆ EdgeGenericScalarFun() [2/2]

template<class... VerticesT>
corbo::EdgeGenericScalarFun< VerticesT >::EdgeGenericScalarFun ( const ExtFunT fun,
const ExtJacobT jacobian,
bool  lsq_form,
VerticesT &...  vertices 
)
inline

Construct generic scalar function edge by passing the function object, a jacobian, and all vertices.

Definition at line 88 of file generic_edge.h.

Member Function Documentation

◆ computeJacobian()

template<class... VerticesT>
void corbo::EdgeGenericScalarFun< VerticesT >::computeJacobian ( int  vtx_idx,
Eigen::Ref< Eigen::MatrixXd >  block_jacobian,
const double *  multipliers = nullptr 
)
inlineoverridevirtual

Compute edge block jacobian for a given vertex.

This interface class provides a numerical computation of the Jacobian using finite differences. However, a user-defined implementation might be specified by overwriting this method. In that case do not forget to also overwrite providesJacobian().

Parameters
[in]vtx_idxVertex number for which the block jacobian should be computed
[out]block_jacobianResulting block jacobian [dimension() x VertexInterface::dimensionUnfixed()] (must be preallocated)
[in]multipliersScale each value component by an optional multiplier if argument is not null null.

Reimplemented from corbo::BaseEdge.

Definition at line 105 of file generic_edge.h.

◆ computeValues()

template<class... VerticesT>
void corbo::EdgeGenericScalarFun< VerticesT >::computeValues ( Eigen::Ref< Eigen::VectorXd >  values)
inlineoverridevirtual

Compute function values.

Here, the actual cost/constraint function values are computed:

  • objective in non-least-squares form: e(x) (hereby, the actual cost is f(x) = e(x)^T e(x))
  • objective in least-squares form: f(x)
  • equality constraints: ceq(x) (in case constraints are satisfied: ceq(x) = 0)
  • inequality constraints: c(x) (in case constraints are satisfied: c(x) < 0)
    Parameters
    [in]valuesvalues should be stored here according to getDimension().

Implements corbo::Edge< VerticesT... >.

Definition at line 103 of file generic_edge.h.

◆ getDimension()

template<class... VerticesT>
int corbo::EdgeGenericScalarFun< VerticesT >::getDimension ( ) const
inlineoverridevirtual

Get dimension of the edge (dimension of the cost-function/constraint value vector)

Implements corbo::Edge< VerticesT... >.

Definition at line 93 of file generic_edge.h.

◆ isLeastSquaresForm()

template<class... VerticesT>
bool corbo::EdgeGenericScalarFun< VerticesT >::isLeastSquaresForm ( ) const
inlineoverridevirtual

Defines if the edge is formulated as Least-Squares form.

Least-squares cost terms are defined as $ f(x) = e(x)^T e(x) $ and the function values and Jacobian are computed for $ e(x) $ rather than for $ f(x) $. Specialiezed least-squares solvers require the optimization problem to be defined in this particular form. Other solvers can automatically compute the square of least-squares edges if required. However, the other way round is more restrictive: general solvers might not cope with non-least-squares forms.

Note, in the LS-form case computeValues() computes e(x) and otherwise f(x).

Returns
true if the edge is given in LS-form

Reimplemented from corbo::BaseEdge.

Definition at line 98 of file generic_edge.h.

◆ isLinear()

template<class... VerticesT>
bool corbo::EdgeGenericScalarFun< VerticesT >::isLinear ( ) const
inlineoverridevirtual

Return true if the edge is linear (and hence its Hessian is always zero)

Implements corbo::Edge< VerticesT... >.

Definition at line 96 of file generic_edge.h.

◆ providesJacobian()

template<class... VerticesT>
bool corbo::EdgeGenericScalarFun< VerticesT >::providesJacobian ( ) const
inlineoverridevirtual

Return true if a custom Jacobian is provided (e.g. computeJacobian() is overwritten)

Reimplemented from corbo::BaseEdge.

Definition at line 100 of file generic_edge.h.

◆ setLinear()

template<class... VerticesT>
void corbo::EdgeGenericScalarFun< VerticesT >::setLinear ( bool  linear)
inline

Definition at line 114 of file generic_edge.h.

Member Data Documentation

◆ _fun

template<class... VerticesT>
ExtFunT corbo::EdgeGenericScalarFun< VerticesT >::_fun
protected

Store the external function or functor.

Definition at line 117 of file generic_edge.h.

◆ _jacob

template<class... VerticesT>
ExtJacobT corbo::EdgeGenericScalarFun< VerticesT >::_jacob
protected

Definition at line 118 of file generic_edge.h.

◆ _linear

template<class... VerticesT>
bool corbo::EdgeGenericScalarFun< VerticesT >::_linear = false
protected

Definition at line 120 of file generic_edge.h.

◆ _lsq_form

template<class... VerticesT>
bool corbo::EdgeGenericScalarFun< VerticesT >::_lsq_form
protected

Definition at line 119 of file generic_edge.h.


The documentation for this class was generated from the following file:


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:08:02