tinyopt 700
Tinyopt, a lightweight, header only optimization library
Loading...
Searching...
No Matches
tinyopt::Optimizer_< SolverType > Class Template Reference

Public Types

using Scalar = typename SolverType::Scalar
using Options = tinyopt::Options

Public Member Functions

 Optimizer_ (const Options &_options={})
template<int FO = SolverType::FirstOrder, std::enable_if_t<!FO, int > = 0>
void InitWith (const auto &g, const auto &h)
 Initialize solver with specific gradient and hessian.
template<int FO = SolverType::FirstOrder, std::enable_if_t< FO, int > = 0>
void InitWith (const auto &g)
 Initialize solver with specific gradient.
void reset ()
 Reset the optimization and solver.
template<typename X_t>
std::variant< StopReason, bool > ResizeIfNeeded (X_t &x)
template<typename T, typename U, typename... Rest, typename Func>
requires (!std::is_same_v<std::remove_cvref_t<Func>, Options>)
Summary Optimize (T &x, U &y, Rest &...rest, const Func &cost_or_acc)
 Performs optimization on the given parameters x using the provided cost or accumulation function cost_or_acc.
template<typename X_t, typename CostOrAccFunc>
Summary Optimize (X_t &x, const CostOrAccFunc &cost_or_acc, int max_iters=-1)
template<typename T, typename U, typename... Rest, typename Func>
requires (!std::is_same_v<std::remove_cvref_t<Func>, tinyopt::Options>)
Summary operator() (T &x, U &y, Rest &...rest, const Func &cost_or_acc, int max_iters=-1)
 Performs optimization on the given parameters x using the provided cost or accumulation function cost_or_acc. See Optimize for more informations.
template<typename X_t, typename CostOrAccFunc>
Summary operator() (X_t &x, const CostOrAccFunc &cost_or_acc, int max_iters=-1)
template<typename X_t, typename AccFunc>
Summary OptimizeAcc (X_t &x, const AccFunc &acc, int max_iters=-1)
 Performs optimization on the given parameters x using the provided accumulation function acc.
template<typename X_t, typename AccFunc>
std::pair< bool, std::optional< Vector< Scalar, Dims > > > Step (X_t &x, const AccFunc &acc, Summary &sum)
SolverType & solver ()
const SolverType & solver () const

Static Public Attributes

static constexpr Index Dims = SolverType::Dims

Static Protected Member Functions

static Options PrepareOptions (const Options &options)

Protected Attributes

const Options options_
 Optimization options.
SolverType solver_
 Linear solver.

Member Function Documentation

◆ Optimize()

template<typename SolverType>
template<typename T, typename U, typename... Rest, typename Func>
requires (!std::is_same_v<std::remove_cvref_t<Func>, Options>)
Summary tinyopt::Optimizer_< SolverType >::Optimize ( T & x,
U & y,
Rest &... rest,
const Func & cost_or_acc )
inline

Performs optimization on the given parameters x using the provided cost or accumulation function cost_or_acc.

This function is the primary interface for optimization within the library. It takes the variable to be optimized and a function (or functor) that calculates the cost function, its gradient, and optionally its Hessian. The optimization process attempts to find a value of x that minimizes the objective function.

Template Parameters
X_tThe type of the parameters x. This can be a scalar, a vector, or a more complex data structure (e.g., a matrix). The type must support the necessary arithmetic operations for the chosen optimization algorithm.
CostOrAccFuncThe type of the cost or accumulation function cost_or_acc. This can be a function pointer, a lambda expression, or a functor (an object that overloads the operator()).
Parameters
xThe variable to be optimized. This is passed by reference, and the function will modify x in place to store the optimized value.
cost_or_accThe cost or accumulation function. This function calculates the cost function and, optionally, its derivatives. The cost_or_acc function can have one of the following signatures:
  • ResidualsType(const X_t& x): Only the cost function is calculated. In this case, the library will use automatic differentiation (if available and enabled) or numerical differentiation to estimate the gradient.
  • ScalarCost(const X_t& x, GradientType grad): An accumulation function where the user must fill the gradient and return the final cost (scalar or scalar+number of residuals)
  • ScalarCost(const X_t& x, GradientType grad, HessianType H): An accumulation function where the user must fill the gradient and Hessian and return the final cost (scalar or scalar+number of residuals)

The GradientType and HessianType will be deduced from the type of x. They can also be nullptr_t.

  • ResidualsType is a scalar or Vector/Matrix of residuals (for NLLS).
  • ScalarCost is the total cost/error or a pair (cost, number of residuals).
    Parameters
    max_itersThe maximum number of iterations to perform. If this is negative (the default), the optimization algorithm will run until a convergence criterion is met. A positive value limits the number of iterations, which can be useful for preventing infinite loops or for controlling the execution time.
    Note
    If the cost_or_acc function only provides the cost function or residuals function, the library will automatically compute the gradient (and approx. Hessian) using either automatic differentiation (if available and enabled) or numerical differentiation. Automatic differentiation is generally more accurate and efficient, but numerical differentiation can be used as a fallback or when automatic differentiation is not supported.

◆ OptimizeAcc()

template<typename SolverType>
template<typename X_t, typename AccFunc>
Summary tinyopt::Optimizer_< SolverType >::OptimizeAcc ( X_t & x,
const AccFunc & acc,
int max_iters = -1 )
inline

Performs optimization on the given parameters x using the provided accumulation function acc.

This function is the primary interface for optimization within the library when the user manually updates the Gradient (and Hessian, if any). The optimization process attempts to find a value of x that minimizes the objective function.

Template Parameters
X_tThe type of the parameters x. This can be a scalar, a vector, or a more complex data structure (e.g., a matrix). The type must support the necessary arithmetic operations for the chosen optimization algorithm.
AccFuncThe type of the accumulation function acc. This can be a function pointer, a lambda expression, or a functor (an object that overloads the operator()).
Parameters
xThe variable to be optimized. This is passed by reference, and the function will modify x in place to store the optimized value.
accThe accumulation function. This function returns the total cost must update the gradient(and Hessian, if any). The acc function must have one of the following signatures:
  • ScalarCost(const X_t& x, GradientType grad): An accumulation function where the user must fill the gradient and return the final cost (scalar or scalar+number of residuals)
  • ScalarCost(const X_t& x, GradientType grad, HessianType H): An accumulation function where the user must fill the gradient and Hessian and return the final cost (scalar or scalar+number of residuals)

The GradientType and HessianType will be deduced from the type of x. They can also be nullptr_t.

  • ResidualsType is a scalar or Vector/Matrix of residuals (for NLLS).
  • ScalarCost is the total cost/error or a pair (cost, number of residuals).
    Parameters
    max_itersThe maximum number of iterations to perform. If this is negative (the default), the optimization algorithm will run until a convergence criterion is met. A positive value limits the number of iterations, which can be useful for preventing infinite loops or for controlling the execution time.

◆ Step()

template<typename SolverType>
template<typename X_t, typename AccFunc>
std::pair< bool, std::optional< Vector< Scalar, Dims > > > tinyopt::Optimizer_< SolverType >::Step ( X_t & x,
const AccFunc & acc,
Summary & sum )
inline

Run one optimization iteration, return the estimated next step (solve + decreased error)


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