如何使用Python Gekko解决绝对值abs()目标? [英] How to solve Absolute Value abs() objective with Python Gekko?

查看:87
本文介绍了如何使用Python Gekko解决绝对值abs()目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有平方目标的优化问题可以在Python Gekko中使用IPOPT成功解决.

An optimization problem with a squared objective solves successfully with IPOPT in Python Gekko.

from gekko import GEKKO
import numpy as np
m = GEKKO()
x = m.Var(); y = m.Param(3.2)
m.Obj((x-y)**2)
m.solve()
print(x.value[0],y.value[0])

但是,当我切换到绝对值目标 np.abs(xy)( abs 的numpy版本)或 m.abs(xy)时( abs 的Gekko版本),IPOPT求解器报告失败的解决方案.绝对值逼近 m.sqrt((x-y)** 2)也会失败.

However, when I switch to an absolute value objective np.abs(x-y) (the numpy version of abs) or m.abs(x-y) (the Gekko version of abs), the IPOPT solver reports a failed solution. An absolute value approximation m.sqrt((x-y)**2) also fails.

失败的解决方案

from gekko import GEKKO
import numpy as np
m = GEKKO()
x = m.Var(); y = m.Param(3.2)
m.Obj(m.abs(x-y))
m.solve()
print(x.value[0],y.value[0])

我了解基于梯度的求解器不喜欢没有连续一阶和二阶导数的函数,因此我怀疑这是通过 abs()发生的,其中 0 是一个没有连续导数的点除了在Gekko中使用基于梯度的求解器可靠地求解绝对值之外,是否还有 abs()替代方案?

I understand that gradient-based solvers don't like functions without continuous first and second derivatives so I suspect that this is happening with abs() where 0 is a point that does not have continuous derivatives. Is there any alternative to abs() to reliably solve an absolute value with gradient-based solvers in Python Gekko?

推荐答案

您可以改用m.abs2,它考虑了派生问题,应该解决该问题.

You can use m.abs2 instead, It takes into account the issue with the derivative and should solve the issue.

这篇关于如何使用Python Gekko解决绝对值abs()目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆