如何在使用GEKKO时对方程加入这些条件?

huangapple go评论69阅读模式
英文:

How do I put these conditions on an equation using GEKKO?

问题

我对正在解决的系统方程有三个条件:

  1. 如果 P > SH < S,则 y = -k(S-H)
  2. 如果 P < SH < S,则 y = -k(P-H)
  3. 如果 H > S,则 y = 0

我如何将这些条件添加到方程中?

我尝试使用 if3(condition,x1,x2) 并在条件中使用 "and" 语句,即 if3(P>S and H>S, -k(S-H), -k(P-H))

这并没有起作用。我得到了这个错误:

File ~\anaconda3\lib\site-packages\gekko\gk_operators.py:25, in GK_Operators.len(self)
24 def len(self):
---> 25 return len(self.value)

File ~\anaconda3\lib\site-packages\gekko\gk_operators.py:144, in GK_Value.len(self)
143 def len(self):
--> 144 return len(self.value)

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

英文:

I have 3 conditions for an equation in a system that is being solved:

  1. If P &gt; S and H &lt; S, then y = -k(S-H)
  2. If P &lt; S and H &lt; S, then y = -k(P-H)
  3. If H &gt; S, then y = 0

How would I add these conditions to an equation?

I tried using if3(condition,x1,x2) with an "and" statement in the condition i.e. if3(P&gt;S and H&gt;S, -k(S-H),-k(P-H))

It did not work. I got this error:

File ~\anaconda3\lib\site-packages\gekko\gk_operators.py:25, in GK_Operators.__len__(self)
 24 def __len__(self):
---&gt; 25     return len(self.value)

File ~\anaconda3\lib\site-packages\gekko\gk_operators.py:144, in GK_Value.__len__(self)
143 def __len__(self):
--&gt; 144     return len(self.value)

TypeError: object of type &#39;int&#39; has no len()

答案1

得分: 1

The Gekko if3函数充当对0的条件,其中如果提供的条件小于0,则选择x1参数,如果提供的条件大于0,则选择x2参数。我不认为像"<"或"and"这样的逻辑表达式在Gekko语法中有效。您可以使用嵌套的if3语句来实现"and",如下所示:

from gekko import GEKKO

m = GEKKO()

P = m.Param(5)
S = m.Param(5)
H = m.Param(5)
k = m.Param(5)

cond1 = P - S
cond2 = H - S

#x1 is cond < 0
y = m.if3(cond2,m.if3(cond1,-k*(P-H),-k*(S-H)),0)

m.solve(disp=False)
print(y.value[0])

希望这是您要寻找的内容。

英文:

The Gekko if3 function acts as a conditional against 0, where the x1 parameter is chosen if the provided conditional is below 0, and the x2 parameter is chosen if the provided conditional is above 0. I don't believe logical expressions like "<" or "and" will work with the Gekko syntax. You can accomplish an "and" with a nested if3 statement, like so:

from gekko import GEKKO

m = GEKKO()

P = m.Param(5)
S = m.Param(5)
H = m.Param(5)
k = m.Param(5)

cond1 = P - S
cond2 = H - S

#x1 is cond&lt;0
y = m.if3(cond2,m.if3(cond1,-k*(P-H),-k*(S-H)),0)

m.solve(disp=False)
print(y.value[0])

Hopefully this is what you are looking for.

huangapple
  • 本文由 发表于 2023年5月10日 12:20:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定