Skip to content

Commit 4c97648

Browse files
committed
wip: idem
1 parent 9b9dd6c commit 4c97648

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/controller/construct.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ function setconstraint!(
434434
JuMP.delete(optim, optim[:linconstraint])
435435
JuMP.unregister(optim, :linconstraint)
436436
@constraint(optim, linconstraint, A*Z̃var .≤ b)
437-
# TODO: change this
437+
# TODO: change this !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
438438
if JuMP.solver_name(optim) "Ipopt"
439439
set_nonlincon!(mpc, model, transcription, optim)
440440
end

src/controller/nonlinmpc.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ function init_optimization!(
533533
init_nonlincon!(mpc, model, transcription, gfuncs, ∇gfuncs!, geqfuncs, ∇geqfuncs!)
534534
set_nonlincon!(mpc, model, transcription, optim)
535535
else
536-
# Test new experimental feature:
536+
# ========= Test new experimental feature ========================================
537537

538538
model = mpc.estim.model
539539
jac = mpc.jacobian
@@ -580,21 +580,23 @@ function init_optimization!(
580580
Z̃_∇g .= Z̃_arg
581581
value_and_jacobian!(gfunc!, g, ∇g, ∇g_prep, jac, Z̃_∇g, ∇g_context...)
582582
end
583+
return nothing
583584
end
584585
function gfunc_set!(g_arg, Z̃_arg)
585586
update_con!(g, ∇g, Z̃_∇g, Z̃_arg)
586-
return g_arg .= g
587+
g_arg .= g
588+
return nothing
587589
end
588590
function ∇gfunc_set!(∇g_arg, Z̃_arg)
589591
update_con!(g, ∇g, Z̃_∇g, Z̃_arg)
590-
return ∇g_arg .= nonzeros(∇g)
592+
diffmat2vec!(∇g_arg, ∇g)
593+
return nothing
591594
end
592595

593596
g_min = fill(-myInf, ng)
594597
g_max = fill(+myInf, ng)
595598

596-
I_∇g, J_∇g = SparseArrays.findnz(∇g)
597-
∇g_structure = collect(zip(I_∇g, J_∇g))
599+
∇g_structure = init_diffstructure(∇g)
598600

599601
g_set = Ipopt._VectorNonlinearOracle(;
600602
dimension = nZ̃,
@@ -606,10 +608,6 @@ function init_optimization!(
606608
)
607609
@constraint(optim, Z̃var in g_set)
608610

609-
610-
611-
612-
613611
function geqfunc!(geq, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g)
614612
update_predictions!(ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
615613
return nothing
@@ -629,21 +627,23 @@ function init_optimization!(
629627
Z̃_∇geq .= Z̃_arg
630628
value_and_jacobian!(geqfunc!, geq, ∇geq, ∇geq_prep, jac, Z̃_∇geq, ∇geq_context...)
631629
end
630+
return nothing
632631
end
633632
function geqfunc_set!(geq_arg, Z̃_arg)
634633
update_con_eq!(geq, ∇geq, Z̃_∇geq, Z̃_arg)
635-
return geq_arg .= geq
634+
geq_arg .= geq
635+
return nothing
636636
end
637637
function ∇geqfunc_set!(∇geq_arg, Z̃_arg)
638638
update_con_eq!(geq, ∇geq, Z̃_∇geq, Z̃_arg)
639-
return ∇geq_arg .= nonzeros(∇geq)
639+
diffmat2vec!(∇geq_arg, ∇geq)
640+
return nothing
640641
end
641642

642643
geq_min = zeros(JNT, mpc.con.neq)
643644
geq_max = zeros(JNT, mpc.con.neq)
644645

645-
I_∇geq, J_∇geq = SparseArrays.findnz(∇geq)
646-
∇geq_structure = collect(zip(I_∇geq, J_∇geq))
646+
∇geq_structure = init_diffstructure(∇geq)
647647

648648
#=
649649
# Langragian of the optimization problem:

src/general.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,22 @@ function limit_solve_time(optim::GenericModel, Ts)
5858
end
5959

6060
"Init a differentiation result matrix as dense or sparse matrix, as required by `backend`."
61-
init_diffmat(T, ::AbstractADType, _ , nx , ny) = Matrix{T}(undef, ny, nx)
62-
init_diffmat(T, ::AutoSparse ,prep , _ , _ ) = similar(sparsity_pattern(prep), T)
61+
init_diffmat(T, ::AbstractADType, _ , nx, ny) = zeros(T, ny, nx)
62+
function init_diffmat(T, ::AutoSparse, prep , _ , _ )
63+
A = similar(sparsity_pattern(prep), T)
64+
return A .= 0
65+
end
66+
67+
"Init the sparsity structure of matrix `A` as required by `JuMP.jl`."
68+
function init_diffstructure(A::AbstractSparseMatrix)
69+
I, J = findnz(A)
70+
return collect(zip(I, J))
71+
end
72+
init_diffstructure(A::AbstractMatrix)= Tuple.(CartesianIndices(A))[:]
73+
74+
"Store the differentiation matrix `A` in the vector `v` as required by `JuMP.jl.`"
75+
diffmat2vec!(v::AbstractVector, A::AbstractSparseMatrix) = v .= nonzeros(A)
76+
diffmat2vec!(v::AbstractVector, A::AbstractMatrix) = v[:] = A
6377

6478
"Verify that x and y elements are different using `!==`."
6579
isdifferent(x, y) = any(xi !== yi for (xi, yi) in zip(x, y))

0 commit comments

Comments
 (0)