How to use gekko - 10 common examples

To help you get started, we’ve selected a few gekko examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github BYU-PRISM / GEKKO / examples / test_directory.py View on Github external
from gekko import GEKKO
import time

m = GEKKO()
x = m.Var()
y = m.Var()
m.Equation(x**2==y)
m.Obj((y-2)**2)
m.solve()

# open folder and view contents for 3 seconds
m.open_folder()
time.sleep(3)

# clear directory (remove files) and solve again
m.clear()
m.solve()

# remove directory and files after 3 seconds
time.sleep(3)
github BYU-PRISM / GEKKO / tests / hw_NMPC.py View on Github external
s.Equation(k==k0*s.exp(-ER/s.T))
s.Equation(rate==k*s.Ca)
#CSTR equations
s.Equation(V* s.Ca.dt() == q*(Ca0-s.Ca)-V*rate)
s.Equation(rho*Cp*V* s.T.dt() == q*rho*Cp*(T0-s.T) + V*mdelH*rate + UA*(s.Tc-s.T))

#Options
s.options.IMODE = 4 #dynamic simulation
s.options.NODES = 3
s.options.SOLVER = 3
s.options.TIME_SHIFT = 1


#%% NMPC model

m = GEKKO(remote=False)

m.time = np.linspace(0,2,21)

Tc = m.MV(value=300)

q = m.Param(value=100)
V = m.Param(value=100)
rho = m.Param(value=1000)
Cp = m.Param(value=0.239)
mdelH = m.Param(value=50000)
ER = m.Param(value=8750)
k0 = m.Param(value=7.2*10**10)
UA = m.Param(value=5*10**4)
Ca0 = m.Param(value=1)
T0 = m.Param(value=350)
github BYU-PRISM / GEKKO / tests / hw_benchmark2.py View on Github external
def benchmark2():
    m = GEKKO()

    nt = 101
    m.time = np.linspace(0,1,nt)

    # Parameters
    u = m.MV(value=9,lb=-4,ub=10)
    u.LOWER = -4
    u.UPPER = 10
    u.STATUS = 1
    u.DCOST = 0

    # Variables
    t = m.Var(value=0)
    x1 = m.Var(value=0)
    x2 = m.Var(value=-1)
    x3 = m.Var(value=-np.sqrt(5))
github BYU-PRISM / GEKKO / tests / hw_MHEandMPC.py View on Github external
m.K.FSTATUS = 0
m.tau.FSTATUS = 0
m.y.FSTATUS = 1

# DMAX = maximum movement each cycle
m.K.DMAX = 1
m.tau.DMAX = .1

# MEAS_GAP = dead-band for measurement / model mismatch
m.y.MEAS_GAP = 0.25

m.y.TR_INIT = 1


#%% MHE Model
c = GEKKO()

c.time = np.linspace(0,5,11) #0-5 by 0.5 -- discretization must match simulation

#Parameters
c.u = c.MV(lb=-10,ub=10) #input
c.K = c.FV(value=10, lb=1, ub=3) #gain
c.tau = c.FV(value=1, lb=1, ub=10) #time constant

#Variables
c.y = c.CV() #measurement

#Equations
c.Equation(c.tau * c.y.dt() == -c.y + c.u * c.K)


#Options
github BYU-PRISM / GEKKO / tests / hw_NMPC.py View on Github external
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt

#%% Simulation

s = GEKKO(remote=False)

#1 step of simulation, discretization matches MHE
s.time = np.linspace(0,.1,2)

#Receive measurement from simulated control
s.Tc = s.MV(value=300)
s.Tc.FSTATUS = 1 #receive measurement
s.Tc.STATUS = 0  #don't optimize

#State variables to watch
s.Ca = s.SV(value=.8, ub=1, lb=0)
s.T = s.SV(value=325,lb=250,ub=500)

#other parameters
q = s.Param(value=100)
V = s.Param(value=100)
github BYU-PRISM / GEKKO / examples / test_pwl_fit.py View on Github external
from scipy import optimize
import matplotlib.pyplot as plt
from gekko import GEKKO
import numpy as np

m = GEKKO()
m.options.SOLVER = 3
m.options.IMODE = 2

xzd = np.linspace(1,5,100)
yzd = np.sin(xzd)

xz = m.Param(value=xzd)
yz = m.CV(value=yzd)
yz.FSTATUS = 1

xp_val = np.array([1, 2, 3, 3.5,   4, 5])
yp_val = np.array([1, 0, 2, 2.5, 2.8, 3])
xp = [m.FV(value=xp_val[i],lb=xp_val[0],ub=xp_val[-1]) for i in range(6)]
yp = [m.FV(value=yp_val[i]) for i in range(6)]
for i in range(6):
    xp[i].STATUS = 0
github BYU-PRISM / GEKKO / tests / hw_cruisecontrol.py View on Github external
#%%Import packages
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt

#%% Build model

#initialize GEKKO model
m = GEKKO()

#time
m.time = np.linspace(0,20,41)

#constants
mass = 500

#Parameters
b = m.Param(value=50)
K = m.Param(value=0.8)
#Manipulated variable
p = m.MV(value=0, lb=0, ub=100)

#Controlled Variable
v = m.CV(value=0)
github BYU-PRISM / GEKKO / tests / hw_LMPC.py View on Github external
u_ss = 280.0
# Feed Temperature (K)
Tf = 350
# Feed Concentration (mol/m^3)
Caf = 1

# Steady State Initial Conditions for the States
Ca_ss = 1
T_ss = 304
x0 = np.empty(2)
x0[0] = Ca_ss
x0[1] = T_ss

#%% GEKKO linear MPC

m = gekko()

m.time = [0,.1,.2,.3,.4,.5,.75,1,1.5,2,3,4,5]

# initial conditions
Tc0 = 280
T0 = 304
Ca0 = 1.0

tau = m.Const(value = 0.5)
Kp = m.Const(value = 1)

m.Tc = m.MV(value = Tc0,lb=250,ub=350)
m.T = m.CV(value = T_ss,ub=400)

m.Equation(tau * m.T.dt() == -(m.T - T0) + Kp * (m.Tc - Tc0))
github BYU-PRISM / GEKKO / tests / exam_2016_3.py View on Github external
import gekko
import numpy as np

#%% Initialize model

m = gekko.gekko()

## Parameters
N = m.Param(value=3.2e6)
mu = m.Param(value=7.8e-4)
gamma = m.FV(value=0.07)
rep_frac = m.Param(value=0.45)
cases = m.MV(value=180,lb=0)

##Variables
beta = m.Var(value=10)
S = m.Var(value=0.06*N.value,lb=0,ub=N.value)
I = m.Var(value=0.0001*N.value,lb=0,ub=N.value)

##Intermediate
R = m.Intermediate(beta*S*I/N)
github BYU-PRISM / GEKKO / gekko / gekko.py View on Github external
def atanh(self,other):
        return GK_Operators('atanh('+str(other) + ')')
    def cos(self,other):