csnlp.Solution#

class csnlp.Solution(*args, **kwargs)[source]#

Bases: Protocol[SymType]

Class containing information on the solution of a solver’s run for an instance of csnlp.Nlp.

Notes

This class is merely a protocol, and as such it just defines the interface of how a solution should look like, plus some minor implementations.

Methods

cmp_key(sol)

Gets the comparison keys to compare a solution with another.

from_casadi_solution(sol_with_stats, nlp)

Creates a new solution from a CasADi solution,

value(expr[, eval])

Computes the value of the expression substituting the values of this solution in the expression.

Attributes

barrier_parameter

Gets the IPOPT barrier parameter at the optimal solution

dual_vals

Optimal values of the dual variables.

dual_vars

Symbolical dual variables.

f

Optimal value of the objective function.

infeasible

Gets whether the solver status indicates infeasibility.

lam_g_and_h

Optimal values of the equality and inequality dual variables in a vector.

lam_g_and_h_sym

Symbolical the equality and inequality dual variables in a vector.

lam_lbx_and_ubx

Optimal values of the lower- and upper-bounds dual variables in a vector.

lam_lbx_and_ubx_sym

Symbolical lower- and upper-bounds dual variables in a vector.

p

Values of the parameters for which this solver's solution was generated.

p_sym

Symbolical parameters in a vector.

solver_plugin

The solver plugin used to generate this solution.

stats

Statistics of the solver for this solution's run.

status

Gets the status of the solver at this solution.

success

Gets whether the solver's run was successful.

unified_return_status

Gets the unified status of the solver at this solution.

vals

Optimal values of the primal variables.

vars

Symbolical primal variables.

x

Optimal values of the primal variables in a vector.

x_and_lam_and_p

Optimal values of the primal and dual variables, as well as the parameters for which the solution was found, in a vector.

x_and_lam_and_p_sym

Symbolical primal and dual variables and parameters in a vector.

x_sym

Symbolical primal variables in a vector.

property barrier_parameter: float#

Gets the IPOPT barrier parameter at the optimal solution

static cmp_key(sol)[source]#

Gets the comparison keys to compare a solution with another. This solution is strictly better if

  • it is feasible and the other is not, or

  • both are feasible or infeasible, and the current is successful and the other

    is not, or

  • both are successful or not, and the current has a lower optimal value than the

    other.

To be used as key argument in, e.g., min or sorted.

Returns:
tuple of (bool, bool, float)

A tuple with (is_infeasible, is_unsuccessful, f).

Return type:

tuple[bool, bool, float]

property dual_vals: dict[str, DM]#

Optimal values of the dual variables.

property dual_vars: dict[str, SymType]#

Symbolical dual variables.

property f: float#

Optimal value of the objective function.

static from_casadi_solution(sol_with_stats, nlp)[source]#

Creates a new solution from a CasADi solution,

Parameters:
sol_with_statsdict of (str, cs.DM) and one entry with stats, i.e., Any

The solution dictionary from the CasADi solver, which contains the optimal values of the primal and dual variables, as well as the parameters, and the solver’s statistics.

nlpNlp[SymType]

The NLP instance for which the solution was computed.

Returns:
Solution[SymType]

The solution corresponding to the CasADi solution.

Return type:

Solution[TypeVar(SymType, SX, MX)]

property infeasible: bool | None#

Gets whether the solver status indicates infeasibility. If False, it does not imply feasibility as the solver or its CasADi interface may have not detect it.

For different solvers, the infeasibility status is stored in different ways. Here is a list of what I gathered so far. The solvers are grouped based on the type of problem they solve. An (F) next to the solver’s name indicates that the the solver will crash the program if "error_on_fail": True and the solver fails. The status and unified_return_status can both be found in the solver’s stats, or in this solution object.

  • NLPs

    • fatrop: unclear; better to return None for now

    • ipopt: status == "Infeasible_Problem_Detected"

    • qrsqp (F): status == "Search_Direction_Becomes_Too_Small" (dubious)

    • sqpmethod (F): status == "Search_Direction_Becomes_Too_Small" (dubious)

  • QPs

    • ipqp (F): no clear way to detect infeasibility; return None for now

    • osqp (F): unified_return_status == "SOLVER_RET_INFEASIBLE" or "infeasible" in status

    • proxqp (F): status == "PROXQP_PRIMAL_INFEASIBLE" or status == "PROXQP_DUAL_INFEASIBLE"

    • qpoases (F): "infeasib" in status

    • qrqp (F): status == "Failed to calculate search direction"

  • LPs

    • clp (F): status == "primal infeasible" or status == "dual infeasible"

  • Mixed-Iteger Problems (MIPs)

    • bonmin (F): status == "INFEASIBLE"

    • cbc (F): "not feasible" in status

    • gurobi (F): status == "INFEASIBLE" or status == "INF_OR_UNBD"

    • knitro: "INFEAS" in status

property lam_g_and_h: DM#

Optimal values of the equality and inequality dual variables in a vector.

property lam_g_and_h_sym: DM#

Symbolical the equality and inequality dual variables in a vector.

property lam_lbx_and_ubx: DM#

Optimal values of the lower- and upper-bounds dual variables in a vector.

property lam_lbx_and_ubx_sym: DM#

Symbolical lower- and upper-bounds dual variables in a vector.

property p: DM#

Values of the parameters for which this solver’s solution was generated.

property p_sym: DM#

Symbolical parameters in a vector.

property solver_plugin: str#

The solver plugin used to generate this solution.

property stats: dict[str, Any]#

Statistics of the solver for this solution’s run.

property status: str#

Gets the status of the solver at this solution.

property success: bool#

Gets whether the solver’s run was successful.

property unified_return_status: str#

Gets the unified status of the solver at this solution.

property vals: dict[str, DM]#

Optimal values of the primal variables.

value(expr, eval=True)[source]#

Computes the value of the expression substituting the values of this solution in the expression.

Parameters:
exprcasadi.SX, MX or an array of these

The symbolic expression to be evaluated at the solution’s values.

evalbool, optional

Evaluates numerically the new expression. By default, True. See csnlp.solutions.subsevalf for more details.

Returns:
casadi.SX or MX or DM

The expression evaluated with the solution’s values.

Raises:
RuntimeError

Raises if eval=True but there are symbolic variables that are still free. This can occur when there are symbols that are outside the solution’s variables, and thus have not been substituted by a numerical value.

Return type:

Union[TypeVar(SymType, SX, MX), DM]

property vars: dict[str, SymType]#

Symbolical primal variables.

property x: DM#

Optimal values of the primal variables in a vector.

property x_and_lam_and_p: DM#

Optimal values of the primal and dual variables, as well as the parameters for which the solution was found, in a vector.

property x_and_lam_and_p_sym: SymType#

Symbolical primal and dual variables and parameters in a vector.

property x_sym: SymType#

Symbolical primal variables in a vector.