# control.optimal.solve_ocp¶

control.optimal.solve_ocp(sys, horizon, X0, cost, trajectory_constraints=None, terminal_cost=None, terminal_constraints=[], initial_guess=None, basis=None, squeeze=None, transpose=None, return_states=False, log=False, **kwargs)

Compute the solution to an optimal control problem

Parameters
• sys (InputOutputSystem) – I/O system for which the optimal input will be computed.

• horizon (1D array_like) – List of times at which the optimal input should be computed.

• X0 (array-like or number, optional) – Initial condition (default = 0).

• cost (callable) – Function that returns the integral cost given the current state and input. Called as cost(x, u).

• trajectory_constraints (list of tuples, optional) –

List of constraints that should hold at each point in the time vector. Each element of the list should consist of a tuple with first element given by `scipy.optimize.LinearConstraint()` or `scipy.optimize.NonlinearConstraint()` and the remaining elements of the tuple are the arguments that would be passed to those functions. The following tuples are supported:

• (LinearConstraint, A, lb, ub): The matrix A is multiplied by stacked vector of the state and input at each point on the trajectory for comparison against the upper and lower bounds.

• (NonlinearConstraint, fun, lb, ub): a user-specific constraint function fun(x, u) is called at each point along the trajectory and compared against the upper and lower bounds.

The constraints are applied at each time point along the trajectory.

• terminal_cost (callable, optional) – Function that returns the terminal cost given the current state and input. Called as terminal_cost(x, u).

• terminal_constraints (list of tuples, optional) – List of constraints that should hold at the end of the trajectory. Same format as constraints.

• initial_guess (1D or 2D array_like) – Initial inputs to use as a guess for the optimal input. The inputs should either be a 2D vector of shape (ninputs, horizon) or a 1D input of shape (ninputs,) that will be broadcast by extension of the time axis.

• log (bool, optional) – If True, turn on logging messages (using Python logging module).

• return_states (bool, optional) – If True, return the values of the state at each time (default = False).

• squeeze (bool, optional) – If True and if the system has a single output, return the system output as a 1D array rather than a 2D array. If False, return the system output as a 2D array even if the system is SISO. Default value set by config.defaults[‘control.squeeze_time_response’].

• transpose (bool, optional) – If True, assume that 2D input arrays are transposed from the standard format. Used to convert MATLAB-style inputs to our format.

• kwargs (dict, optional) – Additional parameters (passed to `scipy.optimal.minimize()`).

Returns

• res (OptimalControlResult) – Bundle object with the results of the optimal control problem.

• res.success (bool) – Boolean flag indicating whether the optimization was successful.

• res.time (array) – Time values of the input.

• res.inputs (array) – Optimal inputs for the system. If the system is SISO and squeeze is not True, the array is 1D (indexed by time). If the system is not SISO or squeeze is False, the array is 2D (indexed by the output number and time).

• res.states (array) – Time evolution of the state vector (if return_states=True).

Notes

Additional keyword parameters can be used to fine tune the behavior of the underlying optimization and integrations functions. See `OptimalControlProblem()` for more information.