使用gekko的Python优化
我第一次使用gekko对python进行优化。我对python没有太多经验,但我知道基本知识。运行优化时,我得到错误代码-13:使用gekko的Python优化,python,python-3.x,optimization,spyder,gekko,Python,Python 3.x,Optimization,Spyder,Gekko,我第一次使用gekko对python进行优化。我对python没有太多经验,但我知道基本知识。运行优化时,我得到错误代码-13: #import Gekko optimization package from gekko import gekko import math #create gekko model m = gekko() #constants pi = math.pi #initialize needed variables frictionLoss = m.Var() emp
#import Gekko optimization package
from gekko import gekko
import math
#create gekko model
m = gekko()
#constants
pi = math.pi
#initialize needed variables
frictionLoss = m.Var()
empirical = m.Var(value=1)
widthInlet = m.Var(value=1) #in meters
heightInlet = m.Var(value=1) #in meters
diameterOutlet = m.Var(value=1) #in meters
diameterCut = m.Var()
viscosity = m.Var(value=1) #kg/m*s
turns = m.Var()
velocityInlet = m.Var(value=1) #m/s
densityParticle = 10000 #kg/m**3
densityGas = 1.225 #kg/m**3
lengthCone = m.Var(value=1) #in meters
lengthCylinder = m.Var(value=1) #in meters
gravity = 9.806 #m/s^2
separation = m.Var()
#define box equations
m.Equation(frictionLoss==empirical*widthInlet*heightInlet/diameterOutlet**2)
m.Equation(turns==((pi*(2*lengthCylinder - lengthCone))/heightInlet))
m.Equation(diameterCut==((9*viscosity*widthInlet)/(2*pi*turns*velocityInlet*(densityParticle-densityGas)))**.5)
m.Equation(separation==((velocityInlet**2)/((diameterCut/2 )+ gravity)))
#add constraint on surface area
#m.Equation(separation<=.9)
#define object function (negative to maximize instead of minimize)
m.Obj(-separation)
#set mode to steady state optimization (solution does not change with time)
m.options.IMODE = 3
m.solve()
#print results
print('the optimized friction loss is: ' + str(frictionLoss.value))
print('the optimized empirical constant is: ' + str(empirical.value))
print('the optimized inlet width is: ' + str(widthInlet.value))
print('the optimized inlet height is: ' + str(heightInlet.value))
print('the optimized outlet diameter is: ' + str(diameterOutlet.value))
print('the optimized cut diameter is: ' + str(diameterCut.value))
print('the optimized viscosity is: ' + str(viscosity.value))
print('the optimized number of turns is: ' + str(turns.value))
print('the optimized inlet velocity is: ' + str(velocityInlet.value))
print('the optimized particle density is: ' + str(densityParticle.value))
print('the optimized gas density is: ' + str(densityGas.value))
print('the optimized cone length is: ' + str(lengthCone.value))
print('the optimized cylinder length is: ' + str(lengthCylinder.value))
#导入Gekko优化包
从gekko进口gekko
输入数学
#创建gekko模型
m=gekko()
#常数
pi=math.pi
#初始化所需的变量
摩擦损失=m.Var()
经验=m.Var(值=1)
宽度入口=m.Var(值=1)#以米为单位
高度入口=m.Var(值=1)#以米为单位
直径出口=m.Var(值=1)#以米为单位
直径切割=m.Var()
粘度=m.Var(值=1)#kg/m*s
匝数=m.Var()
velocityInlet=m.Var(值=1)#m/s
密度颗粒=10000公斤/米**3
密度气体=1.225千克/米**3
长度锥=m.Var(值=1)#以米为单位
长度圆柱=m.Var(值=1)#以米为单位
重力=9.806米/秒^2
分离=m.Var()
#定义长方体方程
m、 方程(摩擦损失==经验*宽度入口*高度入口/直径出口**2)
m、 方程式(圈数==((pi*(2*长度圆柱-长度圆锥))/高度入口)
m、 方程式(直径切割==((9*粘度*宽度入口)/(2*π*圈数*速度入口*(密度粒子密度气体))**.5)
m、 方程(分离==((速度小**2)/((直径切割/2)+重力)))
#在表面积上添加约束
#m、 方程(分离IPOPT解算器误差为:
EXIT: Invalid number in NLP function or derivative detected.
An error occured.
The error code is -13
这通常发生在由于被零除而计算的NaN
时。您可以将x==1/y
等方程重新格式化为x*y==1
,或者在y
上设置一个下限以避免被零除。以下是成功解决问题的修改版本
#导入Gekko优化包
从gekko进口gekko
输入数学
#创建gekko模型
m=gekko()
m、 选项。解算器=1
#常数
pi=math.pi
密度颗粒=10000公斤/米**3
密度气体=1.225千克/米**3
重力=9.806米/秒^2
#初始化所需的变量
下=1e-3
经验=m.Var(值=1,磅=更低)
宽度入口=m.Var(值=1,磅=较低)#以米为单位
高度入口=m.Var(值=1,磅=更低)#以米为单位
直径出口=m.Var(值=1,磅=更低)#以米为单位
粘度=m.Var(值=1,磅=较低)#kg/m*s
velocityInlet=m.Var(值=1,磅=较低)#m/s
长度锥=m.Var(值=1,磅=较低)#以米为单位
长度气缸=m.Var(值=1,磅=较低)#以米为单位
摩擦损失=m.Var(lb=更低)
直径切割=m.Var(磅=更低)
匝数=m.Var(lb=较低)
分离=m.Var(lb=较低)
#定义长方体方程
m、 方程(摩擦损失==经验*宽度入口*高度入口/直径出口**2)
m、 方程式(圈数==((pi*(2*长度圆柱-长度圆锥))/高度入口)
m、 方程式(直径切割==((9*粘度*宽度入口)/(2*π*圈数*速度入口*(密度粒子密度气体))**.5)
m、 方程(分离==((速度小**2)/((直径切割/2)+重力)))
#在表面积上添加约束
m、 方程(分离)
EXIT: Invalid number in NLP function or derivative detected.
An error occured.
The error code is -13
apm 136.36.211.159_gk_model0 <br><pre> ----------------------------------------------------------------
APMonitor, Version 0.9.2
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 13
Intermediates: 0
Connections : 0
Equations : 6
Residuals : 6
Number of state variables: 13
Number of total equations: - 5
Number of slack variables: - 1
---------------------------------------
Degrees of freedom : 7
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter Objective Convergence
0 2.16410E-01 8.99000E-01
1 -4.04256E-01 2.98848E-01
2 -9.00000E-01 7.23825E-02
3 -8.89266E-01 9.38042E-02
4 -8.90825E-01 2.39141E-01
5 -8.96687E-01 4.43769E-02
6 -8.99389E-01 1.59439E-02
7 -9.00000E-01 5.84573E-03
8 -9.00000E-01 2.35805E-10
9 -9.00000E-01 2.35805E-10
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 1.410000000032596E-002 sec
Objective : -0.900000000000000
Successful solution
---------------------------------------------------
the optimized friction loss is: 0.21599357118
the optimized empirical constant is: 0.97878134972
the optimized inlet width is: 0.84720656046
the optimized inlet height is: 0.32475560886
the optimized outlet diameter is: 1.1165943231
the optimized cut diameter is: 0.05806387037
the optimized viscosity is: 0.99182260906
the optimized number of turns is: 0.012001017114
the optimized inlet velocity is: 2.9751518855
the optimized particle density is: 10000
the optimized gas density is: 1.225
the optimized cone length is: 1.2088239788
the optimized cylinder length is: 0.60503227947