1+ # # Minimizing Final Time (Jennings Problem)
2+ # Solve an optimal control problem with a minimal final time.
3+ # Set up and solve the Jennings optimal control benchmark problem.
4+ # # Problem Statement and Model
5+ # When solving differential equations over a variable time interval ``[0,t_f]``,
6+ # we can apply a time-scaling transformation to normalize the interval to``[0,1]``.
7+ # This is achieved by introducing a final time parameter ``t_f``.
8+ # The Jennings optimal control problem divides derivatives by ``t_f``.
9+ # In practice, ``t_f`` appears on the right hand side to avoid any divisions by 0.
10+ # ```math
11+ # \begin{gathered}
12+ # \frac{\frac{dx}{dt}}{t_f} = f(x,u) \\
13+ # \frac{dx}{dt} = t_f f(x,u) \\
14+ # \end{gathered}
15+ # ```
16+ # Our specific problem is defined as the following:
17+ # ```math
18+ # \begin{aligned}
19+ # &&\min_{u(t),t_f} t_f \\
20+ # &&\text{s.t.} &&& \frac{dx_1}{dt}= t_f u, && t \in [0,1] \\
21+ # &&&&&\frac{dx_2}{dt} = t_f \cos(x_1(t)), && t \in [0,1] \\
22+ # &&&&&\frac{dx_3}{dt} = t_f \sin(x_1(t)), && t \in [0,1] \\
23+ # &&&&&x(0) = [\pi/2, 4, 0] \\
24+ # &&&&&x_2(t_f) = 0 \\
25+ # &&&&&x_3(t_f) = 0 \\
26+ # &&&&&-2 \leq u(t) \leq 2
27+ # \end{aligned}
28+ # ```
29+
30+ # # Model Definition
31+
32+ # First we must import ``InfiniteOpt`` and other packages.
33+ using InfiniteOpt, Ipopt, Plots;
34+ # Next we specify an array of initial conditions.
35+ x0 = [π/ 2 , 4 , 0 ]; # x(0) for x1,x2,x3
36+ # We initialize the infinite model and opt to use the Ipopt solver
37+ m = InfiniteModel (Ipopt. Optimizer);
38+ # Recall t is specified as ``\ t \in [0,1]``:
39+ @infinite_parameter (m, t in [0 ,1 ],num_supports= 100 )
40+ # Now let's specify descision variables. Notice that ``t_f`` is
41+ # not a function of time and is a singular value.
42+ @variable (m, x[1 : 3 ], Infinite (t))
43+ @variable (m, - 2 <= u <= 2 , Infinite (t))
44+ @variable (m, 0.1 <= tf);
45+ # Specifying the objective to minimize final time:
46+ @objective (m, Min, tf);
47+ # Define the ODEs which serve as our system model.
48+ @constraint (m, ∂ (x[1 ],t) == tf* u)
49+ @constraint (m, ∂ (x[2 ],t) == tf* cos (x[1 ]))
50+ @constraint (m, ∂ (x[3 ],t) == tf* sin (x[1 ]));
51+ # Set our inital and final conditions.
52+ @constraint (m, [i in 1 : 3 ], x[i](0 ) == x0[i])
53+ @constraint (m, x[2 ](1 ) <= 0 )
54+ @constraint (m, x[3 ](1 ) <= 1e-1 );
55+ # # Problem Solution
56+ # Optimize the model:
57+ optimize! (m)
58+ # Extract the results. Notice that we multiply by ``t_f``
59+ # to scale our time.
60+ ts = value (t)* value (tf)
61+ u_opt = value (u)
62+ x1_opt = value (x[1 ])
63+ x2_opt = value (x[2 ])
64+ x3_opt = value (x[3 ]);
65+ # Plot the results
66+ plot (ts, u_opt, label = " u(t)" , linecolor = :black , linestyle = :dash )
67+ plot! (ts, x1_opt, linecolor = :blue , linealpha = 0.4 , label = " x1" )
68+ plot! (ts, x2_opt, linecolor = :green , linealpha = 0.4 , label = " x2" )
69+ plot! (ts, x3_opt, linecolor = :red , linealpha = 0.4 , label = " x3" );
70+
71+ # ### Maintenance Tests
72+ # These are here to ensure this example stays up to date.
73+ using Test
74+ @test termination_status (m) == MOI. LOCALLY_SOLVED
75+ @test has_values (m)
76+ @test u_opt isa Vector{<: Real }
77+ @test x1_opt isa Vector{<: Real }
0 commit comments