使用 m.time 随时间使用其先前值更新 gekko 变量

时间:2021-04-05 12:49:32

标签: python optimization regression nonlinear-optimization gekko

考虑 gekko 中的方程,其中包含 MV、m.Var、m.CV 和 m.Param 矩阵,其中包括基于非线性回归的最大化问题。

如何将时间相关决策变量“covar”添加到当前代码中,以便:

covar(在时间 t) = [1, gekko_attr1 ** attr1_pow, gekko_attr2 ** attr2_pow, gekko_attr3 ** attr3_pow] * covar(在时间 t-1) * Transpose([1, gekko_attr1 ** attr1_pow, * attr2_pow, gekko_attr3 ** attr3_pow]) + 常数

数据集:

https://drive.google.com/file/d/10tVHghTvRThsDfyzprINdlVTtJTUbSyQ/view?usp=sharing

def gekko_fun():

    xm1 = np.array(data['Attr1'])
    xm2 = np.array(data['Attr2'])
    xm3 = np.array(data['Attr3'])
    ym = np.array(data['Result'])

    m = GEKKO(remote=False)

    # parameters
    gekko_attr1 = m.Param(value = xm1)
    gekko_attr2 = m.Param(value = xm2)
    gekko_attr3 = m.Param(value = xm3)

    intercept = m.MV(lb=0, name="Intercept")
    attr1_multiplier = m.MV(lb=0)
    attr2_multiplier = m.MV(lb=0)
    attr3_multiplier = m.MV(lb=0)

    intercept_lambda = m.MV(lb=0, ub = 1)
    attr1_lambda = m.MV(lb=0, ub = 1)
    attr2_lambda = m.MV(lb=0, ub = 1)
    attr3_lambda = m.MV(lb=0, ub = 1)

    attr1_pow = m.MV(lb=0, ub=1)
    attr2_pow = m.MV(lb=0, ub=1)
    attr3_pow = m.MV(lb=0, ub=1)
    
    intercept.STATUS = 1
    attr1_multiplier.STATUS = 1
    attr2_multiplier.STATUS = 1
    attr3_multiplier.STATUS = 1
    
    intercept_lambda.STATUS = 1
    attr1_lambda.STATUS = 1
    attr2_lambda.STATUS = 1
    attr3_lambda.STATUS = 1

    attr1_lambda_pow.STATUS = 1
    attr2_lambda_pow.STATUS = 1
    attr3_lambda_pow.STATUS = 1
    
    control_value = m.CV(value=ym)
    control_value.STATUS=1

    pred_value = m.Var
    error = m.Var

    maximized_value = m.Var

    m.options.IMODE = 2
    m.options.MAX_ITER = 999

    m.Equation(pred_value == (
        np.dot(
            [[1, gekko_attr1**attr1_pow, gekko_attr2**attr2_pow, gekko_attr3**attr3_pow]]
            ,#multiply
            np.add(
                np.dot(
                    [
                        [intercept_lambda,0,0,0],
                        [0,attr1_lambda,0,0],
                        [0,0,attr2_lambda,0],
                        [0,0,0,attr3_lambda]
                    ]                                                  
                    ,#multiply
                    [
                        [intercept],
                        [attr1_multiplier],
                        [attr2_multiplier],
                        [attr3_multiplier]
                    ]
                ),#add
                np.dot(
                    [
                        [attr1_multiplier,attr2_multiplier,attr3_multiplier],
                        [attr1_multiplier,0,0],
                        [0,attr2_multiplier,0],
                        [0,0,attr3_multiplier]
                    ],#multiply
                    [
                        [gekko_attr1**attr1_pow],
                        [gekko_attr2**attr2_pow],
                        [gekko_attr3**attr3_pow]
                    ]
                )
            )
        )
    ))
    
    m.Equation(error==control_value-pred_value)

    m.Equation(maximized_value == -m.log(covar) - covar*(error)**2)
    
    m.Maximize(maximized_value)

我是 Gekko 的新手,如果我能得到一些帮助来解决这个问题,我将不胜感激。 感谢您抽出宝贵时间。

1 个答案:

答案 0 :(得分:0)

如果存在离散时间方程而不是连续时间方程(例如微分方程),则使用 IMODE=3(默认)并使用数组构建变量。这是 x[t] = x[t-1]+1 的示例问题。

from gekko import GEKKO
m = GEKKO()
x = m.Array(m.Var,10)
for i in range(9):
    m.Equation(x[i+1]==x[i]+1) 
m.Minimize((x[9]-10)**2)
m.solve()
print(x)

解决方案是:

 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   5.399999994551763E-003 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------
 
[[1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0] [10.0]]

Numpy 矩阵运算也可以用于 Gekko 数组。下面是一个例子:

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)

这是另一个例子:

import numpy as np
from gekko import GEKKO

m = GEKKO(remote=False)

# Random 3x3
A = np.random.rand(3,3)
# Random 3x1
b = np.random.rand(3,1)
# Gekko array 3x3
p = m.Array(m.Param,(3,3))
# Gekko array 3x1
y = m.Array(m.Var,(3,1))

# Dot product of A p
x = np.dot(A,p)
# Dot product of x y
w = np.dot(x,y)
# Dot product of p y
z = np.dot(p,y)
# Trace (sum of diag) of p
t = np.trace(p)

# solve Ax = b
s = m.axb(A,b)
m.solve()

对于以后的问题,请发布 Minimal, Reproducible example 以更好地诊断问题。