简体   繁体   English

Gekko 阵列的内积与 Numpy 阵列

[英]Inner product of a Gekko Array with a Numpy Array

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 (>,<)".我正在尝试使用np.inner来执行 numpy 数组的内积 (30,34) 与 Gekko 数组(34,34)在 Gekko 方程中,但它抛出错误“没有相等的方程 (=)或不等式 (>,<)”。 Is it not allowed to use numpy functions in Gekko equations?不允许在 Gekko 方程中使用 numpy 函数吗? If not, then what is the alternative to perform operations like np.inner , np.diag etc?如果不是,那么执行np.innernp.diag等操作的替代方法是什么?

Numpy operations like np.diag and np.inner are allowed with Gekko arrays. Gekko arrays 允许像np.diagnp.inner这样的 Numpy 操作。 The qualification is that you need the result to be symbolically evaluated by Gekko for automatic differentiation so certain functions are not allowed.条件是您需要 Gekko 对结果进行符号评估以进行自动微分,因此不允许使用某些功能。 Here is an example with np.dot and summation functions.这是一个带有np.dot和求和函数的示例。

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. 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM