Skip to content

Commit 6de5521

Browse files
committed
Reformulate optimal control interface
1 parent 306e559 commit 6de5521

2 files changed

Lines changed: 33 additions & 68 deletions

File tree

docs/src/tutorials/optimal_control.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/misc/dynamic_optimization.jl

Lines changed: 33 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,37 @@
1-
@testitem "Linear systems" begin
2-
using BoundaryValueDiffEq
3-
using OptimizationMOI, Ipopt
4-
end
5-
61
@testitem "Rocket launch" begin
72
using BoundaryValueDiffEq
83
using OptimizationMOI, Ipopt
9-
10-
tspan = (0.0, pi / 2)
11-
function simplependulum!(du, u, p, t)
12-
θ = u[1]
13-
= u[2]
14-
du[1] =
15-
du[2] = -9.81 * sin(θ)
16-
end
17-
function bc!(residual, u, p, t)
18-
residual[1] = u(pi / 4)[1] + big(pi / 2)
19-
residual[2] = u(pi / 2)[1] - big(pi / 2)
20-
end
21-
u0 = BigFloat.([pi / 2, pi / 2])
22-
multi_point_bvp = BVProblem(simplependulum!, bc!, u0, tspan)
23-
24-
@testset "BigFloat compatibility with Multi-point BVP" begin
25-
for solver in [MIRK4(), RadauIIa5(), LobattoIIIa4(nested_nlsolve = true)]
26-
sol = solve(multi_point_bvp, solver, dt = 0.05)
27-
@test SciMLBase.successful_retcode(sol.retcode)
28-
end
29-
end
30-
31-
function f!(du, u, p, t)
32-
du[1] = u[2]
33-
du[2] = u[1]
34-
end
35-
function bca!(resid_a, u_a, p)
36-
resid_a[1] = u_a[1] - 1
37-
end
38-
function bcb!(resid_b, u_b, p)
39-
resid_b[1] = u_b[1]
40-
end
41-
bvp_function = BVPFunction(f!, (bca!, bcb!), bcresid_prototype = (zeros(1), zeros(1)), twopoint = Val(true))
42-
tspan = (0.0, 1.0)
43-
two_point_bvp = BVProblem(bvp_function, BigFloat.([1.0, 0.0]), tspan)
44-
45-
@testset "BigFloat compatibility with Two-point BVP" begin
46-
for solver in [MIRK4(), RadauIIa5(), LobattoIIIa4(nested_nlsolve = true)]
47-
sol = solve(two_point_bvp, solver, dt = 0.05)
48-
@test SciMLBase.successful_retcode(sol.retcode)
49-
end
50-
end
51-
52-
function second_f!(ddu, du, u, p, t)
53-
ϵ = 0.1
54-
ddu[1] = u[2]
55-
ddu[2] = (-u[1] * du[2] - u[3] * du[3]) / ϵ
56-
ddu[3] = (du[1] * u[3] - u[1] * du[3]) / ϵ
57-
end
58-
function second_bc!(res, du, u, p, t)
59-
res[1] = u(0.0)[1]
60-
res[2] = u(1.0)[1]
61-
res[3] = u(0.0)[3] + 1
62-
res[4] = u(1.0)[3] - 1
63-
res[5] = du(0.0)[1]
64-
res[6] = du(1.0)[1]
65-
end
66-
u0 = BigFloat.([1.0, 1.0, 1.0])
67-
tspan = (0.0, 1.0)
68-
prob = SecondOrderBVProblem(second_f!, second_bc!, u0, tspan)
69-
@test_broken sol4 = solve(prob, MIRKN4(), dt = 0.01)
70-
@test_broken SciMLBase.successful_retcode(sol4.retcode)
4+
h_0 = 1 # Initial height
5+
v_0 = 0 # Initial velocity
6+
m_0 = 1.0 # Initial mass
7+
m_T = 0.6 # Final mass
8+
g_0 = 1 # Gravity at the surface
9+
h_c = 500 # Used for drag
10+
c = 0.5 * sqrt(g_0 * h_0) # Thrust-to-fuel mass
11+
D_c = 0.5 * 620 * m_0 / g_0 # Drag scaling
12+
u_t_max = 3.5 * g_0 * m_0 # Maximum thrust
13+
T_max = 0.2 # Number of seconds
14+
T = 1_000 # Number of time steps
15+
Δt = 0.2 / T; # Time per discretized step
16+
tspan = (0.0, 0.2)
17+
D(x_h, x_v) = D_c * x_v^2 * exp(-h_c * (x_h - h_0) / h_0)
18+
g(x_h) = g_0 * (h_0 / x_h)^2
19+
function rocket_launch!(du, u, p, t)
20+
# u_t is the control variable (thrust)
21+
x_v, x_h, x_m, u_t = u[1], u[2], u[3], u[4]
22+
du[1] = (u_t-drag(x_h, x_v))/x_m - g(x_h)
23+
du[2] = x_v
24+
du[3] = -u_t/c
25+
end
26+
function constraints!(res, u, p)
27+
res[1] = u[1]
28+
res[2] = u[2]
29+
res[3] = u[3]
30+
res[4] = u[4]
31+
end
32+
cost_fun(u, p) = -u[4]
33+
u0 = [v_0, h_0, m_0, 0.0]
34+
rocket_launch_fun = BVPFunction(rocket_launch!, inequality = constraints!)
35+
rocket_launch_prob = BVProblem(rocket_launch_fun, u0, tspan; cost = cost_fun,
36+
lcons = [0.0, 0.0, m_T, 0.0], ucons = [Inf, Inf, Inf, u_t_max])
7137
end

0 commit comments

Comments
 (0)