英文:
How do I put these conditions on an equation using GEKKO?
问题
我对正在解决的系统方程有三个条件:
- 如果
P > S
且H < S
,则y = -k(S-H)
- 如果
P < S
且H < S
,则y = -k(P-H)
- 如果
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:
- If
P > S
andH < S
, theny = -k(S-H)
- If
P < S
andH < S
, theny = -k(P-H)
- If
H > S
, theny = 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>S and H>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):
---> 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()
答案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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论