Reputation: 95
I am optimizing plane flight using optimal control. The plane flies certain distance (path
variable) and then the simulation stops. The solver is trying to minimize the fuel consumption m.Maximize(mass*tf*final)
, by maximizing the mass value.
I have added 2 solver controlled variables:
throttle control, like this:
Tcontr = m.MV(value=0.2,lb=0.2,ub=1)
Tcontr.STATUS = 1
Tcontr.DCOST = 0
and simulation time, like this:
tf = m.FV(value=1,lb=0.0001,ub=1000.0)#
tf.STATUS = 1
And the system worked as intended. After that, I tried to implement one more controlled variable, bank angle control, that looks like this:
Mu = m.MV(value=0,lb=-1.5,ub=1.5)
Mu.STATUS = 1
Mu.DCOST = 0
But for some reason, the program says that "Mu" is not defined.
How do I define Mu solver controlled variable? How do I define next solver controlled variables?
My code:
import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO
import math
#Gekko model
m = GEKKO(remote=False)
#Time points
nt = 11
tm = np.linspace(0,100,nt)
m.time = tm
# Variables
Ro = m.Var(value=1.1)#air density
g = m.Const(value=9.80665)
pressure = m.Var(value=101325)#
T = m.Var(value=281)#temperature
T0 = m.Const(value=288)#temperature at see level
S = m.Const(value=122.6)
Cd = m.Const(value=0.1)#drag coef
Cl = m.Var(value=1)#lift couef
FuelFlow = m.Var()
D = m.Var()#drag
Thrmax = m.Const(value=200000)#maximum throttle
Thr = m.Var()
V = m.Var(value=100,lb=0,ub=240)#velocity
#Vmin = m.Var(value=100)
gamma = m.Var(value=0)# Flight-path angle
gammaa = gamma.value
Xi = m.Var(value=0)# Heading angle
Xii = Xi.value
#Mu = m.Var()# Bank angle (controlled var)
Muu = Mu.value
#AOA = m.Var()#angle of attack (not needed atm)
x = m.Var(value=0,lb=0)#x position
y = m.Var(value=0,lb=0)#y position
h = m.Var(value=1000)# height
mass = m.Var(value=60000)
path = m.Const(value=5000) #intended distance length
L = m.Var()#lift
p = np.zeros(nt)
p[-1] = 1.0
final = m.Param(value=p)
m.options.MAX_ITER=10000 # iteration number
#Fixed Variable
tf = m.FV(value=1,lb=0.0001,ub=1000.0)#
tf.STATUS = 1
# Controlled parameters
Tcontr = m.MV(value=0.2,lb=0.2,ub=1)# solver controls throttle pedal
Tcontr.STATUS = 1
Tcontr.DCOST = 0
Mu = m.MV(value=0,lb=-1.5,ub=1.5)# solver controls bank angle - does not work
Mu.STATUS = 1
Mu.DCOST = 0
# Equations
m.Equation(x.dt()==tf*(V*(math.cos(gammaa.value))*(math.cos(Xii.value))))#
m.Equation(Thr==Tcontr*Thrmax)
m.Equation(V.dt()==tf*((Thr-D)/mass))#
m.Equation(mass.dt()==tf*(-Thr*(FuelFlow/60000)))#
m.Equation(D==0.5*Ro*(V**2)*Cd*S)
m.Equation(FuelFlow==0.75882*(1+(V/2938.5)))
m.Equation(x*final<=path)
#pressure and density part(density isnt working)
m.Equation(T==T0-(0.0065*h))
m.Equation(pressure==101325*(1-(0.0065*h)/T0)**((g*0.0289652)/(8.31446*0.0065)))# equation works
#m.Equation(Ro==(pressure*0.0289652)/(8.31446*T))
#2D addition part
m.Equation(y.dt()==tf*(V*(math.cos(gamma.value))*(math.sin(Xii.value))))#
m.Equation(Xi.dt()==tf*((L*math.sin(Muu))/(mass*V)))
m.Equation(L==0.5*Ro*(V**2)*Cl*S)
#3D addition part
# Objective Function
m.Minimize(final*(x-path)**2) #1D part
m.Maximize(mass*tf*final) #objective function
m.options.IMODE = 6
m.options.NODES = 2 # it was 3 before
m.options.MV_TYPE = 1
m.options.SOLVER = 3
#m.open_folder() # to search for infeasibilities
m.solve()
tm = tm * tf.value[0]
fig, axs = plt.subplots(6)
fig.suptitle('Results')
axs[0].plot(tm,Tcontr,'r-',LineWidth=2,label=r'$Tcontr$')
axs[0].legend(loc='best')
axs[1].plot(tm,V.value,'b-',LineWidth=2,label=r'$V$')
axs[1].legend(loc='best')
axs[2].plot(tm,x.value,'r--',LineWidth=2,label=r'$x$')
axs[2].legend(loc='best')
axs[3].plot(tm,D.value,'g-',LineWidth=2,label=r'$D$')
axs[3].legend(loc='best')
axs[4].plot(tm,mass.value,'g:',LineWidth=2,label=r'$mass$')
axs[4].legend(loc='best')
axs[5].plot(tm,T.value,'p-',LineWidth=2,label=r'$T$')
axs[5].legend(loc='best')
#axs[6].plot(tm,Ro.value,'p-',LineWidth=2,label=r'$Ro$')
#axs[6].legend(loc='best')
plt.xlabel('Time')
#plt.ylabel('Value')
plt.show()
Upvotes: 1
Views: 58
Reputation: 95
So, I found out what was at fault. The variable Muu that references variable Mu must be defined after the variable Mu, like this:
Mu = m.MV(value=0)
Mu.STATUS = 1
Mu.DCOST = 0
Muu = Mu.value
Not like this:
Muu = Mu.value
Mu = m.MV(value=0)
Mu.STATUS = 1
Mu.DCOST = 0
Upvotes: 1