From 81c903624889affb333d2f00dd93613921647f3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobranowski?= Date: Thu, 9 May 2024 14:43:45 +0200 Subject: [PATCH] Change `Enzyme` to `ForwardDiff` it's way faster --- Project.toml | 2 +- src/RamseyGrowthModel.jl | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 967832e..2339c98 100644 --- a/Project.toml +++ b/Project.toml @@ -5,7 +5,7 @@ version = "0.1.0" [deps] DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" -Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" +ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781" Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" diff --git a/src/RamseyGrowthModel.jl b/src/RamseyGrowthModel.jl index c98961f..5cced9d 100644 --- a/src/RamseyGrowthModel.jl +++ b/src/RamseyGrowthModel.jl @@ -2,8 +2,8 @@ module RamseyGrowthModel export GrowthModel -using Enzyme using DataFrames +using ForwardDiff using Roots include("sample_functions.jl") @@ -25,8 +25,8 @@ GrowthModel( function next_K_C(model::GrowthModel, K, C)::Tuple{Float64,Float64} - u′(c::Float64) = autodiff(Reverse, model.u, Active, Active(c))[1][1] - f′(k::Float64) = autodiff(Reverse, model.f, Active, Active(k))[1][1] + u′(c::Float64) = ForwardDiff.derivative(model.u, c) + f′(k::Float64) = ForwardDiff.derivative(model.f, k) next_K = model.f(K) + (1 - model.δ) * K - C next_K >= 0 || return NaN, NaN @@ -52,7 +52,7 @@ function shooting(model::GrowthModel, T::Int64, K₀::Float64, C₀::Float64)::D allocation end -function find_best_allocation(model::GrowthModel, T::Int64, K₀::Float64; tol::Float64=K₀/1e6)::DataFrame +function find_best_allocation(model::GrowthModel, T::Int64, K₀::Float64; tol::Float64=K₀ / 1e6)::DataFrame C_low, C_high = 0, model.f(K₀) # todo: add a loop failure exit after too many iterations