首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用gekko进行Python优化

使用gekko进行Python优化
EN

Stack Overflow用户
提问于 2020-10-23 06:06:39
回答 1查看 155关注 0票数 2

我第一次使用gekko对python进行优化。我没有多少使用python的经验,但我知道一些基础知识。当我运行优化时,我得到错误代码-13:

代码语言:javascript
复制
#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))

返回的错误为:

代码语言:javascript
复制
  File "/Users/username/Documents/me 46200/optimization/cyclone optimization/cyclone_optimization.py", line 45, in <module>
    m.solve()

  File "/Users/username/opt/anaconda3/lib/python3.8/site-packages/gekko/gekko.py", line 2174, in solve
    raise Exception(response)

Exception:  @error: Solution Not Found

我敢肯定这是我的另一个新手。如有任何帮助,我们将不胜感激:)

EN

回答 1

Stack Overflow用户

发布于 2020-10-25 13:54:40

IPOPT求解器错误为:

代码语言:javascript
复制
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上设置一个下限,以避免被零除。这里是你的问题的修改版本,成功地解决了。

代码语言:javascript
复制
#import Gekko optimization package
from gekko import gekko
import math

#create gekko model
m = gekko()
m.options.SOLVER=1

#constants
pi = math.pi
densityParticle = 10000 #kg/m**3
densityGas = 1.225 #kg/m**3
gravity = 9.806 #m/s^2

#initialize needed variables
lower = 1e-3
empirical = m.Var(value=1,lb=lower)
widthInlet = m.Var(value=1,lb=lower) #in meters
heightInlet = m.Var(value=1,lb=lower) #in meters
diameterOutlet = m.Var(value=1,lb=lower) #in meters
viscosity = m.Var(value=1,lb=lower) #kg/m*s
velocityInlet = m.Var(value=1,lb=lower) #m/s
lengthCone = m.Var(value=1,lb=lower) #in meters
lengthCylinder = m.Var(value=1,lb=lower) #in meters

frictionLoss = m.Var(lb=lower)
diameterCut = m.Var(lb=lower)
turns = m.Var(lb=lower)
separation = m.Var(lb=lower)

#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.Maximize(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[0]))
print('the optimized empirical constant is: ' + str(empirical.value[0]))
print('the optimized inlet width is: ' + str(widthInlet.value[0]))
print('the optimized inlet height is: ' + str(heightInlet.value[0]))
print('the optimized outlet diameter is: ' + str(diameterOutlet.value[0]))
print('the optimized cut diameter is: ' + str(diameterCut.value[0]))
print('the optimized viscosity is: ' + str(viscosity.value[0]))
print('the optimized number of turns is: ' + str(turns.value[0]))
print('the optimized inlet velocity is: ' + str(velocityInlet.value[0]))
print('the optimized particle density is: ' + str(densityParticle))
print('the optimized gas density is: ' + str(densityGas))
print('the optimized cone length is: ' + str(lengthCone.value[0]))
print('the optimized cylinder length is: ' + str(lengthCylinder.value[0]))

解决方案是:

代码语言:javascript
复制
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

Design Optimization course中还有与您的问题相关的其他信息,如two bar truss problem

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64491175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档