我有下面所示的示例代码片段,我需要解决一些Gekko变量并将它们附加到数组中。然而,当我尝试追加,它抛出一个错误AttributeError: 'GEKKO' object has no attribute 'append'
。从我在gekko用户手册中读到的,附加函数与列表一起工作,但是当我转换gekko变量cp时x(印第安纳州),,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)
Gekko变量可以像下面这样添加到列表中:
x = m.Var()
y = m.Var()
my_list = []
my_list.append(x)
my_list.append(y)
arr1
到arr5
的目的是不清楚的,因为它们没有在其他地方使用。下面是成功运行的代码,将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