可以在GEKKO中通过一个变量来乘以一个方程吗?

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

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.

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

发表评论

匿名网友

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

确定