SciML / DelayDiffEq.jl

Delay differential equation (DDE) solvers in Julia for the SciML scientific machine learning ecosystem. Covers neutral and retarded delay differential equations, and differential-algebraic equations.
Other
59 stars 26 forks source link

ForwardDiff on DelayDiffEq #127

Closed ChrisRackauckas closed 5 years ago

ChrisRackauckas commented 5 years ago

Found by DiffEqFlux tests:

using Flux, DiffEqFlux, DelayDiffEq, Test

## Setup DDE to optimize
function delay_lotka_volterra(du,u,h,p,t)
  x, y = u
  α, β, δ, γ = p
  du[1] = dx = (α - β*y)*h(p,t-0.1)[1]
  du[2] = dy = (δ*x - γ)*y
end
h(p,t) = ones(eltype(p),2)
prob = DDEProblem(delay_lotka_volterra,[1.0,1.0],h,(0.0,10.0),constant_lags=[0.1])
p = param([2.2, 1.0, 2.0, 0.4])
function predict_fd_dde()
  diffeq_fd(p,sol->sol[1,:],101,prob,MethodOfSteps(Tsit5()),saveat=0.0:0.1:10.0)
end
loss_fd_dde() = sum(abs2,x-1 for x in predict_fd_dde())
@test_broken loss_fd_dde()
@test_broken Flux.back!(loss_fd_dde())
ChrisRackauckas commented 5 years ago
MethodError: Bool(::ForwardDiff.Dual{ForwardDiff.Tag{getfield(DiffEqFlux, Symbol("##11#16")){Array{Float64,1},Base.Iterators.Pairs{Symbol,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Tuple{Symbol},NamedTuple{(:saveat,),Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}},getfield(Main, Symbol("##25#26")),DDEProblem{Array{Float64,1},Tuple{Float64,Float64},Array{Float64,1},Tuple{},true,Nothing,DDEFunction{true,typeof(delay_lotka_volterra),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(h),Nothing},Tuple{MethodOfSteps{Tsit5,Nothing,Nothing,Nothing,false}}},Float64},Float64,4}) is ambiguous. Candidates:
  (T::Type{#s40} where #s40<:Real)(x::ForwardDiff.Dual) in Tracker at C:\Users\accou\.julia\dev\Tracker\src\lib\real.jl:108
  Bool(x::Real) in Base at float.jl:73
Possible fix, define
  Bool(::ForwardDiff.Dual)
convert(::Type{Bool}, ::ForwardDiff.Dual{ForwardDiff.Tag{getfield(DiffEqFlux, Symbol("##11#16")){Array{Float64,1},Base.Iterators.Pairs{Symbol,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Tuple{Symbol},NamedTuple{(:saveat,),Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}},getfield(Main, Symbol("##25#26")),DDEProblem{Array{Float64,1},Tuple{Float64,Float64},Array{Float64,1},Tuple{},true,Nothing,DDEFunction{true,typeof(delay_lotka_volterra),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(h),Nothing},Tuple{MethodOfSteps{Tsit5,Nothing,Nothing,Nothing,false}}},Float64},Float64,4}) at number.jl:7
DelayDiffEq.DDEIntegrator{Tsit5,true,Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(DiffEqFlux, Symbol("##11#16")){Array{Float64,1},Base.Iterators.Pairs{Symbol,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Tuple{Symbol},NamedTuple{(:saveat,),Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}},getfield(Main, Symbol("##25#26")),DDEProblem{Array{Float64,1},Tuple{Float64,Float64},Array{Float64,1},Tuple{},true,Nothing,DDEFunction{true,typeof(delay_lotka_volterra),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(h),Nothing},Tuple{MethodOfSteps{Tsit5,Nothing,Nothing,Nothing,false}}},Float64},Float64,4},1},Float64,Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(DiffEqFlux, Symbol("##11#16")){Array{Float64,1},Base.Iterators.Pairs{Symbol,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Tuple{Symbol},NamedTuple{(:saveat,),Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}},getfield(Main, Symbol("##25#26")),DDEProblem{Array{Float64,1},Tuple{Float64,Float64},Array{Float64,1},Tuple{},true,Nothing,DDEFunction{true,typeof(delay_lotka_volterra),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(h),Nothing},Tuple{MethodOfSteps{Tsit5,Nothing,Nothing,Nothing,false}}},Float64},Float64,4},1},ForwardDiff.Dual{ForwardDiff.Tag{getfield(DiffEqFlux, Symbol("##11#16")){Array{Float64,1},Base.Iterators.Pairs{Symbol,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Tuple{Symbol},NamedTuple{(:saveat,),Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}},getfield(Main, Symbol("##25#26")),DDEProblem{Array{Float64,1},Tuple{Float64,Float64},Array{Float64,1},Tuple{},true,Nothing,DDEFunction{true,typeof(delay_lotka_volterra),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(h),Nothing},Tuple{MethodOfSteps{Tsit5,Nothing,Nothing,Nothing,false}}},Float64},Float64,4},ForwardDiff.Dual{ForwardDiff.Tag{getfield(DiffEqFlux, Symbol("##11#16")){Array{Float64,1},Base.Iterators.Pairs{Symbol,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Tuple{Symbol},NamedTuple{(:saveat,),Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}},getfield(Main, Symbol("##25#26")),DDEProblem{Array{Float64,1},Tuple{Float64,Float64},Array{Float64,1},Tuple{},true,Nothing,DDEFunction{true,typeof(delay_lotka_volterra),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(h),Nothing},Tuple{MethodOfSteps{Tsit5,Nothing,Nothing,Nothing,false}}},Float64},Float64,4},ForwardDiff.Dual{ForwardDiff.Tag{getfield(DiffEqFlux, Symbol("##11#16")){Array{Float64,1},Base.Iterators.Pairs{Symbol,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Tuple{Symbol},NamedTuple{(:saveat,),Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}},getfield(Main, Symbol("##25#26")),DDEProblem{Array{Float64,1},Tuple{Float64,Float64},Array{Float64,1},Tuple{},true,Nothing,DDEF...
devmotion commented 5 years ago

The order of the arguments in the constructor is messed up somehow :open_mouth: Fixing that resolves the issue.