在循环 GEKKO 中创建变量数组

Create arrays of variables in loop GEKKO

我想知道是否可以在 GEKKO 的循环中创建具有不同长度的变量数组。

下面只是一个简单的例子来说明我的意思。列表“长度”中的参数定义了每个 GEKKO 数组的长度:

lengths = [10,20,30]

m = GEKKO()

for i in lengths:
    # something...

所以我想从中得到类似的东西:

array1 = m.Array(m.Var,10)
array2 = m.Array(m.Var,20)
array3 = m.Array(m.Var,30)

在我试图解决的实际问题中,我想包含在优化中的数组会很多,并且它们可能会根据情况而有所不同。所以每次都手动创建它们不是一个好的选择。

循环定义数组没有问题。这是一个例子:

from gekko import GEKKO
model = GEKKO(remote=True)
lengths = [10,20,30]
m = GEKKO()
x = []
for i in lengths:
    x.append(m.Array(m.Var,i))
    for j in range(i):
        m.Minimize((x[-1][j]-j)**2)
        
m.solve()

for xi in x:
    print(xi)

这给出了一个唯一的解决方案,其中值等于索引。

This is Ipopt version 3.12.10, running with linear solver ma57.

Number of nonzeros in equality constraint Jacobian...:        0
Number of nonzeros in inequality constraint Jacobian.:        0
Number of nonzeros in Lagrangian Hessian.............:       60

Total number of variables............................:       60
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:        0
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  1.1310000e+04 0.00e+00 5.80e+01   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  0.0000000e+00 0.00e+00 0.00e+00 -11.0 2.90e+01    -  1.00e+00 1.00e+00f  1

Number of Iterations....: 1

                                   (scaled)                 (unscaled)
Objective...............:   0.0000000000000000e+00    0.0000000000000000e+00
Dual infeasibility......:   0.0000000000000000e+00    0.0000000000000000e+00
Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   0.0000000000000000e+00    0.0000000000000000e+00


Number of objective function evaluations             = 2
Number of objective gradient evaluations             = 2
Number of equality constraint evaluations            = 0
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 0
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 1
Total CPU secs in IPOPT (w/o function evaluations)   =      0.001
Total CPU secs in NLP function evaluations           =      0.000

EXIT: Optimal Solution Found.
 
 The solution was found.
 
 The final value of the objective function is   0.000000000000000E+000
 
 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   4.600000000209548E-003 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------
 
[[0.0] [1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0]]
[[0.0] [1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0] [10.0] [11.0]
 [12.0] [13.0] [14.0] [15.0] [16.0] [17.0] [18.0] [19.0]]
[[0.0] [1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0] [10.0] [11.0]
 [12.0] [13.0] [14.0] [15.0] [16.0] [17.0] [18.0] [19.0] [20.0] [21.0]
 [22.0] [23.0] [24.0] [25.0] [26.0] [27.0] [28.0] [29.0]]