【问题标题】:Python SciPy Optimize -- Minimize function not iterating, x-values not changing at allPython SciPy Optimize - 最小化函数不迭代,x 值根本不改变
【发布时间】:2022-10-16 23:04:16
【问题描述】:

我是 SciPy 和优化功能的新手,所以这可能是一个简单的问题。我按照教程设置了基本的优化功能。我概述了目标函数、边界、约束、初始猜测等。当我运行该函数时,不会发生任何优化。它说“优化成功终止”,但生成的 x 值与我作为初始猜测输入的 x 值相同。我将把我的代码放在下面:

def objective_fcn(x):
    x1 = x[0]
    x2 = x[1]
    x3 = x[2]
    profit = (128375.0 + x3*147187.0)*149.12*(1+x1) - (44.92*(1+x2))*(x3*147187.0 + 20326.0 + 147187.0*(1-x3))
    return profit * -1

def ineq_const(x):
    x3 = x[2]
    return (1-x3)*147187.0 - 128375.0

x0 = [0.1,0.0,0.1]

bounds_x1 = (-1.0, 0.75)
bounds_x2 = (-1.0, 1.0)
bounds_x3 = (-1.0, 1.0)
bounds = [bounds_x1, bounds_x2, bounds_x3]

const1 = {'type': 'ineq', 'fun': ineq_const}
consts = [const1]

result = minimize(objective_fcn, x0, method='SLSQP', bounds=bounds, constraints=consts)

print("The full result is: ")
print(result)

我也附上了结果,注意它与我最初猜测的[0.1, 0.0, 0.1] 相同。

优化结果:

【问题讨论】:

    标签: python optimization scipy


    【解决方案1】:

    我在本地重播了您的示例,看起来您正面临一个爆炸性梯度问题:算法尝试更新参数,但步长太大,并将其移出约束允许的空间。由于这是被禁止的,因此不会发生更新。为了减小步长,缩放目标函数就足够了:

    def objective_fcn(x):
        x1 = x[0]
        x2 = x[1]
        x3 = x[2]
        profit = (128375.0 + x3*147187.0)*149.12*(1+x1) - (44.92*(1+x2))*(x3*147187.0 + 20326.0 + 147187.0*(1-x3))
        return profit * -1 * 1e-7
    

    收敛到:

    The full result is:
         fun: -3.84099195200049
         jac: array([-2.19485256,  0.75246841, -3.84099194])
     message: 'Optimization terminated successfully'
        nfev: 12
         nit: 3
        njev: 3
      status: 0
     success: True
           x: array([ 0.75     , -1.       ,  0.1278102])
    

    将目标值乘以-1e7 即可知道最佳利润。

    我在第二次试用时选择了1e-71e-5 首先失败了)。它可以被解释为学习率的手动调整,如果你想自动化,请查看 ML 中的超参数优化 - 例如。

    【讨论】:

    • 非常感谢!那行得通。我剩下的唯一问题是,当我将利润乘以 1e-7 时,是否需要修改不等式约束中的任何内容?或者我可以保持原样并且它仍然可以正常工作吗?
    猜你喜欢
    • 1970-01-01
    • 2019-09-22
    • 2019-07-02
    • 1970-01-01
    • 2022-06-23
    • 2019-09-01
    • 1970-01-01
    • 2020-02-23
    • 2017-07-07
    相关资源
    最近更新 更多