API

NamedTrajectory methods

Base.mergeMethod
merge(traj1::NamedTrajectory, traj2::NamedTrajectory)
merge(trajs::AbstractVector{<:NamedTrajectory})

Returns a new NamedTrajectory object by merging NamedTrajectory objects.

Merge names are used to specify which components to merge by index. If no merge names are provided, all components are merged and name collisions are not allowed. If merge names are provided, the names are merged using the data from the index provided in the merge names.

Keyword Arguments

  • timestep::Symbol: The timestep symbol to use for free time problems. Default to the last trajectory.
  • merge_names::Union{Nothing, NamedTuple{<:Any, <:Tuple{Vararg{Int}}}}=nothing: The names to merge by index.
source
NamedTrajectories.MethodsNamedTrajectory.add_control_derivativesMethod
add_control_derivatives(
    traj::NamedTrajectory,
    n_derivatives::Int;
    control_name::Symbol=:u,
    derivative_bounds::Union{Nothing, Tuple{Vararg{VectorBound}}}=nothing,
    zero_initial_and_final_derivative::Bool=false,
    random_init::Bool=false,
    drive_derivative_σ::Float64=0.1
)

Add control derivatives to an existing trajectory.

Takes an existing NamedTrajectory with a control variable and adds its time derivatives (e.g., u̇, ü) as new trajectory components. Can either compute derivatives using finite differences or initialize them randomly.

Arguments

  • traj::NamedTrajectory: Existing trajectory with control variable
  • n_derivatives::Int: Number of derivatives to add (1 for u̇, 2 for u̇ and ü, etc.)

Keyword Arguments

  • control_name::Symbol=:u: Name of the control variable in the trajectory
  • derivative_bounds::Union{Nothing, Tuple{Vararg{VectorBound}}}=nothing: Bounds for each derivative level (du, ddu, ...)
  • zero_initial_and_final_derivative::Bool=false: Enforce zero first derivative at boundaries
  • random_init::Bool=false: If true, randomly initialize derivatives instead of computing via finite differences
  • drive_derivative_σ::Float64=0.1: Standard deviation for random derivative initialization (only used if random_init=true)

Returns

  • NamedTrajectory: New trajectory with control derivatives added

Examples

# Add first and second derivatives computed via finite differences
traj_smooth = add_control_derivatives(
    traj, 
    2; 
    derivative_bounds=(([-2.0, -2.0], [2.0, 2.0]), ([-5.0, -5.0], [5.0, 5.0]))
)

# Add derivative with zero boundary conditions
traj_smooth = add_control_derivatives(
    traj,
    1;
    zero_initial_and_final_derivative=true
)

# Randomly initialize derivatives
traj_random = add_control_derivatives(
    traj,
    2;
    random_init=true,
    drive_derivative_σ=0.05
)

Notes

  • The original trajectory is not modified; a new trajectory is returned
  • When random_init=false, derivatives are computed using finite differences: du/dt ≈ Δu/Δt
  • When random_init=true, derivatives are sampled from N(0, drivederivativeσ²)
  • The penultimate point of each derivative is adjusted to ensure smooth transitions (finite difference mode only)
  • New derivative components become part of the trajectory controls
source
NamedTrajectories.MethodsNamedTrajectory.get_component_namesMethod
get_component_names(traj::NamedTrajectory, comps::AbstractVector{<:Int})

Returns the name of the component with the given indices. If only one component is found, the name is returned as a single symbol. Else, the names are returned as a vector of symbols.

The filter requires that the components are a complete subset of the given indices, so that a partial match is excluded from the returned names.

source
Base.getpropertyMethod
getproperty(slice::KnotPoint, symb::Symbol)

Returns the component of the knot point with name symb (as a view) or the property of the knot point with name symb.

source
Base.setproperty!Method
setproperty!(slice::KnotPoint, symb::Symbol, val::Any)

Dispatches setting properties of knot points as either setting a component or a property via update! or setfield!, respectively.

source

Random trajectory methods

Base.randMethod
rand(
    ::Type{NamedTrajectory},
    N::Int;
    timestep_value::Float64=1.0,
    timestep_name::Symbol=:Δt,
    timestep::Union{Float64,Symbol}=free_time ? timestep_name : timestep_value,
    state_dim::Int=3,
    control_dim::Int=2
)

Create a random NamedTrajectory with N knot points, a state variable x of dimension state_dim, and a control variable u of dimension control_dim. The time step is a symbol timestep_name and the time step value is timestep_value.

source

Struct Methods

NamedTrajectories.StructNamedTrajectory.NamedTrajectoryType
NamedTrajectory{R <: Real}

Container for trajectory optimization problems, which includes the trajectory data, bounds dimensions, initial and final conditions, goal states, and components.

This struct is designed to hold trajectory data in a named format, allowing for easy access to knot points by Symbol.

NamedTrajectory is designed to make allocation-free access easy to write. The data can be updated after construction, but the fields cannot.

source

Trajectory Utilities

Interpolation Methods

NamedTrajectories.MethodsNamedTrajectory.trajectory_interpolationFunction
trajectory_interpolation(
    traj::NamedTrajectory,
    times::AbstractVector;
    interpolations::NamedTuple=NamedTuple(zip(traj.names, fill(:linear, length(traj.names))))
)

Interpolate a NamedTrajectory at specified time points.

Arguments

  • traj::NamedTrajectory: The trajectory to interpolate.
  • times::AbstractVector: The time points at which to interpolate the trajectory.

Keyword Arguments

  • interpolations::NamedTuple: A named tuple specifying the interpolation method for each component. Supported methods are :constant, :linear, and :spline. Defaults to :linear for all components.

Returns

  • NamedTrajectory: A new trajectory with interpolated values at the specified times.

Notes

  • Components not specified in interpolations will be dropped from the returned trajectory.
  • The timestep component is automatically included with linear interpolation if not specified.
  • Spline interpolation requires derivative components (e.g., du for component u).

Examples

# Linear interpolation at new time points
new_times = [0.0, 0.5, 1.0, 1.5, 2.0]
new_traj = trajectory_interpolation(traj, new_times)

# Mix of interpolation methods
interpolations = (x = :linear, u = :spline, Δt = :linear)
new_traj = trajectory_interpolation(traj, new_times; interpolations=interpolations)
source
trajectory_interpolation(
    traj::NamedTrajectory,
    T::Int;
    kwargs...
)

Interpolate a NamedTrajectory to a new number of time steps.

Arguments

  • traj::NamedTrajectory: The trajectory to interpolate.
  • T::Int: The number of time steps in the interpolated trajectory.

Keyword Arguments

  • kwargs...: Additional keyword arguments passed to the main trajectory_interpolation method.

Returns

  • NamedTrajectory: A new trajectory with T time steps, evenly spaced between the original start and end times.

Examples

# Interpolate to 100 time steps
new_traj = trajectory_interpolation(traj, 100)

# With custom interpolation methods
new_traj = trajectory_interpolation(traj, 100; interpolations=(x=:spline, u=:linear))
source