GEKKO是一个用于动态系统建模和优化的Python库。可以轻松地定义和求解各种非线性最优化、动态系统和控制问题。该库能够处理复杂的工业和科学问题。本文介绍GEKKO的基本使用。
1基础使用
安装gekko
首先,需要在您的计算机上安装GEKKO库。可以使用以下命令来安装GEKKO:
pip install gekko
安装完成后,可以通过以下导入语句引入GEKKO库:
from gekko import GEKKO
模型定义
在GEKKO中,可以使用GEKKO对象来定义和解决优化问题。首先创建一个GEKKO对象:
m = GEKKO()
然后,可以通过调用GEKKO对象的方法来定义模型变量、约束条件和目标函数。
定义变量
在GEKKO中,可以使用
m.Var()
方法来定义模型变量。该方法有多种参数设置,可以设置变量的初始值、上限、下限等。
x = m.Var(value=0, lb=0, ub=10)
约束条件
可以使用
m.Equation()
该方法接受等式或不等式作为参数。
m.Equation(x ** 2 >= 8)
目标函数
可以使用
m.Obj()
该方法接受一个表达式作为参数。
m.Obj(x ** 2)
也可以使用
model.Maximize(x ** 2)#求解最大值
求解器的使用
GEKKO提供了多种求解器,可以根据具体的问题选择合适的求解器。下面介绍两种常用的求解器:APOPT和BPOPT。
使用APOPT求解器可以通过以下代码来实现:
m.options.Solver = 1
m.solve()
使用BPOPT求解器可以通过以下代码来实现:
m.options.Solver = 2
m.solve()
案例
下面通过一个简单的案例来演示GEKKO的使用。假设有一个化学反应器,需要确定最佳的温度和反应物浓度来最大化产物的产量。可以使用以下代码来定义和求解该问题:
from gekko import GEKKO
# 创建GEKKO对象
model = GEKKO()
# 定义变量
T = model.Var(value=300, lb=200, ub=500)
C = model.Var(value=1, lb=0, ub=10)
# 定义约束条件
model.Equation(T * C >= 100)
# 定义目标函数
model.Maximize(T * C)
# 设置求解器
model.options.Solver = 1
# 求解
model.solve()
# 输出结果
print("Temperature:", T.value[0])
print("Concentration:", C.value[0])
print("Maximum Yield:", -model.options.objfcnval)
在上面的代码中,我们首先创建了一个GEKKO对象model,然后定义了两个变量 T和C,分别代表温度和反应物浓度。
接着,定义了一个约束条件和一个目标函数。然后,设置了APOPT求解器并求解问题。最后,输出了求解结果。
2更多案例
GEKKO建模求解非常方便,本章节将介绍更多求解案例。
求解线性方程
from gekko import GEKKO
m = GEKKO() # 建立模型
x = m.Var() # 定义变量
y = m.Var() # 定义变量
m.Equations([3*x+2*y==1, x+2*y==0]) # 约束
m.solve(disp=False) # 求解,disp是确定是否显示求解细节,False是不显示
print(x.value,y.value) # 打印输出
输出结果:
[0.5] [-0.25]
如果要显示求解过程,以上代码可以修改为:
from gekko import GEKKO
m = GEKKO() # 建立模型
x = m.Var() # 定义变量
y = m.Var() # 定义变量
m.Equations([3*x+2*y==1, x+2*y==0]) # 约束
m.solve(disp=True) # 求解,disp是确定是否显示求解细节,False是不显示
print(x.value,y.value) # 打印输出
详细显示求解过程。
apm 59.172.223.162_gk_model14
----------------------------------------------------------------
APMonitor, Version 1.0.1
APMonitor Optimization Suite
----------------------------------------------------------------
Warning: there is insufficient data in CSV file 59.172.223.162_gk_model14.csv
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 2
Intermediates: 0
Connections : 0
Equations : 2
Residuals : 2
Number of state variables: 2
Number of total equations: - 2
Number of slack variables: - 0
---------------------------------------
Degrees of freedom : 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 ma57.
Number of nonzeros in equality constraint Jacobian...: 4
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 0
Total number of variables............................: 2
variables with only lower bounds: 0
variables with lower and upper bounds: 0
variables with only upper bounds: 0
Total number of equality constraints.................: 2
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 0.0000000e+00 1.00e+00 0.00e+00 0.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 0.0000000e+00 0.00e+00 0.00e+00 -11.0 5.00e-01 - 1.00e+00 1.00e+00h 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 = 2
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 2
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 : 5.000000000109139E-003 sec
Objective : 0.000000000000000E+000
Successful solution
---------------------------------------------------
[0.5] [-0.25]
求解混合整数非线性规划模型
混合整数非线性规划模型在实际业务中是很难求解的,这里给出gekko一个很好的求解案例。
from gekko import GEKKO
m = GEKKO() # Initialize gekko
m.options.SOLVER=1 # APOPT is an MINLP solver
# optional solver settings with APOPT
m.solver_options = ['minlp_maximum_iterations 500', \
# minlp iterations with integer solution
'minlp_max_iter_with_int_sol 10', \
# treat minlp as nlp
'minlp_as_nlp 0', \
# nlp sub-problem max iterations
'nlp_maximum_iterations 50', \
# 1 = depth first, 2 = breadth first
'minlp_branch_method 1', \
# maximum deviation from whole number
'minlp_integer_tol 0.05', \
# covergence tolerance
'minlp_gap_tol 0.01']
# Initialize variables
x1 = m.Var(value=1,lb=1,ub=5)
x2 = m.Var(value=5,lb=1,ub=5)
# Integer constraints for x3 and x4
x3 = m.Var(value=5,lb=1,ub=5,integer=True)
x4 = m.Var(value=1,lb=1,ub=5,integer=True)
# Equations
m.Equation(x1*x2*x3*x4>=25)
m.Equation(x1**2+x2**2+x3**2+x4**2==40)
m.Obj(x1*x4*(x1+x2+x3)+x3) # Objective
m.solve(disp=False) # Solve
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('x4: ' + str(x4.value))
print('Objective: ' + str(m.options.objfcnval))
输出结果
Results
x1: [1.3589086474]
x2: [4.5992789966]
x3: [4.0]
x4: [1.0]
Objective: 17.532267301
本文介绍了GEKKO的基本使用,包括安模型定义和求解器的使用。通过GEKKO库,可以轻松地定义和求解各种非线性优化问题。