问题描述
from numpy import *;从 scipy.optimize 导入 *;从数学导入 *定义 f(X):x=X[0];y=X[1]返回 x**4-3.5*x**3-2*x**2+12*x+y**2-2*ybnds = ((1,5), (0, 2))min_test = 最小化(f,[1,0.1], bounds = bnds);打印(min_test.x)
我的函数 f(X)
在 x=2.557, y=1
有一个局部最小值,我应该可以找到.
上面显示的代码只会给出 x=1
的结果.我尝试了不同的公差和所有三种方法:L-BFGS-B、TNC 和 SLSQP.这是到目前为止我一直在看的线程:
您刚刚遇到了局部优化的问题:它强烈依赖于您传入的开始(初始)值.如果您提供 [2, 1]
它将找到正确的最小值.
常见的解决方案是:
在循环中使用您的优化,在您的边界内具有随机起点
将 numpy 导入为 np从 numpy 导入 *;从 scipy.optimize 导入 *;从数学导入 *定义 f(X):x=X[0];y=X[1]返回 x**4-3.5*x**3-2*x**2+12*x+y**2-2*ybnds = ((1,3), (0, 2))对于我在范围内(100):x_init = np.random.uniform(low=bnds[0][0], high=bnds[0][1])y_init = np.random.uniform(low=bnds[1][0],high=bnds[1][1])min_test = 最小化(f,[x_init,y_init],边界 = bnds)打印(min_test.x,min_test.fun)
使用可以突破局部最小值的算法,推荐scipy的
basinhopping()
使用全局优化算法并将其结果用作局部算法的初始值.建议是 NLopt 的
DIRECT
或 MADS 算法(例如NOMAD
).scipy 中还有一个,shgo
,我还没试过.
from numpy import *; from scipy.optimize import *; from math import *
def f(X):
x=X[0]; y=X[1]
return x**4-3.5*x**3-2*x**2+12*x+y**2-2*y
bnds = ((1,5), (0, 2))
min_test = minimize(f,[1,0.1], bounds = bnds);
print(min_test.x)
My function f(X)
has a local minima at x=2.557, y=1
which I should be able to find.
The code showed above will only give result where x=1
. I have tried with different tolerance and alle three method: L-BFGS-B, TNC and SLSQP.This is the thread I have been looking at so far:Scipy.optimize: how to restrict argument values
How can I fix this?
I am using Spyder(Python 3.6).
You just encounterd the problem with local optimization: it strongly depends on the start (initial) values you pass in. If you supply [2, 1]
it will find the correct minima.
Common solutions are:
use your optimization in a loop with random starting points inside your boundaries
import numpy as np from numpy import *; from scipy.optimize import *; from math import * def f(X): x=X[0]; y=X[1] return x**4-3.5*x**3-2*x**2+12*x+y**2-2*y bnds = ((1,3), (0, 2)) for i in range(100): x_init = np.random.uniform(low=bnds[0][0], high=bnds[0][1]) y_init = np.random.uniform(low=bnds[1][0], high=bnds[1][1]) min_test = minimize(f,[x_init, y_init], bounds = bnds) print(min_test.x, min_test.fun)
use an algorithm that can break free of local minima, I can recommend scipy's
basinhopping()
use a global optimization algorithm and use it's result as initial value for a local algorithm. Recommendations are NLopt's
DIRECT
or the MADS algorithms (e.g.NOMAD
). There is also another one in scipy,shgo
, that I have no tried yet.
这篇关于scipy.optimize 被困在局部最小值中.我能做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!