Skip to content
Advertisement

Difference between fixing a variable and using a constant in GEKKO

I am solving a large scale optimization problem using Python GEKKO. Some variables of the model have to not change during the optimization process. So I was wondering what is the difference between using the m.fix GEKKO method on the variables (m.Var) that I want to be constant, or replacing the variables with constants declared with m.Const.

I use IMODE = 3 (default) as I do not do anything that requires simulation or estimation, derivatives, time, etc.

Advertisement

Answer

When possible, it is best to use int or float numbers in the model for large-scale problems. It is also possible to use m.Const() or m.Var() and fix the value. The m.Var() option is least efficient because it builds the 1st and 2nd derivative information with operator overloading. If you do need to include simple equations, such as used with c4 definition, then use m.options.REDUCE=1 to do a pre-solve to identify and eliminate variables that associated with simple pre-solvable constraints. Setting m.options.REDUCE=2 scans the model twice and it is possible to do additional scans.

from gekko import GEKKO
m = GEKKO()

c1 = 2.0
c2 = m.Const(3.0)
c3 = m.Var(); m.fix(c3,4.0)
c4 = m.Var(); m.Equation(c4==5)

v = m.Var()
m.Minimize((v-c1-c2-c3-c4)**2)
m.solve(disp=False)
print(c1,c2.value,c3.value[0],c4.value[0],v.value[0])

Results:

2.0 3.0 4.0 5.0 14.0

Constant definition for improved model performance

  • Best: c1 – Python number
  • 2nd Best: c2 – Gekko constant
  • 3rd Best: c3 – Gekko variable, fixed
  • Worst: c4 – Gekko variable, equation
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement