How to declarate control and state variables?

Viewed 36

Good afernoon community, recently someone recommends me GEKKO library to do dynamic optimization through python. I'm trying to replicate this paper with that. Basically the model is:

Energy Eficiency model

According to the paper E is control variable and K is a state variable, the rest are constant values. I think my error is when I define E as a control variable, because when I do the output is that GEKKO can't find a solution. That's is the reason that belows I define as a parameter.

# Initialize GEKKO
m = GEKKO()

# Define values:
# Constant values
beta_1, beta_2, gamma, delta, s, alpha, L_bar = 0.2, 0.8, 0.5, 0.05, 0.4719, 10, 900000000
# Time period 
n = 100
t = np.linspace(0, 15, n) 
m.time = t

# Set initial conditions:
# Define K(0) = K_0 initial capital 
K = m.Var(value = 0)

# Restrictions problem:
# Define constraints
import math    
E = m.Param(-4 * (np.exp(t/2) - np.exp(-1))) # Control variable E
Y = m.Param((1 - gamma) * (K**beta_1) * (L_bar**(beta_2)) + gamma * E)
K_dot = m.SV(1, lb = 0, ub = 1)

m.Equation(K_dot.dt() == s * (Y - E) - delta * K)


J = m.Var(value = 0)                        
Jf = m.FV()                                 
Jf.STATUS = 1
# Define objective function :
m.Connection(Jf,J,pos2='end')
m.Equation(J.dt() == -1 *(E**2) - alpha * K_dot)
# Set solving options
m.Obj(-Jf) # maximize profit
# Set solving options
m.options.IMODE = 6  # optimal control
m.options.NODES = 3  # collocation nodes
m.options.SOLVER = 3 # solver (IPOPT)
# Solve maximization problem
m.solve(disp = False) # Solve

plt.plot(t, K, "--r")

Taking a look to the documentation, I think my problem is a mix between the "load following" exmple and the "optimal control with economic objective". In order to know if I did in the right way, I use figure 5 and 6 as reference. For the figure 5 when x = 11 there is change in the graphic...but for me it isn't. While in figure 6, the y axis is from [0, -3k] and according to my script is from [0, -7k].

1 Answers

The decision variable E should be an MV() type and the Y variable should be an Intermediate type.

from gekko import GEKKO
import numpy as np

# Initialize GEKKO
m = GEKKO()

# Define values:
# Constant values
beta_1, beta_2 = 0.2, 0.8
gamma, delta, s, alpha, L_bar = 0.5, 0.05, 0.4719, 10, 900000000
# Time period 
n = 100
t = np.linspace(0, 15, n) 
m.time = t

# Set initial conditions:
# Define K(0) = K_0 initial capital 
K_0 = 0.1
K = m.Var(K_0)

# Restrictions problem:
# Define constraints
E = m.MV(lb=0.1,ub=0.2); E.STATUS=1 # Variable E is adjusted to Maximize objective
Y = m.Intermediate((1-gamma) * (K**beta_1) * (L_bar**(beta_2)) + gamma*E)
m.Equation(K.dt() == s * (Y-E) - delta*K)
p = np.zeros(n); p[-1]=1; final=m.Param(p)
m.Maximize(final*m.integral(-1 *(E**2) - alpha*K)) # maximize profit
# Set solving options
m.options.IMODE = 6  # optimal control
m.options.NODES = 3  # collocation nodes
m.options.SOLVER = 1 # solver (IPOPT)
m.options.MAX_ITER=1000
# Solve maximization problem
m.solve(disp = True) # Solve

plt.plot(t, K, "--r")
plt.show()

You will need to find the correct value of K_0 from the paper and fill in any additional information such as the correct bounds on E. The solution currently appears unbounded (profit as a negative of the minimization increases with each solver iteration, up to 1000).

  995 -1.11943E+06  9.76562E-04
  996 -1.12056E+06  9.76562E-04
  997 -1.12168E+06  9.76562E-04
  998 -1.12281E+06  9.76562E-04
  999 -1.12393E+06  9.76562E-04
 
 Iter    Objective  Convergence
 1000 -1.12506E+06  9.76562E-04
 Maximum iterations
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :    11.9291999999987      sec
 Objective      :   -1125059.40772567     
 Unsuccessful with error code            0
 ---------------------------------------------------

There are a few additional dynamic optimization problem examples that can help:

  1. Basic Examples
  2. More Advanced Examples
  3. Energy Systems

The commercial fishery example may be the closest application to the one you are solving.

Related