79
79
@testset " Linear systems" begin
80
80
function is_bangbang (input_sol, lbounds, ubounds, rtol = 1e-4 )
81
81
bangbang = true
82
- for v in 1 : length (input_sol. u[1 ]) - 1
83
- all (i -> ≈ (i[v], bounds[v]; rtol) || ≈ (i[v], bounds[u]; rtol), input_sol. u) || (bangbang = false )
82
+ for v in 1 : (length (input_sol. u[1 ]) - 1 )
83
+ all (i -> ≈ (i[v], bounds[v]; rtol) || ≈ (i[v], bounds[u]; rtol), input_sol. u) ||
84
+ (bangbang = false )
84
85
end
85
86
bangbang
86
87
end
87
88
88
89
# Double integrator
89
90
t = M. t_nounits
90
91
D = M. D_nounits
91
- @variables x (.. ) [bounds = (0. , 0.25 )] v (.. )
92
- @variables u (t) [bounds = (- 1. , 1. ), input = true ]
92
+ @variables x (.. ) [bounds = (0.0 , 0.25 )] v (.. )
93
+ @variables u (t) [bounds = (- 1.0 , 1.0 ), input = true ]
93
94
constr = [v (1.0 ) ~ 0.0 ]
94
95
cost = [- x (1.0 )] # Maximize the final distance.
95
- @named block = ODESystem ([D (x (t)) ~ v (t), D (v (t)) ~ u], t; costs = cost, constraints = constr)
96
- block, input_idxs = structural_simplify (block, ([u],[]))
96
+ @named block = ODESystem (
97
+ [D (x (t)) ~ v (t), D (v (t)) ~ u], t; costs = cost, constraints = constr)
98
+ block, input_idxs = structural_simplify (block, ([u], []))
97
99
98
- u0map = [x (t) => 0. , v (t) => 0. ]
99
- tspan = (0. , 1. )
100
- parammap = [u => 0. ]
100
+ u0map = [x (t) => 0.0 , v (t) => 0.0 ]
101
+ tspan = (0.0 , 1.0 )
102
+ parammap = [u => 0.0 ]
101
103
jprob = JuMPControlProblem (block, u0map, tspan, parammap; dt = 0.01 )
102
104
jsol = solve (jprob, Ipopt. Optimizer, :Verner8 )
103
105
# Linear systems have bang-bang controls
104
- @test is_bangbang (jsol. input_sol, [- 1. ], [1. ])
106
+ @test is_bangbang (jsol. input_sol, [- 1.0 ], [1.0 ])
105
107
# Test reached final position.
106
108
@test ≈ (jsol. sol. u[end ][1 ], 0.25 , rtol = 1e-5 )
107
109
108
110
iprob = InfiniteOptControlProblem (block, u0map, tspan, parammap; dt = 0.01 )
109
111
isol = solve (iprob, Ipopt. Optimizer; silent = true )
110
- @test is_bangbang (isol. input_sol, [- 1. ], [1. ])
112
+ @test is_bangbang (isol. input_sol, [- 1.0 ], [1.0 ])
111
113
@test ≈ (isol. sol. u[end ][1 ], 0.25 , rtol = 1e-5 )
112
114
113
115
# ##################
@@ -118,21 +120,21 @@ end
118
120
@parameters b c μ s ν
119
121
120
122
tspan = (0 , 4 )
121
- eqs = [D (w (t)) ~ - μ* w (t) + b* s * α * w (t),
122
- D (q (t)) ~ - ν* q (t) + c* (1 - α)* s * w (t)]
123
+ eqs = [D (w (t)) ~ - μ * w (t) + b * s * α * w (t),
124
+ D (q (t)) ~ - ν * q (t) + c * (1 - α) * s * w (t)]
123
125
costs = [- q (tspan[2 ])]
124
-
126
+
125
127
@named beesys = ODESystem (eqs, t; costs)
126
- beesys, input_idxs = structural_simplify (beesys, ([α],[]))
128
+ beesys, input_idxs = structural_simplify (beesys, ([α], []))
127
129
u0map = [w (t) => 40 , q (t) => 2 ]
128
130
pmap = [b => 1 , c => 1 , μ => 1 , s => 1 , ν => 1 , α => 1 ]
129
131
130
132
jprob = JuMPControlProblem (beesys, u0map, tspan, pmap, dt = 0.01 )
131
133
jsol = solve (jprob, Ipopt. Optimizer, :Tsitouras5 )
132
- @test is_bangbang (jsol. input_sol, [0. ], [1. ])
134
+ @test is_bangbang (jsol. input_sol, [0.0 ], [1.0 ])
133
135
iprob = InfiniteOptControlProblem (beesys, u0map, tspan, pmap, dt = 0.01 )
134
136
isol = solve (jprob, Ipopt. Optimizer, :Tsitouras5 )
135
- @test is_bangbang (isol. input_sol, [0. ], [1. ])
137
+ @test is_bangbang (isol. input_sol, [0.0 ], [1.0 ])
136
138
end
137
139
138
140
@testset " Rocket launch" begin
@@ -144,18 +146,20 @@ end
144
146
drag (h, v) = D_c * v^ 2 * exp (- h_c * (h - h₀) / h₀)
145
147
gravity (h) = g₀ * (h₀ / h)
146
148
147
- eqs = [D (h (t)) ~ v (t),
148
- D (v (t)) ~ (T (t) - drag (h (t), v (t))) / m (t) - gravity (h (t)),
149
- D (m (t)) ~ - T (t) / c]
149
+ eqs = [D (h (t)) ~ v (t),
150
+ D (v (t)) ~ (T (t) - drag (h (t), v (t))) / m (t) - gravity (h (t)),
151
+ D (m (t)) ~ - T (t) / c]
150
152
151
- (ts, te) = (0. , 0.2 )
153
+ (ts, te) = (0.0 , 0.2 )
152
154
costs = [- h (te)]
153
155
constraints = [T (te) ~ 0 ]
154
156
@named rocket = ODESystem (eqs, t; costs, constraints)
155
157
rocket, input_idxs = structural_simplify (rocket, ([T (t)], []))
156
158
157
159
u0map = [h (t) => h₀, m (t) => m₀, v (t) => 0 ]
158
- pmap = [g₀ => 1 , m₀ => 1.0 , h_c => 500 , c => 0.5 *√ (g₀* h₀), D_c => 0.5 * 620 * m₀/ g₀, Tₘ => 3.5 * g₀* m₀, T (t) => 0. , h₀ => 1 , m_c => 0.6 ]
160
+ pmap = [
161
+ g₀ => 1 , m₀ => 1.0 , h_c => 500 , c => 0.5 * √ (g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀,
162
+ Tₘ => 3.5 * g₀ * m₀, T (t) => 0.0 , h₀ => 1 , m_c => 0.6 ]
159
163
jprob = JuMPControlProblem (rocket, u0map, (ts, te), pmap; dt = 0.005 , cse = false )
160
164
jsol = solve (jprob, Ipopt. Optimizer, :RadauIA3 )
161
165
@test jsol. sol. u[end ][1 ] > 1.012
@@ -165,17 +169,17 @@ end
165
169
t = M. t_nounits
166
170
D = M. D_nounits
167
171
168
- @variables x (.. ) u (.. ) [input = true , bounds = (0 ,1 )]
172
+ @variables x (.. ) u (.. ) [input = true , bounds = (0 , 1 )]
169
173
@parameters tf
170
- eqs = [D (x (t)) ~ - 2 + 0.5 * u (t)]
174
+ eqs = [D (x (t)) ~ - 2 + 0.5 * u (t)]
171
175
# Integral cost function
172
- costs = [- ∫ (x (t)- u (t)), - x (tf)]
176
+ costs = [- ∫ (x (t) - u (t)), - x (tf)]
173
177
consolidate (u) = u[1 ] + u[2 ]
174
178
@named rocket = ODESystem (eqs, t; costs, consolidate)
175
179
rocket, input_idxs = structural_simplify (rocket, ([u (t)], []))
176
180
177
181
u0map = [x (t) => 17.5 ]
178
- pmap = [u (t) => 0. , tf => 8 ]
182
+ pmap = [u (t) => 0.0 , tf => 8 ]
179
183
jprob = JuMPControlProblem (rocket, u0map, (0 , tf), pmap; steps = 201 )
180
184
jsol = solve (jprob, Ipopt. Optimizer, :Tsitouras5 )
181
185
@test isapprox (jsol. sol. t[end ], 10.0 , rtol = 1e-3 )
0 commit comments