我开始将 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
为什么会这样?
最佳答案
优化器无法在默认的迭代次数内达到收敛。通过设置 tol
和 options
参数,see the docs here for further details 可以在对方法的调用中设置收敛公差和迭代次数。
关于python-3.x - scipy.optimize.minimize : Iteration limit exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57153111/