M

#### m_nacereddine

##### Guest

I wrote the following code. First, I identified my model using data with the input (the disturbance: external temperature and the control), and the output y , which is the temperature. Then, I built an ARX model (using the arx function in GEKKO. This is my code :

Code:

```
# Import library
import numpy as np
import pandas as pd
import time
# Initialize Model
ts = 300
t = np.arange(0,len(data_1)*ts, ts)
u_id = data_1[['T_ext','beta']]
y_id = data_1[['T_int']]
# system identification
#meas : the time-series next step is predicted from prior measurements as in ARX
na=5; nb=5 # ARX coefficients
print('Identify model')
start = time.time()
yp,p,K = m.sysid(t,u_id,y_id,na,nb,objf=100,scale=False,diaglevel=0,pred='meas')
print('temps de prediction :'+str(time.time()-start)+'s')
#%% create control ARX model
T_externel = np.array([5.450257,5.448852,5.447447,5.446042,5.444637,5.443232,5.441826,5.440421,5.439016,
5.440421,5.437610,5.436205,5.434799,5.433394,5.431988,5.430583,5.429177,5.427771,
5.426365, 5.424959, 5.423553 ])
m = GEKKO(remote=False)
m.y = m.Array(m.CV,1)
m.u = m.Array(m.MV,2)
m.arx(p,m.y,m.u)
# rename CVs
m.T = m.y[0]
# rename MVs
m.beta = m.u[1]
# distrubance
m.d = m.u[0]
# distrubance and parametres
m.d = m.Param(T_externel)
# lower,heigh bound for MV
TL = m.Param(values = 16)
TH = m.Param(values = 18)
# steady state initialization
m.options.IMODE = 1
m.solve(disp=False)
# set up MPC
m.options.IMODE = 6 # MPC
m.options.CV_TYPE = 2 # the objective is an l2-norm (squared error)
m.options.NODES = 2 # Collocation nodes
m.options.SOLVER = 1 # APOPT
m.time = np.arange(0,len(T_externel)*300,300) # step time = 300s
# Manipulated variables
m.beta.STATUS = 1 # calculated by the optimizer
m.beta.FSTATUS = 1 # use measured value
m.beta.DMAX = 1.0 # Delta MV maximum step per horizon interval
m.beta.DCOST = 2.0 # Delta cost penalty for MV movement
m.beta.UPPER = 1.0 # Lower bound
m.beta.LOWER = 0.0
m.beta.MEAS = 0 # set u=0
# Controlled variables
m.T.STATUS = 1 # drive to set point
m.T.FSTATUS = 1 # receive measurement
m.T.options.CV_TYPE=2 # the objective is an l2-norm (squared error)
m.T.SP = 17 # set point
TL.values = np.ones(len(T_externel))*16
TH.values = np.ones(len(T_externel))*18
m.T.value = 17 # Temprature starts at 17
for i in range(len(T_externel)):
m.d = T_externel[i]
m.solve(disp = False)
if m.options.APPSTATUS == 1:
# Retrieve new values
beta = m.beta.NEWVAL
else:
# Solution failed
beta = 0.0
```

I get the following error :

Code:

```
File c:\algoritmempc\gekko_mpc.py:84
m.arx(p,m.y,m.u)
ValueError: operands could not be broadcast together with shapes (2,) (0,)
```

I also have another question. Since one of my inputs is a disturbance, I'm not sure if the way I declared my variables is correct or not (I want to provide the disturbances myself).

<p>I am modeling an MPC to maintain the temperature in a building within a given interval while minimizing the energy consumption. I am using GEKKO to model my algorithm.</p>

<p>I wrote the following code. First, I identified my model using data with the input (the disturbance: external temperature and the control), and the output y , which is the temperature. Then, I built an ARX model (using the arx function in GEKKO. This is my code :</p>

<pre><code># Import library

import numpy as np

import pandas as pd

import time

# Initialize Model

ts = 300

t = np.arange(0,len(data_1)*ts, ts)

u_id = data_1[['T_ext','beta']]

y_id = data_1[['T_int']]

# system identification

#meas : the time-series next step is predicted from prior measurements as in ARX

na=5; nb=5 # ARX coefficients

print('Identify model')

start = time.time()

yp,p,K = m.sysid(t,u_id,y_id,na,nb,objf=100,scale=False,diaglevel=0,pred='meas')

print('temps de prediction :'+str(time.time()-start)+'s')

#%% create control ARX model

T_externel = np.array([5.450257,5.448852,5.447447,5.446042,5.444637,5.443232,5.441826,5.440421,5.439016,

5.440421,5.437610,5.436205,5.434799,5.433394,5.431988,5.430583,5.429177,5.427771,

5.426365, 5.424959, 5.423553 ])

m = GEKKO(remote=False)

m.y = m.Array(m.CV,1)

m.u = m.Array(m.MV,2)

m.arx(p,m.y,m.u)

# rename CVs

m.T = m.y[0]

# rename MVs

m.beta = m.u[1]

# distrubance

m.d = m.u[0]

# distrubance and parametres

m.d = m.Param(T_externel)

# lower,heigh bound for MV

TL = m.Param(values = 16)

TH = m.Param(values = 18)

# steady state initialization

m.options.IMODE = 1

m.solve(disp=False)

# set up MPC

m.options.IMODE = 6 # MPC

m.options.CV_TYPE = 2 # the objective is an l2-norm (squared error)

m.options.NODES = 2 # Collocation nodes

m.options.SOLVER = 1 # APOPT

m.time = np.arange(0,len(T_externel)*300,300) # step time = 300s

# Manipulated variables

m.beta.STATUS = 1 # calculated by the optimizer

m.beta.FSTATUS = 1 # use measured value

m.beta.DMAX = 1.0 # Delta MV maximum step per horizon interval

m.beta.DCOST = 2.0 # Delta cost penalty for MV movement

m.beta.UPPER = 1.0 # Lower bound

m.beta.LOWER = 0.0

m.beta.MEAS = 0 # set u=0

# Controlled variables

m.T.STATUS = 1 # drive to set point

m.T.FSTATUS = 1 # receive measurement

m.T.options.CV_TYPE=2 # the objective is an l2-norm (squared error)

m.T.SP = 17 # set point

TL.values = np.ones(len(T_externel))*16

TH.values = np.ones(len(T_externel))*18

m.T.value = 17 # Temprature starts at 17

for i in range(len(T_externel)):

m.d = T_externel

m.solve(disp = False)

if m.options.APPSTATUS == 1:

# Retrieve new values

beta = m.beta.NEWVAL

else:

# Solution failed

beta = 0.0

</code></pre>

<p>I get the following error :</p>

<pre><code> File c:\algoritmempc\gekko_mpc.py:84

m.arx(p,m.y,m.u)

ValueError: operands could not be broadcast together with shapes (2,) (0,)

</code></pre>

<p>I also have another question. Since one of my inputs is a disturbance, I'm not sure if the way I declared my variables is correct or not (I want to provide the disturbances myself).</p>

m.solve(disp = False)

if m.options.APPSTATUS == 1:

# Retrieve new values

beta = m.beta.NEWVAL

else:

# Solution failed

beta = 0.0

</code></pre>

<p>I get the following error :</p>

<pre><code> File c:\algoritmempc\gekko_mpc.py:84

m.arx(p,m.y,m.u)

ValueError: operands could not be broadcast together with shapes (2,) (0,)

</code></pre>

<p>I also have another question. Since one of my inputs is a disturbance, I'm not sure if the way I declared my variables is correct or not (I want to provide the disturbances myself).</p>