python-3.x - scipy.optimize.minimize : Iteration limit exceeded

标签 python-3.x scipy normal-distribution scipy-optimize scipy-optimize-minimize

我开始将 scipy.optimize.minimize 用于一个工作项目,在该项目中,我尝试根据每个商店的历史销售数据优化跨商店的产品分配。

我从 Excel 工作表中获取了大约 300 家商店的数据,并将其存储在 Store 类中。我使用数据构建直方图并将正态分布拟合到直方图,并将拟合正态分布的均值和标准差保存在各自的 Store 对象中。 (到目前为止这部分没问题)。

当我试图根据我在商店中分配产品的方式来最大化产品销售的总“概率”时,问题就来了。

我的程序中还有更多内容,但这是相关部分(缺少从 excel 文件中获取数据,以及正态分布对每个商店的拟合):

import math, numpy
from scipy.optimize import minimize

unitsAvailable = 400

def distribution_objective(allocations):
    target = 0
    for i in range(len(stores)):
        target += normal(allocations[i], stores[i].mu, stores[i].std)

    return - target


def constraint1(allocations):

    return unitsAvailable - sum(allocations)

def constraint2(allocations):

    return min(allocations)

cons = [{'type':'eq', 'fun':constraint1}, {'type':'ineq', 'fun':constraint2}]
guess_allocs = []

for i in range(len(stores)):
    guess_allocs.append(unitsAvailable / len(stores))

distribution_solution = minimize(distribution_objective, guess_allocs, method='SLSQP', constraints=cons, options={'disp':True})

运行我的程序,我收到以下消息:

Iteration limit exceeded    (Exit mode 9)
            Current function value: -124.1033692190603
            Iterations: 101
            Function evaluations: 46808
            Gradient evaluations: 101

为什么会这样?

最佳答案

优化器无法在默认的迭代次数内达到收敛。通过设置 toloptions 参数,see the docs here for further details 可以在对方法的调用中设置收敛公差和迭代次数。

关于python-3.x - scipy.optimize.minimize : Iteration limit exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57153111/

相关文章:

regex - Python 正则表达式匹配多行

python - 如何获取()这个属于 userprofile.team 的模型

python-3.x - 在电子邮件中呈现的 HTML

python - KDE 有两点失败?

r - 使用 "optim"函数估计 R.problem 中正态分布的平均值和标准差

R:将正常拟合添加到 ggplot2 中的分组直方图

c - 如何用C(或其他语言)实现标准正态累积分布函数

python - 合并单词对列表中的第一个单词,具体取决于这些对中的第二个单词

python - 从 scipy 中的稀疏矩阵中删除对角线元素

python - 对象名称中不允许使用 ``/`` 字符