I am trying to use gurobi with yalmip to formulate an MPC problem.
This is used to calculate the flexibility of a building in a defined interval [t_flex_st, t_flex_end]. In order to do so, the objective of the optimisation problem which must be solved is to minimise the energy consumed in the interval [t_flex_st, t_flex_end], end, at the same time, minimise also the energy cost required to heat the building over a longer period of time (Runtime). In order to do so, the following function is used as the objective function of the optimisation problem.
Where "J1" is the energy used by the building, "RunTime" is the prediction horizon and "t_flex_st" and "t_flex_end" are the initial and ending times of the period over which flexibility must be calculated, respectively. "p" is the energy price, which is considered as an exogenous variable.
The optimisation problem must be solved for a prediction horizon of 5 days. The control horizon is one day. The sampling time is 10 minutes.
The variables are defined in the following ways:
RunTime = 6*24*5; % Time steps in 1h * hours in one day * number of days in the prediction horizon
ContrHor = 6*24; % Time steps in 1h * hours in one day
J1 = sdpvar(1,ContrHor,'full');
p = sdpvar(1,ContrHor,'full');
flextime_st = 6*24*1 + 6*6 + 1; % Start of the flexibility period
flextime_end = flextime_st + 2*6 - 1; % End of the flexibility period
flextime_run = flextime_end - flextime_st;
The solution I found to implement this objective function in the optimisation problem implies the need to modify the objective function at each iteration. This is the code I used (I didn’t include the definition of constraints and other variables in the code as I think they are not necessary to understand the problem I'm trying to describe).
check = 0;
for i = 1:RunTime
% Setting up the Objective Function (Minimum Cost MPC + Minimum energy in the interval where flexibility must be calculated))
obj = 0;
obj = obj + J1*p';
if i >= flextime_st && i <= flextime_end
obj = obj + sum(J1(1:flextime_run - check));
check = check + 1;
end
% Solving the Optimization Problem
options = sdpsettings('verbose', 1, 'solver', '+gurobi');
% P = optimizer(Con,Obj,Options,Parameters,output)
cont = optimizer(con,obj,options,{J10},{J1}); %Definition of the controller for the building
[output,infeasible] = cont{{J1(1)}};
.
.
.
end
This code works, but it is extremely slow. Do you know if this MPC problem can be coded in a more efficient way?