-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunRobustMixedAlgorithm.m
226 lines (197 loc) · 9.51 KB
/
runRobustMixedAlgorithm.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
function [Output] = runRobustMixedAlgorithm(timeStepSize, endTime, kwargs)
% Analysis of the solution found by the Algorithm 1 in (), for a L1/Linfty
% mixed uncertainty set. The solution is based on running the shortest path
% algorithm multiple times, for slightly different graphs.
%% Handling inputs:
arguments
timeStepSize (1,1) double {mustBePositive} = 15 % length of a timestep in [s]
endTime (1,1) double {mustBePositive} = 24; % final time in [h]
kwargs.alphaMixed (1,1) double = 0.1;
% Parameter for second uncertainty set (Algorithm 1 in the paper).
kwargs.alphaSpikes (1,1) double = 4*sqrt( getNumTimesteps(timeStepSize, endTime) );
% Parameter for second uncertainty set (Algorithm 1 in paper).
% In equation (7) in the paper, we take Delta_P(t) = alpha_Mixed*std_P(t) and
% Delta_H(t) = alpha_Mixed*std_H(t). Moreover, we take delta_{P,t} = std_P(t),
% delta_{H,t} = std_H(t) and mu_1 = alpha_spikes.
kwargs.PriceIndex (1,1) double {mustBePositive} = 1;
kwargs.BuildingType (1,1) BuildingType {mustBePositive} = BuildingType.ResidentialHIGH;
kwargs.approx (1,1) ApproximationType = ApproximationType.MaxIter;
kwargs.epsilon (1,1) double = 1E-1;
kwargs.maxIter (1,1) double {mustBeGreaterThanOrEqual(kwargs.maxIter, 2)} = 30;
kwargs.dataPath (1,1) string = "../Data"
kwargs.transitionPenalty (1,1) double = 0.01;
kwargs.powerScalingFactor (1,1) double = NaN;
end
% Constants
ABSOLUTE_CERTAINTY_ALPHA = 0;
% Unpack kwargs:
transitionPenalty = kwargs.transitionPenalty;
epsilon = kwargs.epsilon;
iP = kwargs.PriceIndex;
tB = kwargs.BuildingType;
pD = kwargs.dataPath;
psf = kwargs.powerScalingFactor;
%% Load Parameters
[FUEL_MAP, HEAT_MAP, HEAT_TARIFF, MDOT_FUEL_SU, NODES_CONNECTED_TO_ARTIFICIAL_START, ...
NUM_WINDOWS, POWER_MAP, PRICE_kg_f, RoundHourIndices, SV_states, demands_estimate, ...
demands_true, elecTariffs, g, nTimesteps, nTotalNodes, sol_select, stateFromMap, ...
stateToMap, stepsPerHour, timeFrom, transitionPenaltyFlag] = ...
loadParametersForRobustAlgorithms(endTime, tB, pD, psf, timeStepSize);
% Calling this in a loop is bad, because many files are read from the hard-drive inside
% NWI = 3; %Debug
NWI = NUM_WINDOWS-1; % Number of Windows of Interest
% Explanation of the "-1" above:
% The {averaging} windows are used to predict "the next day".
% The last "next day" happens to be in the next year (01/01/2005).
% We are not interested in predicting that day even though we have enough data for it,
% because we have no ground-truth to compare it with later on.
%=> As a result we use NUM_WINDOWS-1 to reflect this
%% Initialize Variables
% Output Data
Output.Power_Generation = zeros(NWI,endTime); %Does not save the power generation at all times, but just at ``XX:00" times.
Output.Heat_Generation = zeros(NWI,endTime);
Output.Fuel_Consumption = zeros(NWI,endTime);
Output.EstimatedCost = zeros(NWI,1);
Output.TrueCost = zeros(NWI,1);
Output.AlgorithmType = AlgorithmType.Mixed;
switch Output.AlgorithmType
case 0
Output.AlgorithmParameters{1} = [];
case 1
AlgorithmParameters.alpha = kwargs.alphaLin;
Output.AlgorithmParameters{1} = AlgorithmParameters;
case 2
AlgorithmParameters.alphaMixed = kwargs.alphaMixed;
AlgorithmParameters.alphaSpikes = kwargs.alphaSpikes;
AlgorithmParameters.ApproximationType = kwargs.approx;
switch kwargs.approx
case {ApproximationType.Additive, ApproximationType.Multiplicative}
AlgorithmParameters.epsilon = kwargs.epsilon;
case ApproximationType.MaxIter
AlgorithmParameters.maxIterations = kwargs.maxIter;
case ApproximationType.Exact
%Do nothing.
otherwise
error('Unsupported Approximation Type');
end
Output.AlgorithmParameters{1} = AlgorithmParameters;
otherwise
error('Unsupported Algorithm Type');
end
Output.AlgorithmParameters{1} = AlgorithmParameters;
%% Run Algorithm
START_DATE = datetime(2004,1,15); END_DATE = dateshift(START_DATE, 'end', 'year');
DATA_DATES = (START_DATE:END_DATE).';
CURRENT_DAY_OFFSET = +1;
isWeekend = weekday(DATA_DATES) == 7 | weekday(DATA_DATES) == 1;
fuelPrice = PRICE_kg_f(iP);
heatTariff = HEAT_TARIFF(iP);
for iW = 1:NWI
%% Computation using the forecast demand
d = demands_estimate(iW);
mElec = 1e3*d.valMean(:,1, 1+isWeekend(iW)); %1e3* - conversion from kWh to W.
mHeat = 1e3*d.valMean(:,2, 1+isWeekend(iW));
sElec = 1e3*d.valStd(:,1, 1+isWeekend(iW));
sHeat = 1e3*d.valStd(:,2, 1+isWeekend(iW));
decided_costs_nospike = assignCostsInternal(...
sol_select, stateFromMap, stateToMap, nTotalNodes, ...
mElec, sElec, mHeat, sHeat, kwargs.alphaMixed, ...
elecTariffs(iW), heatTariff, fuelPrice,...
POWER_MAP, HEAT_MAP, FUEL_MAP, MDOT_FUEL_SU, ...
transitionPenaltyFlag, transitionPenalty, ...
timeFrom, nTimesteps, stepsPerHour, timeStepSize);
decided_costs_withspike = assignCostsInternal(...
sol_select, stateFromMap, stateToMap, nTotalNodes, ...
mElec, sElec, mHeat, sHeat, kwargs.alphaMixed + kwargs.alphaSpikes, ...
elecTariffs(iW), heatTariff, fuelPrice,...
POWER_MAP, HEAT_MAP, FUEL_MAP, MDOT_FUEL_SU, ...
transitionPenaltyFlag, transitionPenalty, ...
timeFrom, nTimesteps, stepsPerHour, timeStepSize);
W_spike = decided_costs_withspike - decided_costs_nospike;
max_W_spike = max(W_spike);
min_W_spike = min(W_spike);
unique_W_spike = unique(W_spike);
nUWS = numel(unique_W_spike);
if (kwargs.approx == ApproximationType.MaxIter)
epsilon = (max_W_spike - min_W_spike) / (kwargs.maxIter - 1);
end
switch kwargs.approx
case ApproximationType.Exact
Thresholds = unique_W_spike;
case {ApproximationType.Additive, ApproximationType.MaxIter}
Thresholds = [min_W_spike:epsilon:max_W_spike,max_W_spike].'; %Add max(W_spike) at the end to assure that the maximum is also taken into account
case ApproximationType.Multiplicative
Thresholds = exp([log(min_W_spike):log(1+epsilon):log(max_W_spike),log(max_W_spike)]).'; %Add max(W_spike) as before. This is a geometric series.
end
nTrs = numel(Thresholds);
if nTrs > nUWS
Thresholds = unique_W_spike;
nTrs = numel(Thresholds);
end
V_costs = zeros(nTrs,1);
V_paths = cell(nTrs,1);
V_edge_paths = cell(nTrs,1);
g = digraph(g.Edges.EndNodes(:,1), g.Edges.EndNodes(:,2), 1:numel(g.Edges.EndNodes(:,1)));
% Retrieve the index list of the edges - saves time later when changing weights.
edgePermutationMap = g.Edges.Weight;
for iT = 1:nTrs
Weights = decided_costs_nospike;
Weights(W_spike > Thresholds(iT)) = inf;
g.Edges.Weight = Weights(edgePermutationMap);
[V_paths{iT}, path_cost, edge_path] = shortestpath(g, 1,max(g.Edges.EndNodes(:,2)), 'Method', 'acyclic');
V_costs(iT) = path_cost + max(W_spike(edge_path));
V_edge_paths{iT} = edge_path;
end
[path_cost, index_path] = min(V_costs);
path_MGT = V_paths{index_path};
path_edge = V_edge_paths{index_path};
[power_MGT, heat_MGT, mdot_MGT] = extractPath(path_MGT, POWER_MAP, HEAT_MAP, FUEL_MAP, SV_states);
%% Check Performance of the Schedule on True Demand
% Replace old cost calculation method, which uses a ZOH for the demands
% and generations, with a new edge-based method that linearly
% interpolates the demand and generation.
d = demands_true(iW + CURRENT_DAY_OFFSET);
mElec = 1e3*d.valMean(:,1, 1+isWeekend(iW)); %1e3* - conversion from kWh to W.
mHeat = 1e3*d.valMean(:,2, 1+isWeekend(iW));
sElec = 1e3*d.valStd(:,1, 1+isWeekend(iW)); % should be zero anyway
sHeat = 1e3*d.valStd(:,2, 1+isWeekend(iW)); % should be zero anyway
true_cost = sum(assignCostsInternal(...
sol_select(path_edge), stateFromMap(path_edge), stateToMap(path_edge), NODES_CONNECTED_TO_ARTIFICIAL_START, ...
mElec, zeros(size(sElec)), mHeat, zeros(size(sHeat)), ABSOLUTE_CERTAINTY_ALPHA, ...
elecTariffs(iW), heatTariff, fuelPrice,...
POWER_MAP, HEAT_MAP, FUEL_MAP, MDOT_FUEL_SU, ...
transitionPenaltyFlag(path_edge), transitionPenalty, ...
timeFrom(path_edge), nTimesteps(path_edge), stepsPerHour, timeStepSize));
%% "Parse" Output Data
Output.Power_Generation(iW,:) = power_MGT(RoundHourIndices);
Output.Heat_Generation(iW,:) = heat_MGT(RoundHourIndices);
Output.Fuel_Consumption(iW,:) = mdot_MGT(RoundHourIndices);
Output.EstimatedCost(iW) = path_cost;
Output.TrueCost(iW) = true_cost;
end
end
function [decided_costs] = assignCostsInternal(...
sol_select, stateFromMap, stateToMap, nTotalNodes, ...
elecDemandMean, elecDemandStd, heatDemandMean, heatDemandStd, demandStandardEnvelope, ...
elecTariff, heatTariff, fuelPrice,...
powerMap, heatMap, fuelMap, mdot_fuel_SU, ...
transitionPenaltyFlag, transitionPenalty, ...
time_from, nTimesteps, stepsPerHour, timeStepSize)
% Apply alpha:
% 1kWh = 3.6e6J
elecDemand = elecDemandMean + demandStandardEnvelope * elecDemandStd;
heatDemand = heatDemandMean + demandStandardEnvelope * heatDemandStd;
% Assign edge costs
decided_costs = assignCosts(...
timeStepSize, powerMap, heatMap, fuelMap, ...
mdot_fuel_SU, nTotalNodes, sol_select, time_from, nTimesteps,...
stateFromMap, stateToMap,...
elecDemand, heatDemand, elecTariff, heatTariff, fuelPrice, ...
transitionPenaltyFlag, transitionPenalty, stepsPerHour);
end
function T = getNumTimesteps(dt, endTime)
SECONDS_PER_MINUTE = 60;
MINUTES_PER_HOUR = 60;
SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
T = endTime * SECONDS_PER_HOUR / dt;
end