Class NonlinearSolver#

Inheritance Relationships#

Base Type#

Derived Types#

Class Documentation#

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

NonlinearSolver class manages a nonlinear problem and associated solver.

Subclassed by Karana::Dynamics::CeresNonlinearSolver, Karana::Dynamics::EigenNonlinearSolver

Public Types

enum Status#

Contains status of last solve() call.

Values:

enumerator CONVERGED#

Successfully converged to target tolerance.

enumerator DID_NOT_CONVERGE#

Hit iteration or time limit before converging.

enumerator FAILED#

Hit an error internally.

enumerator NOT_RUN_YET#

Solver hasn’t been run; no status to return.

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 and constructs internal state using healthiness before each solve. Calls down into child classes for implementation details.

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.

double solve(km::Vec &x, const km::Vec &lb = {}, const km::Vec &ub = {})#

Solve the nonlinear system.

Some solvers may support upper and lower bounds; pass an empty vector for no bounds, or a NaN entry for no bounds on a given variable.

Parameters:
  • x – The initial guess.

  • lb – Lower bounds on parameters.

  • ub – Upper bounds on parameters.

Returns:

The norm squared of the final cost error.

void setFTol(double ftol)#

Set stopping tolerance for reduction in residual norm.

Parameters:

ftol – New tolerance value

void setGTol(double gtol)#

Set stopping tolerance for jacobian norm.

Parameters:

gtol – New tolerance value

void setXTol(double xtol)#

Set stopping tolerance for changes in parameter value.

Parameters:

xtol – New tolerance value

Status getStatus() const#

Get the status of the last solve() call.

Returns:

Status The outcome of the most recent solve() call.

int getInputDim() const#

Get the dimensionality of the input space.

Returns:

int Input space dimensionality.

int getValueDim() const#

Get the dimensionality of the value space.

Returns:

int Value space dimensionality.

int getLastNumEvals() const#

Get the number of cost function evaluations for the last solve, or -1 if no solves have been run.

Returns:

int Number of cost function evaluations

Protected Functions

virtual void _solve(km::Vec &x, const km::Vec &lb, const km::Vec &ub) = 0#

Child class implementation of solve routine; stores converged solution in input.

Parameters:
  • x – State variable that holds initial guess and stores solution at the end.

  • lb – Lower bounds on parameters; only supported by Ceres solvers.

  • ub – Upper bounds on parameters; only supported by Ceres solvers.

Protected Attributes

int _input_dim#

Dimensionality of input space.

int _value_dim#

Dimensionality of value (output) space.

cost_fn _f#

Cost function F(x).

jac_fn _j#

Jacobian function dF/dx.

int _last_num_evals#

Number of function evals on last call.

double _ftol#

Stopping tolerance for reduction in residual norm.

double _gtol#

Stopping tolerance for jacobian norm.

double _xtol#

Stopping tolerance for changes in parameter value.

Status _status#

Cached status for return.