how to sum up a gekko intermediate function that consist of multiple values?

Viewed 23

The problem I have is that I can't take the sum of values of the intermediate equation b when calculating c. I get no error with this code but once I put b[0] it gives me a TypeError: 'int' object is not subscriptable. I don't know how to solve this and get the sum of this intermediate variable b.

This code is a short version of mine, but it captures the problem.

from math import ceil
import numpy as np
import pandas as pd
import os
from gekko import GEKKO

x1=[10.2203,10.2203,10.2203,10.2203,10.2203,10.2203,\
    10.2203,10.2203,10.2203,10.2203,10.2203,10.2203,\
    10.2203,10.2203,10.2203,10.2203,10.4247,10.4247,\
    10.4247,10.4247,10.4247,10.4247,10.4247,10.4247,\
    10.4247,10.4247,10.4247,10.4247,10.4247,10.4247,\
    10.4247,10.4247,10.4247,10.4247,10.4247,10.4247,\
    10.4247,10.4247,10.4247,10.4247,9.17603,9.17603,\
    9.17603,9.17603,9.17603,9.17603,9.17603,9.17603,\
    9.17603,9.17603,9.17603,9.17603,9.17603,9.17603,\
    9.17603,9.17603,9.17603,9.17603,9.17603,9.17603,\
    9.17603,9.17603,9.17603,9.17603,10.1014,10.1014,\
    10.1014,10.1014,10.1014,10.1014,10.1014,10.1014,\
    10.1014,10.1014,10.1014,10.1014,10.1014,10.1014,\
    10.1014,10.1014,10.1014,10.1014,10.1014,10.1014,\
    10.1014,10.1014,10.1014,12.0657,12.0657,12.0657,\
    12.0657,12.0657,12.0657,12.0657,12.0657,12.0657,\
    12.0657,12.0657,12.0657]

m = GEKKO(remote=False)       
T_g= m.Param(value=x1)
e = m.FV( 2.7,lb=1,ub=4)
e.STATUS=1
l = m.FV(-35.1,lb=-38,ub=-30)
l.STATUS=1
g = m.FV(7.1,lb=5,ub=9)
g.STATUS=1
h = m.FV( 0.20,lb=0.0,ub=1.5)
h.STATUS=1
a=[None]*1
b=[None]*1
c=[None]*1
d=[None]*1
a[0]=m.Intermediate((e / (1 + (l / (T_g - 40)) ** g) + h))
b[0] = m.Intermediate(a[0]*f)
c[0] = m.Intermediate(sum(b))
m.Equation(d[0]==c[0])
m.Obj(d[0])
m.options.IMODE = 2
m.options.SOLVER = 1 # 1=APOPT, 3=IPOPT
       
# optimize
m.options.MAX_ITER = 40
m.options.OTOL = 1.0e-2
m.options.RTOL = 1.0e-3
m.solve(disp=True)
1 Answers

Use the m.vsum() function to get a summation of b for across the measurement dimension. The vsum function requires a Param or Var type, so just switch the m.Intermediate to an m.Var definition with an associated equation.

a=m.Intermediate((e / (1 + (l / (T_g - 40)) ** g) + h))
b = m.Var(); m.Equation(b==a*5)
c=m.Intermediate(m.vsum(b))

Here is the complete code:

from gekko import GEKKO

x1=[10.2203,10.2203,10.2203,10.2203,10.2203,10.2203,\
    10.2203,10.2203,10.2203,10.2203,10.2203,10.2203,\
    10.2203,10.2203,10.2203,10.2203,10.4247,10.4247,\
    10.4247,10.4247,10.4247,10.4247,10.4247,10.4247,\
    10.4247,10.4247,10.4247,10.4247,10.4247,10.4247,\
    10.4247,10.4247,10.4247,10.4247,10.4247,10.4247,\
    10.4247,10.4247,10.4247,10.4247,9.17603,9.17603,\
    9.17603,9.17603,9.17603,9.17603,9.17603,9.17603,\
    9.17603,9.17603,9.17603,9.17603,9.17603,9.17603,\
    9.17603,9.17603,9.17603,9.17603,9.17603,9.17603,\
    9.17603,9.17603,9.17603,9.17603,10.1014,10.1014,\
    10.1014,10.1014,10.1014,10.1014,10.1014,10.1014,\
    10.1014,10.1014,10.1014,10.1014,10.1014,10.1014,\
    10.1014,10.1014,10.1014,10.1014,10.1014,10.1014,\
    10.1014,10.1014,10.1014,12.0657,12.0657,12.0657,\
    12.0657,12.0657,12.0657,12.0657,12.0657,12.0657,\
    12.0657,12.0657,12.0657]
m = GEKKO(remote=False)       
T_g= m.Param(value=x1)
e = m.FV( 2.7,lb=1,ub=4);      e.STATUS=1
l = m.FV(-35.1,lb=-38,ub=-30); l.STATUS=1
g = m.FV(7.1,lb=5,ub=9);       g.STATUS=1
h = m.FV( 0.20,lb=0.0,ub=1.5); h.STATUS=1
a=m.Intermediate((e / (1 + (l / (T_g - 40)) ** g) + h))
b = m.Var(); m.Equation(b==a*5)
c=m.Intermediate(m.vsum(b))
m.Minimize(c)
m.options.IMODE = 2
m.options.SOLVER = 1 # 1=APOPT, 3=IPOPT
# optimize
m.options.MAX_ITER = 40; m.options.OTOL = 1.0e-2
m.options.RTOL = 1.0e-3; m.solve(disp=True)
Related