How to use the gekko.GEKKO function in gekko

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 / examples / mpc_closedloop.py View on Github external
#%%Import packages
import numpy as np
from random import random
from gekko import GEKKO
# import matplotlib.pyplot as plt

# noise level
# noise = 0.2
noise = 0.0

#%% Process
p = GEKKO()

p.time = [0,.5]

#Parameters
p.u = p.MV()
p.K = p.Param(value=1.25) #gain
p.tau = p.Param(value=8) #time constant

#variable
p.y = p.CV(1) #measurement

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

#options
p.options.IMODE = 4
github BYU-PRISM / GEKKO / examples / mpc_closedloop.py View on Github external
p.Equation(p.tau * p.y.dt() == -p.y + p.K * p.u)

#options
p.options.IMODE = 4
p.options.NODES = 4

def process_simulator(meas):
    if meas is not None:
        p.u.MEAS = meas
    p.solve(disp=False)

    return p.y.MODEL + (random()-0.5)*noise


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

#Parameters
u = c.MV(lb=-10,ub=10) #input
K = c.Param(value=1) #gain
tau = c.Param(value=10) #time constant
#Variables
y = c.CV(1)
#Equations
c.Equation(tau * y.dt() == -y + u * K)
#Options
c.options.IMODE = 6 #MPC
c.options.CV_TYPE = 1
c.options.NODES = 3

y.STATUS = 1
github BYU-PRISM / GEKKO / examples / rosenbrock_ndim.py View on Github external
from time import time
import numpy as np

# benchmark local and remote solve with Rosenbrock optimization problem

data = np.zeros([12,3])
np.random.seed(0) # Set random seed for repeatability

for row,k in enumerate(range(2,60,5)): # Increasing dimensions
	# Remote Solve
	solveTimeRemote = 0
	for n in range(10): # Average solve time over 10 trials
		print("Remote Solve: k"+str(k)+" n"+str(n))
		start = time()

		m = GEKKO(remote=True)

		x = [m.Var(np.random.random()*5,name='vremote_'+str(i)) for i in range(k)]

		f = 0

		for i in range(k-1):
			f = f + 100*(x[i+1]-x[i]**2)**2 + (x[i]-1)**2 # Rosenbrock

		m.Obj(f)

		m.options.SOLVER = 1
		m.options.IMODE = 3
		m.options.MAX_ITER = 1000

		m.solve(disp=False)