Python Optimization Using Gekko
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 optimiza
Solution 1:
The IPOPT solver error is:
EXIT: Invalid number in NLP functionor 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 packagefrom 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 resultsprint('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:
apm136.36.211.159_gk_model0<br><pre>----------------------------------------------------------------APMonitor,Version0.9.2APMonitorOptimizationSuite-------------------------------------------------------------------------APMModelSize------------EachtimestepcontainsObjects :0Constants :0Variables :13Intermediates:0Connections :0Equations :6Residuals :6Number of state variables:13Number of total equations:-5Number of slack variables:-1---------------------------------------Degrees of freedom :7----------------------------------------------SteadyStateOptimizationwithAPOPTSolver----------------------------------------------IterObjectiveConvergence02.16410E-018.99000E-011-4.04256E-012.98848E-012-9.00000E-017.23825E-023-8.89266E-019.38042E-024-8.90825E-012.39141E-015-8.96687E-014.43769E-026-8.99389E-011.59439E-027-9.00000E-015.84573E-038-9.00000E-012.35805E-109-9.00000E-012.35805E-10Successfulsolution---------------------------------------------------Solver :APOPT(v1.0)Solution time :1.410000000032596E-002secObjective :-0.900000000000000Successfulsolution---------------------------------------------------the optimized friction loss is:0.21599357118the optimized empirical constant is:0.97878134972the optimized inlet width is:0.84720656046the optimized inlet height is:0.32475560886the optimized outlet diameter is:1.1165943231the optimized cut diameter is:0.05806387037the optimized viscosity is:0.99182260906the optimized number of turns is:0.012001017114the optimized inlet velocity is:2.9751518855the optimized particle density is:10000the optimized gas density is:1.225the optimized cone length is:1.2088239788the 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.
Post a Comment for "Python Optimization Using Gekko"