├── .gitignore ├── Building-project-description.pdf ├── MATLAB ├── FIGURES │ ├── disturbances.fig │ ├── section1 │ │ ├── trackingMPC_power.fig │ │ ├── trackingMPC_temp.fig │ │ └── tuning_horizon.fig │ ├── section2 │ │ ├── economicMPC_sc_power.fig │ │ └── economicMPC_sc_temp.fig │ ├── section3 │ │ ├── economicMPC_sc_vc_power.fig │ │ └── economicMPC_sc_vc_temp.fig │ ├── section4 │ │ ├── economicMPC_sc_vc_sb_power.fig │ │ └── economicMPC_sc_vc_sb_temp.fig │ └── section5 │ │ ├── battery_usage.fig │ │ ├── economicMPC_sc_vc_sb_battery_power.fig │ │ ├── economicMPC_sc_vc_sb_battery_powerInputs.fig │ │ ├── economicMPC_sc_vc_sb_battery_states.fig │ │ └── economicMPC_sc_vc_sb_battery_temp.fig ├── battery.mat ├── battery_usage.m ├── building.mat ├── economicMPC_sc.m ├── economicMPC_sc_vc.m ├── economicMPC_sc_vc_sb.m ├── economicMPC_sc_vc_sb_battery.m ├── main.m ├── shiftPred.m ├── simBuild.m ├── simBuildStorage.m ├── trackingMPC.m └── tuning_horizon_length.m ├── README.md └── Report.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | .AppleDouble 3 | .LSOverride 4 | 5 | # Icon must end with two \r 6 | Icon 7 | 8 | 9 | # Thumbnails 10 | ._* 11 | 12 | # Files that might appear in the root of a volume 13 | .DocumentRevisions-V100 14 | .fseventsd 15 | .Spotlight-V100 16 | .TemporaryItems 17 | .Trashes 18 | .VolumeIcon.icns 19 | .com.apple.timemachine.donotpresent 20 | 21 | # Directories potentially created on remote AFP share 22 | .AppleDB 23 | .AppleDesktop 24 | Network Trash Folder 25 | Temporary Items 26 | .apdisk 27 | 28 | 29 | ##--------------------------------------------------- 30 | ## Remove autosaves generated by the Matlab editor 31 | ## We have git for backups! 32 | ##--------------------------------------------------- 33 | 34 | # Windows default autosave extension 35 | *.asv 36 | 37 | # OSX / *nix default autosave extension 38 | *.m~ 39 | 40 | # Compiled MEX binaries (all platforms) 41 | *.mex* 42 | 43 | # Simulink Code Generation 44 | slprj/ 45 | 46 | # Session info 47 | octave-workspace 48 | 49 | # Simulink autosave extension 50 | .autosave 51 | -------------------------------------------------------------------------------- /Building-project-description.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/Building-project-description.pdf -------------------------------------------------------------------------------- /MATLAB/FIGURES/disturbances.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/disturbances.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section1/trackingMPC_power.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section1/trackingMPC_power.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section1/trackingMPC_temp.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section1/trackingMPC_temp.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section1/tuning_horizon.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section1/tuning_horizon.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section2/economicMPC_sc_power.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section2/economicMPC_sc_power.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section2/economicMPC_sc_temp.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section2/economicMPC_sc_temp.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section3/economicMPC_sc_vc_power.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section3/economicMPC_sc_vc_power.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section3/economicMPC_sc_vc_temp.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section3/economicMPC_sc_vc_temp.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section4/economicMPC_sc_vc_sb_power.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section4/economicMPC_sc_vc_sb_power.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section4/economicMPC_sc_vc_sb_temp.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section4/economicMPC_sc_vc_sb_temp.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section5/battery_usage.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section5/battery_usage.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section5/economicMPC_sc_vc_sb_battery_power.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section5/economicMPC_sc_vc_sb_battery_power.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section5/economicMPC_sc_vc_sb_battery_powerInputs.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section5/economicMPC_sc_vc_sb_battery_powerInputs.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section5/economicMPC_sc_vc_sb_battery_states.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section5/economicMPC_sc_vc_sb_battery_states.fig -------------------------------------------------------------------------------- /MATLAB/FIGURES/section5/economicMPC_sc_vc_sb_battery_temp.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/FIGURES/section5/economicMPC_sc_vc_sb_battery_temp.fig -------------------------------------------------------------------------------- /MATLAB/battery.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/battery.mat -------------------------------------------------------------------------------- /MATLAB/battery_usage.m: -------------------------------------------------------------------------------- 1 | %%% Olivier Leveque & Maxime Maurin -- 7 June 2016 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | 4 | function battery_usage(ssM, ssModel, param_constraints, param_objective, param_simulation) 5 | 6 | alpha = param_simulation.alpha; 7 | capacity = param_simulation.capacity; 8 | 9 | total_cost = zeros(length(alpha), length(capacity)); 10 | for i = 1:length(alpha) 11 | ssModel.A = alpha(i); 12 | 13 | for j = 1:length(capacity) 14 | 15 | %redefine constraints matrix 16 | xbmax = capacity(j); xbmin = 0; 17 | param_constraints.Mxb = [eye(length(xbmax)); -eye(length(xbmin))]; 18 | param_constraints.mxb = [xbmax; -xbmin]; 19 | 20 | %run economic MPC with variable cost, night-setbacks and a battery storage 21 | [~, ~, ~, ~, ~, ~, total_cost(i,j)] = economicMPC_sc_vc_sb_battery(ssM, ssModel, param_constraints, param_objective, param_simulation); 22 | end 23 | end 24 | 25 | figure, 26 | surf(capacity, alpha, total_cost); 27 | xlabel('Storage capacity') 28 | ylabel('Dissipation factor of the battery') 29 | zlabel('Total economic cost') 30 | view([1, 1, 1]); 31 | 32 | savefig('battery_usage.fig'); %save the figure 33 | end -------------------------------------------------------------------------------- /MATLAB/building.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleveque/BuildingClimateControl/762b7293e4a31eea04d1dbd6bf62d7fe9196c6e3/MATLAB/building.mat -------------------------------------------------------------------------------- /MATLAB/economicMPC_sc.m: -------------------------------------------------------------------------------- 1 | %%% Olivier Leveque & Maxime Maurin -- 7 June 2016 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | 4 | function [xt, yt, ut, t, total_cost] = economicMPC_sc(ssM, param_constraints, param_objective, param_simulation) 5 | 6 | %parameters of the Building Model 7 | A = ssM.A; 8 | Bu = ssM.Bu; 9 | Bd = ssM.Bd; 10 | C = ssM.C; 11 | 12 | %other parameters 13 | nx = length(A); %number of states 14 | ny = size(C,1); %number of outputs 15 | nu = size(Bu,2); %number of inputs 16 | nd = size(Bd,2); %number of disturbances 17 | 18 | %define simulation parameters 19 | N = param_simulation.N; 20 | T = param_simulation.T; 21 | 22 | %define objective parameters 23 | R = param_objective.R; 24 | yref = param_objective.yref; 25 | c = param_objective.c; 26 | S = param_objective.S; 27 | 28 | %define constraints matrix 29 | Mu = param_constraints.Mu; 30 | My = param_constraints.My; 31 | mu = param_constraints.mu; 32 | my = param_constraints.my; 33 | 34 | %define optimization variables 35 | x = sdpvar(nx, N, 'full'); %states 36 | x0 = sdpvar(nx, 1, 'full'); %initial states 37 | y = sdpvar(ny, N, 'full'); %outputs 38 | u = sdpvar(nu, N, 'full'); %inputs 39 | d = sdpvar(nd, N, 'full'); %disturbances 40 | epsilon = sdpvar(ny, N, 'full'); %slack variables 41 | 42 | %define constraints and objective 43 | constraints = []; 44 | objective = 0; 45 | for i = 1:N-1 46 | if i == 1 47 | constraints = [constraints, x(:,i+1) == A*x0 + Bu*u(:,i) + Bd*d(:,i)]; %system dynamics - first step 48 | else 49 | constraints = [constraints, x(:,i+1) == A*x(:,i) + Bu*u(:,i) + Bd*d(:,i)]; %system dynamics 50 | end 51 | constraints = [constraints, y(:,i) == C*x(:,i)]; %observability 52 | constraints = [constraints, Mu*u(:,i) <= mu]; %input constraints 53 | constraints = [constraints, My*y(:,i) <= my + kron([1; -1],epsilon(:,i))]; %soft output constraints 54 | constraints = [constraints, zeros(ny,1) <= epsilon(:,i)]; %slack variable constraints 55 | objective = objective + c*sum(u(:,i)) + epsilon(:,i)'*S*epsilon(:,i); %economic cost function 56 | end 57 | constraints = [constraints, y(:,N) == C*x(:,N)]; %observability 58 | constraints = [constraints, Mu*u(:,N) <= mu]; %terminal input constraints 59 | constraints = [constraints, My*y(:,N) <= my + kron([1; -1], epsilon(:,N))]; %terminal soft output constraints 60 | constraints = [constraints, zeros(ny,1) <= epsilon(:,N)]; %terminal slack variable constraints 61 | objective = objective + c*sum(u(:,N)) + epsilon(:,N)'*S*epsilon(:,N); %terminal economic cost function 62 | 63 | %create a controller 64 | ops = sdpsettings('verbose', 1); 65 | parameters_in = [x0; d(:)]; 66 | solutions_out = u; 67 | controller = optimizer(constraints, objective, ops, parameters_in, solutions_out); 68 | 69 | %simulate the system 70 | option = 1; %simulation with no night-setbacks and no variable cost 71 | [xt, yt, ut, t, ~] = simBuild(controller, T, @shiftPred, N, option); 72 | 73 | %compute total economic cost 74 | total_cost = c*sum(ut(:)); 75 | 76 | end -------------------------------------------------------------------------------- /MATLAB/economicMPC_sc_vc.m: -------------------------------------------------------------------------------- 1 | %%% Olivier Leveque & Maxime Maurin -- 7 June 2016 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | 4 | function [xt, yt, ut, t, total_cost] = economicMPC_sc_vc(ssM, param_constraints, param_objective, param_simulation) 5 | 6 | %parameters of the Building Model 7 | A = ssM.A; 8 | Bu = ssM.Bu; 9 | Bd = ssM.Bd; 10 | C = ssM.C; 11 | 12 | %other parameters 13 | nx = length(A); %number of states 14 | ny = size(C,1); %number of outputs 15 | nu = size(Bu,2); %number of inputs 16 | nd = size(Bd,2); %number of disturbances 17 | 18 | %define simulation parameters 19 | N = param_simulation.N; 20 | T = param_simulation.T; 21 | 22 | %define objective parameters 23 | R = param_objective.R; 24 | yref = param_objective.yref; 25 | S = param_objective.S; 26 | 27 | %define constraints matrix 28 | Mu = param_constraints.Mu; 29 | My = param_constraints.My; 30 | mu = param_constraints.mu; 31 | my = param_constraints.my; 32 | 33 | %define optimization variables 34 | x = sdpvar(nx, N, 'full'); %states 35 | x0 = sdpvar(nx, 1, 'full'); %initial states 36 | y = sdpvar(ny, N, 'full'); %outputs 37 | u = sdpvar(nu, N, 'full'); %inputs 38 | d = sdpvar(nd, N, 'full'); %disturbances 39 | epsilon = sdpvar(ny, N, 'full'); %slack variables 40 | cp = sdpvar(1, N, 'full'); %variable prices 41 | 42 | %define constraints and objective 43 | constraints = []; 44 | objective = 0; 45 | for i = 1:N-1 46 | if i == 1 47 | constraints = [constraints, x(:,i+1) == A*x0 + Bu*u(:,i) + Bd*d(:,i)]; %system dynamics - first step 48 | else 49 | constraints = [constraints, x(:,i+1) == A*x(:,i) + Bu*u(:,i) + Bd*d(:,i)]; %system dynamics 50 | end 51 | constraints = [constraints, y(:,i) == C*x(:,i)]; %observability 52 | constraints = [constraints, Mu*u(:,i) <= mu]; %input constraints 53 | constraints = [constraints, My*y(:,i) <= my + kron([1; -1], epsilon(:,i))]; %soft output constraints 54 | constraints = [constraints, zeros(ny,1) <= epsilon(:,i)]; %slack variable constraints 55 | objective = objective + cp(i)*sum(u(:,i)) + epsilon(:,i)'*S*epsilon(:,i); %economic cost function 56 | end 57 | constraints = [constraints, y(:,N) == C*x(:,N)]; %observability 58 | constraints = [constraints, Mu*u(:,N) <= mu]; %terminal input constraints 59 | constraints = [constraints, My*y(:,N) <= my + kron([1; -1], epsilon(:,N))]; %terminal soft output constraints 60 | constraints = [constraints, zeros(ny,1) <= epsilon(:,N)]; %terminal slack variable constraints 61 | objective = objective + cp(N)*sum(u(:,N)) + epsilon(:,N)'*S*epsilon(:,N); %terminal economic cost function 62 | 63 | %create a controller 64 | ops = sdpsettings('verbose', 1, 'solver', '+gurobi'); 65 | parameters_in = [x0; d(:); cp(:)]; 66 | solutions_out = u; 67 | controller = optimizer(constraints, objective, ops, parameters_in, solutions_out); 68 | 69 | %simulate the system 70 | option = 2; %simulation with variable cost, but no night-setbacks 71 | [xt, yt, ut, t, total_cost] = simBuild(controller, T, @shiftPred, N, option); 72 | 73 | end -------------------------------------------------------------------------------- /MATLAB/economicMPC_sc_vc_sb.m: -------------------------------------------------------------------------------- 1 | %%% Olivier Leveque & Maxime Maurin -- 7 June 2016 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | 4 | function [xt, yt, ut, t, total_cost] = economicMPC_sc_vc_sb(ssM, param_constraints, param_objective, param_simulation) 5 | 6 | %parameters of the Building Model 7 | A = ssM.A; 8 | Bu = ssM.Bu; 9 | Bd = ssM.Bd; 10 | C = ssM.C; 11 | 12 | %other parameters 13 | nx = length(A); %number of states 14 | ny = size(C,1); %number of outputs 15 | nu = size(Bu,2); %number of inputs 16 | nd = size(Bd,2); %number of disturbances 17 | 18 | %define simulation parameters 19 | N = param_simulation.N; 20 | T = param_simulation.T; 21 | 22 | %define objective parameters 23 | R = param_objective.R; 24 | yref = param_objective.yref; 25 | S = param_objective.S; 26 | 27 | %define constraints matrix 28 | Mu = param_constraints.Mu; 29 | My = param_constraints.My; 30 | mu = param_constraints.mu; 31 | my = param_constraints.my; 32 | 33 | %define optimization variables 34 | x = sdpvar(nx, N, 'full'); %states 35 | x0 = sdpvar(nx, 1, 'full'); %initial states 36 | y = sdpvar(ny, N, 'full'); %outputs 37 | u = sdpvar(nu, N, 'full'); %inputs 38 | d = sdpvar(nd, N, 'full'); %disturbances 39 | epsilon = sdpvar(ny, N, 'full'); %slack variables 40 | cp = sdpvar(1, N, 'full'); %variable prices 41 | sb = sdpvar(2*ny, N, 'full'); %variable prices 42 | 43 | %define constraints and objective 44 | constraints = []; 45 | objective = 0; 46 | for i = 1:N-1 47 | if i == 1 48 | constraints = [constraints, x(:,i+1) == A*x0 + Bu*u(:,i) + Bd*d(:,i)]; %system dynamics - first step 49 | else 50 | constraints = [constraints, x(:,i+1) == A*x(:,i) + Bu*u(:,i) + Bd*d(:,i)]; %system dynamics 51 | end 52 | constraints = [constraints, y(:,i) == C*x(:,i)]; %observability 53 | constraints = [constraints, Mu*u(:,i) <= mu]; %input constraints 54 | constraints = [constraints, My*y(:,i) <= my + sb(:,i) + kron([1; -1], epsilon(:,i))]; %soft output constraints 55 | constraints = [constraints, zeros(ny,1) <= epsilon(:,i)]; %slack variable constraints 56 | objective = objective + cp(i)*sum(u(:,i)) + epsilon(:,i)'*S*epsilon(:,i); %economic cost function 57 | end 58 | constraints = [constraints, y(:,N) == C*x(:,N)]; %observability 59 | constraints = [constraints, Mu*u(:,N) <= mu]; %terminal input constraints 60 | constraints = [constraints, My*y(:,N) <= my + sb(:,N) + kron([1; -1], epsilon(:,N))]; %terminal soft output constraints 61 | constraints = [constraints, zeros(ny,1) <= epsilon(:,N)]; %terminal slack variable constraints 62 | objective = objective + cp(N)*sum(u(:,N)) + epsilon(:,N)'*S*epsilon(:,N); %terminal economic cost function 63 | 64 | %create a controller 65 | ops = sdpsettings('verbose', 1, 'solver', '+gurobi'); 66 | parameters_in = [x0; d(:); cp(:); sb(:)]; 67 | solutions_out = u; 68 | controller = optimizer(constraints, objective, ops, parameters_in, solutions_out); 69 | 70 | %simulate the system 71 | option = 3; %simulation with variable cost and night-setbacks 72 | [xt, yt, ut, t, total_cost] = simBuild(controller, T, @shiftPred, N, option); 73 | 74 | end -------------------------------------------------------------------------------- /MATLAB/economicMPC_sc_vc_sb_battery.m: -------------------------------------------------------------------------------- 1 | %%% Olivier Leveque & Maxime Maurin -- 7 June 2016 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | 4 | function [xt, yt, ut, t, et, xbt, total_cost] = economicMPC_sc_vc_sb_battery(ssM, ssModel, param_constraints, param_objective, param_simulation) 5 | 6 | %parameters of the Building Model 7 | A = ssM.A; 8 | Bu = ssM.Bu; 9 | Bd = ssM.Bd; 10 | C = ssM.C; 11 | 12 | %parameters of the Storage Model 13 | a = ssModel.A; 14 | b = ssModel.Bu; 15 | 16 | %other parameters 17 | nx = length(A); %number of states 18 | ny = size(C,1); %number of outputs 19 | nu = size(Bu,2); %number of inputs 20 | nd = size(Bd,2); %number of disturbances 21 | 22 | %define simulation parameters 23 | N = param_simulation.N; 24 | T = param_simulation.T; 25 | 26 | %define objective parameters 27 | R = param_objective.R; 28 | yref = param_objective.yref; 29 | S = param_objective.S; 30 | 31 | %define constraints matrix 32 | Mu = param_constraints.Mu; 33 | My = param_constraints.My; 34 | mu = param_constraints.mu; 35 | my = param_constraints.my; 36 | 37 | Mv = param_constraints.Mv; 38 | Mxb = param_constraints.Mxb; 39 | mv = param_constraints.mv; 40 | mxb = param_constraints.mxb; 41 | 42 | %define optimization variables 43 | x = sdpvar(nx, N, 'full'); %states (building) 44 | x0 = sdpvar(nx, 1, 'full'); %initial states (building) 45 | xb = sdpvar(1, N, 'full'); %states (battery) 46 | xb0 = sdpvar(1, 1, 'full'); %initial states (battery) 47 | e = sdpvar(1, N, 'full'); %power consumption from the grid 48 | v = sdpvar(1, N, 'full'); %charging power of the battery 49 | u = sdpvar(nu, N, 'full'); %power consumption of the HVAC system 50 | y = sdpvar(ny, N, 'full'); %outputs 51 | d = sdpvar(nd, N, 'full'); %disturbances 52 | epsilon = sdpvar(ny, N, 'full'); %slack variables 53 | cp = sdpvar(1, N, 'full'); %variable prices 54 | sb = sdpvar(2*ny, N, 'full'); %variable prices 55 | 56 | %define constraints and objective 57 | constraints = []; 58 | objective = 0; 59 | for i = 1:N-1 60 | if i == 1 61 | constraints = [constraints, x(:,i+1) == A*x0 + Bu*u(:,i) + Bd*d(:,i)]; %system dynamics - first step 62 | constraints = [constraints, xb(i+1) == a*xb0 + b*v(i)]; %battery dynamics - first step 63 | else 64 | constraints = [constraints, x(:,i+1) == A*x(:,i) + Bu*u(:,i) + Bd*d(:,i)]; %system dynamics 65 | constraints = [constraints, xb(i+1) == a*xb(i) + b*v(i)]; %battery dynamics 66 | end 67 | constraints = [constraints, y(:,i) == C*x(:,i)]; %observability 68 | constraints = [constraints, Mu*u(:,i) <= mu]; %input constraints 69 | constraints = [constraints, My*y(:,i) <= my + sb(:,i) + kron([1; -1], epsilon(:,i))]; %soft output constraints 70 | constraints = [constraints, zeros(ny,1) <= epsilon(:,i)]; %slack variable constraints 71 | 72 | constraints = [constraints, v(i) == e(i) - sum(u(:,i))]; %charging power of the battery equation 73 | constraints = [constraints, Mv*v(:,i) <= mv]; %charging power of the battery contraints 74 | constraints = [constraints, Mxb*xb(:,i) <= mxb]; %battery state constraints 75 | constraints = [constraints, 0 <= e(i)]; %power consumption from the grid constraints 76 | 77 | objective = objective + cp(i)*e(i) + epsilon(:,i)'*S*epsilon(:,i); %economic cost function 78 | end 79 | constraints = [constraints, y(:,N) == C*x(:,N)]; %observability 80 | constraints = [constraints, Mu*u(:,N) <= mu]; %terminal input constraints 81 | constraints = [constraints, My*y(:,N) <= my + sb(:,N) + kron([1; -1], epsilon(:,N))]; %terminal soft output constraints 82 | constraints = [constraints, zeros(ny,1) <= epsilon(:,N)]; %terminal slack variable constraints 83 | 84 | constraints = [constraints, v(N) == e(N) - sum(u(:,N))]; %charging power of the battery equation 85 | constraints = [constraints, Mv*v(:,N) <= mv]; %terminal charging power of the battery contraints 86 | constraints = [constraints, Mxb*xb(:,N) <= mxb]; %terminal battery state constraints 87 | constraints = [constraints, 0 <= e(N)]; %terminal power consumption from the grid constraints 88 | 89 | objective = objective + cp(N)*e(N) + epsilon(:,N)'*S*epsilon(:,N); %terminal economic cost function 90 | 91 | %create a controller 92 | ops = sdpsettings('verbose', 1, 'solver', '+gurobi'); 93 | parameters_in = [x0; xb0; d(:); cp(:); sb(:)]; 94 | solutions_out = [u; v; e]; 95 | controller = optimizer(constraints, objective, ops, parameters_in, solutions_out); 96 | 97 | %simulate the system 98 | [xt, yt, ut, t, et, xbt, total_cost] = simBuildStorage(controller, T, @shiftPred, N); 99 | 100 | end -------------------------------------------------------------------------------- /MATLAB/main.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | %%% MPC -- Mini Project %%% 3 | %%% Olivier Leveque & Maxime Maurin %%% 4 | %%% 7 June 2016 %%% 5 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 6 | 7 | clc; 8 | close all; 9 | 10 | yalmip('clear'); 11 | clear all; 12 | 13 | %% Model data 14 | 15 | load building.mat; 16 | load battery.mat; 17 | 18 | % Parameters of the Building Model 19 | A = ssM.A; 20 | Bu = ssM.Bu; 21 | Bd = ssM.Bd; 22 | C = ssM.C; 23 | Ts = ssM.timestep; 24 | 25 | % Parameters of the Storage Model 26 | a = ssModel.A; 27 | b = ssModel.Bu; 28 | 29 | % Installation Test 30 | yalmip('version') 31 | sprintf('The Project files are successfully installed') 32 | 33 | % Other parameters 34 | nx = length(A); %number of states 35 | ny = size(C,1); %number of outputs 36 | nu = size(Bu,2); %number of inputs 37 | nd = size(Bd,2); %number of disturbances 38 | 39 | umax = 15*ones(nu,1); umin = 0*ones(nu,1); %input constraints 40 | ymax = 26*ones(ny,1); ymin = 22*ones(ny,1); %output constraints 41 | 42 | vmax = 20; vmin = -20; 43 | xbmax = 20; xbmin = 0; 44 | 45 | %% Controller Design (Setting-up MPC optimizer) 46 | 47 | figure, 48 | subplot(1,2,1), 49 | stairs(refDist(2:3,:)'); %plot two of disturbance inputs 50 | legend(ssM.disturbance(2:3)); 51 | xlabel('Step-Time: 20min'); 52 | ylabel('Disturbances (kW)'); 53 | subplot(1,2,2), 54 | stairs(refDist(1,:)'); %plot one of disturbance inputs 55 | legend(ssM.disturbance(1)); 56 | xlabel('Step-Time: 20min'); 57 | ylabel('Disturbances (C)'); 58 | 59 | savefig('disturbances.fig'); %save figure 60 | disp('Click on Enter to continue with the Section 1.'); pause; 61 | %% Section 1: tracking MPC 62 | 63 | %define objective parameters 64 | param_objective.R = eye(ny); %cost weight 65 | param_objective.yref = [24 24 24]'; %reference outputs 66 | 67 | %define constraints matrix 68 | param_constraints.Mu = [eye(nu); -eye(nu)]; 69 | param_constraints.My = [eye(ny); -eye(ny)]; 70 | param_constraints.mu = [umax; -umin]; 71 | param_constraints.my = [ymax; -ymin]; 72 | 73 | %Determine the influence of the horizon length on the MPC scheme 74 | param_simulation.horizon_lengths = [4, 15, 30, 50, 72, 90]; %arbitrary values of horizon length 75 | tuning_horizon_length(ssM, param_constraints, param_objective, param_simulation, refDist); 76 | 77 | %define simulation parameters 78 | param_simulation.N = 72; %prediction horizon chosen - 1 day = 72*20min 79 | param_simulation.T = size(refDist,2)-param_simulation.N; %simulation length in time-steps 80 | 81 | %run tracking MPC with no night-setbacks and no variable cost 82 | [xt, yt, ut, t] = trackingMPC(ssM, param_constraints, param_objective, param_simulation); 83 | 84 | %display total economic cost 85 | total_cost = 0.2*sum(ut(:)); 86 | sprintf('Total economic cost: $%d', total_cost) 87 | 88 | disp('Click on Enter to continue with the Section 2.'); pause; 89 | %% Section 2: economic MPC and soft constraints 90 | 91 | %define other objective parameters 92 | param_objective.c = 0.2; %fixed electricity price 93 | param_objective.S = 50*eye(ny); %economic cost weight 94 | 95 | %run economic MPC with no night-setbacks and no variable cost 96 | [xt, yt, ut, t, total_cost] = economicMPC_sc(ssM, param_constraints, param_objective, param_simulation); 97 | 98 | %display total economic cost 99 | sprintf('Total economic cost: $%d', total_cost) 100 | 101 | disp('Click on Enter to continue with the Section 3.'); pause; 102 | %% Section 3: economic, soft constraints, and variable cost 103 | 104 | %run economic MPC with variable cost, but no night-setbacks 105 | [xt, yt, ut, t, total_cost] = economicMPC_sc_vc(ssM, param_constraints, param_objective, param_simulation); 106 | 107 | %display total economic cost 108 | sprintf('Total economic cost: $%d', total_cost) 109 | 110 | disp('Click on Enter to continue with the Section 4.'); pause; 111 | %% Section 4 : Night setbacks 112 | 113 | %run economic MPC with variable cost and night-setbacks 114 | [xt, yt, ut, t, total_cost] = economicMPC_sc_vc_sb(ssM, param_constraints, param_objective, param_simulation); 115 | 116 | %display total economic cost 117 | sprintf('Total economic cost: $%d', total_cost) 118 | 119 | disp('Click on Enter to continue with the Section 5.'); pause; 120 | %% Section 5 : Battery coupled with the building 121 | 122 | %define constraints matrix 123 | param_constraints.Mv = [eye(length(vmax)); -eye(length(vmin))]; 124 | param_constraints.Mxb = [eye(length(xbmax)); -eye(length(xbmin))]; 125 | param_constraints.mv = [vmax; -vmin]; 126 | param_constraints.mxb = [xbmax; -xbmin]; 127 | 128 | %run economic MPC with variable cost, night-setbacks and a battery storage 129 | [xt, yt, ut, t, et, xbt, total_cost] = economicMPC_sc_vc_sb_battery(ssM, ssModel, param_constraints, param_objective, param_simulation); 130 | 131 | %display total economic cost 132 | sprintf('Total economic cost: $%d', total_cost) 133 | 134 | %Analyse the influence of dissipation factor and storage capacity of the 135 | %battery on total economic cost 136 | param_simulation.alpha = [0.7 0.8 0.9]; %arbitrary values of dissipation factor 137 | param_simulation.capacity = [10 20 30 40 50]; %arbitrary values of storage capacity 138 | battery_usage(ssM, ssModel, param_constraints, param_objective, param_simulation); 139 | 140 | disp('Finished.'); -------------------------------------------------------------------------------- /MATLAB/shiftPred.m: -------------------------------------------------------------------------------- 1 | %%% Olivier Leveque & Maxime Maurin -- 7 June 2016 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | 4 | %INPUT: 5 | % t - simulation time-step 6 | % N - Prediction Horizon of your MPC controller 7 | 8 | %OUTPUTS: 9 | % pred - prediction of the disturbance input, over the MPC prediction horizon 10 | % cp - shifted price of electricity consumption over the MPC prediction horizon, at time step t 11 | % sb - shifted comfort constraint off-sets over the MPC prediction horizon, at time step t 12 | 13 | 14 | function [pred, cp, sb] = shiftPred(t, N) 15 | 16 | load building.mat; 17 | 18 | 19 | %% Disturbance Prediction 20 | pred = refDist(:,t:t+N-1); 21 | 22 | 23 | %% Variable Price Prediction 24 | cp = zeros(1,N); 25 | for i = 1:N 26 | if and(10