![]() |
Sleipnir Python API
|
Public Member Functions | |
| None | __init__ (self) |
| Real | decision_variable (self) |
| Matrix | decision_variable (self, int rows, int cols=1) |
| Real | parameter (self) |
| Matrix | parameter (self, int rows, int cols=1) |
| None | minimize (self, float cost) |
| None | minimize (self, Real cost) |
| None | minimize (self, Matrix|Submatrix cost) |
| None | maximize (self, float objective) |
| None | maximize (self, Real objective) |
| None | maximize (self, Matrix|Submatrix objective) |
| None | subject_to (self, EqualityConstraints constraint) |
| None | subject_to (self, InequalityConstraints constraint) |
| ExpressionType | cost_function_type (self) |
| ExpressionType | equality_constraint_type (self) |
| ExpressionType | inequality_constraint_type (self) |
| ExitStatus | solve (self, *float tolerance=1.4901161193847656e-08, int max_iterations=5000, float timeout=float('inf'), bool feasible_ipm=False, bool diagnostics=False, bool spy=False) |
| None | add_callback (self, Callable[[IterationInfo], None] callback) |
| None | clear_callbacks (self) |
This class allows the user to pose a constrained nonlinear
optimization problem in natural mathematical notation and solve it.
This class supports problems of the form:
```
minₓ f(x)
subject to cₑ(x) = 0
cᵢ(x) ≥ 0
```
where f(x) is the scalar cost function, x is the vector of decision
variables (variables the solver can tweak to minimize the cost
function), cᵢ(x) are the inequality constraints, and cₑ(x) are the
equality constraints. Constraints are equations or inequalities of the
decision variables that constrain what values the solver is allowed to
use when searching for an optimal solution.
The nice thing about this class is users don't have to put their
system in the form shown above manually; they can write it in natural
mathematical form and it'll be converted for them.
Template Args:
Scalar: Scalar type.
| None sleipnir.optimization.Problem.__init__ | ( | self | ) |
Constructs the optimization problem.
Reimplemented in sleipnir.optimization.OCP.
| None sleipnir.optimization.Problem.add_callback | ( | self, | |
| Callable[[IterationInfo], None] | callback | ||
| ) |
Adds a callback to be called at the beginning of each solver
iteration.
Args:
callback: The callback. Raising StopIteration from the callback stops
solving and returns the solution so far.
| None sleipnir.optimization.Problem.clear_callbacks | ( | self | ) |
Clears the registered callbacks.
| ExpressionType sleipnir.optimization.Problem.cost_function_type | ( | self | ) |
Returns the cost function's type.
Returns:
The cost function's type.
| Real sleipnir.optimization.Problem.decision_variable | ( | self | ) |
Creates a decision variable in the optimization problem.
Decision variables have an initial value of zero.
Returns:
A decision variable in the optimization problem.
| Matrix sleipnir.optimization.Problem.decision_variable | ( | self, | |
| int | rows, | ||
| int | cols = 1 |
||
| ) |
Creates a matrix of decision variables in the optimization problem.
Decision variables have an initial value of zero.
Args:
rows: Number of matrix rows.
cols: Number of matrix columns.
Returns:
A matrix of decision variables in the optimization problem.
| ExpressionType sleipnir.optimization.Problem.equality_constraint_type | ( | self | ) |
Returns the type of the highest order equality constraint.
Returns:
The type of the highest order equality constraint.
| ExpressionType sleipnir.optimization.Problem.inequality_constraint_type | ( | self | ) |
Returns the type of the highest order inequality constraint.
Returns:
The type of the highest order inequality constraint.
| None sleipnir.optimization.Problem.maximize | ( | self, | |
| float | objective | ||
| ) |
| None sleipnir.optimization.Problem.maximize | ( | self, | |
| Matrix | Submatrix | objective | ||
| ) |
Tells the solver to maximize the output of the given objective
function.
Note that this is optional. If only constraints are specified, the
solver will find the closest solution to the initial conditions that's
in the feasible set.
Args:
objective: The objective function to maximize. A 1x1 Matrix will
implicitly convert to a Real, and a non-1x1 Matrix will
raise an assertion.
| None sleipnir.optimization.Problem.maximize | ( | self, | |
| Real | objective | ||
| ) |
| None sleipnir.optimization.Problem.minimize | ( | self, | |
| float | cost | ||
| ) |
| None sleipnir.optimization.Problem.minimize | ( | self, | |
| Matrix | Submatrix | cost | ||
| ) |
Tells the solver to minimize the output of the given cost function.
Note that this is optional. If only constraints are specified, the
solver will find the closest solution to the initial conditions that's
in the feasible set.
Args:
cost: The cost function to minimize. A 1x1 Matrix will implicitly
convert to a Real, and a non-1x1 Matrix will raise an
assertion.
| None sleipnir.optimization.Problem.minimize | ( | self, | |
| Real | cost | ||
| ) |
| Real sleipnir.optimization.Problem.parameter | ( | self | ) |
Creates a parameter in the optimization problem.
A parameter is a constant whose value can be changed between solves
with set_value(). Unlike constants, parameters aren't folded into
expressions.
Parameters have an initial value of zero.
Returns:
A parameter in the optimization problem.
| Matrix sleipnir.optimization.Problem.parameter | ( | self, | |
| int | rows, | ||
| int | cols = 1 |
||
| ) |
Creates a matrix of parameters in the optimization problem.
A parameter is a constant whose value can be changed between solves
with set_value(). Unlike constants, parameters aren't folded into
expressions.
Parameters have an initial value of zero.
Args:
rows: Number of matrix rows.
cols: Number of matrix columns.
Returns:
A matrix of parameters in the optimization problem.
| ExitStatus sleipnir.optimization.Problem.solve | ( | self, | |
| *float | tolerance = 1.4901161193847656e-08, |
||
| int | max_iterations = 5000, |
||
| float | timeout = float('inf'), |
||
| bool | feasible_ipm = False, |
||
| bool | diagnostics = False, |
||
| bool | spy = False |
||
| ) |
Solves the optimization problem. The solution will be stored in the
original variables used to construct the problem.
Args:
tolerance: The solver will stop once the error is below this
tolerance.
Defaults to the square root of the scalar type's
machine epsilon.
max_iterations: The maximum number of solver iterations before
returning a solution.
timeout: The maximum elapsed wall clock time before returning a
solution.
feasible_ipm: Enables the feasible interior-point method.
When the inequality constraints are all feasible,
step sizes are reduced when necessary to prevent
them becoming infeasible again. This is useful when
parts of the problem are ill-conditioned in
infeasible regions (e.g., square root of a negative
value). This can slow or prevent progress toward a
solution though, so only enable it if necessary.
diagnostics: Enables diagnostic output.
See
https://sleipnirgroup.github.io/Sleipnir/md_usage.html#output
for more information.
spy: Enables writing sparsity patterns of H, Aₑ, and Aᵢ to files
named H.spy, A_e.spy, and A_i.spy respectively during solve.
Use tools/spy.py to plot them.
Returns:
The solver status.
| None sleipnir.optimization.Problem.subject_to | ( | self, | |
| EqualityConstraints | constraint | ||
| ) |
Tells the solver to solve the problem while satisfying the given
equality constraint.
Args:
constraint: The constraint to satisfy.
| None sleipnir.optimization.Problem.subject_to | ( | self, | |
| InequalityConstraints | constraint | ||
| ) |
Tells the solver to solve the problem while satisfying the given
inequality constraint.
Args:
constraint: The constraint to satisfy.