Skip to content

Creating of OptimizationSystem with registered function traces #3473

@avinashresearch1

Description

@avinashresearch1

MWE:

using Symbolics, ModelingToolkit
using LinearAlgebra
import LinearAlgebra: eigvals

p_free = @variables begin
    p1, [bounds = (0, 1)]
    p2, [bounds = (0, 1)]
    p3, [bounds = (0, 1)]
    p4, [bounds = (0, 1)]
end

m = diagm(p_free)

function myeigvals_1(A::AbstractMatrix)
    eigvals(A)[1]
end

@register_symbolic myeigvals_1(A::AbstractMatrix)

obj = myeigvals_1(m)
@named opt_sys = OptimizationSystem(obj, p_free, [])

This line in the OptimizationSystemconstructor calls substitute with fold = true by default and no way to change it:

op′ = substitute(op′, irreducible_subs)

and this ends up tracing.

Error & Stacktrace ⚠️

ERROR: MethodError: no method matching zero(::Type{Any})
This error has been manually thrown, explicitly, so the method may exist but be intentionally marked as unimplemented.

Closest candidates are:
  zero(::Type{Union{Missing, T}}) where T
   @ Base missing.jl:105
  zero(::Type{Union{}}, Any...)
   @ Base number.jl:310
  zero(::Type{Dates.DateTime})
   @ Dates ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/Dates/src/types.jl:458
  ...

Stacktrace:
 [1] zero(::Type{Any})
   @ Base ./missing.jl:106
 [2] eigtype(T::Type)
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/eigen.jl:319
 [3] eigvals(A::Matrix{Any}; kws::@Kwargs{})
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/eigen.jl:343
 [4] eigvals(A::Matrix{Any})
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/eigen.jl:343
 [5] myeigvals_1(A::Matrix{Any})
   @ Main ~/JULIAHUB/AFRL/SymbolicRegisterTest.jl/much_simpler_MWE.jl:17
 [6] substitute(expr::SymbolicUtils.BasicSymbolic{Real}, dict::Dict{Any, Any}; fold::Bool)
   @ SymbolicUtils ~/.julia/packages/SymbolicUtils/QUEf3/src/substitute.jl:28
 [7] substitute(expr::SymbolicUtils.BasicSymbolic{Real}, dict::Dict{Any, Any})
   @ SymbolicUtils ~/.julia/packages/SymbolicUtils/QUEf3/src/substitute.jl:16
 [8] OptimizationSystem(op::Num, unknowns::Vector{…}, ps::Vector{…}; observed::Vector{…}, constraints::Vector{…}, default_u0::Dict{…}, default_p::Dict{…}, defaults::Dict{…}, name::Symbol, description::String, systems::Vector{…}, checks::Bool, metadata::Nothing, gui_metadata::Nothing)
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/8S2W1/src/systems/optimization/optimizationsystem.jl:117
 [9] top-level scope
   @ ~/.julia/packages/ModelingToolkit/8S2W1/src/systems/abstractsystem.jl:1958
Some type information was truncated. Use `show(err)` to see complete types.

Note that this traced into the eigvals call.

One can also look at:

julia> substitute(myeigvals_1(m), Dict())
ERROR: MethodError: no method matching zero(::Type{Any})
This error has been manually thrown, explicitly, so the method may exist but be intentionally marked as unimplemented.

Closest candidates are:
  zero(::Type{Union{Missing, T}}) where T
   @ Base missing.jl:105
  zero(::Type{Union{}}, Any...)
   @ Base number.jl:310
  zero(::Type{Dates.DateTime})
   @ Dates ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/Dates/src/types.jl:458
  ...

Stacktrace:
  [1] zero(::Type{Any})
    @ Base ./missing.jl:106
  [2] eigtype(T::Type)
    @ LinearAlgebra ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/eigen.jl:319
  [3] eigvals(A::Matrix{Any}; kws::@Kwargs{})
    @ LinearAlgebra ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/eigen.jl:343
  [4] eigvals(A::Matrix{Any})
    @ LinearAlgebra ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/eigen.jl:343
  [5] myeigvals_1(A::Matrix{Any})
    @ Main ~/JULIAHUB/AFRL/SymbolicRegisterTest.jl/much_simpler_MWE.jl:17
  [6] substitute(expr::SymbolicUtils.BasicSymbolic{Real}, dict::Dict{Any, Any}; fold::Bool)

yet, if fold = false it doesn't trace as expected:

A proposed fix is for OptimizationSystem to expose the fold kwarg?

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
⌃ [961ee093] ModelingToolkit v9.64.3
  [7f7a1694] Optimization v4.1.1
  [d1185830] SymbolicUtils v3.19.0
  [0c5d862f] Symbolics v6.29.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions