# The DMISER problem 1
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt
# Initialize gekko model
m = GEKKO()
M=100 # time from i=0 to i=99
u = [m.Var(0.04,lb=0,ub=0.04) for i in range(M)]
x1 = [m.Var(1,lb=-1000,ub=1000) for i in range(M)]
#note, initial value 1 must also be put here
x2 = [m.Var(0,lb=-1000,ub=1000) for i in range(M)]
#note, initial value 0 must also be put here
x3 = [m.Var(0,lb=-1000,ub=1000) for i in range(M)]
#note, initial value 0 must also be put here
# initial conditions must be put here as well
m.Equation(x1[0]==1.0)
m.Equation(x2[0]==0.0)
m.Equation(x3[0]==0.0)
# Equations (different for each phase)
for i in range(M-1):
m.Equation(x1[i+1]==x1[i]-u[i])
m.Equation(x2[i+1]==x2[i]+x3[i])
m.Equation(x3[i+1]==x3[i]+(2*u[i]-0.05*(x3[i])**2*m.exp(0.01*x2[i]))/x1[i]-0.01)
# terminal constraint
m.Equation(x1[M-1]==0.2)
# Objective
# Maximize final x2 while keeping third phase = -1
m.Obj(-x2[M-1])
# Solver options
m.options.IMODE = 3
# Solve
m.solve(disp=False)
# Plot
plt.figure(figsize=(15,8))
plt.subplot(3,1,1)
plt.plot(x1,'ro', label='$x1$')
plt.plot(x3,'go', label='$x3$')
plt.legend()
plt.subplot(3,1,2)
plt.plot(x2,'bo', label='$x2$')
plt.legend()
plt.subplot(3,1,3)
plt.plot(u,'yo', label='$u$')
plt.legend()
plt.show()