I am trying to formulate this minimax optimization function. I am not what part of this code is not written right.
This function seeks to schedule the charging start time of EVs in a way that the peak of the demand is minimized. On the other hand, peak of the demand is the maximum of the equations, which determine in which time slot each EV is receiving charge.
def testGEKKO():
# set of all EVs
n = np.linspace(1,10,10)
# set of contract time for 10 hours
t = np.linspace(1,10,10)
arival_time = [0,2,1,5,2,3,6,0,6,4]
duration = 2
I = []
L = []
for ev in range(len(n)):
all_intervals = []
start = arival_time[ev]
while start < 10 - duration:
all_intervals.append([start, start+duration])
L.append(start)
start += 1
I.append(all_intervals)
L = list(set(L))
h = np.array([50, 50, 50, 50, 50, 50, 50, 50, 50, 50])
mdl = GEKKO(remote = False)
idx = []
H = mdl.Var(1,lb=0,integer=True)
mdl.Minimize(H)
for ev in range(len(n)):
idx.append(mdl.Array(mdl.Var, (len(I[ev])), lb = 0, ub = 1, integer = True))
for ev in range(len(n)):
mdl.Equation(np.sum(idx[ev]) == 1)
for time in L:
evs = []
all_idx_start = []
for ev in range(len(n)):
st = np.array(I[ev])[:,0]
if time in st:
evs.append(ev)
all_idx_start.append(list(np.array(I[ev])[:,0]).index(time))
equ = 0
for i in range(len(all_idx_start)):
equ += idx[evs[i]][all_idx_start[i]] * h[evs[i]]
equations.append(equ <= z)
mdl.Equation(equ <= z)
#mdl.Equations([z>=equations[0],z>=equations[1],z>=equations[2], z>=equations[3],z>=equations[4],z>=equations[5],z>=equations[6],z>=equations[7]])
#mdl.Equations([z[0]>=equations[0]])
mdl.Minimize(z)
mdl.options.MAX_ITER = 3000
#mdl.open_folder()
mdl.options.SOLVER = 1
mdl.options.IMODE = 3
mdl.solve(disp=True)
return idx
I have tried Gekko minimax suggestion, but it does not work.
Minimax in gekko requires that the variable (e.g. x1-x3) are all Gekko types.
min Z
s.t. x1 + x2 + x3 = 15
Z >= x1
Z >= x2
Z >= x3
Here is an example for the above problem where the solution is x1=x2=x3=5.
from gekko import GEKKO
m = GEKKO(remote=False)
x1,x2,x3,Z = m.Array(m.Var,4)
m.Minimize(Z)
m.Equation(x1+x2+x3==15)
m.Equations([Z>=x1,Z>=x2,Z>=x3])
m.solve()
print(x1.value[0],x2.value[0],x3.value[0],Z.value[0])
For your specific application, it is not necessary to minimize the variables H
. Minimizing z
is the only thing that should be in the objective function.
#(removed) mdl.Minimize(H)
Here is the complete code. The original code was missing the definition of z
. For future questions, please post a complete and minimal example of the problem.
from gekko import GEKKO
import numpy as np
# set of all EVs
n = np.linspace(1,10,10)
# set of contract time for 10 hours
t = np.linspace(1,10,10)
arival_time = [0,2,1,5,2,3,6,0,6,4]
duration = 2
I = []
L = []
for ev in range(len(n)):
all_intervals = []
start = arival_time[ev]
while start < 10 - duration:
all_intervals.append([start, start+duration])
L.append(start)
start += 1
I.append(all_intervals)
L = list(set(L))
h = np.array([50, 50, 50, 50, 50, 50, 50, 50, 50, 50])
mdl = GEKKO(remote = False)
idx = []
z = mdl.Var()
H = mdl.Var(1,lb=0,integer=True)
#mdl.Minimize(H)
for ev in range(len(n)):
idx.append(mdl.Array(mdl.Var, (len(I[ev])), lb = 0, ub = 1, integer = True))
for ev in range(len(n)):
mdl.Equation(np.sum(idx[ev]) == 1)
for time in L:
evs = []
all_idx_start = []
for ev in range(len(n)):
st = np.array(I[ev])[:,0]
if time in st:
evs.append(ev)
all_idx_start.append(list(np.array(I[ev])[:,0]).index(time))
equ = 0
for i in range(len(all_idx_start)):
equ += idx[evs[i]][all_idx_start[i]] * h[evs[i]]
print(equ)
mdl.Equation(equ <= z)
mdl.Minimize(z)
mdl.options.MAX_ITER = 3000
#mdl.open_folder()
mdl.options.SOLVER = 1
mdl.options.IMODE = 3
mdl.solve(disp=True)
If this is still not generating the correct answer, you may need to increase the number of MINLP iterations before it reports a successful solution or lower the gap tolerance closer to zero.
m.solver_options = ['minlp_gap_tol 1.0e-2',\
'minlp_maximum_iterations 10000',\
'minlp_max_iter_with_int_sol 1500']
m.options.solver = 1
Also, look at the gk0_model.apm
file that is in mdl.path
(run directory). You can open the run directory with mdl.open_folder()
. This shows the model equations that are generated by the script.