Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating of OptimizationSystem with registered function traces #3473

Open
avinashresearch1 opened this issue Mar 19, 2025 · 0 comments · May be fixed by #3475
Open

Creating of OptimizationSystem with registered function traces #3473

avinashresearch1 opened this issue Mar 19, 2025 · 0 comments · May be fixed by #3475
Labels
bug Something isn't working

Comments

@avinashresearch1
Copy link
Contributor

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
@avinashresearch1 avinashresearch1 added the bug Something isn't working label Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant