Class NonlinearSolver#

Inheritance Relationships#

Base Type#

Class Documentation#

class NonlinearSolver : public Karana::Core::LockingBase#

NonlinearSolver class manages a nonlinear problem and associated solver.

Public Functions

NonlinearSolver(std::string_view name, int input_dim, int value_dim, cost_fn f, jac_fn j = nullptr)#

NonlinearSolver constructor. This is a container that holds the information needed to solve a nonlinear problem. However, the solver itself is not added during construction. You must either pass in a solver with setSolver or create one with createSolver.

Parameters:
  • name – The name of the nonlinear solver.

  • input_dim – The number of inputs.

  • value_dim – The number of values (outputs).

  • f – The cost function. This should take in a vector of inputs and output a vector of outputs.

  • j – The jacobian function. This is optional, if not specified, the jacobian will be computed by forward numerical differentiation.

cost_fn getF()#

Get the cost function.

Returns:

The cost function.

void setF(cost_fn f)#

Set the cost function. After setting the function, you will need to re-create the solver if one has already been set. Otherwise, the solver will still retain the old cost function.

Parameters:

f – The cost function.

jac_fn getJac()#

Get the jacobian function.

Returns:

The jacobian function.

void setJac(jac_fn jac)#

Set the jacobian function. After setting the jacobian.function, you will need to re-create the solver if one has already been set. Otherwise, the solver will still retain the old cost function.

Parameters:

jac – The jacobian function.

kc::ks_ptr<solver_type> getSolver()#

Get the non-linear solver.

Returns:

The current non-linear solver.

void setSolver(const kc::ks_ptr<solver_type> &solver)#

Set the solver.

Parameters:

solver – The solver.

kc::ks_ptr<solver_type> createSolver(const SolverType &solver)#

Create a solver of the given type. This will return a copy of the shared pointer to the solver as well as set the solver for this NonlinearSolver.

Parameters:

solver – The solver type to create.

Returns:

The solver that was created.

Mat getCachedJacobian()#

Get the current Jacobian matrix being used by the solver.

Returns:

The current Jacobian matrix being used by the solver.

double solve(Vec &x)#

Solve the nonlinear system.

Parameters:

x – The initial guess.

Returns:

The norm squared of the final cost error.

Public Members

std::variant<Eigen::LevenbergMarquardtSpace::Status, Eigen::HybridNonLinearSolverSpace::Status> status#

The status of the nonlinear solver.

Public Static Functions

static kc::ks_ptr<NonlinearSolver> create(std::string_view name, int input_dim, int value_dim, cost_fn f, jac_fn j = nullptr)#

NonlinearSolver constructor. This is a container that holds the information needed to solve a nonlinear problem. However, the solver itself is not added during construction. You must either pass in a solver with setSolver or create one with createSolver.

Parameters:
  • name – The name of the Nonlinear solver.

  • input_dim – The number of inputs.

  • value_dim – The number of values (outputs).

  • f – The cost function. This should take in a vector of inputs and output a vector of outputs.

  • j – The jacobian function. This is optional, if not specified, the jacobian will be computed by forward numerical differentiation.

Returns:

A ks_ptr to a new Nonlinear solver instance.

Protected Functions

virtual void _makeHealthy() final#

Internal method to ensure the solver is ready.