from gekko import GEKKO import numpy as np import matplotlib.pyplot as plt m = GEKKO(remote=False) m.time = np.linspace(0,1,101) g = m.FV(); g.STATUS = 1 # production s = m.Var(1e-2, lb=0) # storage inventory store = m.Var() # store energy rate s_in = m.Var(lb=0) # store slack variable recover = m.Var() # recover energy rate s_out = m.Var(lb=0) # recover slack variable eta = 0.7 d = m.Param(-2*np.sin(2*np.pi*m.time)+10) m.periodic(s) m.Equations([g + recover/eta - store >= d, g - d == s_out - s_in, store == g - d + s_in, recover == d - g + s_out, s.dt() == store - recover/eta, store * recover <= 0]) m.Minimize(g) m.options.SOLVER = 1 m.options.IMODE = 6 m.options.NODES = 3 m.solve() plt.figure(figsize=(6,3)) plt.subplot(2,1,1) plt.plot(m.time,d,'r-',label='Demand') plt.plot(m.time,g,'b:',label='Prod') plt.legend(); plt.grid(); plt.xlim([0,1]) plt.subplot(2,1,2) plt.plot(m.time,s,'k-',label='Storage') plt.plot(m.time,store,'g--', label='Store Rate') plt.plot(m.time,recover,'b:', label='Recover Rate') plt.legend(); plt.grid(); plt.xlim([0,1]) plt.show()