Free initial and final times
This tutorial is part of a series on optimal control problems with free time variables. See also: Free final time and Free initial time.
In this tutorial, we consider an optimal control problem with the initial and final times as free variables, that is there are parts of the variable to be optimized. The required packages for the tutorial are:
using OptimalControl # Main package
using NLPModelsIpopt # Direct solver
using Plots # VisualizationThe problem we consider is the following:
@def ocp begin
v = (t0, tf) ∈ R², variable # the initial and final times are free variables
t ∈ [t0, tf], time
x ∈ R², state
u ∈ R, control
-1 ≤ u(t) ≤ 1
x(t0) == [0, 0]
x(tf) == [1, 0]
0.05 ≤ t0 ≤ 10
0.05 ≤ tf ≤ 10
0.01 ≤ tf - t0 ≤ Inf
ẋ(t) == [x₂(t), u(t)]
t0 → max
endWe now solve the problem using a direct method.
sol = solve(ocp; grid_size=100)▫ This is OptimalControl 2.0.1, solving with: collocation → adnlp → ipopt (cpu)
📦 Configuration:
├─ Discretizer: collocation (grid_size = 100)
├─ Modeler: adnlp
└─ Solver: ipopt
▫ This is Ipopt version 3.14.19, running with linear solver MUMPS 5.8.2.
Number of nonzeros in equality constraint Jacobian...: 1104
Number of nonzeros in inequality constraint Jacobian.: 2
Number of nonzeros in Lagrangian Hessian.............: 402
Total number of variables............................: 304
variables with only lower bounds: 0
variables with lower and upper bounds: 102
variables with only upper bounds: 0
Total number of equality constraints.................: 204
Total number of inequality constraints...............: 1
inequality constraints with only lower bounds: 1
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 1.0000000e-01 9.00e-01 5.00e-01 0.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 5.0499990e-02 8.82e-01 6.08e+02 -6.0 4.98e+00 - 1.98e-03 2.00e-02h 1
2 1.0406183e-01 8.82e-01 5.54e+02 2.0 8.61e+02 - 1.18e-03 5.49e-04h 2
3 1.4252831e-01 8.81e-01 4.77e+02 2.0 7.03e+02 - 3.71e-03 4.42e-04h 2
4 2.1720495e-01 8.80e-01 6.67e+02 2.0 1.69e+02 - 1.32e-01 1.04e-03h 3
5 3.4948612e-01 8.76e-01 1.85e+03 2.0 5.04e+01 - 1.03e-02 5.02e-03h 3
6 3.5416471e-01 8.76e-01 2.14e+03 2.6 2.13e+02 - 1.37e-02 5.45e-05h 7
7 3.4485956e-01 8.76e-01 2.41e+03 2.2 8.43e+01 - 3.86e-03 3.25e-04h 6
8 3.2661512e-01 8.75e-01 2.39e+03 1.0 1.36e+02 4.0 3.30e-03 2.68e-04h 5
9 4.8740674e-01 8.73e-01 3.36e+05 1.9 1.20e+02 4.4 7.09e-03 2.69e-03f 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
10 8.1136168e-01 8.72e-01 3.00e+05 3.0 1.04e+03 - 3.01e-04 1.62e-03f 1
11 9.5844594e-01 8.71e-01 3.32e+05 3.0 1.68e+03 - 2.64e-04 8.00e-04f 1
12 9.8683592e-01 8.71e-01 3.32e+05 3.0 6.73e+02 - 1.15e-03 1.62e-04h 1
13 1.0292921e+00 8.70e-01 3.32e+05 3.0 4.33e+03 - 2.89e-04 2.57e-04h 1
14 1.2582402e+00 8.69e-01 3.30e+05 3.0 8.82e+02 - 8.43e-04 1.42e-03f 1
15 1.3841146e+00 8.69e-01 3.31e+05 3.0 1.08e+03 - 9.45e-04 8.61e-04h 1
16 1.4298778e+00 8.68e-01 3.30e+05 3.0 1.13e+03 - 1.01e-03 3.37e-04h 1
17 1.4939307e+00 8.68e-01 3.30e+05 3.0 1.10e+03 - 9.49e-04 4.97e-04h 1
18 1.5668308e+00 8.67e-01 3.29e+05 3.0 1.03e+03 - 1.02e-03 5.97e-04h 1
19 1.6561784e+00 8.67e-01 3.28e+05 3.0 9.91e+02 - 1.17e-03 7.77e-04h 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
20 1.7426670e+00 8.66e-01 3.27e+05 3.0 8.25e+02 - 1.67e-03 8.05e-04h 1
21 1.8441208e+00 8.65e-01 3.25e+05 3.0 7.05e+02 - 1.72e-03 1.03e-03h 1
22 1.9538610e+00 8.64e-01 3.23e+05 3.0 5.23e+02 - 2.34e-03 1.21e-03h 1
23 2.0859372e+00 8.63e-01 3.20e+05 3.0 4.19e+02 - 3.12e-03 1.62e-03h 1
24 2.2390088e+00 8.61e-01 3.15e+05 3.0 3.04e+02 - 4.38e-03 2.14e-03h 1
25 2.4284602e+00 8.58e-01 3.10e+05 3.0 2.05e+02 - 6.65e-03 3.14e-03h 1
26 2.6660774e+00 8.54e-01 3.04e+05 3.0 1.20e+02 - 1.24e-02 4.92e-03h 1
27 3.0454505e+00 8.44e-01 2.99e+05 3.0 4.01e+01 - 6.05e-02 1.11e-02h 1
28 4.5010598e+00 7.46e-01 3.38e+05 3.0 1.74e+01 - 1.00e+00 1.16e-01h 1
29 3.6265964e+00 1.42e-02 1.59e+05 2.3 1.82e+00 - 9.99e-01 1.00e+00h 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
30 3.9071083e+00 1.27e-03 2.82e+04 1.5 5.18e-01 - 9.73e-01 1.00e+00f 1
31 4.2126572e+00 1.94e-05 9.31e+02 -0.2 3.06e-01 - 9.53e-01 1.00e+00f 1
32 7.6233949e+00 2.43e-05 2.21e+01 -1.5 3.45e+00 - 6.14e-01 1.00e+00f 1
33 7.8795305e+00 9.13e-06 7.93e+00 -2.4 4.11e-01 - 9.83e-01 6.52e-01f 1
34 7.8792563e+00 6.08e-10 5.22e+00 -2.8 5.87e-04 3.9 9.99e-01 1.00e+00h 1
35 7.9075627e+00 6.69e-05 5.56e-01 -3.2 2.88e-01 - 1.00e+00 8.94e-01f 1
36 7.9805028e+00 3.29e-04 1.06e-01 -3.6 4.56e-01 - 1.00e+00 9.86e-01f 1
37 7.9952232e+00 6.71e-05 8.30e-03 -4.2 3.58e-01 - 1.00e+00 9.13e-01h 1
38 7.9993731e+00 1.72e-05 1.05e-05 -5.2 4.22e-01 - 1.00e+00 1.00e+00h 1
39 7.9999944e+00 2.90e-07 2.57e-07 -7.3 4.72e-02 - 9.87e-01 1.00e+00h 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
40 8.0000001e+00 3.88e-11 3.62e-11 -11.0 6.90e-04 - 1.00e+00 1.00e+00h 1
Number of Iterations....: 40
(scaled) (unscaled)
Objective...............: -8.0000001088122872e+00 8.0000001088122872e+00
Dual infeasibility......: 3.6248167661900865e-11 3.6248167661900865e-11
Constraint violation....: 3.8754555120590339e-11 3.8754555120590339e-11
Variable bound violation: 9.9990000279603919e-08 9.9990000279603919e-08
Complementarity.........: 2.1604106493335526e-11 2.1604106493335526e-11
Overall NLP error.......: 3.8754555120590339e-11 3.8754555120590339e-11
Number of objective function evaluations = 70
Number of objective gradient evaluations = 41
Number of equality constraint evaluations = 70
Number of inequality constraint evaluations = 70
Number of equality constraint Jacobian evaluations = 41
Number of inequality constraint Jacobian evaluations = 41
Number of Lagrangian Hessian evaluations = 40
Total seconds in IPOPT = 3.788
EXIT: Optimal Solution Found.• Solver:
✓ Successful : true
│ Status : first_order
│ Message : Ipopt/generic
│ Iterations : 40
│ Objective : 8.000000108812287
└─ Constraints violation : 3.875455512059034e-11
• Variable: v = (t0, tf) = [8.000000108812287, 10.00000009999]
│ Var dual (lb) : [-2.6159030934388453e-12, -2.105878723782692e-12]
└─ Var dual (ub) : [-1.0802053294263163e-11, -0.99999999999392]
• Boundary duals: [-0.9999999955798888, -0.9999347865237022, 0.9999999955798888, -1.0000651957982982, 5.20606831015179e-12]
And plot the solution.
plot(sol)