Inner product of a Gekko Array with a Numpy Array

617 views Asked by At

I am trying to use np.inner to perform inner product of a numpy array of dimension (30,34) with Gekko Array of dimension (34,34) in Gekko equation but it is throwing error that "equation without an equality (=) or inequality (>,<)". Is it not allowed to use numpy functions in Gekko equations? If not, then what is the alternative to perform operations like np.inner, np.diag etc?

1

There are 1 answers

0
John Hedengren On

Numpy operations like np.diag and np.inner are allowed with Gekko arrays. The qualification is that you need the result to be symbolically evaluated by Gekko for automatic differentiation so certain functions are not allowed. Here is an example with np.dot and summation functions.

from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
ni = 3; nj = 2; nk = 4
# solve AX=B
A = m.Array(m.Var,(ni,nj),lb=0)
X = m.Array(m.Var,(nj,nk),lb=0)
AX = np.dot(A,X)
B = m.Array(m.Var,(ni,nk),lb=0)
# equality constraints
m.Equations([AX[i,j]==B[i,j] for i in range(ni) \
                             for j in range(nk)])
m.Equation(5==m.sum([m.sum([A[i][j] for i in range(ni)]) \
                                    for j in range(nj)]))
m.Equation(2==m.sum([m.sum([X[i][j] for i in range(nj)]) \
                                    for j in range(nk)]))
# objective function
m.Minimize(m.sum([m.sum([B[i][j] for i in range(ni)]) \
                                 for j in range(nk)]))
m.solve()
print(A)
print(X)
print(B)

Here is another example with np.trace() to define the objective function: Gekko optimization package and numpy inverse function I recommend that you try a minimal example and modify your question if you run into any problems.