from gekko import GEKKO import numpy as np import matplotlib.pyplot as plt # create GEKKO model m = GEKKO() # time points n=501 m.time = np.linspace(0,10,n) # constants E,c,r,k,U_max = 1,17.5,0.71,80.5,20 # fishing rate u = m.MV(value=1,lb=0,ub=1) u.STATUS = 1 u.DCOST = 0 x = m.Var(value=70) # fish population # fish population balance m.Equation(x.dt() == r*x*(1-x/k)-u*U_max) J = m.Var(value=0) # objective (profit) Jf = m.FV() # final objective Jf.STATUS = 1 m.Connection(Jf,J,pos2='end') m.Equation(J.dt() == (E-c/x)*u*U_max) m.Obj(-Jf) # maximize profit m.options.IMODE = 6 # optimal control m.options.NODES = 3 # collocation nodes m.options.SOLVER = 3 # solver (IPOPT) m.solve(disp=False) # Solve print('Optimal Profit: ' + str(Jf.value[0])) plt.figure(1) # plot results plt.subplot(2,1,1) plt.plot(m.time,J.value,'r--',label='profit') plt.plot(m.time,x.value,'b-',label='fish') plt.legend() plt.subplot(2,1,2) plt.plot(m.time,u.value,'k--',label='rate') plt.xlabel('Time (yr)') plt.legend() plt.show()