BYU-PRISM / GEKKO

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

Bspline error with different sizes of x and y #70

Closed abe-mart closed 4 years ago

abe-mart commented 4 years ago

I've run into a problem with the bspline function in data=True mode. The existing test coverage is only for cases where x and y are the same size. Inputting x and y matrices that are not the same shape gives an error. This is on Linux.

Case 1: x and y are equal sizes. Solves succesfully

from gekko import gekko
import numpy as np

m = gekko(remote=False)

xg = np.linspace(-1,1,10)
yg = np.linspace(-1,1,10)
zg = np.outer(xg,yg)

print(xg.shape)
print(yg.shape)
print(zg.shape)

x = m.Var(0)
y = m.Var(0)
z = m.Var(0)

m.bspline(x,y,z,xg,yg,zg,data=True)

m.solve()

Case 2: x is larger than y

from gekko import gekko
import numpy as np

m = gekko(remote=False)

xg = np.linspace(-1,1,15)
yg = np.linspace(-1,1,10)
zg = np.outer(xg,yg)

print(xg.shape)
print(yg.shape)
print(zg.shape)

x = m.Var(0)
y = m.Var(0)
z = m.Var(0)

m.bspline(x,y,z,xg,yg,zg,data=True)

m.solve()

Screenshot from 2019-08-30 10-33-35

Case 3: y is larger than x

from gekko import gekko
import numpy as np

m = gekko(remote=False)

xg = np.linspace(-1,1,10)
yg = np.linspace(-1,1,15)
zg = np.outer(xg,yg)

print(xg.shape)
print(yg.shape)
print(zg.shape)

x = m.Var(0)
y = m.Var(0)
z = m.Var(0)

m.bspline(x,y,z,xg,yg,zg,data=True)

m.solve()

Screenshot from 2019-08-30 10-35-13

APMonitor commented 4 years ago

I fixed the bug with APM v0.9.2 that is updated in Github for Windows and will be updated on the next release cycle. You can use the new local EXE now by updating your distribution at: Python37\Lib\site-packages\gekko\bin

New Local Executable https://github.com/BYU-PRISM/GEKKO/blob/master/gekko/bin/apm.exe

ChangeLog https://github.com/BYU-PRISM/GEKKO/blob/master/CHANGELOG.md

I also added some new features (smoothing factor, order of fit) to gekko.py:

    Generate a 2D Bspline with continuous first and seconds derivatives
    from 1-D arrays of x_data and y_data coordinates (in strictly ascending order)
    and 2-D z data of size (x.size,y.size). GEKKO variables x, y and z are 
    linked with function z=f(x,y) where the function f is a bspline.

    Usage: m.bspline(x,y,z,x_data,y_data,z_data,data=True,kx=3,ky=3,sf=None)
    Inputs:
      x,y = independent Gekko parameters or variables as predictors for z
      z   = dependent Gekko variable with z = f(x,y)
      If data is True (default) then the bspline is built from data
        x_data = 1D list or array of x values, size (nx)
        y_data = 1D list or array of y values, size (ny)
        z_data = 2D list or matrix of z values, size (nx,ny)
      If data is False then the bspline knots and coefficients are loaded
        x_data = 1D list or array of x knots, size (nx)
        y_data = 1D list or array of y knots, size (ny)
        z_data = 2D list or matrix of c coefficients, size (nx-kx-1)*(ny-ky-1)

      kx = degree of spline in x-direction, default=3
      ky = degree of spline in y-direction, default=3
      sf = smooth factor (sf), only for data=True
        sf controls the tradeoff between smoothness and closeness of fit
        if sf is small, the approximation may follow too much signal noise
        if sf is large, the approximation does not follow the general trend
        a proper sf depends on the data and level of noise
        when sf is None a default value of nx*ny*(0.1)**2 is used
        where 0.1 is the approximate statistical error of each point
        the sf is only used when constructing the bspline (data=True)
    Outputs:
      None