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 CairoMakieProblem 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)
probDirectTrajOptProblem
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.369s
jacobian structure: 1764 nonzeros (0.391s)
integrator 1 hessian structure: 0.018s
computing objective hessian structure (CompositeObjective)...
sub-objective 1 (QuadraticRegularizer): 0.363s
sub-objective 2 (MinimumTimeObjective): 0.006s
objective hessian structure: 0.423s
hessian structure: 2814 nonzeros (0.441s)
linear index maps built (0.004s)
evaluator ready (total: 0.974s)
evaluator created (1.531s)
NL constraint bounds extracted (0.024s)
NLP block data built (0.0s)
Ipopt optimizer configured (0.009s)
variables set (0.136s)
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.477s)
optimizer initialization complete (total: 2.177s)
******************************************************************************
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.6847125e+00 1.48e-01 9.15e-02 0.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 2.6575048e+00 1.30e-01 5.35e-01 -4.0 1.74e+00 - 1.55e-01 1.22e-01f 1
2 2.2777749e+00 1.17e-01 3.84e+00 -0.6 2.94e+00 - 2.94e-01 1.02e-01h 1
3 2.0955442e+00 1.06e-01 5.53e+00 -4.0 4.05e+00 0.0 1.52e-01 9.09e-02h 1
4 2.0592870e+00 1.01e-01 3.34e+01 -0.1 3.42e+00 0.4 4.43e-01 4.88e-02h 1
5 2.1291118e+00 9.94e-02 2.90e+01 0.7 1.08e+01 - 1.49e-01 3.51e-02f 1
6 2.1114936e+00 9.07e-02 1.03e+02 0.1 2.63e+00 -0.1 7.35e-01 9.02e-02h 1
7 2.7041974e+00 7.03e-02 2.93e+02 0.7 4.11e+00 - 8.93e-01 2.31e-01h 1
8 2.6653828e+00 6.59e-02 2.67e+03 1.5 2.54e+00 2.2 7.76e-01 6.33e-02f 1
9 2.7240401e+00 8.54e-02 2.69e+03 1.9 7.42e+00 1.7 2.24e-01 1.17e-01f 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
10 2.9461081e+00 5.74e-02 1.93e+03 1.4 3.20e+00 - 2.28e-01 2.23e-01f 1
11 3.2803858e+00 6.38e-02 9.14e+03 2.2 1.35e+00 3.0 6.11e-01 7.64e-01f 1
12 3.0664818e+00 2.11e-02 5.96e+03 2.1 1.57e-01 4.4 9.81e-01 1.00e+00f 1
13 3.0614070e+00 2.25e-04 1.75e+03 1.2 2.84e-02 4.8 9.99e-01 1.00e+00f 1
14 3.0615576e+00 7.64e-08 8.51e+00 -0.4 4.03e-04 4.3 1.00e+00 1.00e+00f 1
15 3.0615327e+00 4.22e-10 3.14e-01 -2.3 4.61e-05 3.8 1.00e+00 1.00e+00h 1
16 3.0614530e+00 1.97e-09 1.12e-01 -4.0 4.94e-05 3.4 1.00e+00 1.00e+00h 1
17 3.0612141e+00 1.77e-08 1.12e-01 -4.0 1.48e-04 2.9 1.00e+00 1.00e+00f 1
18 3.0604988e+00 1.58e-07 1.12e-01 -4.0 4.44e-04 2.4 1.00e+00 1.00e+00f 1
19 3.0583666e+00 1.40e-06 1.12e-01 -4.0 1.33e-03 1.9 1.00e+00 1.00e+00f 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
20 3.0520892e+00 1.20e-05 1.10e-01 -4.0 3.93e-03 1.4 1.00e+00 1.00e+00f 1
21 3.0342451e+00 9.23e-05 1.06e-01 -4.0 1.14e-02 1.0 1.00e+00 1.00e+00f 1
22 2.9878112e+00 5.44e-04 9.66e-02 -4.0 3.09e-02 0.5 1.00e+00 1.00e+00f 1
23 2.9426176e+00 7.08e-04 8.81e-02 -4.0 7.63e-02 0.0 1.00e+00 4.38e-01h 1
24 2.9109599e+00 3.99e-04 8.41e-02 -4.0 3.04e-02 0.4 1.00e+00 1.00e+00h 1
25 2.8927377e+00 4.05e-04 7.96e-02 -4.0 7.03e-02 -0.0 1.00e+00 2.35e-01h 1
26 2.7550793e+00 4.22e-03 5.57e-02 -4.0 1.60e-01 -0.5 1.00e+00 7.89e-01f 1
27 2.7157347e+00 9.25e-04 5.47e-02 -4.0 6.67e-02 -0.1 1.00e+00 9.72e-01h 1
28 2.6714039e+00 1.11e-03 4.99e-02 -4.0 1.58e-01 -0.6 1.00e+00 4.06e-01h 1
29 2.6142829e+00 5.55e-03 6.17e-02 -3.3 6.56e-01 -1.0 9.07e-01 3.02e-01f 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
30 2.5478237e+00 1.43e-03 3.89e-02 -4.0 1.49e-01 -0.6 1.00e+00 7.79e-01h 1
31 2.5212259e+00 1.36e-03 6.22e-02 -3.2 2.83e-01 -1.1 9.77e-01 2.36e-01h 1
32 2.4780513e+00 8.37e-04 2.27e-02 -4.0 1.04e-01 -0.7 1.00e+00 9.13e-01h 1
33 2.4587046e+00 1.21e-03 2.82e-02 -3.2 2.47e-01 -1.1 1.00e+00 3.38e-01h 1
34 2.3894114e+00 4.59e-02 3.66e-02 -4.0 1.09e+00 -1.6 1.47e-01 5.74e-01h 1
35 2.3823541e+00 2.78e-02 4.77e-02 -2.9 1.61e+00 -2.1 3.06e-01 4.74e-01f 1
36 2.3230076e+00 2.74e-03 7.27e-03 -3.3 2.96e-01 -1.7 1.00e+00 9.74e-01h 1
37 2.3049515e+00 7.52e-03 1.18e-02 -3.5 4.26e-01 -2.1 9.07e-01 7.72e-01h 1
38 2.2889273e+00 6.30e-03 3.95e-03 -3.7 4.23e-01 -2.6 1.00e+00 7.07e-01h 1
39 2.2809922e+00 4.56e-03 2.64e-03 -3.8 3.34e-01 - 1.00e+00 7.72e-01h 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
40 2.2752159e+00 2.33e-04 3.72e-04 -4.0 7.82e-02 - 1.00e+00 1.00e+00h 1
41 2.2751951e+00 2.33e-05 2.36e-05 -4.0 2.14e-02 - 1.00e+00 1.00e+00h 1
42 2.2751890e+00 3.19e-08 1.02e-08 -4.0 7.29e-04 - 1.00e+00 1.00e+00h 1
43 2.2751890e+00 2.50e-13 1.25e-13 -4.0 2.29e-06 - 1.00e+00 1.00e+00h 1
Number of Iterations....: 43
(scaled) (unscaled)
Objective...............: 2.2751889659278057e+00 2.2751889659278057e+00
Dual infeasibility......: 1.2548133105499959e-13 1.2548133105499959e-13
Constraint violation....: 2.5024426975051028e-13 2.5024426975051028e-13
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 1.0000000000026431e-04 1.0000000000026431e-04
Overall NLP error.......: 2.5024426975051028e-13 2.5024426975051028e-13
Number of objective function evaluations = 44
Number of objective gradient evaluations = 44
Number of equality constraint evaluations = 44
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 44
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 43
Total seconds in IPOPT = 12.178
EXIT: Optimal Solution Found.Visualize Solution
plot(prob.trajectory) # See NamedTrajectories.jl documentation for plotting options
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.2461719205676305 seconds
Δt range: [0.072857964224019, 0.175]
Max |u₁|: 0.9967728853584535
Max |u₂|: 0.9979498454887172
Final error: 0.0Key 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.