Skip to content

Assertion error with FastDifferentiation and ModelPredictiveControl #765

Closed
@franckgaga

Description

@franckgaga

I'm raising an issue here, related this discussion : JuliaControl/ModelPredictiveControl.jl#174

I think it's a bug/limitation in FastDifferentiation. Here's a not-so-minimal exemple:

using ModelPredictiveControl
using DifferentiationInterface
import FastDifferentiation
function f!(ẋ, x, u, _ , p)
    g, L, K, m = p       # [m/s²], [m], [kg/s], [kg]
    θ, ω = x[1], x[2]    # [rad], [rad/s]
    τ = u[1]             # [Nm]
    ẋ[1] = ω
    ẋ[2] = -g/L*sin(θ) - K/m*ω + τ/m/L^2
end
h!(y, x, _ , _ ) = (y[1] = 180/π*x[1])   # [°]
p = [9.8, 0.4, 1.2, 0.3]
nu = 1; nx = 2; ny = 1; Ts = 0.1
model = NonLinModel(f!, h!, Ts, nu, nx, ny; p)

σQ = [0.1, 1.0]; σR=[5.0]; nint_u=[1]; σQint_u=[0.1]
estim = UnscentedKalmanFilter(model; σQ, σR, nint_u, σQint_u)

p_plant = copy(p); p_plant[3] = 1.25*p[3]
plant = NonLinModel(f!, h!, Ts, nu, nx, ny; p=p_plant)
N = 35; u = [0.5]; 
res = sim!(estim, N, u; plant, y_noise=[0.5]) # no error by commenting out this line ! 😮😮😮

Hp, Hc, Mwt, Nwt, Cwt = 20, 2, [0.5], [2.5], Inf
backend = AutoFastDifferentiation() 
transcription = MultipleShooting()
nmpc = NonLinMPC(estim; Hp, Hc, Mwt, Nwt, Cwt, transcription, jacobian=backend)

giving this error:

ERROR: AssertionError: Should only be one path from root 5 to variable 1. Instead have 2 children from node 122 on the path
Stacktrace:
  [1] follow_path(a::FastDifferentiation.DerivativeGraph{Int64}, root_index::Int64, var_index::Int64)
    @ FastDifferentiation ~/.julia/packages/FastDifferentiation/kJ1qL/src/Factoring.jl:525
  [2] evaluate_path
    @ ~/.julia/packages/FastDifferentiation/kJ1qL/src/Factoring.jl:555 [inlined]
  [3] _symbolic_jacobian!(graph::FastDifferentiation.DerivativeGraph{…}, partial_variables::Vector{…})
    @ FastDifferentiation ~/.julia/packages/FastDifferentiation/kJ1qL/src/Jacobian.jl:47
  [4] _symbolic_jacobian
    @ ~/.julia/packages/FastDifferentiation/kJ1qL/src/Jacobian.jl:61 [inlined]
  [5] jacobian
    @ ~/.julia/packages/FastDifferentiation/kJ1qL/src/Jacobian.jl:90 [inlined]
  [6] prepare_jacobian_nokwarg(::Val{…}, ::Function, ::Vector{…}, ::AutoFastDifferentiation, ::Vector{…}, ::Cache{…}, ::Cache{…}, ::Cache{…}, ::Cache{…}, ::Cache{…}, ::Cache{…}, ::Cache{…}, ::Cache{…}, ::Cache{…}, ::Cache{…}, ::Cache{…})
    @ DifferentiationInterfaceFastDifferentiationExt ~/.julia/packages/DifferentiationInterface/7eD1K/ext/DifferentiationInterfaceFastDifferentiationExt/twoarg.jl:320
  [7] #prepare_jacobian#48
    @ ~/.julia/packages/DifferentiationInterface/7eD1K/src/first_order/jacobian.jl:23 [inlined]
  [8] prepare_jacobian
    @ ~/.julia/packages/DifferentiationInterface/7eD1K/src/first_order/jacobian.jl:15 [inlined]
  [9] get_optim_functions(mpc::NonLinMPC{…}, ::JuMP.Model)
    @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/controller/nonlinmpc.jl:706
 [10] init_optimization!(mpc::NonLinMPC{…}, model::NonLinModel{…}, optim::JuMP.Model)
    @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/controller/nonlinmpc.jl:543
 [11] (NonLinMPC{})(estim::UnscentedKalmanFilter{…}, Hp::Int64, Hc::Int64, M_Hp::Matrix{…}, N_Hc::Matrix{…}, L_Hp::Matrix{…}, Cwt::Float64, Ewt::Float64, JE::ModelPredictiveControl.var"#159#163", gc!::ModelPredictiveControl.var"#160#164", nc::Int64, p::Vector{…}, transcription::MultipleShooting, optim::JuMP.Model, gradient::AutoForwardDiff{…}, jacobian::AutoFastDifferentiation)
    @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/controller/nonlinmpc.jl:126
 [12] NonLinMPC(estim::UnscentedKalmanFilter{…}; Hp::Int64, Hc::Int64, Mwt::Vector{…}, Nwt::Vector{…}, Lwt::Vector{…}, M_Hp::Matrix{…}, N_Hc::Matrix{…}, L_Hp::Matrix{…}, Cwt::Float64, Ewt::Float64, JE::ModelPredictiveControl.var"#159#163", gc!::ModelPredictiveControl.var"#160#164", gc::ModelPredictiveControl.var"#160#164", nc::Int64, p::Vector{…}, transcription::MultipleShooting, optim::JuMP.Model, gradient::AutoForwardDiff{…}, jacobian::AutoFastDifferentiation)
    @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/controller/nonlinmpc.jl:393
 [13] top-level scope
    @ ~/Dropbox/Programmation/Julia/TestMPC/src/debug_FD.jl:28
Some type information was truncated. Use `show(err)` to see complete types.

Now comes the very weird part: if I comment out the line where I simulate the state estimator estim, there is no error and the mpc objects work as expected. I really don't know what's happening here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions