Gekko非线性优化,约束函数中的对象类型错误评估if语句

问题描述 投票:5回答:2

我正在尝试解决非线性优化问题。我通过创建下面的代码重复了我的问题。 Python返回TypeError: object of type 'int' has no len()。如何在约束函数中包含IF语句?

控制台打印以下内容:

  File "<ipython-input-196-8d29d410dcea>", line 1, in <module>
    runfile('C:/Users/***/Documents/***/Project/untitled.py', wdir='C:/Users/***/Documents/***/***/Project')

  File "C:\Users\***\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "C:\Users\***\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/***/Documents/***/***/Project/untitled.py", line 27, in <module>
    m.Equation(Cx(x1,x2,x3,x4) < 0)

  File "C:/Users/***/Documents/***/***/Project/untitled.py", line 17, in Cx
    if K > 15:

  File "C:\Users\***\Anaconda3\lib\site-packages\gekko\gk_operators.py", line 25, in __len__
    return len(self.value)

  File "C:\Users\***\Anaconda3\lib\site-packages\gekko\gk_operators.py", line 134, in __len__
    return len(self.value)

TypeError: object of type 'int' has no len()

-

from gekko import GEKKO
m = GEKKO()


def Cr(x1,x2,x3,x4):
    return (x1*x4*(x1+x2+x3)+x3**2)

def Cw(x1,x2,x3,x4):
    return x1*x2*x3*x4

def Ck(x1,x2,x3,x4):
    return x1*x2*x3*x4+1

def Cx(x1,x2,x3,x4):
    K = Ck(x1,x2,x3,x4)
    if K > 15:  #Issue here
        K = 15
    return x1**2+x2**2+x3**2+x4**2 - K

x1 = m.Var(value=1,lb=-5000,ub=5000)
x2 = m.Var(value=1,lb=-5000,ub=5000)
x3 = m.Var(value=1,lb=-5000,ub=5000)
x4 = m.Var(value=1,lb=-5000,ub=5000)

m.Equation(Cw(x1,x2,x3,x4) >= 14)
m.Equation(Cx(x1,x2,x3,x4) < 0)

m.Obj(Cr(x1,x2,x3,x4))

m.solve(disp=False)
print(x1.value)
print(x2.value)
print(x3.value)
print(x4.value)

-

我期待让GEKKO在约束中运行IF语句,我不关心代码中的优化问题是否有解决方案。先感谢您。

python optimization ipopt gekko
2个回答
2
投票

我建议您在GEKKO中使用min2或min3函数或最近添加的if3函数。

K = m.min3(Ck(x1,x2,x3,x4),15)

以下是有关MPCCs and Binary Switching variables的其他信息。您不能使用具有非连续渐变的函数,因为它会导致求解器出现问题以及它们如何搜索解法。 min3和if3函数使用二进制变量并将问题解决为混合整数问题,min2使用MPCC。这是一个简短的例子,演示了min2和min3的使用。

Example use of min2 and min3 functions

import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO
m = GEKKO(remote=False) 
p = m.Param(value=np.linspace(10,20,21))
x = m.Var()
m.Equation(x==p)
# with MPCCs
y2 = m.min2(p,15)
# with integer variables
y3 = m.min3(p,16)
m.options.IMODE = 2
m.solve()
plt.plot(p,x,'b-',label='x')
plt.plot(p,y2,'g:',label='MPCC')
plt.plot(p,y3,'r--',label='Integer Switch')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.show()

1
投票

(免责声明:我不知道这个lib或它会为你做什么)

if语句使得这个问题不可区分,这使得NLP求解器(如Ipopt)的假设无效。

就MINLP求解器(Bonmin,Couenne)而言,这可以通过重新形成来实现(当所需的辅助二元变量被放宽时,所得到的问题是可区分的)。期望lib为你做这件事几乎是不可能的。

因此,似乎你需要遵守一些MINLP模型的规则,例如Bonmin here所描述的。 “基于if的分支”没有概念。

要么引入像MIP世界中常见的指标变量,请参阅here。忽略开销的想法将是这样的:

K_ = Ck(x1,x2,x3,x4)
I = K_ > 15 (binary variable; see link for formulation idea)

return x1**2+x2**2+x3**2+x4**2 - I*15 - (1-I) * K_

这是一个MINLP。

在解释等式时,您可以在不使用额外的二进制变量(并触摸MINLP)的情况下逃脱:

return x1**2+x2**2+x3**2+x4**2 - min(Ck(x1,x2,x3,x4), 15)

这也是不可区分的,但可以很容易地重新制定(有一个怪癖),如:

return x1**2+x2**2+x3**2+x4**2 - A

# extra constraints
A <= Ck(x1,x2,x3,x4)
A <= 15

如果我们能够强制执行最大可能的A。这意味着,它必须是目标的一部分:

m.Obj(Cr(x1,x2,x3,x4) + c * A) (if it's a maximization problem)

那么这将是一个NLP,但c的价值需要一些关注(它必须足够大)。

© www.soinside.com 2019 - 2024. All rights reserved.