[英]Why does scipy.optimize.minimize not find the minimum?
I am looking to find for which (x) the following function is minimal given a parameter dim.在给定参数dim的情况下,我正在寻找以下function最小的(x)。
This is the function:这是 function:
def func(x, dim):
return np.abs(np.abs(np.mean(np.sqrt(np.sum(np.diff(
np.random.rand(100000,dim,2)/x, axis=2)**2, axis=1))))
- 1/3)
And this is how it looks:这就是它的外观:
for xx in np.arange(1,5,0.1):
plt.scatter(xx, func(xx,2), color='blue')
But when I try to find the x value which should be around 1.5 the result very close to my x0 guess (here around 1.0).但是当我试图找到应该在 1.5 左右的 x 值时,结果非常接近我的 x0 猜测(这里大约为 1.0)。
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
params = minimize(func, x0=1, args=(2))
I also tried different solvers, but I cannot get it to minimize.我也尝试了不同的求解器,但我无法将其最小化。
Your function needs to be deterministic for the minimization to work.您的 function 需要确定性才能使最小化起作用。 So you need to remove your call to
np.random.rand
.因此,您需要删除对
np.random.rand
的调用。 Once solution could be to generate those random numbers once at the beginning and fix them throughout the minimization.曾经的解决方案可能是在开始时生成这些随机数并在整个最小化过程中修复它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.