# "Int64" error message during Optimisation

**URL:** <https://discourse.julialang.org/t/int64-error-message-during-optimisation/123382>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [December 3, 2024, 4:02am UTC](https://discourse.julialang.org/t/int64-error-message-during-optimisation/123382 "2024-12-03T04:02:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordie](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jordie](https://discourse.julialang.org/u/Jordie)\
**Post date:** [December 3, 2024, 4:02am UTC](https://discourse.julialang.org/t/int64-error-message-during-optimisation/123382/1 "2024-12-03T04:02:11Z")

</div>

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:

```julia
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:

```julia
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!

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [December 3, 2024, 6:58am UTC](https://discourse.julialang.org/t/int64-error-message-during-optimisation/123382/2 "2024-12-03T06:58:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 3, 2024, 1:23pm UTC](https://discourse.julialang.org/t/int64-error-message-during-optimisation/123382/3 "2024-12-03T13:23:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [December 3, 2024, 1:24pm UTC](https://discourse.julialang.org/t/int64-error-message-during-optimisation/123382/4 "2024-12-03T13:24:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 3, 2024, 1:30pm UTC](https://discourse.julialang.org/t/int64-error-message-during-optimisation/123382/5 "2024-12-03T13:30:06Z")

</div>

When I run your code I get:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Jordie](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jordie](https://discourse.julialang.org/u/Jordie)\
**Post date:** [December 4, 2024, 1:19am UTC](https://discourse.julialang.org/t/int64-error-message-during-optimisation/123382/6 "2024-12-04T01:19:30Z")

</div>

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

```julia
# 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?
