BYU-PRISM / GEKKO

GEKKO Python for Machine Learning and Dynamic Optimization
https://machinelearning.byu.edu
Other
573 stars 102 forks source link

"Equation without an equality (=) or inequality (>,<)" error with identity equation #98

Closed mcpiroman closed 3 years ago

mcpiroman commented 3 years ago

Say I have code like:

vars = [m.Var() for i in range(x)]
m.Equation(sum(vars) <= 0)

The problem is, if x becomes zero then sum(vars) gives 0 and m.solve() yields:

Exception: @error: Equation Definition
 Equation without an equality (=) or inequality (>,<)
 true
 STOPPING...

I guess that's because the equality becomes an identity and at some point the inequality operation is optimized out. Is there a way around that? Or do I have to manually look for identities before invoking Equation?

APMonitor commented 3 years ago

I recommend a conditional statement to catch that:

if x>=1:
   vars = [m.Var() for i in range(x)]
   m.Equation(m.sum(vars) <= 0)

Otherwise the equation is empty on the left side as sum([])<=0.

APMonitor commented 3 years ago

There is an active Gekko discussion on StackOverflow. It is easier for others to find questions such as this if you would consider posting future questions there.

mcpiroman commented 3 years ago

Otherwise the equation is empty on the left side as sum([])<=0.

More precisely this gives 0 <= 0. I wonder if this shouldn't be handled at the framework level, since such equations are technically correct and always true and so they should be accepted. This would make it easier to work with such cases. Or at least I would clarify the error message to indicate identity, but perhaps this isn't to be done as the gekko level.