Skip to content

Commit 0f0cb4e

Browse files
committed
Change the position of constraints
1 parent b7e7042 commit 0f0cb4e

3 files changed

Lines changed: 20 additions & 25 deletions

File tree

docs/src/tutorials/optimal_control.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $$
4141
Similar solving for such optimal control problem can be found on JuMP.jl and InfiniteOpt.jl. The detailed parameters are taken from [COPS](https://www.mcs.anl.gov/%7Emore/cops/cops3.pdf).
4242

4343
```julia
44-
using BoundaryValueDiffEqMIRK, OptimizationMOI, Ipopt
44+
using BoundaryValueDiffEqMIRK, OptimizationIpopt
4545
h_0 = 1 # Initial height
4646
v_0 = 0 # Initial velocity
4747
m_0 = 1.0 # Initial mass
@@ -84,7 +84,7 @@ rocket_launch_fun = BVPFunction(rocket_launch!, rocket_launch_bc!; cost = cost_f
8484
inequality = constraints!, f_prototype = zeros(3))
8585
rocket_launch_prob = BVProblem(rocket_launch_fun, u0, tspan; lcons = [0.0, 0.0, m_T, 0.0],
8686
ucons = [Inf, Inf, m_0, u_t_max])
87-
sol = solve(rocket_launch_prob, MIRK4(; optimize = Ipopt.Optimizer()); dt = 0.002)
87+
sol = solve(rocket_launch_prob, MIRK4(; optimize = IpoptOptimizer()); dt = 0.002)
8888
```
8989

9090
Similar optimal control problem solving can also be deployed in JuMP.jl and InfiniteOpt.jl.

lib/BoundaryValueDiffEqCore/src/utils.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,6 @@ function __extract_problem_details(prob, u0::AbstractArray; dt = 0.0,
272272
new_u = vcat(u0, prob.p)
273273
return Val(false), eltype(new_u), length(new_u), Int(cld(t₁ - t₀, dt)), new_u
274274
end
275-
if !isnothing(prob.f.f_prototype)
276-
length_f_prototype = length(prob.f.f_prototype)
277-
return Val(false), eltype(u0), length_f_prototype, Int(cld(t₁ - t₀, dt)), prob.u0
278-
end
279275
return Val(false), eltype(u0), length(u0), Int(cld(t₁ - t₀, dt)), prob.u0
280276
end
281277
function __extract_problem_details(prob, f::F; dt = 0.0, check_positive_dt::Bool = false,

lib/BoundaryValueDiffEqMIRK/src/mirk.jl

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ function SciMLBase.__init(
8181
end
8282
else
8383
if prob.problem_type isa TwoPointBVProblem
84-
vcat([__alloc(__vec(bcresid_prototype))],
85-
__alloc.(copy.(@view(y₀.u[2:end]))), __alloc.(copy.(y₀.u)))
84+
vcat(__alloc.(copy.(y₀.u)), [__alloc(__vec(bcresid_prototype))],
85+
__alloc.(copy.(@view(y₀.u[2:end]))))
8686
else
87-
vcat([__alloc(bcresid_prototype)],
88-
__alloc.(copy.(@view(y₀.u[2:end]))), __alloc.(copy.(y₀.u)))
87+
vcat(__alloc.(copy.(y₀.u)), [__alloc(bcresid_prototype)],
88+
__alloc.(copy.(@view(y₀.u[2:end]))))
8989
end
9090
end
9191
else
@@ -296,10 +296,10 @@ end
296296

297297
# Let's only do inplace version now since Optimization.jl only supports that.
298298

299-
# J = [J_equality;
300-
# J_inequality]
301-
# where J_equality is the Jacobian we are computed from bc and collocation
302-
# and J_inequality is the Jacobian from inequality constraints(whole-time inequality)
299+
# J = [J_constraints;
300+
# J_bvp]
301+
# where J_constraints is the Jacobian from equality/inequality constraints(whole-time equality/inequality)
302+
# and J_bvp is the Jacobian computed from bc and collocation
303303

304304
@views function __mirk_loss!(
305305
resid, u, p, y, pt::StandardBVProblem, bc!::BC, residual, mesh, cache,
@@ -357,13 +357,13 @@ end
357357
y_ = recursive_unflatten!(y, u)
358358
L = length(y_)
359359
resids = [get_tmp(r, u) for r in residual]
360-
Φ!(resids[2:L], cache, y_, u, trait)
360+
# whole-time inequality constraints
361+
cache.prob.f.inequality.(resids[1:L], y_, nothing)
362+
Φ!(resids[(L + 2):2L], cache, y_, u, trait)
361363
EvalSol.u[1:end] .= __restructure_sol(y_, cache.in_size)
362364
EvalSol.cache.k_discrete[1:end] .= cache.k_discrete
363-
eval_bc_residual!(resids[1], pt, bc!, EvalSol, p, mesh)
365+
eval_bc_residual!(resids[L + 1], pt, bc!, EvalSol, p, mesh)
364366

365-
# whole-time inequality constraints
366-
cache.prob.f.inequality.(resids[(L + 1):end], y_, nothing)
367367
recursive_flatten!(resid, resids)
368368
return nothing
369369
end
@@ -497,7 +497,7 @@ function __construct_problem(cache::MIRKCache{iip}, y, loss_bc::BC, loss_colloca
497497
cache.prob.f.inequality, resid_prototype, bc_diffmode, y, Constant(cache.p))
498498
J_inequality = DI.jacobian(cache.prob.f.inequality, resid_prototype,
499499
cache_inequality, bc_diffmode, y, Constant(cache.p))
500-
jac_prototype = vcat(jac_prototype, J_inequality)
500+
jac_prototype = vcat(J_inequality, jac_prototype)
501501
end
502502

503503
jac = if iip
@@ -554,14 +554,13 @@ function __mirk_mpoint_jacobian!(
554554
inequality_diffcache, loss_bc::BC, loss_collocation::C, loss_inequality,
555555
resid_bc, resid_collocation, resid_prototype, L::Int, p) where {BC, C}
556556
N = length(x)
557-
DI.jacobian!(
558-
loss_bc, resid_bc, @view(J[1:L, :]), bc_diffcache, bc_diffmode, x, Constant(p))
559-
DI.jacobian!(loss_collocation, resid_collocation, @view(J[(L + 1):N, :]),
560-
nonbc_diffcache, nonbc_diffmode, x, Constant(p))
561-
562557
# The Jacobian of constraints
563-
DI.jacobian!(loss_inequality, resid_prototype, @view(J[(N + 1):end, :]),
558+
DI.jacobian!(loss_inequality, resid_prototype, @view(J[1:N, :]),
564559
inequality_diffcache, bc_diffmode, x, Constant(p))
560+
DI.jacobian!(loss_bc, resid_bc, @view(J[(N + 1):(N + L), :]),
561+
bc_diffcache, bc_diffmode, x, Constant(p))
562+
DI.jacobian!(loss_collocation, resid_collocation, @view(J[(N + L + 1):2N, :]),
563+
nonbc_diffcache, nonbc_diffmode, x, Constant(p))
565564
return nothing
566565
end
567566

0 commit comments

Comments
 (0)