%% Optimize House = Lower level function
function [Objective, EV_x, EV_E, EV_P] = OptimizeHouse(Time, Cost, House)
%% Decision variables
% Declaration of the decision variable
EV_E = sdpvar(1,Time.T,'full');
EV_P = sdpvar(1,Time.T,'full');
EV_x = binvar(1,Time.T,'full');
%% General parameters
Constraints = [];
Objective = 0;
%% Constraints
% Initial conditions of the energy must be set
EV_E(1) = House.SI{1,'SoC_ini'}*House.SI{1,'Energy'};
for t=1:Time.T
% The EV can only charge at home and without exceeding the connection power of the house
Constraints = [Constraints, EV_P(t) + House.occupancy(t)*House.p_baseload(t) <= House.occupancy(t)*House.data{1,'connection'}];
% The energy added in one time slot is bounded, ex for an EV : min and max charging power
Constraints = [Constraints, EV_x(t)*House.SI{1,'P_min'} <= EV_P(t) <= EV_x(t)*House.SI{1,'P_max'}];
end
for t=2:Time.T
% Evolution of the energy
Constraints = [Constraints, EV_E(t) == EV_E(t-1) + House.SI{1,'Eff_ch'}*EV_P(t)*Time.tau/60 - House.travel(t)];
% The total amount of energy is the storage is bounded, ex for an EV : min and max SoC
Constraints = [Constraints, House.SI{1,'SoC_min'} <= EV_E(t)/House.SI{1,'Energy'} <= House.SI{1,'SoC_max'}];
end
% The last time slot of energy must be greater than the first one for limit conditions
Constraints = [Constraints, EV_E(Time.T) >= EV_E(1)];
%% Objective
for t=1:Time.T
Objective = Objective + Cost.c(t)*EV_P(t);
end