From a2a48364b434fa5daf447a46fb5a045e2d1cf84b Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Wed, 14 Feb 2024 12:10:07 -0600 Subject: [PATCH] Run JuliaFormatter.format() Using JuliaFormatter v1.0.47 --- docs/make.jl | 14 +++---- docs/pages.jl | 10 ++--- docs/src/getting_started.md | 34 +++++++-------- docs/src/methods/collocation_loss.md | 4 +- .../src/methods/optimization_based_methods.md | 28 ++++++------- docs/src/tutorials/ensemble.md | 12 +++--- docs/src/tutorials/generalized_likelihood.md | 10 ++--- docs/src/tutorials/global_optimization.md | 8 ++-- docs/src/tutorials/stochastic_evaluations.md | 10 ++--- src/DiffEqParamEstim.jl | 10 ++--- src/build_loss_objective.jl | 12 +++--- src/cost_functions.jl | 20 +++++---- src/multiple_shooting_objective.jl | 16 +++---- src/two_stage_method.jl | 16 +++---- test/dae_tests.jl | 8 ++-- test/dde_tests.jl | 8 ++-- test/likelihood.jl | 34 +++++++-------- test/lorenz_test.jl | 18 ++++---- test/lorenz_true_test.jl | 28 ++++++------- test/multiple_shooting_objective_test.jl | 20 ++++----- test/out_of_place_odes.jl | 4 +- test/runtests.jl | 28 +++++++++---- test/steady_state_tests.jl | 6 +-- test/test_on_monte.jl | 6 +-- test/tests_on_odes/blackboxoptim_test.jl | 6 +-- test/tests_on_odes/genetic_algorithm_test.jl | 42 +++++++++---------- test/tests_on_odes/l2_colloc_grad_test.jl | 28 ++++++------- test/tests_on_odes/l2loss_test.jl | 16 +++---- test/tests_on_odes/nlopt_test.jl | 4 +- test/tests_on_odes/optim_test.jl | 6 +-- test/tests_on_odes/regularization_test.jl | 30 ++++++------- test/tests_on_odes/weighted_loss_test.jl | 10 ++--- 32 files changed, 263 insertions(+), 243 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index 2191f5f8..2a59306c 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -8,12 +8,12 @@ ENV["GKSwstype"] = "100" include("pages.jl") makedocs(sitename = "DiffEqParamEstim.jl", - authors = "Chris Rackauckas et al.", - modules = [DiffEqParamEstim], - clean = true, doctest = false, linkcheck = true, - format = Documenter.HTML(assets = ["assets/favicon.ico"], - canonical = "https://docs.sciml.ai/DiffEqParamEstim/stable/"), - pages = pages) + authors = "Chris Rackauckas et al.", + modules = [DiffEqParamEstim], + clean = true, doctest = false, linkcheck = true, + format = Documenter.HTML(assets = ["assets/favicon.ico"], + canonical = "https://docs.sciml.ai/DiffEqParamEstim/stable/"), + pages = pages) deploydocs(repo = "github.com/SciML/DiffEqParamEstim.jl"; - push_preview = true) + push_preview = true) diff --git a/docs/pages.jl b/docs/pages.jl index 568587f8..b17b1efd 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -1,10 +1,10 @@ pages = ["index.md", "getting_started.md", "Tutorials" => Any["tutorials/global_optimization.md", - "tutorials/generalized_likelihood.md", - "tutorials/stochastic_evaluations.md", - "tutorials/ensemble.md"], + "tutorials/generalized_likelihood.md", + "tutorials/stochastic_evaluations.md", + "tutorials/ensemble.md"], "Methods" => Any["methods/recommended_methods.md", - "methods/optimization_based_methods.md", - "methods/collocation_loss.md"], + "methods/optimization_based_methods.md", + "methods/collocation_loss.md"] ] diff --git a/docs/src/getting_started.md b/docs/src/getting_started.md index 51923b43..8882b5fc 100644 --- a/docs/src/getting_started.md +++ b/docs/src/getting_started.md @@ -77,8 +77,8 @@ function: ```@example ode cost_function = build_loss_objective(prob, Tsit5(), L2Loss(t, data), - Optimization.AutoForwardDiff(), - maxiters = 10000, verbose = false) + Optimization.AutoForwardDiff(), + maxiters = 10000, verbose = false) ``` This objective function internally is calling the ODE solver to get solutions @@ -103,8 +103,8 @@ of parameter values: ```@example ode vals = 0.0:0.1:10.0 plot(vals, [cost_function(i) for i in vals], yscale = :log10, - xaxis = "Parameter", yaxis = "Cost", title = "1-Parameter Cost Function", - lw = 3) + xaxis = "Parameter", yaxis = "Cost", title = "1-Parameter Cost Function", + lw = 3) ``` Here we see that there is a very well-defined minimum in our cost function at @@ -168,8 +168,8 @@ We can build an objective function and solve the multiple parameter version just ```@example ode cost_function = build_loss_objective(prob, Tsit5(), L2Loss(t, data), - Optimization.AutoForwardDiff(), - maxiters = 10000, verbose = false) + Optimization.AutoForwardDiff(), + maxiters = 10000, verbose = false) optprob = Optimization.OptimizationProblem(cost_function, [1.3, 0.8, 2.8, 1.2]) result_bfgs = solve(optprob, BFGS()) ``` @@ -184,10 +184,10 @@ differencing loss to the total loss. ```@example ode cost_function = build_loss_objective(prob, Tsit5(), - L2Loss(t, data, differ_weight = 0.3, - data_weight = 0.7), - Optimization.AutoForwardDiff(), - maxiters = 10000, verbose = false) + L2Loss(t, data, differ_weight = 0.3, + data_weight = 0.7), + Optimization.AutoForwardDiff(), + maxiters = 10000, verbose = false) optprob = Optimization.OptimizationProblem(cost_function, [1.3, 0.8, 2.8, 1.2]) result_bfgs = solve(optprob, BFGS()) ``` @@ -206,14 +206,14 @@ ms_prob = ODEProblem(ms_f1, ms_u0, tspan, ms_p) t = collect(range(0, stop = 10, length = 200)) data = Array(solve(ms_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)] + (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)] ms_obj = multiple_shooting_objective(ms_prob, Tsit5(), L2Loss(t, data), - Optimization.AutoForwardDiff(); - discontinuity_weight = 1.0, abstol = 1e-12, - reltol = 1e-12) + Optimization.AutoForwardDiff(); + discontinuity_weight = 1.0, abstol = 1e-12, + reltol = 1e-12) ``` This creates the objective function that can be passed to an optimizer, from which we can then get the parameter values @@ -222,7 +222,7 @@ a global optimization method to improve robustness even more: ```@example ode optprob = Optimization.OptimizationProblem(ms_obj, zeros(18), lb = first.(bound), - ub = last.(bound)) + ub = last.(bound)) optsol_ms = solve(optprob, BBO_adaptive_de_rand_1_bin_radiuslimited(), maxiters = 10_000) ``` diff --git a/docs/src/methods/collocation_loss.md b/docs/src/methods/collocation_loss.md index 8d2fe6d6..6c9a169a 100644 --- a/docs/src/methods/collocation_loss.md +++ b/docs/src/methods/collocation_loss.md @@ -11,7 +11,7 @@ but is much faster, and is a good method to try first to get in the general ```julia function two_stage_objective(prob::DEProblem, tpoints, data, adtype = SciMLBase.NoAD(), ; - kernel = :Epanechnikov, - loss_func = L2DistLoss) + kernel = :Epanechnikov, + loss_func = L2DistLoss) end ``` diff --git a/docs/src/methods/optimization_based_methods.md b/docs/src/methods/optimization_based_methods.md index b46c53cd..1196ec12 100644 --- a/docs/src/methods/optimization_based_methods.md +++ b/docs/src/methods/optimization_based_methods.md @@ -9,11 +9,11 @@ and MathProgBase-associated solvers like NLopt. ```julia function build_loss_objective(prob::DEProblem, alg, loss, - adtype = SciMLBase.NoAD(), - regularization = nothing; - priors = nothing, - prob_generator = STANDARD_PROB_GENERATOR, - kwargs...) + adtype = SciMLBase.NoAD(), + regularization = nothing; + priors = nothing, + prob_generator = STANDARD_PROB_GENERATOR, + kwargs...) end ``` @@ -38,12 +38,12 @@ proceeds as follows: ```julia function multiple_shooting_objective(prob::DiffEqBase.DEProblem, alg, loss, - adtype = SciMLBase.NoAD(), - regularization = nothing; - priors = nothing, - discontinuity_weight = 1.0, - prob_generator = STANDARD_PROB_GENERATOR, - kwargs...) + adtype = SciMLBase.NoAD(), + regularization = nothing; + priors = nothing, + discontinuity_weight = 1.0, + prob_generator = STANDARD_PROB_GENERATOR, + kwargs...) end ``` @@ -67,7 +67,7 @@ cost functions: ```julia L2Loss(t, data; differ_weight = nothing, data_weight = nothing, - colloc_grad = nothing, dudt = nothing) + colloc_grad = nothing, dudt = nothing) ``` where `t` is the set of timepoints which the data are found at, and @@ -213,6 +213,6 @@ the parameters or a multivariate distribution. ```julia ms_obj = multiple_shooting_objective(ms_prob, Tsit5(), L2Loss(t, data); priors = priors, - discontinuity_weight = 1.0, abstol = 1e-12, - reltol = 1e-12) + discontinuity_weight = 1.0, abstol = 1e-12, + reltol = 1e-12) ``` diff --git a/docs/src/tutorials/ensemble.md b/docs/src/tutorials/ensemble.md index c7fe0059..004812ad 100644 --- a/docs/src/tutorials/ensemble.md +++ b/docs/src/tutorials/ensemble.md @@ -44,7 +44,7 @@ initial_conditions = [ [0.5, 0.5], [2.0, 1.0], [1.0, 2.0], - [2.0, 2.0], + [2.0, 2.0] ] function prob_func(prob, i, repeat) ODEProblem(prob.f, initial_conditions[i], prob.tspan, prob.p) @@ -111,8 +111,8 @@ Put this into build_loss_objective. ```@example ensemble obj = build_loss_objective(enprob, Tsit5(), loss, Optimization.AutoForwardDiff(), - trajectories = N, - saveat = data_times) + trajectories = N, + saveat = data_times) ``` Notice that we added the kwargs for `solve` of the `EnsembleProblem` into this. They get passed to the internal `solve` @@ -141,9 +141,9 @@ to decrease the tolerance of the ODE solvers via ```@example ensemble obj = build_loss_objective(enprob, Tsit5(), loss, Optimization.AutoForwardDiff(), - trajectories = N, - abstol = 1e-8, reltol = 1e-8, - saveat = data_times) + trajectories = N, + abstol = 1e-8, reltol = 1e-8, + saveat = data_times) optprob = OptimizationProblem(obj, [1.3, 0.9], lb = lower, ub = upper) result = solve(optprob, BFGS()) #OptimizationOptimJL detects that it's a box constrained problem and use Fminbox wrapper over BFGS ``` diff --git a/docs/src/tutorials/generalized_likelihood.md b/docs/src/tutorials/generalized_likelihood.md index 4eade6b5..d3fb91dc 100644 --- a/docs/src/tutorials/generalized_likelihood.md +++ b/docs/src/tutorials/generalized_likelihood.md @@ -69,7 +69,7 @@ corresponding to that distribution fit: ```@example likelihood obj = build_loss_objective(prob1, Tsit5(), LogLikeLoss(t, distributions), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) ``` First, let's use the objective function to plot the likelihood landscape: @@ -79,8 +79,8 @@ using Plots; plotly(); prange = 0.5:0.1:5.0 heatmap(prange, prange, [obj([j, i]) for i in prange, j in prange], - yscale = :log10, xlabel = "Parameter 1", ylabel = "Parameter 2", - title = "Likelihood Landscape") + yscale = :log10, xlabel = "Parameter 1", ylabel = "Parameter 2", + title = "Likelihood Landscape") ``` ![2 Parameter Likelihood](../assets/2paramlike.png) @@ -92,8 +92,8 @@ one-dimensional slice: ```julia plot(prange, [obj([1.5, i]) for i in prange], lw = 3, - title = "Parameter 2 Likelihood (Parameter 1 = 1.5)", - xlabel = "Parameter 2", ylabel = "Objective Function Value") + title = "Parameter 2 Likelihood (Parameter 1 = 1.5)", + xlabel = "Parameter 2", ylabel = "Objective Function Value") ``` ![1 Parameter Likelihood](../assets/1paramlike.png) diff --git a/docs/src/tutorials/global_optimization.md b/docs/src/tutorials/global_optimization.md index 99c4dff4..140863c7 100644 --- a/docs/src/tutorials/global_optimization.md +++ b/docs/src/tutorials/global_optimization.md @@ -56,10 +56,10 @@ We can even use things like the Improved Stochastic Ranking Evolution Strategy ```@example global_optimization optprob = Optimization.OptimizationProblem(obj, [0.2], lb = [-1.0], ub = [5.0]) res = solve(optprob, - OptimizationMOI.MOI.OptimizerWithAttributes(NLopt.Optimizer, - "algorithm" => :GN_ISRES, - "xtol_rel" => 1e-3, - "maxeval" => 10000)) + OptimizationMOI.MOI.OptimizerWithAttributes(NLopt.Optimizer, + "algorithm" => :GN_ISRES, + "xtol_rel" => 1e-3, + "maxeval" => 10000)) ``` which is very robust to the initial condition. We can also directly use the NLopt interface as below. The fastest result comes from the diff --git a/docs/src/tutorials/stochastic_evaluations.md b/docs/src/tutorials/stochastic_evaluations.md index 8a36192e..d8f83007 100644 --- a/docs/src/tutorials/stochastic_evaluations.md +++ b/docs/src/tutorials/stochastic_evaluations.md @@ -52,8 +52,8 @@ We use Optim.jl for optimization below ```@example sde obj = build_loss_objective(monte_prob, SOSRI(), L2Loss(t, aggregate_data), - Optimization.AutoForwardDiff(), - maxiters = 10000, verbose = false, trajectories = 1000) + Optimization.AutoForwardDiff(), + maxiters = 10000, verbose = false, trajectories = 1000) optprob = Optimization.OptimizationProblem(obj, [1.0, 0.5]) result = solve(optprob, Optim.BFGS()) ``` @@ -68,9 +68,9 @@ Instead, when we use `L2Loss` with first differencing enabled, we get much more ```@example sde obj = build_loss_objective(monte_prob, SRIW1(), - L2Loss(t, aggregate_data, differ_weight = 1.0, - data_weight = 0.5), Optimization.AutoForwardDiff(), - verbose = false, trajectories = 1000, maxiters = 1000) + L2Loss(t, aggregate_data, differ_weight = 1.0, + data_weight = 0.5), Optimization.AutoForwardDiff(), + verbose = false, trajectories = 1000, maxiters = 1000) optprob = Optimization.OptimizationProblem(obj, [1.0, 0.5]) result = solve(optprob, Optim.BFGS()) result.original diff --git a/src/DiffEqParamEstim.jl b/src/DiffEqParamEstim.jl index 27f5c649..20d9f7ef 100644 --- a/src/DiffEqParamEstim.jl +++ b/src/DiffEqParamEstim.jl @@ -7,10 +7,10 @@ import PreallocationTools STANDARD_PROB_GENERATOR(prob, p) = remake(prob; u0 = eltype(p).(prob.u0), p = p) function STANDARD_PROB_GENERATOR(prob::EnsembleProblem, p) EnsembleProblem(remake(prob.prob; u0 = eltype(p).(prob.prob.u0), p = p), - output_func = prob.output_func, - prob_func = prob.prob_func, - reduction = prob.reduction, - u_init = prob.u_init) + output_func = prob.output_func, + prob_func = prob.prob_func, + reduction = prob.reduction, + u_init = prob.u_init) end STANDARD_MS_PROB_GENERATOR = function (prob, p, k) t0, tf = prob.tspan @@ -18,7 +18,7 @@ STANDARD_MS_PROB_GENERATOR = function (prob, p, k) K = Int((length(p) - P) / N) τ = range(t0, tf, length = K + 1) remake(prob; u0 = p[(1 + (k - 1) * N):(k * N)], p = p[(end - P + 1):end], - tspan = (τ[k], τ[k + 1])) + tspan = (τ[k], τ[k + 1])) end include("cost_functions.jl") diff --git a/src/build_loss_objective.jl b/src/build_loss_objective.jl index 4f3f26ba..77c63448 100644 --- a/src/build_loss_objective.jl +++ b/src/build_loss_objective.jl @@ -1,16 +1,16 @@ export build_loss_objective function build_loss_objective(prob::SciMLBase.AbstractSciMLProblem, alg, loss, - adtype = SciMLBase.NoAD(), - regularization = nothing, args...; - priors = nothing, - prob_generator = STANDARD_PROB_GENERATOR, - kwargs...) + adtype = SciMLBase.NoAD(), + regularization = nothing, args...; + priors = nothing, + prob_generator = STANDARD_PROB_GENERATOR, + kwargs...) cost_function = function (p, _ = nothing) tmp_prob = prob_generator(prob, p) if loss isa Union{L2Loss, LogLikeLoss} sol = solve(tmp_prob, alg, args...; saveat = loss.t, save_everystep = false, - dense = false, kwargs...) + dense = false, kwargs...) else sol = solve(tmp_prob, alg, args...; kwargs...) end diff --git a/src/cost_functions.jl b/src/cost_functions.jl index b76bbbed..61a440ba 100644 --- a/src/cost_functions.jl +++ b/src/cost_functions.jl @@ -89,10 +89,12 @@ function (f::L2Loss)(sol::SciMLBase.AbstractSciMLSolution) for j in 1:length(sol[i]) if diff_weight isa Real sumsq += diff_weight * - ((data[j, i] - data[j, i - 1] - sol[j, i] + sol[j, i - 1])^2) + ((data[j, i] - data[j, i - 1] - sol[j, i] + + sol[j, i - 1])^2) else sumsq += diff_weight[j, i] * - ((data[j, i] - data[j, i - 1] - sol[j, i] + sol[j, i - 1])^2) + ((data[j, i] - data[j, i - 1] - sol[j, i] + + sol[j, i - 1])^2) end end end @@ -112,10 +114,12 @@ function (f::L2Loss)(sol::SciMLBase.AbstractSciMLSolution) for j in 1:length(sol[i]) if diff_weight isa Real sumsq += diff_weight * - ((data[j, i] - data[j, i - 1] - sol[j, i] + sol[j, i - 1])^2) + ((data[j, i] - data[j, i - 1] - sol[j, i] + + sol[j, i - 1])^2) else sumsq += diff_weight[j, i] * - ((data[j, i] - data[j, i - 1] - sol[j, i] + sol[j, i - 1])^2) + ((data[j, i] - data[j, i - 1] - sol[j, i] + + sol[j, i - 1])^2) end end end @@ -135,11 +139,11 @@ end matrixize(x) = x isa Vector ? reshape(x, 1, length(x)) : x function L2Loss(t, data; differ_weight = nothing, data_weight = nothing, - colloc_grad = nothing, - dudt = nothing) + colloc_grad = nothing, + dudt = nothing) L2Loss(t, matrixize(data), matrixize(differ_weight), - matrixize(data_weight), matrixize(colloc_grad), - colloc_grad == nothing ? nothing : zeros(size(colloc_grad))) + matrixize(data_weight), matrixize(colloc_grad), + colloc_grad == nothing ? nothing : zeros(size(colloc_grad))) end function (f::L2Loss)(sol::DiffEqBase.AbstractEnsembleSolution) diff --git a/src/multiple_shooting_objective.jl b/src/multiple_shooting_objective.jl index 6f338633..19074bbf 100644 --- a/src/multiple_shooting_objective.jl +++ b/src/multiple_shooting_objective.jl @@ -22,11 +22,11 @@ struct Merged_Solution{T1, T2, T3} end function multiple_shooting_objective(prob::DiffEqBase.DEProblem, alg, loss, - adtype = SciMLBase.NoAD(), - regularization = nothing; priors = nothing, - discontinuity_weight = 1.0, - prob_generator = STANDARD_MS_PROB_GENERATOR, - kwargs...) + adtype = SciMLBase.NoAD(), + regularization = nothing; priors = nothing, + discontinuity_weight = 1.0, + prob_generator = STANDARD_MS_PROB_GENERATOR, + kwargs...) cost_function = function (p, _ = nothing) t0, tf = prob.tspan P, N = length(prob.p), length(prob.u0) @@ -39,8 +39,8 @@ function multiple_shooting_objective(prob::DiffEqBase.DEProblem, alg, loss, if loss isa Union{L2Loss, LogLikeLoss} time_save = loss.t[findall(t -> τ[k] <= t <= τ[k + 1], loss.t)] push!(sol, - solve(tmp_prob, alg; saveat = time_save, - save_everystep = false, dense = false, kwargs...)) + solve(tmp_prob, alg; saveat = time_save, + save_everystep = false, dense = false, kwargs...)) else push!(sol, solve(tmp_prob, alg; kwargs...)) end @@ -52,7 +52,7 @@ function multiple_shooting_objective(prob::DiffEqBase.DEProblem, alg, loss, t = [tc for k in 1:K for tc in (k == K ? sol[k].t : sol[k].t[1:(end - 1)])] sol_loss = Merged_Solution(u, t, sol) sol_new = DiffEqBase.build_solution(prob, alg, sol_loss.t, sol_loss.u, - retcode = ReturnCode.Success) + retcode = ReturnCode.Success) loss_val = loss(sol_new) if priors !== nothing loss_val += prior_loss(priors, p[(end - length(priors)):end]) diff --git a/src/two_stage_method.jl b/src/two_stage_method.jl index f9bbe834..f33090bf 100644 --- a/src/two_stage_method.jl +++ b/src/two_stage_method.jl @@ -98,13 +98,14 @@ function construct_oop_cost_function(f, du, preview_est_sol, preview_est_deriv, end function two_stage_objective(prob::DiffEqBase.DEProblem, tpoints, data, - adtype = SciMLBase.NoAD(); - kernel = EpanechnikovKernel()) + adtype = SciMLBase.NoAD(); + kernel = EpanechnikovKernel()) f = prob.f kernel_function = decide_kernel(kernel) - estimated_derivative, estimated_solution = construct_estimated_solution_and_derivative!(data, - kernel_function, - tpoints) + estimated_derivative, estimated_solution = construct_estimated_solution_and_derivative!( + data, + kernel_function, + tpoints) # Step - 2 preview_est_sol = [@view estimated_solution[:, i] @@ -118,6 +119,7 @@ function two_stage_objective(prob::DiffEqBase.DEProblem, tpoints, data, construct_oop_cost_function(f, prob.u0, preview_est_sol, preview_est_deriv, tpoints) end - return OptimizationFunction(TwoStageCost(cost_function, estimated_solution, - estimated_derivative), adtype) + return OptimizationFunction( + TwoStageCost(cost_function, estimated_solution, + estimated_derivative), adtype) end diff --git a/test/dae_tests.jl b/test/dae_tests.jl index b3135958..0b5e6153 100644 --- a/test/dae_tests.jl +++ b/test/dae_tests.jl @@ -20,14 +20,14 @@ data = convert(Array, randomized) using DiffEqParamEstim, OptimizationNLopt, OptimizationOptimJL, ForwardDiff, Zygote, Optimization, SciMLSensitivity cost_function = build_loss_objective(prob, DFBDF(), L2Loss(t, data), - Optimization.AutoZygote(), abstol = 1e-8, - reltol = 1e-8, verbose = false) + Optimization.AutoZygote(), abstol = 1e-8, + reltol = 1e-8, verbose = false) optprob = Optimization.OptimizationProblem(cost_function, [0.01]; lb = [0.0], ub = [1.0]) res = solve(optprob, OptimizationOptimJL.BFGS()) cost_function = build_loss_objective(prob, DFBDF(), L2Loss(t, data), - Optimization.AutoForwardDiff(), abstol = 1e-8, - reltol = 1e-8, verbose = false) + Optimization.AutoForwardDiff(), abstol = 1e-8, + reltol = 1e-8, verbose = false) optprob = Optimization.OptimizationProblem(cost_function, [0.01]; lb = [0.0], ub = [1.0]) res = solve(optprob, OptimizationOptimJL.BFGS()) @test res.u[1]≈0.04 atol=5e-3 diff --git a/test/dde_tests.jl b/test/dde_tests.jl index 40838853..26f03742 100644 --- a/test/dde_tests.jl +++ b/test/dde_tests.jl @@ -10,7 +10,7 @@ tspan = (0.0, 10.0) p = [0.5, 1.0] prob = DDEProblem(f_lotka, u0, h, tspan, p, - constant_lags = [0.5]) + constant_lags = [0.5]) sol = solve(prob, MethodOfSteps(Tsit5())) t = collect(range(0, stop = 10, length = 30)) @@ -29,9 +29,9 @@ p = [0.5] prob_opt = DDEProblem(f_lotka2, u0, h, tspan, p, constant_lags = [0.5]) cost_function = build_loss_objective(prob_opt, MethodOfSteps(Tsit5()), - L2Loss(t, data), Optimization.AutoZygote(), - abstol = 1e-8, - reltol = 1e-8) + L2Loss(t, data), Optimization.AutoZygote(), + abstol = 1e-8, + reltol = 1e-8) optprob = Optimization.OptimizationProblem(cost_function, [1.0], lb = [0.0], ub = [1.0]) opt = Opt(:GN_ESCH, 1) diff --git a/test/likelihood.jl b/test/likelihood.jl index f4186778..f6a3e8d0 100644 --- a/test/likelihood.jl +++ b/test/likelihood.jl @@ -20,36 +20,36 @@ aggregate_data = convert(Array, VectorOfArray([generate_data(sol, t) for i in 1: distributions = [fit_mle(Normal, aggregate_data[i, j, :]) for i in 1:2, j in 1:200] obj = build_loss_objective(prob1, Tsit5(), LogLikeLoss(t, distributions), maxiters = 10000, - verbose = false) + verbose = false) optprob = Optimization.OptimizationProblem(obj, [2.0, 2.0], lb = [0.5, 0.5], - ub = [5.0, 5.0]) + ub = [5.0, 5.0]) result = solve(optprob, BBO_adaptive_de_rand_1_bin_radiuslimited(), maxiters = 11e3) @test result.original.archive_output.best_candidate≈[1.5, 1.0] atol=1e-1 data_distributions = [fit_mle(Normal, aggregate_data[i, j, :]) for i in 1:2, j in 1:200] diff_distributions = [fit_mle(Normal, - aggregate_data[i, j, :] - aggregate_data[i, j - 1, :]) + aggregate_data[i, j, :] - aggregate_data[i, j - 1, :]) for j in 2:200, i in 1:2] obj = build_loss_objective(prob1, Tsit5(), - LogLikeLoss(t, data_distributions, diff_distributions), - Optimization.AutoForwardDiff(), maxiters = 10000, - verbose = false) + LogLikeLoss(t, data_distributions, diff_distributions), + Optimization.AutoForwardDiff(), maxiters = 10000, + verbose = false) optprob = Optimization.OptimizationProblem(obj, [2.0, 2.0], lb = [0.5, 0.5], - ub = [5.0, 5.0]) + ub = [5.0, 5.0]) result = solve(optprob, BBO_adaptive_de_rand_1_bin_radiuslimited(), maxiters = 11e3) @test result.original.archive_output.best_candidate≈[1.5, 1.0] atol=1e-1 data_distributions = [fit_mle(Normal, aggregate_data[i, j, :]) for i in 1:2, j in 1:200] diff_distributions = [fit_mle(Normal, - aggregate_data[i, j, :] - aggregate_data[i, j - 1, :]) + aggregate_data[i, j, :] - aggregate_data[i, j - 1, :]) for j in 2:200, i in 1:2] obj = build_loss_objective(prob1, Tsit5(), - LogLikeLoss(t, data_distributions, diff_distributions, 0.3), - Optimization.AutoForwardDiff(), maxiters = 10000, - verbose = false) + LogLikeLoss(t, data_distributions, diff_distributions, 0.3), + Optimization.AutoForwardDiff(), maxiters = 10000, + verbose = false) optprob = Optimization.OptimizationProblem(obj, [2.0, 2.0], lb = [0.5, 0.5], - ub = [5.0, 5.0]) + ub = [5.0, 5.0]) result = solve(optprob, BBO_adaptive_de_rand_1_bin_radiuslimited(), maxiters = 11e3) @test result.u≈[1.5, 1.0] atol=1e-1 using OptimizationBBO.BlackBoxOptim @@ -58,14 +58,14 @@ result = bboptimize(obj, SearchRange = [(0.5, 5.0), (0.5, 5.0)], MaxSteps = 11e3 distributions = [fit_mle(MvNormal, aggregate_data[:, j, :]) for j in 1:200] diff_distributions = [fit_mle(MvNormal, - aggregate_data[:, j, :] - aggregate_data[:, j - 1, :]) + aggregate_data[:, j, :] - aggregate_data[:, j - 1, :]) for j in 2:200] priors = [Truncated(Normal(1.5, 0.1), 0, 2), Truncated(Normal(1.0, 0.1), 0, 1.5)] obj = build_loss_objective(prob1, Tsit5(), - LogLikeLoss(t, distributions, diff_distributions), - Optimization.AutoForwardDiff(), maxiters = 10000, - verbose = false, priors = priors) + LogLikeLoss(t, distributions, diff_distributions), + Optimization.AutoForwardDiff(), maxiters = 10000, + verbose = false, priors = priors) optprob = Optimization.OptimizationProblem(obj, [2.0, 2.0], lb = [0.5, 0.5], - ub = [5.0, 5.0]) + ub = [5.0, 5.0]) result = solve(optprob, BBO_adaptive_de_rand_1_bin_radiuslimited(), maxiters = 11e3) @test result.u≈[1.5, 1.0] atol=1e-1 diff --git a/test/lorenz_test.jl b/test/lorenz_test.jl index e50f7a4a..69bb2e09 100644 --- a/test/lorenz_test.jl +++ b/test/lorenz_test.jl @@ -33,22 +33,22 @@ data = convert(Array, solve(prob, Euler(), tstops = t)) # Use BlackBoxOptim obj_short = build_loss_objective(prob_short, Euler(), L2Loss(t_short, data_short), - tstops = t_short, dense = false) + tstops = t_short, dense = false) res1 = bboptimize((x) -> obj_short(x, nothing); SearchRange = Xiang2015Bounds, - MaxSteps = 11e3) + MaxSteps = 11e3) optprob = Optimization.OptimizationProblem(obj_short, [9.0, 20.0, 2.0]; - lb = [9.0, 20.0, 2.0], - ub = [11.0, 30.0, 3.0]) + lb = [9.0, 20.0, 2.0], + ub = [11.0, 30.0, 3.0]) res2 = solve(optprob, BBO_adaptive_de_rand_1_bin_radiuslimited()) # Use NLopt obj_short = build_loss_objective(prob_short, Euler(), L2Loss(t_short, data_short), - Optimization.AutoForwardDiff(), tstops = t_short, - dense = false) + Optimization.AutoForwardDiff(), tstops = t_short, + dense = false) opt = Opt(:GN_ORIG_DIRECT_L, 3) optprob = Optimization.OptimizationProblem(obj_short, [9.0, 20.0, 2.0]; - lb = [9.0, 20.0, 2.0], - ub = [11.0, 30.0, 3.0]) + lb = [9.0, 20.0, 2.0], + ub = [11.0, 30.0, 3.0]) @time res = solve(optprob, opt) # opt = Opt(:GN_CRS2_LM, 3) @@ -102,7 +102,7 @@ res = solve(optprob, opt) #### Now let's solve the longer version obj = build_loss_objective(prob, Euler(), L2Loss(t, data), Optimization.AutoZygote(), - tstops = t, dense = false) + tstops = t, dense = false) # res1 = bboptimize(obj;SearchRange = Xiang2015Bounds, MaxSteps = 8e3) opt = Opt(:GN_ORIG_DIRECT_L, 3) diff --git a/test/lorenz_true_test.jl b/test/lorenz_true_test.jl index b9eef024..d4c36352 100644 --- a/test/lorenz_true_test.jl +++ b/test/lorenz_true_test.jl @@ -43,7 +43,7 @@ t_short = collect(tinterval_short) # Generate Data data_sol_short = solve(prob_short, Vern7(), saveat = t_short, reltol = 1e-12, - abstol = 1e-12) + abstol = 1e-12) data_short = convert(Array, data_sol_short) data_sol = solve(prob, Vern7(), saveat = t, reltol = 1e-12, abstol = 1e-12) data = convert(Array, data_sol) @@ -60,29 +60,29 @@ data = convert(Array, data_sol) # Note: Euler uses tstops to hit the estimation timepoints exactly since it's not adaptive obj_short = build_loss_objective(prob_short, Euler(), L2Loss(t_short, data_short), - tstops = t_short) + tstops = t_short) res1 = bboptimize((x) -> obj_short(x, nothing); SearchRange = Xiang2015Bounds, - MaxSteps = 11e3) + MaxSteps = 11e3) # Euler could not recover the correct results since its error is too high! obj_short = build_loss_objective(prob_short, Tsit5(), L2Loss(t_short, data_short), - Optimization.AutoForwardDiff()) + Optimization.AutoForwardDiff()) optprob = Optimization.OptimizationProblem(obj_short, [9.0, 20.0, 2.0], lb = xlow_bounds, - ub = xhigh_bounds) + ub = xhigh_bounds) res = solve(optprob, BBO_de_rand_1_bin_radiuslimited()) # Tolernace is still too high to get close enough obj_short = build_loss_objective(prob_short, Tsit5(), L2Loss(t_short, data_short), - Optimization.AutoZygote(), - reltol = 1e-9) + Optimization.AutoZygote(), + reltol = 1e-9) optprob = Optimization.OptimizationProblem(obj_short, [9.0, 20.0, 2.0], lb = xlow_bounds, - ub = xhigh_bounds) + ub = xhigh_bounds) res = solve(optprob, BFGS()) # With the tolerance lower, it achieves the correct solution in 4.5 seconds. obj_short = build_loss_objective(prob_short, Vern7(), L2Loss(t_short, data_short), - Optimization.AutoForwardDiff(), - reltol = 1e-12, abstol = 1e-12) + Optimization.AutoForwardDiff(), + reltol = 1e-12, abstol = 1e-12) optprob = Optimization.OptimizationProblem(obj_short, [9.0, 20.0, 2.0]) res = solve(optprob, Newton()) # But too much accuracy in the numerical solution of the ODE actually leads to @@ -94,10 +94,10 @@ res = solve(optprob, Newton()) # using NLopt obj_short = build_loss_objective(prob_short, Tsit5(), L2Loss(t_short, data_short), - Optimization.AutoForwardDiff(), - reltol = 1e-14) + Optimization.AutoForwardDiff(), + reltol = 1e-14) optprob = Optimization.OptimizationProblem(obj_short, [9.0, 20.0, 2.0], lb = xlow_bounds, - ub = xhigh_bounds) + ub = xhigh_bounds) opt = Opt(:GN_ORIG_DIRECT_L, 3) res = solve(optprob, opt) @@ -131,7 +131,7 @@ res1 = bboptimize(x -> obj(x, nothing); SearchRange = Xiang2015Bounds, MaxSteps opt = Opt(:GN_ORIG_DIRECT_L, 3) optprob = Optimization.OptimizationProblem(obj_short, GloIniPar, lb = first.(LooserBounds), - ub = last.(LooserBounds)) + ub = last.(LooserBounds)) res = solve(optprob, opt) # opt = Opt(:GN_CRS2_LM, 3) diff --git a/test/multiple_shooting_objective_test.jl b/test/multiple_shooting_objective_test.jl index 319d6e49..5155a572 100644 --- a/test/multiple_shooting_objective_test.jl +++ b/test/multiple_shooting_objective_test.jl @@ -10,23 +10,23 @@ ms_prob = ODEProblem(ms_f, ms_u0, tspan, ms_p) t = collect(range(0, stop = 10, length = 200)) data = Array(solve(ms_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)] + (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)] ms_obj = multiple_shooting_objective(ms_prob, Tsit5(), L2Loss(t, data), - Optimization.AutoZygote(); - discontinuity_weight = 1.0, abstol = 1e-12, - reltol = 1e-12) + Optimization.AutoZygote(); + discontinuity_weight = 1.0, abstol = 1e-12, + reltol = 1e-12) result = bboptimize(ms_obj; SearchRange = bound, MaxSteps = 21e3) @test result.archive_output.best_candidate[(end - 1):end]≈[1.5, 1.0] atol=2e-1 priors = [Truncated(Normal(1.5, 0.5), 0, 2), Truncated(Normal(1.0, 0.5), 0, 1.5)] ms_obj1 = multiple_shooting_objective(ms_prob, Tsit5(), L2Loss(t, data), - Optimization.AutoForwardDiff(); priors = priors, - discontinuity_weight = 1.0, abstol = 1e-6, - reltol = 1e-6) + Optimization.AutoForwardDiff(); priors = priors, + discontinuity_weight = 1.0, abstol = 1e-6, + reltol = 1e-6) optprob = Optimization.OptimizationProblem(ms_obj1, zeros(18), lb = first.(bound), - ub = last.(bound)) + ub = last.(bound)) result = solve(optprob, BFGS(), maxiters = 500) @test result.u[(end - 1):end]≈[1.5, 1.0] atol=2e-1 diff --git a/test/out_of_place_odes.jl b/test/out_of_place_odes.jl index f973828e..0db73633 100644 --- a/test/out_of_place_odes.jl +++ b/test/out_of_place_odes.jl @@ -24,8 +24,8 @@ data = convert(Array, randomized) soll = solve(prob, Tsit5()) cost_function = build_loss_objective(prob, Tsit5(), L2Loss(t, data), - Optimization.AutoZygote(), - maxiters = 10000, verbose = false) + Optimization.AutoZygote(), + maxiters = 10000, verbose = false) optprob = Optimization.OptimizationProblem(cost_function, [1.0], lb = [0.0], ub = [10.0]) sol = solve(optprob, BFGS()) diff --git a/test/runtests.jl b/test/runtests.jl index 74d4d6dd..0eaad8cf 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,10 +13,24 @@ using DiffEqParamEstim, Test #include("tests_on_odes/genetic_algorithm_test.jl") # Not updated to v0.6 end -@time @testset "Multiple Shooting Objective" begin include("multiple_shooting_objective_test.jl") end -@time @testset "Likelihood Loss" begin include("likelihood.jl") end -@time @testset "Out-of-place ODE Tests" begin include("out_of_place_odes.jl") end -@time @testset "Steady State Tests" begin include("steady_state_tests.jl") end -@time @testset "DAE Tests" begin include("dae_tests.jl") end -@time @testset "DDE Tests" begin include("dde_tests.jl") end -@time @testset "Test on Monte" begin include("test_on_monte.jl") end +@time @testset "Multiple Shooting Objective" begin + include("multiple_shooting_objective_test.jl") +end +@time @testset "Likelihood Loss" begin + include("likelihood.jl") +end +@time @testset "Out-of-place ODE Tests" begin + include("out_of_place_odes.jl") +end +@time @testset "Steady State Tests" begin + include("steady_state_tests.jl") +end +@time @testset "DAE Tests" begin + include("dae_tests.jl") +end +@time @testset "DDE Tests" begin + include("dde_tests.jl") +end +@time @testset "Test on Monte" begin + include("test_on_monte.jl") +end diff --git a/test/steady_state_tests.jl b/test/steady_state_tests.jl index 76b2969b..1c7214cb 100644 --- a/test/steady_state_tests.jl +++ b/test/steady_state_tests.jl @@ -15,8 +15,8 @@ s_sol = solve(s_prob, DynamicSS(Tsit5(), abstol = 1e-4, reltol = 1e-3)) # true data is 1.00, 0.25 data = [1.05, 0.23] obj = build_loss_objective(s_prob, SSRootfind(), L2Loss([Inf], data), - Optimization.AutoZygote(), - maxiters = Int(1e8), - abstol = 1e-10, reltol = 1e-10, verbose = true) + Optimization.AutoZygote(), + maxiters = Int(1e8), + abstol = 1e-10, reltol = 1e-10, verbose = true) result = Optim.optimize(obj, [2.0], Optim.BFGS()) @test result.minimizer[1]≈2.0 atol=2e-1 diff --git a/test/test_on_monte.jl b/test/test_on_monte.jl index 94f59af1..f74f5d70 100644 --- a/test/test_on_monte.jl +++ b/test/test_on_monte.jl @@ -19,9 +19,9 @@ data = convert(Array, randomized) monte_prob = EnsembleProblem(prob) obj = build_loss_objective(monte_prob, Tsit5(), L2Loss(t, data), - Optimization.AutoForwardDiff(), maxiters = 10000, - abstol = 1e-8, reltol = 1e-8, - verbose = false, trajectories = 25) + Optimization.AutoForwardDiff(), maxiters = 10000, + abstol = 1e-8, reltol = 1e-8, + verbose = false, trajectories = 25) optprob = Optimization.OptimizationProblem(obj, [1.3, 0.8]) result = solve(optprob, Optim.BFGS()) @test result.u≈[1.5, 1.0] atol=3e-1 diff --git a/test/tests_on_odes/blackboxoptim_test.jl b/test/tests_on_odes/blackboxoptim_test.jl index eb137f88..77527300 100644 --- a/test/tests_on_odes/blackboxoptim_test.jl +++ b/test/tests_on_odes/blackboxoptim_test.jl @@ -2,19 +2,19 @@ using BlackBoxOptim println("Use BlackBoxOptim to fit the parameter") cost_function = build_loss_objective(prob1, Tsit5(), L2Loss(t, data), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) bound1 = Tuple{Float64, Float64}[(1, 2)] result = bboptimize(cost_function; SearchRange = bound1, MaxSteps = 11e3) @test result.archive_output.best_candidate[1]≈1.5 atol=3e-1 cost_function = build_loss_objective(prob2, Tsit5(), L2Loss(t, data), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) bound2 = Tuple{Float64, Float64}[(1, 2), (2, 4)] result = bboptimize(cost_function; SearchRange = bound2, MaxSteps = 11e3) @test result.archive_output.best_candidate≈[1.5; 3.0] atol=3e-1 cost_function = build_loss_objective(prob3, Tsit5(), L2Loss(t, data), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) bound3 = Tuple{Float64, Float64}[(1, 2), (0, 2), (2, 4), (0, 2)] result = bboptimize(cost_function; SearchRange = bound3, MaxSteps = 11e3) @test result.archive_output.best_candidate≈[1.5; 1.0; 3.0; 1.0] atol=5e-1 diff --git a/test/tests_on_odes/genetic_algorithm_test.jl b/test/tests_on_odes/genetic_algorithm_test.jl index dcacca72..ddfc47eb 100644 --- a/test/tests_on_odes/genetic_algorithm_test.jl +++ b/test/tests_on_odes/genetic_algorithm_test.jl @@ -14,37 +14,37 @@ println("Use Genetic Algorithm to fit the parameter") # Floating number specifies fraction of population. cost_function = build_loss_objective(prob1, Tsit5(), L2Loss(t, data), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) N = 1 result, fitness, cnt = ga(cost_function, N; - initPopulation = Float64[1.2], - populationSize = 100, - ɛ = 0.1, - selection = sus, - crossover = intermediate(0.25), - mutation = domainrange(fill(0.5, N))) + initPopulation = Float64[1.2], + populationSize = 100, + ɛ = 0.1, + selection = sus, + crossover = intermediate(0.25), + mutation = domainrange(fill(0.5, N))) @test result[1]≈1.5 atol=3e-1 cost_function = build_loss_objective(prob2, Tsit5(), L2Loss(t, data), - maxiters = 10000) + maxiters = 10000) N = 2 result, fitness, cnt = ga(cost_function, N; - initPopulation = Float64[1.2, 2.8], - populationSize = 500, - ɛ = 0.1, - selection = sus, - crossover = intermediate(0.25), - mutation = domainrange(fill(0.5, N))) + initPopulation = Float64[1.2, 2.8], + populationSize = 500, + ɛ = 0.1, + selection = sus, + crossover = intermediate(0.25), + mutation = domainrange(fill(0.5, N))) @test result≈[1.5; 3.0] atol=3e-1 cost_function = build_loss_objective(prob3, Tsit5(), L2Loss(t, data), - maxiters = 10000) + maxiters = 10000) N = 4 result, fitness, cnt = ga(cost_function, N; - initPopulation = Float64[1.3, 0.8, 2.8, 1.2], - populationSize = 1000, - ɛ = 0.1, - selection = sus, - crossover = intermediate(0.25), - mutation = domainrange(fill(0.5, N))) + initPopulation = Float64[1.3, 0.8, 2.8, 1.2], + populationSize = 1000, + ɛ = 0.1, + selection = sus, + crossover = intermediate(0.25), + mutation = domainrange(fill(0.5, N))) @test result≈[1.5; 1.0; 3.0; 1.0] atol=3e-1 diff --git a/test/tests_on_odes/l2_colloc_grad_test.jl b/test/tests_on_odes/l2_colloc_grad_test.jl index 6d8e67e7..10c8cb71 100644 --- a/test/tests_on_odes/l2_colloc_grad_test.jl +++ b/test/tests_on_odes/l2_colloc_grad_test.jl @@ -1,31 +1,31 @@ weight = 1.0e-6 cost_function = build_loss_objective(prob1, Tsit5(), - L2Loss(t, data, colloc_grad = colloc_grad(t, data)), - maxiters = 10000, verbose = false) + L2Loss(t, data, colloc_grad = colloc_grad(t, data)), + maxiters = 10000, verbose = false) result = Optim.optimize(cost_function, 1.0, 2.0) @test result.minimizer≈1.5 atol=3e-1 cost_function = build_loss_objective(prob2, Tsit5(), - L2Loss(t, data, - differ_weight = weight, data_weight = weight, - colloc_grad = colloc_grad(t, data)), - maxiters = 10000, verbose = false) + L2Loss(t, data, + differ_weight = weight, data_weight = weight, + colloc_grad = colloc_grad(t, data)), + maxiters = 10000, verbose = false) result = Optim.optimize(cost_function, [1.3, 2.8], Optim.BFGS()) @test result.minimizer≈[1.5; 3.0] atol=3e-1 cost_function = build_loss_objective(prob3, Tsit5(), - L2Loss(t, data, - differ_weight = weight, - colloc_grad = colloc_grad(t, data)), - maxiters = 10000, verbose = false) + L2Loss(t, data, + differ_weight = weight, + colloc_grad = colloc_grad(t, data)), + maxiters = 10000, verbose = false) result = Optim.optimize(cost_function, [1.4, 0.9, 2.9, 1.2], Optim.BFGS()) @test result.minimizer≈[1.5, 1.0, 3.0, 1.0] atol=3e-1 cost_function = build_loss_objective(prob1, Tsit5(), - L2Loss(t, data, - data_weight = weight, - colloc_grad = colloc_grad(t, data)), - maxiters = 10000, verbose = false) + L2Loss(t, data, + data_weight = weight, + colloc_grad = colloc_grad(t, data)), + maxiters = 10000, verbose = false) result = Optim.optimize(cost_function, 1.0, 2) @test result.minimizer≈1.5 atol=3e-1 diff --git a/test/tests_on_odes/l2loss_test.jl b/test/tests_on_odes/l2loss_test.jl index 4d60d066..65bcb525 100644 --- a/test/tests_on_odes/l2loss_test.jl +++ b/test/tests_on_odes/l2loss_test.jl @@ -1,29 +1,29 @@ using BlackBoxOptim, Optim cost_function = build_loss_objective(prob1, Tsit5(), L2Loss(t, data), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) bound1 = Tuple{Float64, Float64}[(1, 2)] result = bboptimize(cost_function; SearchRange = bound1, MaxSteps = 11e3) @test result.archive_output.best_candidate[1]≈1.5 atol=3e-1 cost_function = build_loss_objective(prob2, Tsit5(), - L2Loss(t, data, differ_weight = nothing, - data_weight = 1.0), - maxiters = 10000, verbose = false) + L2Loss(t, data, differ_weight = nothing, + data_weight = 1.0), + maxiters = 10000, verbose = false) bound2 = Tuple{Float64, Float64}[(1, 2), (1, 4)] result = bboptimize(cost_function; SearchRange = bound2, MaxSteps = 11e3) @test result.archive_output.best_candidate≈[1.5; 3.0] atol=3e-1 cost_function = build_loss_objective(prob3, Tsit5(), L2Loss(t, data, differ_weight = 10), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) bound3 = Tuple{Float64, Float64}[(1, 2), (0, 2), (2, 4), (0, 2)] result = bboptimize(cost_function; SearchRange = bound3, MaxSteps = 11e3) @test result.archive_output.best_candidate≈[1.5; 1.0; 3.0; 1.0] atol=5e-1 cost_function = build_loss_objective(prob3, Tsit5(), - L2Loss(t, data, differ_weight = 0.3, - data_weight = 0.7), - maxiters = 10000, verbose = false) + L2Loss(t, data, differ_weight = 0.3, + data_weight = 0.7), + maxiters = 10000, verbose = false) bound3 = Tuple{Float64, Float64}[(1, 2), (0, 2), (1, 4), (0, 2)] result = bboptimize(cost_function; SearchRange = bound3, MaxSteps = 11e3) @test result.archive_output.best_candidate≈[1.5; 1.0; 3.0; 1.0] atol=5e-1 diff --git a/test/tests_on_odes/nlopt_test.jl b/test/tests_on_odes/nlopt_test.jl index 9bfaf7ab..afd70765 100644 --- a/test/tests_on_odes/nlopt_test.jl +++ b/test/tests_on_odes/nlopt_test.jl @@ -3,7 +3,7 @@ using OptimizationNLopt, Zygote println("Use NLOpt to fit the parameter") obj = build_loss_objective(prob1, Tsit5(), L2Loss(t, data), Optimization.AutoZygote(), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) opt = Opt(:LN_COBYLA, 1) optprob = OptimizationNLopt.OptimizationProblem(obj, [1.4]) @@ -29,7 +29,7 @@ res = solve(optprob, opt) # test differentiation obj = build_loss_objective(prob1, Tsit5(), L2Loss(t, data), Optimization.AutoForwardDiff(); - maxiters = 10000) #zygote behaves weirdly here + maxiters = 10000) #zygote behaves weirdly here opt = Opt(:LD_MMA, 1) xtol_rel!(opt, 1e-3) maxeval!(opt, 10000) diff --git a/test/tests_on_odes/optim_test.jl b/test/tests_on_odes/optim_test.jl index 5409d3ae..b57141a5 100644 --- a/test/tests_on_odes/optim_test.jl +++ b/test/tests_on_odes/optim_test.jl @@ -1,6 +1,6 @@ using Optim, Random obj = build_loss_objective(prob1, Tsit5(), L2Loss(t, data), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) ### Optim Method @@ -15,13 +15,13 @@ result = Optim.optimize(obj, [1.0], Optim.BFGS()) #plot!(sol_optimized2,leg=false) cost_function2 = build_loss_objective(prob2, Tsit5(), L2Loss(t, data), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) result_bfgs = Optim.optimize(cost_function2, [1.0, 2.5], Optim.BFGS()) @test_broken result_bfgs.minimizer≈[1.5; 3.0] atol=3e-1 Random.seed!(200) cost_function3 = build_loss_objective(prob3, Tsit5(), L2Loss(t, data), - maxiters = 10000, verbose = false) + maxiters = 10000, verbose = false) result_bfgs = Optim.optimize(cost_function3, [1.3, 0.8, 2.8, 1.2], Optim.BFGS()) @test result_bfgs.minimizer≈[1.5; 1.0; 3.0; 1.0] atol=5e-1 diff --git a/test/tests_on_odes/regularization_test.jl b/test/tests_on_odes/regularization_test.jl index 0ae12680..3351fc6f 100644 --- a/test/tests_on_odes/regularization_test.jl +++ b/test/tests_on_odes/regularization_test.jl @@ -1,23 +1,23 @@ using PenaltyFunctions, OptimizationOptimJL, LinearAlgebra, SciMLSensitivity cost_function_1 = build_loss_objective(prob1, Tsit5(), L2Loss(t, data), - Optimization.AutoZygote(), - Regularization(0.6, L2Penalty()), maxiters = 10000, - verbose = false, abstol = 1e-8, reltol = 1e-8) + Optimization.AutoZygote(), + Regularization(0.6, L2Penalty()), maxiters = 10000, + verbose = false, abstol = 1e-8, reltol = 1e-8) cost_function_2 = build_loss_objective(prob2, Tsit5(), L2Loss(t, data), - Optimization.AutoZygote(), - Regularization(0.1, - MahalanobisPenalty(Matrix(1.0I, 2, 2))), - verbose = false, - abstol = 1e-8, reltol = 1e-8, - maxiters = 10000) + Optimization.AutoZygote(), + Regularization(0.1, + MahalanobisPenalty(Matrix(1.0I, 2, 2))), + verbose = false, + abstol = 1e-8, reltol = 1e-8, + maxiters = 10000) cost_function_3 = build_loss_objective(prob3, Tsit5(), L2Loss(t, data), - Optimization.AutoZygote(), - Regularization(0.1, - MahalanobisPenalty(Matrix(1.0I, 4, 4))), - verbose = false, - abstol = 1e-8, reltol = 1e-8, - maxiters = 10000) + Optimization.AutoZygote(), + Regularization(0.1, + MahalanobisPenalty(Matrix(1.0I, 4, 4))), + verbose = false, + abstol = 1e-8, reltol = 1e-8, + maxiters = 10000) println("Use Optim BFGS to fit the parameter") optprob = Optimization.OptimizationProblem(cost_function_1, [1.0]) diff --git a/test/tests_on_odes/weighted_loss_test.jl b/test/tests_on_odes/weighted_loss_test.jl index 8ec9e9e6..3acb60a3 100644 --- a/test/tests_on_odes/weighted_loss_test.jl +++ b/test/tests_on_odes/weighted_loss_test.jl @@ -4,9 +4,9 @@ Random.seed!(123) original_solution = VectorOfArray([(sol(t[i])) for i in 1:length(t)]) original_solution_matrix_form = convert(Array, original_solution) weight = zeros(size(original_solution_matrix_form)[1], - size(original_solution_matrix_form)[2]) + size(original_solution_matrix_form)[2]) error = zeros(size(original_solution_matrix_form)[1], - size(original_solution_matrix_form)[2]) + size(original_solution_matrix_form)[2]) for i in 1:size(original_solution_matrix_form)[1] for j in 1:size(original_solution_matrix_form)[2] @@ -20,9 +20,9 @@ end weighted_data = original_solution_matrix_form + error weighted_cost_function = build_loss_objective(prob1, Tsit5(), - L2Loss(t, weighted_data, - data_weight = weight), - maxiters = 10000, verbose = false) + L2Loss(t, weighted_data, + data_weight = weight), + maxiters = 10000, verbose = false) opt = Opt(:LN_COBYLA, 1) min_objective!(opt, weighted_cost_function) (minf, minx, ret) = NLopt.optimize(opt, [1.3])