using DifferentialEquations, Optimization, OptimizationPolyalgorithms, Plots function lotka_volterra!(du, u, p, t) x, y = u α, β, δ, γ = p du[1] = dx = α*x - β*x*y du[2] = dy = -δ*y + γ*x*y end # Initial condition u0 = [1.0, 1.0] # Simulation interval and intermediary points tspan = (0.0, 10.0) tsteps = 0.0:0.1:10.0 # LV equation parameter. p = [α, β, δ, γ] p = [1.5, 1.0, 3.0, 1.0] # Setup the ODE problem, then solve prob = ODEProblem(lotka_volterra!, u0, tspan, p) sol = solve(prob, Tsit5(), saveat=tsteps) dataset = Array(sol) # Plot the solution using Plots plot(sol) savefig("LV_ode.png") function loss(p) sol = solve(prob, Tsit5(), p=p, saveat = tsteps) loss = sum(abs2, Array(sol) - dataset) return loss, sol end callback = function (p, l, pred) display(l) plt = plot(pred, ylim = (0, 6)) scatter!(plt,sol) display(plt) # Tell Optimization.solve to not halt the optimization. If return true, then # optimization stops. return false end adtype = Optimization.AutoZygote() optf = Optimization.OptimizationFunction((x,p)->loss(x), adtype) pinit = [1.3, 1.2, 2.8, 0.8] optprob = Optimization.OptimizationProblem(optf, pinit) result_ode = Optimization.solve(optprob, PolyOpt(), callback = callback, maxiters = 100) result_ode.u