I'm trying to use Gekko to optimize across time (a vector) to generate schedules based on several constraints, namely price and the number of times an event can occur (say we limit it to 5 per x7 below).
I'm getting the following error on my sum call: TypeError: x must be a python list of GEKKO parameters, variables, or expressions.
Even if I remove the sum, I also get the following error: TypeError: @error: Model Expression *** Error in syntax of function string: Invalid element: ame
The following variables are vectors with length 42: sp, base, neg_ln,
The following variables are scaler values (length 1): min, max, co_ln, co1,
I can get this to work by looking at only 1 row of the 42 rows at a time to optimize x1, but I'm unable to figure out how to translate this doing it across multiple rows in 1 go while constraining frequency (x7). Any guidance in appreciated.
m = GEKKO(server='XXX', remote=False)
m.options.IMODE = 2
m.options.MAX_ITER = 1000
#initialize variables
x1, x2, x3, x4, x5, x6, x7 = [m.Var() for i in range(7)]
x1.value = 1
x2.value = 1
x3.value = 1
x4.value = 0
x5.value = 0
x6.value = 0
x7.value = 1
neg_ln = m.Intermediate(-m.log(x1/sp))
vol1 = m.Intermediate(co1+ base + (neg_ln*co_ln))
vol2 = m.Intermediate(co2+ base + (neg_ln*co_ln))
vol3 = m.Intermediate(co3+ base + (neg_ln*co_ln))
vol4 = m.Intermediate(base + (neg_ln*co_ln))
total_vol = m.Intermediate((
(m.max2(0,base*(m.exp(vol1)-1)) * x3 +
m.max2(0,base*(m.exp(vol2)-1)) * x4 +
m.max2(0,base*(m.exp(vol3)-1)) * x5 +
m.max2(0,base*(m.exp(vol4)-1)) * x6) + base) * x7)
m.Equation(x3+x4+x5+x6 == 1)
m.Equation(x3+x4+x5+x6 >= 0)
m.Equation(m.sum(x7)<= 5)
m.Equation(x1 >= min)
m.Equation(x1 <= max)
z1 = m.Var(lb = 3, ub = 15)
m.Equation(z1 == x1)
z2 = m.Var(lb = 3, ub = 15)
m.Equation(z2 == x2)
z3 = m.Var(lb = 0,ub = 1, integer=True)
m.Equation(z3 == x3)
z4 = m.Var(lb = 0,ub = 1, integer=True)
m.Equation(z4 == x4)
z5 = m.Var(lb = 0,ub = 1, integer=True)
m.Equation(z5 == x5)
z6 = m.Var(lb = 0,ub = 1, integer=True)
m.Equation(z6 == x6)
z7 = m.Var(lb = 0,ub = 1, integer=True)
m.Equation(z7 == x7)
m.Maximize(m.sum(simu_total_volume))
try:
m.solve(disp = True)
except:
"error"
Try using list comprehensions to define the Intermediate values. I plugged in sample random values for constants that are not defined in the question:
It is unclear if
xvalues should be vectors. This is solved withIMODE=3where the time discretization is explicitly defined versusIMODE=6where the time discretization is defined bym.time. Here is a sample problem that solves successfully.This produces a solution:
A few suggestions:
zvariables to add on integer constraints. Define integer constraints when declaringxvariables.m.Array()to efficiently create arrays of variables. Thexvalues can be defined as a vectorm.Array(m.Var,7)or matrixm.Array(m.Var,(7,42)).lbandubto define the bounds at initialization or usex[6].upper=5to define a bound after it is initialized. Usingm.Equation(x[6]<=5)is less efficient because the inequality constraint is solved implicitly.