BYU-PRISM / GEKKO

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

Cannot assign a GEKKO Param to a Var #51

Closed ericman314 closed 5 years ago

ericman314 commented 5 years ago

When I try to assign a gekko Param to a Var:

import numpy as np
from gekko import GEKKO

m = GEKKO()
m.options.IMODE = 4

m.time = np.linspace(0, 10, 11)

T0 = m.Param(298.15)
T = m.Var(T0.value)     # Assign T0 to T

m.Equation(T.dt() == 1)
m.solve()

I get this:

Traceback (most recent call last):
  File "/home/eric/Documents/Dynamic Optimization/Temp control Lab A/debug.py", line 13, in <module>
    m.solve()
  File "/home/eric/.local/lib/python2.7/site-packages/gekko/gekko.py", line 661, in solve
    self._write_csv()
  File "/home/eric/.local/lib/python2.7/site-packages/gekko/gk_write_files.py", line 166, in _write_csv
    elif len(vp.VALUE) == 1:
  File "/home/eric/.local/lib/python2.7/site-packages/gekko/gk_operators.py", line 134, in __len__
    return len(self.value)
  File "/home/eric/.local/lib/python2.7/site-packages/gekko/gk_operators.py", line 134, in __len__
    return len(self.value)
TypeError: object of type 'float' has no len()

If I just assign a number to it, it works:

T0 = m.Param(298.15)
T = m.Var(298.15)     # This works

Being able to assign one Param to another, or at the very least a better error message, would be helpful.

APMonitor commented 5 years ago

T0.value is a GK_Value, not a float. GK_Value has T0.value.value, T0.value.change, etc. The issue appears to be nested GK_Value definitions when initializing. Something like: T = m.Var(T0.value.value) would work but this is non-intuitive. I'm working on a fix either in gk_operators.py or else in gekko.py.

APMonitor commented 5 years ago

This will be fixed in 0.2rc5. I updated gk_operators.py to identify when the initialized value is a GK_Value and extract the value.value.