scipy.optimize.minimize for scipy.integrate.solve_ivp results in "success: False"
I'm trying to find the optimal parameters to supply to a population model . . . I've got the Schaefer Population Model method spm below, and an objective function, objective. The optimization does not work, though, and I can't tell why! The output is below . . . Any ideas out there? Thank you!!!
import numpy as np
from scipy.integrate import solve_ivp
from scipy.interpolate import interp1d
# For finding optimal values for constants in ODEs
from scipy.optimize import minimize
biomass_obs = [7049, 6536, 5886, 5585, 5826]
years = [1961, 1962, 1963, 1964, 1965]
f_obs = [0.22 , 0.269, 0.227, 0.135, 0.129]
def spm(t, B, r, K, f_func):
"""
t: time
B: biomass
r: growth rate
K: karrying kapacity
f_func: interpolation function for F
"""
b_val = B[0]
current_F = f_func(t)
# Differential Equation
dBdT = r * b_val * (1 - b_val / K) - current_F * b_val
return dBdT
# And objective function
def objective(params, t, B, f_func):
# print(f"t = {t}")
r, K = params
sol = solve_ivp(spm, (t[0], t[-1]), [B[0]], args=(r,K,f_func,), t_eval=t)
# calculate sum of squres
return np.sum((sol.y[0] - B)**2)
# Our first guess at r. It looks like r should be slightly negative,
# And K is around 8000?
initial_guess = [0.9, 9500]
# interpolation function
f_interp = interp1d(years, f_obs, kind='previous', fill_value="extrapolate")
result = minimize(objective, initial_guess, args=(years, biomass_obs, f_interp))
result
[ins] In [32]: result
Out[32]:
message: Desired error not necessarily achieved due to precision loss.
success: False
status: 2
fun: 16142.459294231905
x: [ 3.678e-01 1.058e+04]
nit: 11
jac: [-1.797e+06 -8.096e+01]
hess_inv: [[ 1.954e-08 -4.690e-04]
[-4.690e-04 1.407e+01]]
nfev: 254
njev: 82