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

[BUG]: Symbolic regression fails with a dimension mismatch #389

Closed
sdwfrost opened this issue Dec 17, 2024 · 5 comments
Closed

[BUG]: Symbolic regression fails with a dimension mismatch #389

sdwfrost opened this issue Dec 17, 2024 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@sdwfrost
Copy link

What happened?

Describe the bug 🐞

Symbolic regression fails on a problem where Sindy and DMD work, when linking DataDrivenDiffEq with SymbolicRegression. I've also filed the issue on DataDrivenDiffEq (SciML/DataDrivenDiffEq.jl#523), but @ChrisRackauckas seems to think it's a SymbolicRegression issue.

Expected behavior

A data-driven solution should be found.

Minimal Reproducible Example 👇

# Load libraries
using OrdinaryDiffEq
using DataDrivenDiffEq
using DataDrivenSparse
using DataDrivenSR
using ModelingToolkit

# Define the SIR model
function sir_ode(u,p,t)
    (s,i,r) = u
    (β,γ) = p
    ds = -β*s*i
    di = β*s*i - γ*i
    dr = γ*i
    [ds,di,dr]
end

p = [0.5,0.25]
u0 = [0.99, 0.01, 0.0]
tspan = (0.0, 40.0)
δt = 1

solver = ExplicitRK()
sir_prob = ODEProblem(sir_ode, u0, tspan, p)
sir_sol = solve(sir_prob, solver, saveat = δt)

# Define the data-driven problem
dd_prob = DataDrivenProblem(sir_sol)

# Define the basis
@parameters t
@variables (u(t))[1:3]
Ψ = Basis([u; u[1]*u[2]], u, independent_variable = t)

## Sparse regression (works)
res_sparse = solve(dd_prob, Ψ, STLSQ(),digits=1)

## Symbolic regression
eqsearch_options = SymbolicRegression.Options(binary_operators = [+, *],
                                              loss = L1DistLoss(),
                                              verbosity = -1, progress = false, npop = 30,
                                              timeout_in_seconds = 60.0)
eqs = EQSearch(eq_options = eqsearch_options)
res_sr = solve(dd_prob, Ψ, eqs)

Version

[8254be44] SymbolicRegression v0.14.5

Operating System

macOS

Interface

Julia REPL

Relevant log output

ERROR: BoundsError: attempt to access 0-element Vector{Any} at index [1]
Stacktrace:
[1] getindex
@ ./essentials.jl:13 [inlined]

Extra Info

No response

@sdwfrost sdwfrost added the bug Something isn't working label Dec 17, 2024
@MilesCranmer
Copy link
Owner

Are you able to reproduce it in pure SymbolicRegression.jl? I'm not familiar with the DataDrivenDiffEq.jl syntax unfortunately.

Can you also show the full traceback?

@MilesCranmer
Copy link
Owner

Oh, I think the issue is:

[8254be44] SymbolicRegression v0.14.5

The current version of SymbolicRegression.jl is 1.5.0. The last 0. version was 0.24.5. So 0.14.5 is quite old. Can you figure out why it's not updating?

@sdwfrost
Copy link
Author

I'm not sure why it didn't update. I ran from a fresh environment to get 1.5.0, but I still get the same error. The code spends some time running, so it may be post-processing the output from SymbolicRegression that's giving it problems, which would be a DataDrivenDiffEq issue:

ERROR: BoundsError: attempt to access 0-element Vector{Any} at index [1]
Stacktrace:
  [1] throw_boundserror(A::Vector{Any}, I::Tuple{Int64})
    @ Base ./essentials.jl:14
  [2] getindex
    @ ./essentials.jl:916 [inlined]
  [3] (::DataDrivenSR.var"#15#23"{Vector{Float64}, Vector{Any}})(i::Int64)
    @ DataDrivenSR ~/.julia/packages/DataDrivenSR/w4puU/src/DataDrivenSR.jl:170
  [4] iterate
    @ ./generator.jl:48 [inlined]
  [5] _collect(c::Base.OneTo{…}, itr::Base.Generator{…}, ::Base.EltypeUnknown, isz::Base.HasShape{…})
    @ Base ./array.jl:800
  [6] collect_similar(cont::Base.OneTo{Int64}, itr::Base.Generator{Base.OneTo{…}, DataDrivenSR.var"#15#23"{…}})
    @ Base ./array.jl:709
  [7] map(f::Function, A::Base.OneTo{Int64})
    @ Base ./abstractarray.jl:3371
  [8] convert_to_basis(paretofrontier::Vector{…}, prob::DataDrivenDiffEq.InternalDataDrivenProblem{…})
    @ DataDrivenSR ~/.julia/packages/DataDrivenSR/w4puU/src/DataDrivenSR.jl:169
  [9] DataDrivenSR.SRResult(prob::DataDrivenDiffEq.InternalDataDrivenProblem{…}, hof::Vector{…}, paretos::Vector{…})
    @ DataDrivenSR ~/.julia/packages/DataDrivenSR/w4puU/src/DataDrivenSR.jl:71
 [10] (::EQSearch)(ps::DataDrivenDiffEq.InternalDataDrivenProblem{…}, X::Matrix{…}, Y::Matrix{…})
    @ DataDrivenSR ~/.julia/packages/DataDrivenSR/w4puU/src/DataDrivenSR.jl:203
 [11] (::DataDrivenSR.var"#26#27"{DataDrivenDiffEq.InternalDataDrivenProblem{}, EQSearch})(::Tuple{Matrix{…}, Matrix{…}})
    @ DataDrivenSR ~/.julia/packages/DataDrivenSR/w4puU/src/DataDrivenSR.jl:214
 [12] iterate
    @ ./generator.jl:48 [inlined]
 [13] collect(itr::Base.Generator{DataLoader{…}, DataDrivenSR.var"#26#27"{…}})
    @ Base ./array.jl:780
 [14] map
    @ ./abstractarray.jl:3399 [inlined]
 [15] solve!(ps::DataDrivenDiffEq.InternalDataDrivenProblem{…})
    @ DataDrivenSR ~/.julia/packages/DataDrivenSR/w4puU/src/DataDrivenSR.jl:213
 [16] solve(::DataDrivenProblem{Float64, false, DataDrivenDiffEq.Continuous}, ::Vararg{Any}; kwargs::@Kwargs{})
    @ CommonSolve ~/.julia/packages/CommonSolve/JfpfI/src/CommonSolve.jl:23
 [17] top-level scope

@MilesCranmer
Copy link
Owner

Yeah it looks like the entire traceback is inside that package rather than SymbolicRegression.jl. Not sure there's anything I can do from my end. Maybe try to bisect the specific SymbolicRegression.jl version where it starts failing like this? https://github.com/MilesCranmer/SymbolicRegression.jl/releases. I'm assuming there is a version where this at one point worked?

@sdwfrost
Copy link
Author

I'm assuming that there was a working version...however, v0.14.5 is the one that gets installed due to compatibility with DataDrivenSR, so I have to fiddle a bit more to try out lower version numbers. Closing for now - it looks like DataDrivenSR needs some love.

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

No branches or pull requests

2 participants