3

我想使用 Optim 和微分方程来估计 ODE 的复杂参数。我通过更改文档中示例中的参数构建了一个示例案例: https ://diffeq.sciml.ai/latest/analysis/parameter_estimation/#Optimization-Based-ODE-Inference-Examples-1

我到了可以建立损失目标的地步,然后返回复杂的值。

using DifferentialEquations
using Optim
using DiffEqParamEstim
using RecursiveArrayTools # for VectorOfArray

# ODE function
function f(du,u,p,t)
  du[1] = p[1]*u[1] - u[1]*u[2]
  du[2] = dy = -3*u[2] + u[1]*u[2]
end

# A set of comlex Initial conditions and parameters
u0 = [1.0 + 1im ;1.0 + 1im]
tspan = (0.0,10.0)
p = [1.5 + 1.5im]
prob = ODEProblem(f,u0,tspan,p)

sol = solve(prob,Tsit5())
t = collect(range(0,stop=10,length=200))
randomized = VectorOfArray([(sol(t[i]) + .01randn(2)) for i in 1:length(t)])
data = convert(Array,randomized)

cost_function = build_loss_objective(prob,Tsit5(),L2Loss(t,data),
                                     maxiters=10000,verbose=false)
cost_function(1.0im+0.1)

result = optimize(cost_function, 0.0+ 0im, 10.0+ 10im)

然后我尝试使用 optimize() 来优化我的成本函数,它返回以下错误:

LoadError: MethodError: no method matching optimize(::DiffEqObjective{DiffEqParamEstim.var"#43#48"{Nothing,Bool,Int64,typeof(DiffEqParamEstim.STANDARD_PROB_GENERATOR),Base.Iterators.Pairs{Symbol,Integer,Tuple{Symbol,Symbol},Name
dTuple{(:maxiters, :verbose),Tuple{Int64,Bool}}},ODEProblem{Array{Complex{Float64},1},Tuple{Float64,Float64},true,Array{Complex{Float64},1},ODEFunction{true,typeof(f),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},Tsit5,L2Loss{Array{Float64,1},Array{Complex{Float64},2},Nothing,Nothing,Nothing},Nothing},DiffEqParamEstim.var"#47#53"{DiffEqParamEstim.var"#43#48"{Nothing,Bool,Int64,typeof(DiffEqParamEstim.STANDARD_PROB_GENERATOR),Base.Iterators.Pairs{Symbol,Integer,Tuple{Symbol,Symbol},NamedTuple{(:maxiters, :verbose),Tuple{Int64,Bool}}},ODEProblem{Array{Complex{Float64},1},Tuple{Float64,Float64},true,Array{Complex{Float64},1},ODEFunction{true,typeof(f),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},Tsit5,L2Loss{Array{Float64,1},Array{Complex{Float64},2},Nothing,Nothing,Nothing},Nothing}}}, ::Complex{Float64}, ::Complex{Float64})
Closest candidates are:
  optimize(::Any, ::Number, ::Number, ::AbstractArray{T,N} where N; kwargs...) where T at /home/.julia/packages/Optim/L5T76/src/multivariate/solvers/constrained/fminbox.jl:156
  optimize(::Any, ::Number, ::Number, ::AbstractArray{T,N} where N, ::Optim.AbstractConstrainedOptimizer) where T at /home/.julia/packages/Optim/L5T76/src/multivariate/solvers/constrained/fminbox.jl:160
  optimize(::Any, ::Number, ::Number, ::AbstractArray{T,N} where N, ::Optim.AbstractConstrainedOptimizer, ::Optim.Options; kwargs...) where T at /home/.julia/packages/Optim/L5T76/src/multivariate/solvers/constrained/fminbox.jl:160
  ...
Stacktrace:
 [1] top-level scope at /home/Documents/random stuff/minimal_example.jl:27
in expression starting at /home/Documents/random stuff/minimal_example.jl:27

是否可以在复值函数上使用此方法?如果是的话,这样做的正确方法是什么?

编辑:

using DifferentialEquations
using Optim
using DiffEqParamEstim
using RecursiveArrayTools # for VectorOfArray

# ODE function
function f(du::AbstractArray{T}, u::AbstractArray{T},p::AbstractArray{T},t) where T<:Complex
  du[1] = p[1]*u[1] - u[1]*u[2]
  du[2] = dy = -3*u[2] + u[1]*u[2]
end

# A set of complex Initial conditions and parameters
u0 = [1.0 + 1im ;1.0 + 1im]
tspan = (0.0,10.0)
p = [1.5 + 1.5im]
prob = ODEProblem(f,u0,tspan,p)

sol = solve(prob,Tsit5())
sol.u
t = collect(range(0,stop=10,length=200))
randomized = VectorOfArray([(sol(t[i]) + .01randn(2)) for i in 1:length(t)])
data = convert(Array,randomized)

cost_function = build_loss_objective(prob,Tsit5(),L2Loss(t,data[1,:]),
                                     maxiters=10000,verbose=false)

result = optimize(cost_function, u0,BFGS())

InexactError: Float64(-567409.1782943244 - 312256.811660436im)
in top-level scope at minimal_example.jl:27
in optimize at Optim/L5T76/src/multivariate/optimize/interface.jl:115
in optimize at Optim/L5T76/src/multivariate/optimize/interface.jl:115
in #optimize#93 at Optim/L5T76/src/multivariate/optimize/interface.jl:116 
in optimize at Optim/L5T76/src/multivariate/optimize/optimize.jl:33 
in initial_state at Optim/L5T76/src/multivariate/solvers/first_order/bfgs.jl:66
in value_gradient!! at NLSolversBase/mGaJg/src/interface.jl:82
in setproperty! at base/Base.jl:21
in convert at base/number.jl:7
in Real at base/complex.jl:37 
4

0 回答 0