我正在寻找一种有效的方法来查找多个(> 100万)但独立的四阶多项式的局部最小值给定/指定范围/边界.
我有两个要求:
R1:即使对于 100 万个不同的多项式方程也有效
R2:局部最小值精确到 0.01(即 2dp)
这是我使用创建的一些代码scipy
。没关系,但我想知道是否有其他更好的软件包可以执行此类任务在我进行并行编程之前。
为了说明我的问题,让我们首先从一个多项式开始:
下面我试图在 (-5, 5) 范围内找到 4x^4 + 6x^3 + 3x^2 + x + 5 的局部最小值。
在我的笔记本电脑上,大约需要 2 毫秒才能找到本地最小值(约为 -0.72770502)。
对于一个多项式来说时间还可以,但我想要更快的速度,因为我需要定期执行此操作超过 100 万次。
from scipy import optimize
import numpy as np
# Define a objective and gradient function for 4th order polynomial
# x is the value to be evaluated
# par is a numpy array of len 5 that specifies the polynomial coefficients.
def obj_grad_fun_custom(x,par):
obj = (np.array([x**4,x**3,x**2,x**1,1]) * par).sum()
grad = (np.array([4*x**3,3*x**2,2*x,1]) * par[:-1]).sum()
return obj, grad
# Try minimise an example polynomial of 4x^4 + 6x^3 + 3x^2 + x + 5
# with contrainted bound
res = optimize.minimize(
fun = obj_grad_fun_custom,
x0 = 0,
args=(np.array([4,6,3,1,5])), # polynomial coefficients
jac=True ,
bounds=[(-2, 10)],
tol=1e-10)
print(res.x)
# Timing (this takes about 2 ms for me)
%timeit optimize.minimize(fun = obj_grad_fun_custom, x0 = 0, args=(np.array([4,6,3,1,5])), jac=True, bounds=[(-5, 5)], tol=1e-10)
下面是我计划对 100 万个不同的 4 阶多项式进行常规操作,我希望在本地最小化这些多项式。希望有人能给我指出一个比scipy
。或者还有其他替代方法吗?谢谢!
# Multiple polynomials
result = [] # saving the local minima
poly_sim_no = 1000000 #ideally 1 million or even more
np.random.seed(0)
par_set = np.random.choice(np.arange(10), size=(poly_sim_no, 5), replace=True) #generate some order 4 polynomial coefficients
for a in par_set:
res = optimize.minimize(obj_grad_fun_custom, 0,args=(a), jac=True ,bounds=[(-5, 5)], tol=1e-10)
result.append(res.x)
print(result)