使用Gekko进行Python优化

2022-04-05 00:00:00 python python-3.x spyder optimization 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()
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))

返回的错误为:


  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

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


解决方案

IPOPT求解器错误为:

EXIT: Invalid number in NLP function or derivative detected.

 An error occured.
 The error code is          -13
NaN由于被零除而求值时,通常会发生这种情况。您可以改写x==1/ytox*y==1之类的公式,也可以给y设置一个下限,以避免被零除。以下是成功解决您的问题的修改版本。

#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]))

解决方案是:

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。

相关文章