Skip to content

Commit feb9da4

Browse files
committed
format
1 parent 7e41540 commit feb9da4

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

ext/MTKJuMPControlExt.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false)
126126
stidxmap = Dict([v => i for (i, v) in enumerate(states)])
127127
u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map])
128128
u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) :
129-
[stidxmap[MTK.default_toterm(k)] for (k, v) in u0map]
129+
[stidxmap[MTK.default_toterm(k)] for (k, v) in u0map]
130130
add_initial_constraints!(model, u0, u0_idxs, tspan[1])
131131
return model
132132
end
@@ -193,7 +193,7 @@ function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = fals
193193

194194
auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)])
195195
jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap)
196-
196+
197197
# Substitute to-term'd variables
198198
for (i, cons) in enumerate(jconstraints)
199199
if cons isa Equation
@@ -298,7 +298,7 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false)
298298
ΔU = @view ΔUs[i, :]
299299
Uₙ = U + ΔU * h * dt
300300
@constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]),
301-
DomainRestrictions(t => τ), base_name="solve_K$i()")
301+
DomainRestrictions(t => τ), base_name="solve_K$i()")
302302
end
303303
@constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min+ dt, tmax)),
304304
DomainRestrictions(t => τ), base_name="solve_U()")

test/dynamic_optimization/jump_control.jl

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ using DataInterpolations
3535
osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB
3636
@test (jsol2.sol.u, osol2.u, rtol = 0.001)
3737
iprob = InfiniteOptControlProblem(sys, u0map, tspan, parammap, dt = 0.01)
38-
isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()), silent = true) # 11.540 ms, 4.00 MiB
38+
isol = solve(iprob, Ipopt.Optimizer,
39+
derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()),
40+
silent = true) # 11.540 ms, 4.00 MiB
3941
@test (jsol2.sol.u, osol2.u, rtol = 0.001)
4042

4143
# With a constraint
@@ -52,7 +54,8 @@ using DataInterpolations
5254

5355
iprob = InfiniteOptControlProblem(
5456
lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01)
55-
isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB
57+
isol = solve(iprob, Ipopt.Optimizer,
58+
derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB
5659
sol = isol.sol
5760
@test sol(0.6)[1] 3.5
5861
@test sol(0.3)[1] 7.0
@@ -62,7 +65,8 @@ using DataInterpolations
6265
@mtkbuild lksys = ODESystem(eqs, t; constraints = constr)
6366
iprob = InfiniteOptControlProblem(
6467
lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01)
65-
isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB
68+
isol = solve(iprob, Ipopt.Optimizer,
69+
derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB
6670
@test all(u -> u > [1, 1], isol.sol.u)
6771

6872
jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01)
@@ -93,7 +97,7 @@ end
9397
constr = [v(1.0) ~ 0.0]
9498
cost = [-x(1.0)] # Maximize the final distance.
9599
@named block = ODESystem(
96-
[D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr)
100+
[D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr)
97101
block, input_idxs = structural_simplify(block, ([u(t)], []))
98102

99103
u0map = [x(t) => 0.0, v(t) => 0.0]
@@ -146,9 +150,13 @@ end
146150

147151
@parameters (α_interp::LinearInterpolation)(..)
148152
eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t),
149-
D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)]
153+
D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)]
150154
@mtkbuild beesys_ode = ODESystem(eqs, t)
151-
oprob = ODEProblem(beesys_ode, u0map, tspan, merge(Dict(pmap), Dict(α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation))))
155+
oprob = ODEProblem(beesys_ode,
156+
u0map,
157+
tspan,
158+
merge(Dict(pmap),
159+
Dict(α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation))))
152160
osol = solve(oprob, Tsit5(); dt = 0.01, adaptive = false)
153161
@test (osol.u, jsol.sol.u, rtol = 0.01)
154162
osol2 = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false)
@@ -182,10 +190,12 @@ end
182190
jsol = solve(jprob, Ipopt.Optimizer, :RadauIIA5, silent = true)
183191
@test jsol.sol.u[end][1] > 1.012
184192

185-
iprob = InfiniteOptControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false)
186-
isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true)
193+
iprob = InfiniteOptControlProblem(
194+
rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false)
195+
isol = solve(iprob, Ipopt.Optimizer,
196+
derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true)
187197
@test isol.sol.u[end][1] > 1.012
188-
198+
189199
# Test solution
190200
@parameters (T_interp::CubicSpline)(..)
191201
eqs = [D(h(t)) ~ v(t),
@@ -229,16 +239,16 @@ end
229239

230240
s = sin(θ(t))
231241
c = cos(θ(t))
232-
H = [mₖ+mₚ mₚ*l*c
242+
H = [mₖ+mₚ mₚ*l*c
233243
mₚ*l*c mₚ*l^2]
234244
C = [0 -mₚ*D(θ(t))*l*s
235-
0 0]
245+
0 0]
236246
qd = [D(x(t)), D(θ(t))]
237-
G = [0, mₚ*g*l*s]
247+
G = [0, mₚ * g * l * s]
238248
B = [1, 0]
239249

240250
tf = 5
241-
rhss = -H \ Vector(C*qd + G - B*u)
251+
rhss = -H \ Vector(C * qd + G - B * u)
242252
eqs = [D(D(x(t))) ~ rhss[1], D(D(θ(t))) ~ rhss[2]]
243253
cons = [θ(tf) ~ π, x(tf) ~ 0, D(θ(tf)) ~ 0, D(x(tf)) ~ 0]
244254
costs = [(u^2)]
@@ -247,8 +257,8 @@ end
247257
@named cartpole = ODESystem(eqs, t; costs, constraints = cons)
248258
cartpole, input_idxs = structural_simplify(cartpole, ([u], []))
249259

250-
u0map = [D(x(t)) => 0., D(θ(t)) => 0., θ(t) => 0., x(t) => 0.]
251-
pmap = [mₖ => 1., mₚ => 0.2, l => 0.5, g => 9.81, u => 0]
260+
u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0]
261+
pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0]
252262
jprob = JuMPControlProblem(cartpole, u0map, tspan, pmap; dt = 0.04)
253263
jsol = solve(jprob, Ipopt.Optimizer, :RK4)
254264
@test jsol.sol.u[end] [π, 0, 0, 0]

0 commit comments

Comments
 (0)