Hello all,
The following is a MWE for a problem I am working on. Here I am trying to fit Qgen as a function of I,u[1],u[2] using a SINDy algorithm but I am running into errors. Here is the code
using OrdinaryDiffEq
using Plots
using DataDrivenSparse, DataDrivenDiffEq
using StableRNGs
using LinearAlgebra
rng = StableRNG(1111)
# Generating synthetic data
function actualODE!(du,u,p,t,T∞,I)
Cbat = 5*3600
du[1] = -I/Cbat
C₁ = -0.00153 # Unit is s-1
C₂ = 0.020306 # Unit is K/J
R0 = 0.03 # Resistance set a 30mohm
Qgen =(I^2)*R0 # This is just an approximate value. In actual case Qgen = f(I,u[1],u[2])
du[2] = (C₁*(u[2]-T∞)) + (C₂*Qgen)
end
t1 = collect(0:1:3400)
T∞1,I1 = 298.15,5
t2 = collect(0:1:6000)
T∞2,I2 = 273.15,2.5
actualODE1!(du,u,p,t) = actualODE!(du,u,p,t,T∞1,I1)
actualODE2!(du,u,p,t) = actualODE!(du,u,p,t,T∞2,I2)
prob1 = ODEProblem(actualODE1!,[1.0,T∞1],(t1[1],t1[end]))
prob2 = ODEProblem(actualODE2!,[1.0,T∞2],(t2[1],t2[end]))
sol1 = Array(solve(prob1,Tsit5(),saveat=t1))
sol2 = Array(solve(prob2,Tsit5(),saveat=t2))
# Plotting the results
P = plot(layout = (2,2),size = (600,400))
plot!(P[1],t1,sol1[2,:],label="Ambient Temp 25C",xlabel="Time (s)",ylabel="Temperature (K)")
plot!(P[2],t1,sol1[1,:],label="SOC",xlabel="Time (s)",ylabel="SOC")
plot!(P[3],t2,sol2[2,:],label="Ambient Temp 0C",xlabel="Time (s)",ylabel="Temperature (K)")
plot!(P[4],t2,sol2[1,:],label="SOC",xlabel="Time (s)",ylabel="SOC")
display(P)
# The current vector
I1_t = fill(I1,length(t1))
I2_t = fill(I2,length(t2))
# The heat generation rate vector
Qgen1 = (I1_t.^2).*0.03
Qgen2 = (I2_t.^2).*0.03
# Trying to perform SINDy so that we obtain a representation of Qgen as a function of I,u[1],u[2]
# Creating input vector
xin1 = zeros(3,length(t1))
for i in eachindex(t1)
It1 = I1_t[i]
xin1[:,i] = [It1,sol1[1,i],sol1[2,i]]
end
xin2 = zeros(3,length(t2))
for i in eachindex(t2)
It2 = I2_t[i]
xin2[:,i] = [It2,sol2[1,i],sol2[2,i]]
end
G1 = reshape(Qgen1,1,length(t1))
G2 = reshape(Qgen2,1,length(t2))
X̂ = hcat(xin1,xin2)
Ŷ = hcat(G1,G2)
N = size(X̂,1)
@variables u[1:N]
b = polynomial_basis(u,2)
basis = Basis(b,u)
nn_problem = DirectDataDrivenProblem(X̂,Ŷ)
λ = 1e-1
opt = ADMM(λ)
options = DataDrivenCommonOptions()
nn_res = solve(nn_problem,basis,opt,options=options)
The following error is shown
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:315
zero(::Type{Symbolics.TermCombination})
@ Symbolics C:\Users\Kalath_A\.julia\packages\Symbolics\xD5Pj\src\linearity.jl:51
...
Stacktrace:
[1] zero(::Type{Any})
@ Base .\missing.jl:106
[2] reduce_empty(::typeof(+), ::Type{Any})
@ Base .\reduce.jl:335
[3] reduce_empty(::typeof(Base.add_sum), ::Type{Any})
@ Base .\reduce.jl:342
[4] mapreduce_empty(::typeof(abs2), op::Function, T::Type)
@ Base .\reduce.jl:363
[5] reduce_empty(op::Base.MappingRF{typeof(abs2), typeof(Base.add_sum)}, ::Type{Any})
@ Base .\reduce.jl:350
[6] reduce_empty_iter
@ .\reduce.jl:373 [inlined]
[7] mapreduce_empty_iter(f::Function, op::Function, itr::Matrix{Any}, ItrEltype::Base.HasEltype)
@ Base .\reduce.jl:369
[8] _mapreduce(f::typeof(abs2), op::typeof(Base.add_sum), ::IndexLinear, A::Matrix{Any})
@ Base .\reduce.jl:421
[9] _mapreduce_dim(f::Function, op::Function, ::Base._InitialValue, A::Matrix{Any}, ::Colon)
@ Base .\reducedim.jl:334
[10] mapreduce
@ .\reducedim.jl:326 [inlined]
[11] _sum
@ .\reducedim.jl:984 [inlined]
[12] sum(f::Function, a::Matrix{Any})
@ Base .\reducedim.jl:980
[13] DataDrivenSolution(b::Basis{…}, p::DataDrivenProblem{…}, alg::ADMM{…}, result::Vector{…}, internal_problem::DataDrivenDiffEq.InternalDataDrivenProblem{…}, retcode::DDReturnCode)
@ DataDrivenDiffEq C:\Users\Kalath_A\.julia\packages\DataDrivenDiffEq\bgE8Q\src\solution.jl:38
[14] solve!(ps::DataDrivenDiffEq.InternalDataDrivenProblem{…})
@ DataDrivenSparse C:\Users\Kalath_A\.julia\packages\DataDrivenSparse\5sJbZ\src\commonsolve.jl:21
[15] solve(::DataDrivenProblem{…}, ::Vararg{…}; kwargs::@Kwargs{…})
@ CommonSolve C:\Users\Kalath_A\.julia\packages\CommonSolve\JfpfI\src\CommonSolve.jl:23
[16] top-level scope
@ c:\Users\Kalath_A\OneDrive - University of Warwick\PhD\ML Notebooks\Neural ODE\Julia\T Mixed\With Qgen multiplied with I\squareQgen\2_20_20_tanh\PEM-UDE\MWE_UDE_PEM.jl:92
Some type information was truncated. Use `show(err)` to see complete types.
In the real problem , I have a trained neural network Qgen(I,u[1],u[2]) which I am trying to fit using SINDy algorithms but similar error is shown.
Can anybody help me? I am new to this field and any help would be much appreciated.