Python Optimization using gekko

回眸只為那壹抹淺笑 提交于 2021-02-08 04:10:07

问题


I'm using gekko for the first time to do optimization with python. I don't have a lot of experience with python, but I know the basics. I get error code -13 when I run the optimization:

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

The error returned is:


  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

I'm sure this is another rookie fudge up of mine. Any assistance would be appreciated :)


回答1:


The IPOPT solver error is:

EXIT: Invalid number in NLP function or derivative detected.

 An error occured.
 The error code is          -13

This typically occurs when there is a NaN evaluated because of divide by zero. You can either reformulate equations such as x==1/y to x*y==1 or else put a lower bound on y to avoid divide by zero. Here is a modified version of your problem that solves successfully.

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

The solution is:

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

There is additional information in the Design Optimization course such as the two bar truss problem that is related to your problem.



来源:https://stackoverflow.com/questions/64491175/python-optimization-using-gekko

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!