Class Problem

java.lang.Object
org.wpilib.math.optimization.Problem
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
OCP

public class Problem extends Object implements AutoCloseable
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.

  • Constructor Details

    • Problem

      public Problem()
      Construct the optimization problem.
  • Method Details

    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • decisionVariable

      Creates a decision variable in the optimization problem.

      Decision variables have an initial value of zero.

      Returns:
      A decision variable in the optimization problem.
    • decisionVariable

      public VariableMatrix decisionVariable(int rows)
      Creates a column vector of decision variables in the optimization problem.

      Decision variables have an initial value of zero.

      Parameters:
      rows - Number of column vector rows.
      Returns:
      A column vector of decision variables in the optimization problem.
    • decisionVariable

      public VariableMatrix decisionVariable(int rows, int cols)
      Creates a matrix of decision variables in the optimization problem.

      Decision variables have an initial value of zero.

      Parameters:
      rows - Number of matrix rows.
      cols - Number of matrix columns.
      Returns:
      A matrix of decision variables in the optimization problem.
    • symmetricDecisionVariable

      Creates a symmetric matrix of decision variables in the optimization problem.

      Variable instances are reused across the diagonal, which helps reduce problem dimensionality.

      Decision variables have an initial value of zero.

      Parameters:
      rows - Number of matrix rows.
      Returns:
      A symmetric matrix of decision varaibles in the optimization problem.
    • minimize

      public void minimize(Variable 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.

      Parameters:
      cost - The cost function to minimize.
    • minimize

      public void minimize(VariableMatrix 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.

      Parameters:
      cost - The cost function to minimize. An assertion is raised if the VariableMatrix isn't 1x1.
    • maximize

      public void maximize(Variable 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.

      Parameters:
      objective - The objective function to maximize.
    • maximize

      public void maximize(VariableMatrix 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.

      Parameters:
      objective - The objective function to maximize. An assertion is raised if the VariableMatrix isn't 1x1.
    • subjectTo

      public void subjectTo(EqualityConstraints constraint)
      Tells the solver to solve the problem while satisfying the given equality constraint.
      Parameters:
      constraint - The constraint to satisfy.
    • subjectTo

      public void subjectTo(InequalityConstraints constraint)
      Tells the solver to solve the problem while satisfying the given inequality constraint.
      Parameters:
      constraint - The constraint to satisfy.
    • costFunctionType

      Returns the cost function's type.
      Returns:
      The cost function's type.
    • equalityConstraintType

      Returns the type of the highest order equality constraint.
      Returns:
      The type of the highest order equality constraint.
    • inequalityConstraintType

      Returns the type of the highest order inequality constraint.
      Returns:
      The type of the highest order inequality constraint.
    • solve

      public ExitStatus solve()
      Solves the optimization problem. The solution will be stored in the original variables used to construct the problem.
      Returns:
      The solver status.
    • solve

      public ExitStatus solve(Options options)
      Solves the optimization problem. The solution will be stored in the original variables used to construct the problem.
      Parameters:
      options - Solver options.
      Returns:
      The solver status.
    • addCallback

      public void addCallback(Predicate<IterationInfo> callback)
      Adds a callback to be called at the beginning of each solver iteration.

      The callback for this overload should return bool.

      Parameters:
      callback - The callback. Returning true from the callback causes the solver to exit early with the solution it has so far.
    • clearCallbacks

      public void clearCallbacks()
      Clears the registered callbacks.