如何将 gekko 对象插入/附加到现有列表/数组?

Posted

技术标签:

【中文标题】如何将 gekko 对象插入/附加到现有列表/数组?【英文标题】:How can I insert/append gekko objects to an existing list/array? 【发布时间】:2021-12-08 23:52:41 【问题描述】:

我有如下所示的示例代码,我需要在其中求解一些 Gekko 变量并将它们附加到数组中。但是,当我尝试追加时,它会引发错误AttributeError: 'GEKKO' object has no attribute 'append'。根据我在 gekko 用户手册中阅读的内容,append 函数适用于列表,但是当我转换 gekko 变量 cpx[ind] , 和 power 到列表(包含 gekko 运算符),使用 [] 方法,它会抛出相同的错误。任何有关如何解决此问题的帮助将不胜感激。示例代码如下:

m = GEKKO(remote=True)
list1 = [2, 4, 6, 8, 10]
velocity = 10
beta = 0.7850                          
yaw_init = 0
rotor_rad = 64                          
rho = 1.2253
axi = 0.230742                            
rot_surf_area = np.pi * (rotor_rad)**2
c_pow_free = 4 * axi * (m.cos((beta * yaw_init * np.pi)/180) - axi)**2 
p_max = 0.5 * rho * rot_surf_area * c_pow_free * velocity**3


# #initialize variables, Set lower and upper bounds
x = [m.Var(value = 5.02525317e-03, lb = 0.01, ub = axial_max) for i in range(len(list1))]

powers = list()
for ind, i in enumerate(list1):
    m.Equation(x[ind] - axial_max <= 0)
    cp = 4 * x[ind] * (m.cos((beta * yaw_init * np.pi)/180) - x[ind])**2       
    ct = 4 * x[ind] * (m.cos((beta * yaw_init * np.pi)/180) - x[ind])              
    power = 0.5 * rho * rot_surf_area * cp * velocity**3
    powers.append(power)                       
    m.Equation((0.5 * rho * rot_surf_area * cp * velocity**3) - p_max <= 0)
    ct = [ct]
    x = [x]
    power = [power]
    arr1 = np.array(i)
    arr2 = np.insert(arr1, 0, yaw_init)
    arr3 = m.append(arr2, 1, ct)
    arr4 = m.append(arr3, 2, x[ind])
    arr5 = m.append(arr4, 3, power)

y = sum(powers)
m.Maximize(y)  # Maximize
m.options.IMODE = 3 #steady state optimization
m.options.SOLVER = 3
m.solver_options = ['linear_solver mumps','mu_strategy adaptive','max_iter 10000', 'tol 1.0e-5' ]
m.solve()
x = np.array(x)
print(x, m.options.objfcnval)

【问题讨论】:

【参考方案1】:

Gekko 变量可以附加到列表中,如下所示:

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

my_list = []
my_list.append(x)
my_list.append(y)

arr1arr5 的用途尚不清楚,因为它们没有在其他地方使用。这是成功运行的代码,将 my_list 构建为 Gekko 变量或表达式列表。

from gekko import GEKKO
import numpy as np
m = GEKKO(remote=True)
list1 = [2, 4, 6, 8, 10]
velocity = 10
axial_max = 1
beta = 0.7850                          
yaw_init = 0
rotor_rad = 64                          
rho = 1.2253
axi = 0.230742                            
rot_surf_area = np.pi * (rotor_rad)**2
c_pow_free = 4 * axi * (m.cos((beta * yaw_init * np.pi)/180) - axi)**2 
p_max = 0.5 * rho * rot_surf_area * c_pow_free * velocity**3


# #initialize variables, Set lower and upper bounds
n = len(list1)
x = m.Array(m.Var,n,value=5.02525317e-03,lb=0.01,ub=axial_max)

powers = list()
for ind, i in enumerate(list1):
    m.Equation(x[ind] - axial_max <= 0)
    cp = 4 * x[ind] * (m.cos((beta * yaw_init * np.pi)/180) - x[ind])**2       
    ct = 4 * x[ind] * (m.cos((beta * yaw_init * np.pi)/180) - x[ind])              
    power = 0.5 * rho * rot_surf_area * cp * velocity**3
    powers.append(power)                      
    m.Equation((0.5 * rho * rot_surf_area * cp * velocity**3) - p_max <= 0)

    my_list = [yaw_init]
    my_list.append(ct)
    my_list.append(x[ind])
    my_list.append(power)
    
y = sum(powers)
m.Maximize(y)  # Maximize
m.options.IMODE = 3 #steady state optimization
m.options.SOLVER = 3
m.solver_options = ['linear_solver mumps','mu_strategy adaptive','max_iter 10000', 'tol 1.0e-5' ]
m.solve()
x = np.array(x)
print(x, m.options.objfcnval)

这给出了一个成功的解决方案:

 APMonitor, Version 1.0.1
 APMonitor Optimization Suite
 ----------------------------------------------------------------
 
 
 --------- APM Model Size ------------
 Each time step contains
   Objects      :            0
   Constants    :            0
   Variables    :           15
   Intermediates:            0
   Connections  :            0
   Equations    :           11
   Residuals    :           11
 
 Number of state variables:             15
 Number of total equations: -           10
 Number of slack variables: -           10
 ---------------------------------------
 Degrees of freedom       :             -5
 
 * Warning: DOF <= 0
 **********************************************
 Steady State Optimization with Interior Point Solver
 **********************************************
  
  
 Info: Exact Hessian

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
 Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

This is Ipopt version 3.12.10, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

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

Total number of variables............................:       15
                     variables with only lower bounds:       10
                variables with lower and upper bounds:        5
                     variables with only upper bounds:        0
Total number of equality constraints.................:       10
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 -3.0140184e+06 3.70e+06 1.00e+00   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1 -1.6888208e+07 9.28e+05 8.43e+01  -6.6 8.43e-01    -  1.16e-02 1.00e+00f  1
   2 -2.0626346e+07 1.81e+05 9.23e+00  -1.3 6.19e-02    -  9.90e-01 1.00e+00f  1
   3 -2.1448380e+07 1.61e+04 1.35e+00  -3.5 3.31e-02    -  9.90e-01 1.00e+00f  1
   4 -2.1528033e+07 1.84e+02 3.00e-02  -4.5 3.24e-01    -  1.00e+00 1.00e+00h  1
   5 -2.1528954e+07 2.51e-02 8.18e-06  -6.2 2.36e-01    -  1.00e+00 1.00e+00h  1
   6 -2.1528955e+07 3.27e-08 2.88e-12 -11.0 1.84e-01    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 6

                                   (scaled)                 (unscaled)
Objective...............:  -6.9666792441078528e+01   -2.1528954985702936e+07
Dual infeasibility......:   2.8790992163187654e-12    8.8972084483325336e-07
Constraint violation....:   1.0578034152693821e-13    3.2689034923372162e-08
Complementarity.........:   1.1324470786964081e-11    3.4995729423836697e-06
Overall NLP error.......:   1.1324470786964081e-11    3.4995729423836697e-06


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

EXIT: Optimal Solution Found.
 
 The solution was found.
 
 The final value of the objective function is   -21528954.9857029     
 
 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   1.219999999739230E-002 sec
 Objective      :   -21528954.9857029     
 Successful solution
 ---------------------------------------------------
 
[[0.230742] [0.230742] [0.230742] [0.230742] [0.230742]] -21528954.986

【讨论】:

太棒了!!工作得很好。谢谢你。抱歉,arr1arr5 只是按照原始代码中的方式进行了改编。忘记脱了。 我知道这与上一个问题无关,但请允许我通过评论提问。解决了上面的代码部分后,我需要对 Gekko 变量执行具有上限和下限的双积分,老实说,我什至不知道如何开始。我看过以前关于积分的问题,但所有问题都是关于没有下限或上限的单积分。你能给出任何简单表达式的双积分(外积分和内积分都有上限和下限)的样本解吗?我肯定能够适应我的情况。谢谢。 积分是用m.integral(x) 定义的,所以双积分是m.integral(m.integral(x))。如果您需要在内部积分上定义约束,则将y=integral(x) 定义为等式和z=integral(y)。使用y=m.Var(lb=0.5,ub=10) 和 z=m.Var(lb=-5,5)`。如果第二个积分是关于不同变量的,这里还有关于 PDE 的信息:apmonitor.com/do/index.php/Main/PartialDifferentialEquations 衷心感谢您的指导。实施后我会发表评论。

以上是关于如何将 gekko 对象插入/附加到现有列表/数组?的主要内容,如果未能解决你的问题,请参考以下文章

如何将NSMutable数组附加到现有文件中

如何将数据附加到 MERN 堆栈中 mongodb 中的现有数组

如何在 Java DocumentBuilder 中解析 XSD 文件期间将元素附加到现有节点列表

以角度将行附加到现有数据列表

如何附加到PHP中的现有数组值[重复]

为啥在将项目附加到现有列表时会得到“无”项目? [复制]