BYU-PRISM / GEKKO

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

Error with one variable #124

Closed APMonitor closed 2 years ago

APMonitor commented 2 years ago

A recent fix for long variable names (>=25 characters) introduced another bug. The problem is when there is one variable in a problem and the variable is initialized. This breaks v1.0.0 Gekko but not older versions:

from gekko import GEKKO
m = GEKKO() 
y = m.Var(1)
m.Equation(y**2==1)
m.solve(disp=True)
APMonitor commented 2 years ago

Bug is fixed with a modification to gk_write_files.py to detect ndim==1:

        if csv_data.ndim==1:
            # with only one variable
            hdr = csv_data[0]
        else:
            # with multiple variables
            hdr = csv_data[0,0]
            for i in range(1,np.size(csv_data,0)):
                hdr += ','+csv_data[i,0]
            np.savetxt(os.path.join(self._path,file_name), csv_data[:,1:].T,\
                       delimiter=",",comments='',header=hdr,fmt='%1.25s')
            self.csv_status = 'generated'