英文:
Is it possible to multiply an equation by a variable in GEKKO?
问题
I am trying to solve an MINLP problem where I define some constraints between variables by setting them to a dummy variable with a lower and upper bound. After that, I try to enable or disable those constraints by multiplying the equation by a binary variable belonging to special ordered set. I kind of assumed the multiplication would carry through the equation, however I am getting the following error (with modified variable names):
----------------------------------------------------------------
APMonitor, Version 1.0.0
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 7
Constants : 0
Variables : 81
Intermediates: 205
Connections : 57
Equations : 302
Residuals : 47
@error: Model Expression
*** Error in syntax of function string: Invalid element: dummy_var=(function_1
Position: 23
((binary_var)*(dummy_var=(function_1-function_2)
?
Any help is greatly appreciated.
英文:
I am trying to solve an MINLP problem where I define some constraints between variables by setting them to a dummy variable with a lower and upper bound. After that, I try to enable or disable those constraints by multiplying the equation by a binary variable belonging to special ordered set. I kind of assumed the multiplication would carry through the equation, however I am getting the following error (with modified variable names):
----------------------------------------------------------------
APMonitor, Version 1.0.0
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 7
Constants : 0
Variables : 81
Intermediates: 205
Connections : 57
Equations : 302
Residuals : 47
@error: Model Expression
*** Error in syntax of function string: Invalid element: dummy_var=(function_1
Position: 23
((binary_var)*(dummy_var=(function_1-function_2)
?
Any help is greatly appreciated.
答案1
得分: 1
Multiply both sides of the equation to activate or deactivate it with the binary variable.
m.Equation(b*y==b*x)
Here is a complete example with an m.sos1()
variable.
from gekko import GEKKO
m = GEKKO()
x = m.Param(-5)
y = m.Var()
b = m.sos1([0,1])
m.Equation(b*y==b*x)
m.solve()
print(x.value[0],y.value[0])
If there is a condition that leads to an ON / OFF condition for an equation, then use the m.if3()
statement such as:
from gekko import GEKKO
m = GEKKO()
x = m.Param(-1)
y = m.Var(1)
b = m.if3(x,0,1)
m.Equation(b*y==b*x)
m.solve()
print(x.value[0],y.value[0])
Changing the value to x = m.Param(1)
re-activates the equation. You may also want to try slack variables that are typically much more efficient for logical conditions.
英文:
Multiply both sides of the equation to activate or deactivate it with the binary variable.
m.Equation(b*y==b*x)
Here is a complete example with an m.sos1()
variable.
from gekko import GEKKO
m = GEKKO()
x = m.Param(-5)
y = m.Var()
b = m.sos1([0,1])
m.Equation(b*y==b*x)
m.solve()
print(x.value[0],y.value[0])
If there is a condition that leads to an ON / OFF condition for an equation then use the m.if3()
statement such as:
from gekko import GEKKO
m = GEKKO()
x = m.Param(-1)
y = m.Var(1)
b = m.if3(x,0,1)
m.Equation(b*y==b*x)
m.solve()
print(x.value[0],y.value[0])
Changing the value to x = m.Param(1)
re-activates the equation. You may also want to try slack variables that are typically much more efficient for logical conditions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论