python - 使用 Scipy.optimize 方法 ='SLSQP' 返回初始猜测

标签 python optimization numpy scipy

我尝试使用 scipy 深入研究基于多个变量的函数优化

在使用批处理文件调用该工具后,我有一个函数从数据挖掘工具返回预测。

def query(x):
    import numpy as np
    file_direc_in="path_to_input_file.csv"
    file_direc_out="path_to_output_file.csv"


    with open(file_direc_in, 'w') as f:
        np.savetxt(f, x, delimiter=';', fmt='%.3f',newline='\r\n')
    f.close()
    os.system("Dataset_query.bat")
    #batch file takes the array i wrote to from input_file and estimates a result
    #afterwards the output will be taken from the output file:
    f = open(file_direc_out,'r')
    out = np.array([[float(f.readlines()[0])]])
    f.close()
    return out


from scipy.optimize import minimize
from calc import query
import numpy as np

x0=np.array([[1.5,50,30]])

bnds = ((1, 2), (0.1, 100), (20, 100))

res=minimize(query,x0,method='SLSQP',bounds=bnds, options={'maxiter': 10 , 'disp': True}, callback=True)

当我运行脚本时,我在我的控制台中看到了循环,但似乎没有为我的变量测试真正的值,我得到了返回的初始猜测:

Optimization terminated successfully.    (Exit mode 0)
            Current function value: [[ 1636.724]]
            Iterations: 1
            Function evaluations: 5
            Gradient evaluations: 1

虽然我知道这个问题的最小值是 x_minimum=[1,0.1,100] out 的值大约为 out=400 (我必须减少变量的第一个和第二个值并增加第三个值以获得更低的out)

我做错了什么?

最佳答案

我的解决方案是改变步长,因为我的预测函数 query 不平滑

res=minimize(query,args=(hist,ana),x0=x0,method='SLSQP',/
bounds=bnds, options={'disp': True ,'eps' : 1e0}) 

在我的例子中,搜索局部最小值没有意义,我现在正在以整数步长搜索最小值。

根据@ali_m 搜索全局最小值 basinhopping 可以改为使用。 我会在接下来的几天尝试一下

关于python - 使用 Scipy.optimize 方法 ='SLSQP' 返回初始猜测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20820432/

相关文章:

python - 使用未保存的相关对象保存对象

python - Python 的 MANIFEST.in 文件中的嫁接命令是什么?

python - 基于两列 Pandas 映射字典的最有效方法

python - 为什么我的 tanh 激活函数表现如此糟糕?

python - settings.py 文件中的 Django AttributeError

python - 无效元素状态异常 : Message: invalid element state: Element is not currently interactable and may not be manipulated

assembly - 为 X86 编译时如何防止函数对齐到 16 字节边界?

algorithm - 将 M 人分成 N 个团队,并设置比例限制

c++ - 不同类型初始化的性能

python-2.7 - 在 theano 卷积 MLP 中可视化每一层的输出