"Int64" error message during Optimisation

Hi, I am trying to find multiple parameters for the optimized system which is governed by a system of equations.

In my code, I need to be able to use 4 or 5 equations and determine the parameters for the optimized system. I have simplified the code to a system of 3 equations for simplicity, (but it is 5 equations), as below:

using Optimization, ForwardDiff, OptimizationOptimJL, OptimizationBBO

function f1(du, u, p, t)
    x, y = u
    α, β, δ, γ = p
    du[1] = dx = α * x - β * x * y
    du[2] = dy = -δ * y + γ *x * y
    du[3] = dz = α*x
end

u0 = [1.0; 1.0; 1.0]
tspan = (0.0, 10.0)
p = [1.5, 1.0, 3.0, 1.0]
prob = ODEProblem(f1, u0, tspan, p)

t = collect(range(0, stop = 10, length = 200))  # Creates the time vector
data = Array(solve(prob, Tsit5(), saveat = t, abstol = 1e-12, reltol = 1e-12))

bound = Tuple{Float64, Float64}[(0, 10), (0, 10), (0, 10), (0, 10),
                                (0, 10), (0, 10), (0, 10), (0, 10),
                                (0, 10), (0, 10), (0, 10), (0, 10),
                                (0, 10), (0, 10), (0, 10), (0, 10), (0, 10), (0, 10)]


obj = multiple_shooting_objective(prob, Tsit5(), L2Loss(t, data),Optimization.AutoForwardDiff(); discontinuity_weight = 1.0, abstol = 1e-12, reltol = 1e-12)

optprob = Optimization.OptimizationProblem(obj, zeros(18), lb = first.(bound), ub = last.(bound))
optsol = solve(optprob, BBO_adaptive_de_rand_1_bin_radiuslimited(), maxiters = 10000)

Optimised_parameters = optsol.u[(end - 1):end]
println("Optimised parameters: ")
println("p₁: ", Optimised_parameters[1])
println("p₂: ", Optimised_parameters[2])
println(" ") 

However, I am getting the error message:

ERROR: InexactError: Int64(4.666666666666667)
Stacktrace:
  [1] Int64
    @ .\float.jl:994 [inlined]
  [2] (::DiffEqParamEstim.var"#43#48"{…})(p::Vector{…}, ::SciMLBase.NullParameters)
    @ DiffEqParamEstim C:\Users\Finn Mezzetti\.julia\packages\DiffEqParamEstim\6z5Ou\src\multiple_shooting_objective.jl:33
  [3] (::OptimizationFunction{…})(::Vector{…}, ::Vararg{…})
    @ SciMLBase C:\Users\Finn Mezzetti\.julia\packages\SciMLBase\VAClc\src\scimlfunctions.jl:3944
  [4] (::OptimizationBBO.var"#20#22"{OptimizationCache{…}})(θ::Vector{Float64})
    @ OptimizationBBO C:\Users\Finn Mezzetti\.julia\packages\OptimizationBBO\UYBCT\src\OptimizationBBO.jl:146
  [5] fitness(x::Vector{…}, p::BlackBoxOptim.FunctionBasedProblem{…})
    @ BlackBoxOptim C:\Users\Finn Mezzetti\.julia\packages\BlackBoxOptim\lZtsr\src\problem.jl:61
  [6] setup_problem(func::Function, parameters::BlackBoxOptim.ParamsDictChain)
    @ BlackBoxOptim C:\Users\Finn Mezzetti\.julia\packages\BlackBoxOptim\lZtsr\src\bboptimize.jl:37
  [7] bbsetup(functionOrProblem::Function, parameters::Dict{…}; kwargs::@Kwargs{…})
    @ BlackBoxOptim C:\Users\Finn Mezzetti\.julia\packages\BlackBoxOptim\lZtsr\src\bboptimize.jl:111
  [8] bbsetup
    @ C:\Users\Finn Mezzetti\.julia\packages\BlackBoxOptim\lZtsr\src\bboptimize.jl:109 [inlined]
  [9] __solve(cache::OptimizationCache{…})
    @ OptimizationBBO C:\Users\Finn Mezzetti\.julia\packages\OptimizationBBO\UYBCT\src\OptimizationBBO.jl:158
 [10] solve!(cache::OptimizationCache{…})
    @ SciMLBase C:\Users\Finn Mezzetti\.julia\packages\SciMLBase\VAClc\src\solve.jl:186
 [11] solve(::OptimizationProblem{…}, ::BBO_adaptive_de_rand_1_bin_radiuslimited; kwargs::@Kwargs{…})
    @ SciMLBase C:\Users\Finn Mezzetti\.julia\packages\SciMLBase\VAClc\src\solve.jl:94

Which indicates that somewhere it is receiving an integer but expecting a Float. I am not sure how to fix this so I can extend the system to include more equations.
Any help would be much appreciated!

At first glance it seems due to the fact that your time scale and/or your variable bounds have integer type. Can you try using floats for those?

Both t and bound are float values, if those are what you refer to.

My bad, I saw only the integer definitions but I missed the conversions. Ignore my previous message.

When I run your code I get:

julia> data = Array(solve(prob, Tsit5(), saveat = t, abstol = 1e-12, reltol = 1e-12))
ERROR: UndefVarError: `Tsit5` not defined in `Main`
Suggestion: check for spelling errors or missing imports.

Please make sure you provide minimal working examples that others can run, it appears you are missing package imports here.

Good idea. Here is a working example with all the necessary imports:

# Install and import all necessary packages:
using Pkg
function install_package(pkg_name::String)
    if !(pkg_name in keys(Pkg.installed()))
        Pkg.add(pkg_name)
    end
end

packages = [
    "DifferentialEquations",
    "RecursiveArrayTools",
    "Plots",
    "DiffEqParamEstim",
    "Optimization",
    "ForwardDiff",
    "OptimizationOptimJL",
    "OptimizationBBO"
]

for pkg in packages
    install_package(pkg)
end

using DifferentialEquations
using RecursiveArrayTools
using Plots
using DiffEqParamEstim
using Optimization
using ForwardDiff
using OptimizationOptimJL
using OptimizationBBO

function f1(du, u, p, t)
    x, y = u
    α, β, δ, γ = p
    du[1] = dx = α * x - β * x * y
    du[2] = dy = -δ * y + γ *x * y
end

u0 = [1.0; 1.0]
tspan = (0.0, 10.0)
p = [1.5, 1.0, 3.0, 1.0]
prob = ODEProblem(f1, u0, tspan, p)

t = collect(range(0, stop = 10, length = 200))  # Creates the time vector
data = Array(solve(prob, Tsit5(), saveat = t, abstol = 1e-12, reltol = 1e-12))

bound = Tuple{Float64, Float64}[(0, 10), (0, 10), (0, 10), (0, 10),
                                (0, 10), (0, 10), (0, 10), (0, 10),
                                (0, 10), (0, 10), (0, 10), (0, 10),
                                (0, 10), (0, 10), (0, 10), (0, 10), (0, 10), (0, 10)]


obj = multiple_shooting_objective(prob, Tsit5(), L2Loss(t, data),Optimization.AutoForwardDiff(); discontinuity_weight = 1.0, abstol = 1e-12, reltol = 1e-12)

optprob = Optimization.OptimizationProblem(obj, zeros(18), lb = first.(bound), ub = last.(bound))
optsol = solve(optprob, BBO_adaptive_de_rand_1_bin_radiuslimited(), maxiters = 10000)

Optimised_parameters = optsol.u[(end - 1):end]
println("Optimised parameters: ")
println("p₁: ", Optimised_parameters[1])
println("p₂: ", Optimised_parameters[2])
println(" ")

This example has one less “du” term. I am trying to have a system that can have up to 5 du equations. I added the third term and received the error from up above. Any ideas on how to fix this?