Is it possible to define start and end time for variable in GEKKO dynamic optimization?

Viewed 64

I have an overall time frame, and I want to introduce several variables to be optimized along that time frame, but they are active in different time intervals. Let's say I have:

Overall time frame: m.time = np.linspace(0,100,101)

Variable 1 time frame: m.time = np.linspace(20,50,31)

Variable 2 time frame: m.time = np.linspace(40,80,41)

How can I use both variables in the same optimization and communicate that they should start and end at specific intervals in the overall time frame? In the end, I want to be able to optimize the sum of the variables in each time step.

Is there any option in GEKKO that can tell a variable in which time intervals it can be modified by the optimizer? For example, Variable 1 above should not be possible to change in the time intervals 0-19 and 51-100.

1 Answers

One method is to define a specific window where an objective function is active such as with xobj that is 1 over the interval 20-50 and 0 otherwise.

Optimize

from gekko import GEKKO
import numpy as np
m = GEKKO()
m.time = np.linspace(0,100,101)
x = m.Var(2)
x_on = np.zeros(101); x_on[20:51]=1
xobj = m.Param(x_on)
m.Minimize(xobj*(x-3)**2)
m.options.IMODE=6
m.solve()

import matplotlib.pyplot as plt
plt.plot(m.time,x.value,'bo')
plt.show()

The optimizer recognizes that there is no objective function improvement to change x outside of the active region. If there is an objective function benefit but the variable shouldn't move then try using m.fix(var,val,pos).

from gekko import GEKKO
import numpy as np
m = GEKKO()
m.time = np.linspace(0,100,101)
x = m.Var(2)
for i in range(1,21):
    m.fix(x,val=2,pos=i)
for i in range(51,101):
    m.fix(x,val=2,pos=i)
    
m.Minimize((x-3)**2)
m.options.IMODE=6
m.solve()

import matplotlib.pyplot as plt
plt.plot(m.time,x.value,'bo')
plt.show()

This gives the same result.

Related