how to multiply a gekko array of FV or variables with a gekko intermediate array if both have different dimensions without changing the amount of FV?

Viewed 23

So this is the whole problem that I'm posting because I could not explain it well on my previous question. There is no problem until equation ysim (thanks to your help on the vsum). Now the new problem I'm facing is that I want the equation ysim to be like ysimm[0] so I only keep one 1 intermediate equation with 99 values but for doing that I get a problem when I want to multiply the factor x that has another dimension. I'm trying to do it this way because if I run for bigger datasets the time running takes way too long for optimizing. I gave you here an example with a dataset of 100 values but keep in your mind that I should be able to run it for 8760 values in a decent amount of time. So my goal is too get rid of the for loop so I have less amount of equations and have small time for optimization.

`data:`
        x1= [10.22025   , 10.22025   , 10.22025   , 10.22025   , 10.22025   ,
       10.22025   , 10.22025   , 10.22025   , 10.22025   , 10.22025   ,
       10.22025   , 10.22025   , 10.22025   , 10.22025   , 10.22025   ,
       10.22025   , 10.42466667, 10.42466667, 10.42466667, 10.42466667,
       10.42466667, 10.42466667, 10.42466667, 10.42466667, 10.42466667,
       10.42466667, 10.42466667, 10.42466667, 10.42466667, 10.42466667,
       10.42466667, 10.42466667, 10.42466667, 10.42466667, 10.42466667,
       10.42466667, 10.42466667, 10.42466667, 10.42466667, 10.42466667,
        9.17602778,  9.17602778,  9.17602778,  9.17602778,  9.17602778,
        9.17602778,  9.17602778,  9.17602778,  9.17602778,  9.17602778,
        9.17602778,  9.17602778,  9.17602778,  9.17602778,  9.17602778,
        9.17602778,  9.17602778,  9.17602778,  9.17602778,  9.17602778,
        9.17602778,  9.17602778,  9.17602778,  9.17602778, 10.10144444,
       10.10144444, 10.10144444, 10.10144444, 10.10144444, 10.10144444,
       10.10144444, 10.10144444, 10.10144444, 10.10144444, 10.10144444,
       10.10144444, 10.10144444, 10.10144444, 10.10144444, 10.10144444,
       10.10144444, 10.10144444, 10.10144444, 10.10144444, 10.10144444,
       10.10144444, 10.10144444, 12.06572222, 12.06572222, 12.06572222,
       12.06572222, 12.06572222, 12.06572222, 12.06572222, 12.06572222,
       12.06572222, 12.06572222, 12.06572222, 12.06572222]
        x2= [1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06,
       1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06,
       1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06,
       1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 0.85, 0.85, 0.85, 0.85,
       0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85,
       0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85,
       0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85,
       0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 1.06,
       1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06, 1.06]
        x3= 65034
        ym= array([[867.00620479],
       [806.51494795],
       [743.24106807],
       [720.18636383],
       [695.22311179],
       [653.94037345],
       [586.42011771],
       [606.63642812],
       [663.42812525],
       [698.12521007],
       [664.02744076],
       [650.69550134],
       [648.38897701],
       [640.74173932],
       [627.02365531],
       [630.8000835 ],
       [643.78791701],
       [676.45184639],
       [730.12179025],
       [789.60255185],
       [840.26610391],
       [861.12694198],
       [850.26615548],
       [804.06561751],
       [769.39954258],
       [733.54365183],
       [658.57522502],
       [582.95404305],
       [547.87318396],
       [596.37389932],
       [564.87134208],
       [565.75337059],
       [551.91634026],
       [618.54778959],
       [621.5932033 ],
       [587.51596359],
       [590.33141833],
       [662.38526847],
       [706.33703141],
       [773.38769209],
       [769.89174372],
       [799.49764387],
       [757.29506965],
       [754.80491508],
       [791.17269423],
       [888.97427012],
       [908.22655116],
       [916.29938631],
       [871.5263805 ],
       [859.75888297],
       [828.08427277],
       [792.96960253],
       [749.86351032],
       [731.63318427],
       [638.68470451],
       [589.72452176],
       [542.10804849],
       [597.44776599],
       [654.59285436],
       [694.12771646],
       [724.34043483],
       [726.62756574],
       [751.45567109],
       [731.55608073],
       [719.52446046],
       [704.72612366],
       [694.41928383],
       [663.55557334],
       [642.74991838],
       [643.37172242],
       [607.84930058],
       [587.9253411 ],
       [550.22194285],
       [519.49911871],
       [473.92007136],
       [426.91321758],
       [439.60127581],
       [434.81392142],
       [418.89791625],
       [420.07407832],
       [425.6347601 ],
       [496.21214396],
       [503.95746536],
       [537.14548727],
       [532.69303182],
       [553.71912535],
       [545.76615722],
       [570.55784603],
       [586.6199875 ],
       [655.09052092],
       [686.62949469],
       [728.60943635],
       [758.07778033],
       [714.92052083],
       [664.33699112],
       [571.36947054],
       [499.19859383],
       [418.50424937],
       [337.30943708]])

        N = len(ym)

        
        # GEKKO model
        m = GEKKO(remote=False)       
        
        # variables
        x = m.Array(m.FV,24)
        for i in range(24):
                    x[i].value = x4[i]
                    x[i].lower = 0.01
                    x[i].upper = 0.15
                    x[i].STATUS=1

        T_g= m.Param(value=x1)
        f=m.Param(value=x2)
        annual_demand=m.Param(value=x3)
        sf=m.Param(value=x4)
        ymeas = m.Param(value=ym)
        w= [None]*1        
        z= [None]*1
        den= [None]*1
        num= [None]*1
        denn= [None]*N
        numm= [None]*N
        obj=[None]*1
        objj=[None]*1
        a=[None]*1
        b=m.Var()
        c=[None]*1
        d=[None]*1
        xop=[None]*1
        index = np.ones(N,int)
        for i in range(N):
                    index[i]= int(i%24)

        e= m.Var(2.7,lb=1,ub=4)
        l = m.Var(-35.1,lb=-38,ub=-30)
        g = m.Var(7.1,lb=5,ub=9)
        h = m.Var(0.142,lb=0.0,ub=1.5)

        objj = m.Var(value=0)
        ysim=[None]*N
        ysimm=[None]*1

        # regression equation
       
        a[0]=m.Intermediate((e / (1 + (l / (T_g - 40)) ** g) + h))
        m.Equation(b==a[0]*f)
        c[0]=m.Intermediate(m.vsum(b))
        d[0] = m.Intermediate(1.0/(c[0]/24))
       
        z[0] = m.Intermediate(sum([x[i] for i in range(24)]))
        den[0]=0
        num[0]=0
        for i in range(N):
            ysim[i]=m.Intermediate(a[0]*d[0]*f*x[i%24]*annual_demand)
            denn[i] = m.Intermediate((ymeas[i][0]-np.mean(ymeas))**2)
            numm[i] = m.Intermediate((ysim[i]-ymeas[i][0])**2)
            den[0]= m.Intermediate(den[0]+denn[i])
            num[0]= m.Intermediate(num[0]+numm[i])
        ysimm[0]=m.Intermediate(a[0]*d[0]x[index]*f*annual_demand)

        obj[0]= m.Intermediate(num[0]/den[0])
        m.Equation(z[0]==1)
        m.Equation(objj == m.sqrt(obj[0])/(N-1)) 
        m.Obj(objj)  
 
        # regression mode
        m.options.IMODE = 2
        m.options.SOLVER = 1 # considering APOPT solver for 1 and IPOPT for 3
       
       
        # optimize
        m.options.MAX_ITER = 40
        m.options.OTOL = 1.0e-2
        m.options.RTOL = 1.0e-2
        m.solve(disp=True)


```
0 Answers
Related