Modification of the objective function at each iteration. Is there a better way to formulate a MPC problem??
Hi,
I would like to calculate the flexibility of a building in a defined interval [t_flex_st, t_flex_end] using model predictive control. 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,{variables},{J1}); %Definition of the controller for the building
[output,infeasible] = cont{{xs1;xs2;price;u_curr;u_lim}};
.
.
.
end
This code works, but it is extremely slow. Do you know if this optimisation problem can be coded in a more efficient way?
-
Hi,
I think you need to give more information.... mow many problems are you solving? how long does each one takes? Are you certain that you can not solve the problem in one go? Do you have any (Gurobi) logs? what are the termination conditions for the solver?
Daniel
0 -
Hi Daniel,
Thank you for your reply.
I'm sorry but I didn't write that I'm trying to formulate an MPC problem. In this case, the very first time slot of the result of the optimisation problem solved in the Control Horizon is then used as the initial condition to solve the optimisation problem in the next iteration. That's why I cannot solve the problem in one go but I need multiple iterations.
I don't have ant Gurobi logs but I run the simulation again to produce one as soon as possible so that I can post it here.
0 -
I have a similar problem in that I am solving a Model Predictive Control problem and need to repeatedly solve a problem which is very similar. I am using the Matlab interface (https://www.gurobi.com/documentation/9.0/refman/matlab_api_overview.html#matlab:problem) where my Q and A matrices are the same at each time instant, but my c and b matrices change every time I solve the problem. I am transitioning from using osqp as osqp does not allow for mixed-integer constraints. One feature in osqp that I am looking for is that, in osqp, you can create the problem once and update the matrices at each iteration. This allows osqp to do a great deal of the pre-optimization configuration once, speeding up the optimization at subsequent calls.
0
Please sign in to leave a comment.
Comments
3 comments