Complete Example: Time-Optimal Bilinear Control

This example demonstrates solving a time-optimal trajectory optimization problem with:

  • Multiple control inputs with bounds
  • Free time steps (variable Δt)
  • Combined objective (control effort + minimum time)
using DirectTrajOpt
using NamedTrajectories
using LinearAlgebra
using CairoMakie

Problem Setup

System: 3D oscillator with 2 control inputs

\[\dot{x} = (G_0 + u_1 G_1 + u_2 G_2) x\]

Goal: Drive from [1, 0, 0] to [0, 0, 1] minimizing ∫ ||u||² dt + w·T

Constraints:-1 ≤ u ≤ 1, 0.05 ≤ Δt ≤ 0.3

Define System Dynamics

G_drift = [
    0.0 1.0 0.0;
    -1.0 0.0 0.0;
    0.0 0.0 -0.1
]

G_drives = [
    [
        1.0 0.0 0.0;
        0.0 0.0 0.0;
        0.0 0.0 0.0
    ],
    [
        0.0 0.0 0.0;
        0.0 0.0 1.0;
        0.0 1.0 0.0
    ],
]

G = u -> G_drift + sum(u .* G_drives)
#2 (generic function with 1 method)

Create Trajectory

N = 50
x_init = [1.0, 0.0, 0.0]
x_goal = [0.0, 0.0, 1.0]
x_guess = hcat([x_init + (x_goal - x_init) * (k/(N-1)) for k = 0:(N-1)]...)

traj = NamedTrajectory(
    (x = x_guess, u = 0.1 * randn(2, N), Δt = fill(0.15, N));
    timestep = :Δt,
    controls = (:u, :Δt),
    initial = (x = x_init,),
    final = (x = x_goal,),
    bounds = (u = 1.0, Δt = (0.05, 0.3)),
)
N = 50, (x = 1:3, u = 4:5, → Δt = 6:6)

Build and Solve Problem

integrator = BilinearIntegrator(G, :x, :u, traj)

obj = (QuadraticRegularizer(:u, traj, 1.0) + 0.5 * MinimumTimeObjective(traj, 1.0))

prob = DirectTrajOptProblem(traj, obj, integrator)

prob
DirectTrajOptProblem
  Trajectory
    Timesteps: 50
    Duration:  7.35
    Knot dim:  6
    Variables: x (3), u (2), Δt (1)
    Controls:  u, Δt
  Objective (2 terms)
         1.0 * QuadraticRegularizer on :u (R = [1.0, 1.0], all)
         0.5 * MinimumTimeObjective (D = 1.0)
  Dynamics (1 integrators)
    BilinearIntegrator: :x = exp(Δt G(:u)) :x  (dim = 3)
  Constraints (4 total: 2 equality, 2 bounds)
    EqualityConstraint: "initial value of x"
    EqualityConstraint: "final value of x"
    BoundsConstraint: "bounds on u"
    BoundsConstraint: "bounds on Δt"
solve!(prob; max_iter = 50)
    initializing optimizer...
      building evaluator: 1 integrators, 0 nonlinear constraints
      dynamics constraints: 147, nonlinear constraints: 0
        integrator 1 jacobian structure: 0.359s
      jacobian structure: 1764 nonzeros (0.381s)
        integrator 1 hessian structure: 0.019s
        computing objective hessian structure (CompositeObjective)...
          sub-objective 1 (QuadraticRegularizer): 0.362s
          sub-objective 2 (MinimumTimeObjective): 0.007s
        objective hessian structure: 0.425s
      hessian structure: 2814 nonzeros (0.444s)
      linear index maps built (0.004s)
      evaluator ready (total: 0.969s)
    evaluator created (1.67s)
    NL constraint bounds extracted (0.026s)
    NLP block data built (0.0s)
    Ipopt optimizer configured (0.009s)
    variables set (0.346s)
        applying constraint: initial value of x
        applying constraint: final value of x
        applying constraint: bounds on u
        applying constraint: bounds on Δt
    linear constraints added: 4 (0.597s)
    optimizer initialization complete (total: 2.68s)

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
 Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit https://github.com/coin-or/Ipopt
******************************************************************************

This is Ipopt version 3.14.19, running with linear solver MUMPS 5.8.2.

Number of nonzeros in equality constraint Jacobian...:     1746
Number of nonzeros in inequality constraint Jacobian.:        0
Number of nonzeros in Lagrangian Hessian.............:     2748

Total number of variables............................:      294
                     variables with only lower bounds:        0
                variables with lower and upper bounds:      150
                     variables with only upper bounds:        0
Total number of equality constraints.................:      147
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  3.6866113e+00 1.47e-01 1.06e-01   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  2.7221034e+00 1.30e-01 5.83e-01  -4.0 9.20e-01    -  1.47e-01 1.15e-01f  1
   2  2.1393487e+00 1.20e-01 1.23e+01  -0.5 2.86e+00    -  4.27e-01 7.50e-02h  1
   3  2.1368666e+00 1.15e-01 2.38e+01   0.3 7.70e+00   0.0 2.83e-01 5.33e-02f  1
   4  2.3018848e+00 1.17e-01 4.21e+01   0.8 1.54e+01    -  1.67e-01 2.76e-02f  1
   5  2.4828336e+00 1.13e-01 1.14e+02   0.9 1.52e+01    -  3.65e-01 3.68e-02f  1
   6  2.6288592e+00 7.53e-02 3.03e+02   0.6 1.60e+00    -  9.52e-01 3.72e-01h  1
   7  2.3863779e+00 6.61e-02 3.41e+03   1.3 1.64e+00   2.2 8.51e-01 1.27e-01h  1
   8r 2.3863779e+00 6.61e-02 9.99e+02  -1.2 0.00e+00   1.8 0.00e+00 2.69e-07R 12
   9r 2.3902088e+00 7.87e-02 9.82e+02   1.7 8.44e+01    -  3.10e-01 2.30e-03f  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  10r 2.4160589e+00 7.37e-02 8.17e+02   1.1 2.13e+00    -  3.39e-01 7.04e-02f  1
  11r 2.4708980e+00 4.67e-02 7.95e+02   1.1 7.77e-01    -  9.62e-01 1.63e-01f  1
  12r 3.0580182e+00 8.40e-02 1.49e+02   1.1 1.55e+00    -  1.00e+00 7.10e-01f  1
  13r 2.8532884e+00 5.36e-02 9.72e+01   0.4 2.01e-01   2.0 6.89e-01 1.00e+00f  1
  14  3.1230607e+00 1.83e-02 6.03e+00  -1.1 1.17e+00    -  7.13e-01 7.98e-01H  1
  15  3.1062004e+00 3.75e-03 1.82e+00  -1.3 1.03e-01   1.3 9.96e-01 9.21e-01f  1
  16  3.1201915e+00 3.83e-05 6.01e-01  -2.3 1.18e-02   1.7 1.00e+00 1.00e+00h  1
  17  3.1097735e+00 1.53e-05 8.59e-02  -3.9 5.25e-03   1.2 1.00e+00 1.00e+00h  1
  18  3.0792583e+00 1.36e-04 8.85e-02  -4.0 1.58e-02   0.7 1.00e+00 1.00e+00f  1
  19  3.0460727e+00 2.04e-04 8.73e-02  -4.0 4.60e-02   0.3 1.00e+00 5.05e-01f  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  20  3.0000570e+00 6.36e-04 8.86e-02  -4.0 1.47e-01  -0.2 1.00e+00 3.21e-01f  1
  21  2.9606755e+00 3.45e-04 7.40e-02  -4.0 4.17e-02   0.2 1.00e+00 7.92e-01h  1
  22  2.9100483e+00 6.57e-04 6.43e-02  -4.0 9.29e-02  -0.3 1.00e+00 4.39e-01f  1
  23  2.8707608e+00 3.63e-04 5.80e-02  -4.0 3.91e-02   0.2 1.00e+00 1.00e+00h  1
  24  2.8389592e+00 4.09e-04 5.26e-02  -4.0 8.83e-02  -0.3 1.00e+00 3.30e-01h  1
  25  2.7403930e+00 1.28e-03 5.45e-02  -4.0 1.82e-01  -0.8 1.00e+00 4.82e-01f  1
  26  2.6990850e+00 7.15e-04 3.35e-02  -3.7 7.16e-02  -0.4 1.00e+00 7.07e-01h  1
  27  2.6632686e+00 6.14e-04 7.08e-02  -4.0 1.87e-01  -0.8 1.00e+00 2.72e-01h  1
  28  2.6240485e+00 3.20e-04 2.90e-02  -4.0 7.42e-02  -0.4 1.00e+00 8.20e-01h  1
  29  2.5664670e+00 7.62e-04 5.27e-02  -4.0 2.06e-01  -0.9 1.00e+00 5.20e-01h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  30  2.5476597e+00 4.36e-04 2.72e-02  -4.0 7.68e-02  -0.5 1.00e+00 5.32e-01h  1
  31  2.5135738e+00 5.42e-04 5.40e-02  -3.7 2.14e-01  -0.9 1.00e+00 4.16e-01h  1
  32  2.4889240e+00 1.18e-03 1.38e-01  -3.0 5.11e-01  -1.4 9.67e-01 2.12e-01f  1
  33  2.4701982e+00 8.91e-04 5.67e-02  -3.8 2.03e-01  -1.0 9.99e-01 3.01e-01h  1
  34  2.4323555e+00 4.85e-03 7.70e-02  -3.2 4.44e-01  -1.5 1.00e+00 4.58e-01f  1
  35  2.4010655e+00 4.46e-03 3.53e-02  -2.9 7.92e-01  -1.9 1.00e+00 4.71e-01h  1
  36  2.3695181e+00 1.55e-02 3.33e-02  -3.0 8.14e-01  -2.4 9.48e-01 4.82e-01h  1
  37  2.3040929e+00 2.64e-03 4.22e-03  -3.6 2.17e-01  -2.0 1.00e+00 1.00e+00h  1
  38  2.2896708e+00 7.33e-03 4.96e-03  -3.9 3.73e-01  -2.5 9.87e-01 7.10e-01h  1
  39  2.2827047e+00 1.90e-03 1.45e-03  -4.0 1.31e-01  -2.0 1.00e+00 1.00e+00h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  40  2.2800069e+00 1.63e-02 1.23e-02  -4.0 3.68e-01  -2.5 6.84e-01 8.85e-01h  1
  41  2.3002013e+00 1.05e-02 7.43e-03  -3.4 4.93e-01  -3.0 1.00e+00 1.00e+00f  1
  42  2.2849927e+00 1.47e-02 6.07e-03  -3.5 5.89e-01    -  9.83e-01 1.00e+00H  1
  43  2.2909631e+00 2.45e-02 1.29e-02  -3.5 6.12e-01  -2.6 9.58e-01 8.58e-01h  1
  44  2.2927249e+00 7.09e-03 5.22e-03  -3.5 2.84e-01    -  8.65e-01 1.00e+00f  1
  45  2.2903881e+00 2.42e-03 1.11e-03  -3.5 1.72e-01  -2.1 1.00e+00 1.00e+00h  1
  46  2.2890615e+00 1.25e-02 8.75e-03  -4.0 3.06e+00    -  1.30e-01 1.08e-01h  1
  47  2.2842244e+00 8.40e-04 4.16e-04  -3.6 7.82e-02    -  1.00e+00 1.00e+00h  1
  48  2.2752887e+00 1.43e-03 8.15e-04  -4.0 8.48e-02    -  1.00e+00 1.00e+00h  1
  49  2.2751414e+00 3.85e-05 4.38e-05  -4.0 2.37e-02    -  1.00e+00 1.00e+00h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  50  2.2751282e+00 1.05e-07 5.11e-08  -4.0 9.90e-04    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 50

                                   (scaled)                 (unscaled)
Objective...............:   2.2751281942176518e+00    2.2751281942176518e+00
Dual infeasibility......:   5.1113402583700084e-08    5.1113402583700084e-08
Constraint violation....:   1.0494078961365005e-07    1.0494078961365005e-07
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   1.0000169815782573e-04    1.0000169815782573e-04
Overall NLP error.......:   1.0494078961365005e-07    1.0494078961365005e-07


Number of objective function evaluations             = 66
Number of objective gradient evaluations             = 47
Number of equality constraint evaluations            = 66
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 52
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 50
Total seconds in IPOPT                               = 12.351

EXIT: Maximum Number of Iterations Exceeded.

Visualize Solution

plot(prob.trajectory) # See NamedTrajectories.jl documentation for plotting options
Example block output

Analyze Solution

x_sol = prob.trajectory.x
u_sol = prob.trajectory.u
Δt_sol = prob.trajectory.Δt

println("Solution found!")
println("  Total time: $(sum(Δt_sol)) seconds")
println("  Δt range: [$(minimum(Δt_sol)), $(maximum(Δt_sol))]")
println("  Max |u₁|: $(maximum(abs.(u_sol[1,:])))")
println("  Max |u₂|: $(maximum(abs.(u_sol[2,:])))")
println("  Final error: $(norm(x_sol[:,end] - x_goal))")
Solution found!
  Total time: 4.247067644947218 seconds
  Δt range: [0.07224452600159269, 0.175]
  Max |u₁|: 0.9966535058647275
  Max |u₂|: 0.9978914179084062
  Final error: 0.0

Key Insights

Free time optimization: Variable Δt allows the optimizer to adjust trajectory speed, with shorter steps where control is needed and longer steps in smooth regions.

Control bounds: With time weight 0.5, controls don't fully saturate. Increase the weight to push toward bang-bang control.

Combined objectives: The + operator makes it easy to balance multiple goals.

Exercises

1. Bang-bang control: Set time weight to 5.0 - do controls saturate the bounds?

2. Fixed time: Remove Δt from controls and compare total time.

3. Add waypoint: Require passing through [0.5, 0, 0.5] at the midpoint:

constraint = NonlinearKnotPointConstraint(
    x -> x - [0.5, 0, 0.5], :x, traj;
    times=[div(N,2)], equality=true
)
prob = DirectTrajOptProblem(traj, obj, integrator; constraints=[constraint])

4. Different goal: Try reaching [0, 1, 0] or [0.5, 0.5, 0.5]

5. Tighter bounds: Use bounds=(u = 0.5, Δt = (0.05, 0.3)) - how does time change?


This page was generated using Literate.jl.