Cost Functions and Objectives
This page details the functions related to building and evaluating cost functions and objectives.
Cost Functions
TrajectoryOptimization.CostFunction
— TypeCost Function
Abstract type that represents a scalar-valued function that accepts a state and control at a single knot point.
TrajectoryOptimization.QuadraticCostFunction
— TypeQuadraticCostFunction
An abstract type that represents any CostFunction
of the form
\[\frac{1}{2} x^T Q x + \frac{1}{2} u^T R u + u^T H x + q^T x + r^T u + c\]
These types all support the following methods
As well as standard addition.
TrajectoryOptimization.DiagonalCost
— TypeDiagonalCost{n,m,T}
Cost function of the form
\[\frac{1}{2} x^T Q x + \frac{1}{2} u^T R u + q^T x + r^T u + c\]
where $Q$ and $R$ are positive semi-definite and positive definite diagonal matrices, respectively, and $x$ is n
-dimensional and $u$ is m
-dimensional.
Constructors
DiagonalCost(Qd, Rd, q, r, c; kwargs...)
DiagonalCost(Q, R, q, r, c; kwargs...)
DiagonalCost(Qd, Rd; [q, r, c, kwargs...])
DiagonalCost(Q, R; [q, r, c, kwargs...])
where Qd
and Rd
are the diagonal vectors, and Q
and R
are matrices.
Any optional or omitted values will be set to zero(s). The keyword arguments are
terminal
- ABool
specifying if the cost function is terminal cost or not.checks
- ABool
specifying ifQ
andR
will be checked for the required definiteness.
TrajectoryOptimization.QuadraticCost
— TypeQuadraticCost{n,m,T,TQ,TR}
Cost function of the form
\[\frac{1}{2} x^T Q x + \frac{1}{2} u^T R u + u^T H x + q^T x + r^T u + c\]
where $R$ must be positive definite, $Q$ and $Q_f$ must be positive semidefinite.
The type parameters TQ
and TR
specify the type of $Q$ and $R$.
Constructor
QuadraticCost(Q, R, H, q, r, c; kwargs...)
QuadraticCost(Q, R; H, q, r, c, kwargs...)
Any optional or omitted values will be set to zero(s). The keyword arguments are
terminal
- ABool
specifying if the cost function is terminal cost or not.checks
- ABool
specifying ifQ
andR
will be checked for the required definiteness.
TrajectoryOptimization.LQRCost
— FunctionLQRCost(Q, R, xf, [uf; kwargs...])
Convenience constructor for a QuadraticCostFunction
of the form:
\[\frac{1}{2} (x-x_f)^T Q (x-xf) + \frac{1}{2} (u-u_f)^T R (u-u_f)\]
If $Q$ and $R$ are diagonal, the output will be a DiagonalCost
, otherwise it will be a QuadraticCost
.
TrajectoryOptimization.is_diag
— Functionis_diag(::QuadraticCostFunction)
Determines if the hessian of a quadratic cost function is strictly diagonal.
TrajectoryOptimization.is_blockdiag
— Functionis_diag(::QuadraticCostFunction)
Determines if the hessian of a quadratic cost function is block diagonal (i.e. $\norm(H) = 0$).
TrajectoryOptimization.invert!
— Functioninvert!(Ginv, cost::QuadraticCostFunction)
Invert the hessian of the cost function, storing the result in Ginv
. Performs the inversion efficiently, depending on the structure of the Hessian (diagonal or block diagonal).
Adding Cost Functions
Right now, TrajectoryOptimization supports addition of QuadraticCost
s, but extensions to general cost function addition should be straightforward, as long as the cost function all have the same state and control dimensions.
Adding quadratic cost functions:
n,m = 4,5
Q1 = Diagonal(@SVector [1.0, 1.0, 1.0, 1.0, 0.0])
R1 = Diagonal(@SVector [1.0, 0.0, 0.0, 0.0, 0.0, 0.0])
Q2 = Diagonal(@SVector [1.0, 1.0, 1.0, 1.0, 2.0])
R2 = Diagonal(@SVector [0.0, 1.0, 1.0, 1.0, 1.0, 1.0])
cost1 = QuadraticCost(Q1, R1)
cost2 = QuadraticCost(Q2, R2)
cost3 = cost1 + cost2
# cost3 is equivalent to QuadraticCost(Q1+Q2, R1+R2)
Objectives
TrajectoryOptimization.Objective
— TypeObjective{C}
Objective: stores stage cost(s) and terminal cost functions. All the cost functions at each time step must have the same type.
Constructors:
Objective(cost, N)
Objective(cost, cost_term, N)
Objective(costs::Vector{<:CostFunction}, cost_term)
Objective(costs::Vector{<:CostFunction})
TrajectoryOptimization.LQRObjective
— FunctionLQRObjective(Q, R, Qf, xf, N)
Create an objective of the form $(x_N - x_f)^T Q_f (x_N - x_f) + \sum_{k=0}^{N-1} (x_k-x_f)^T Q (x_k-x_f) + u_k^T R u_k$
Where eltype(obj) <: DiagonalCost
if Q
, R
, and Qf
are Union{Diagonal{<:Any,<:StaticVector}}, <:StaticVector}
TrajectoryOptimization.get_J
— FunctionGet the vector of costs at each knot point. sum(get_J(obj))
is equal to the cost
TrajectoryOptimization.TrackingObjective
— FunctionTrackingObjective(Q, R, Z; [Qf])
Generate a quadratic objective that tracks the reference trajectory specified by Z
.
TrajectoryOptimization.update_trajectory!
— Functionupdate_trajectory!(obj, Z, [start=1])
For use with a tracking-style trajectory (see TrackingObjective
). Update the costs to track the new trajectory Z
. The start
parameter specifies the index of reference trajectory that should be used as the starting point of the reference tracked by the objective. This is useful when a single, long time-horizon trajectory is given but the optimization only tracks a portion of the reference at each solve (e.g. MPC).
Evaluating the Cost
TrajectoryOptimization.cost
— Functioncost(obj::Objective, Z::SampledTrajectory)
Evaluate the cost for a trajectory.
cost(::Problem)
Compute the cost for the current trajectory