-
Notifications
You must be signed in to change notification settings - Fork 2
/
runAlgorithms_1p.m
208 lines (181 loc) · 7.12 KB
/
runAlgorithms_1p.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
function [] = runAlgorithms_1p(fuelIdx, iter, psf)
arguments
fuelIdx (1,:) double = []
iter (1,1) double {mustBePositive, mustBeInteger} = 1
psf (1,1) = NaN % power scaling factor (of the consumer's demand, relative to the turbine's capacity)
end
%% Preparations for running on a cluster
patchJobStorageLocation(); % Fix for a race condition related to temporary files
try PBSinfo(); catch, end % Record job info in the log file
%% Constants
OUTPUT_FOLDER = "../Data/Results";
if ~isfolder(OUTPUT_FOLDER), mkdir(OUTPUT_FOLDER); end
RUN_BANK = [391,... Benchmark 1:1 (1)
392,... Nominal 2:2 (1)
395,400,403,404,405,406,407,408,... L_inf 3:10 (8)
411,413,421,422,424,432,433,435,440,443,444,446,... Mixed1 11:22 (12)
454,455,457,458,461,462,463,464,465,466,467,468,... Mixed2 23:34 (12)
473,474,475,476,477,478,479,480,484,485,487,488,... Mixed3 35:46 (12)
490,498,499,501,509,510,512,519,520].'; % Mixed4 47:55 (9)
iter = RUN_BANK(iter);
%% Choose Running Algorithms
runBenchmark = 1;
runNominal = 1;
runRobustLinfty = 1;
LinftyAlphas = [0.01:0.01:0.05, 0.1:0.05:0.7];
runRobustMixed = 1;
mixedAlphas = repmat([0.01:0.01:0.05,0.07,0.1,0.13,0.15,0.17,0.2],[1,10]);
mixedSpikeAlphas = [.5*sqrt(5760)*ones(1,11),...
1*sqrt(5760)*ones(1,11),...
1.5*sqrt(5760)*ones(1,11),...
2*sqrt(5760)*ones(1,11),...
2.5*sqrt(5760)*ones(1,11),...
3*sqrt(5760)*ones(1,11),...
3.5*sqrt(5760)*ones(1,11),...
4*sqrt(5760)*ones(1,11),...
1*5760*ones(1,11),...
2*5760*ones(1,11)];
%% Choose Prices and Building Combinations
%i-th scenario has building type BuildingTypes(i) and the index of the gas
%price is PriceIndices(i).
%
%The building type should be in the set {1,2,3,4}, and the price index
%should be in the set {1 ... numFuelPrices}
BUILDINGS_TO_TEST = BuildingType(1:4);
nBuildTypes = numel(BUILDINGS_TO_TEST);
nFuelPrices = numel(NATURAL_GAS_PARAMS());
[buildingTypes, priceIndices] = getCaseIndices(nBuildTypes, nFuelPrices);
%% Reporting on the current run:
if nargin > 0 && ~isempty(fuelIdx)
tsdisp("Only fuelIdx = " + mat2str(fuelIdx) + " will be considered!")
keepIdx = priceIndices == fuelIdx;
priceIndices = priceIndices(keepIdx);
buildingTypes = buildingTypes(keepIdx);
else
tsdisp("All " + nFuelPrices + " fuelIdx will be considered!")
end
%% Assert and initilize variables
assert(numel(buildingTypes) == numel(priceIndices), ...
'Incompatible number of building types and gas price indices.');
assert(numel(mixedAlphas) == numel(mixedSpikeAlphas), ...
'Incompatible number of parameters for robust mixed algorithm.');
numScenarios = numel(buildingTypes);
numRobustLinfty = numel(LinftyAlphas);
numRobustMixed = numel(mixedAlphas);
% Prepare arrays to allow a single itertor to go over all
% algorithm-scenario pairs.
scenarioList = 1:numScenarios;
algorithmList = 1:(1*runBenchmark + 1*runNominal + numRobustLinfty*runRobustLinfty + numRobustMixed *runRobustMixed);
numAlgorithms = numel(algorithmList);
[scenarios, algorithms] = meshgrid(scenarioList, algorithmList);
numPairs = numel(scenarios);
scenarios = scenarios(:);
algorithms = algorithms(:);
%-1 = Benchmark. 0 = Nominal. 1 = Robust Linfty. 2 = Robust Mixed.
algorithmTypeKey = [-ones(runBenchmark,1);
zeros(runNominal,1);
ones(numRobustLinfty*runRobustLinfty,1);
2*ones(numRobustMixed *runRobustMixed,1)];
algorithmType = algorithmTypeKey(algorithms);
algorithmParameterKey = [1:runBenchmark,1:runNominal,1:numRobustLinfty*runRobustLinfty,1:numRobustMixed *runRobustMixed]';
algorithmParameters = algorithmParameterKey(algorithms);
% Prepare structure for output data
%{
outputData(numPairs).Data.Power_Generation = [];
outputData(numPairs).Data.Heat_Generation = [];
outputData(numPairs).Data.Fuel_Consumption = [];
outputData(numPairs).Data.EstimatedCost = [];
outputData(numPairs).Data.TrueCost = [];
outputData(numPairs).Data.AlgorithmType = [];
outputData(numPairs).Data.AlgorithmParameters{1} = [];
outputData(numPairs).BuildingType = 0;
outputData(numPairs).PriceIndex = 0;
%}
%% Run Algorithms on Scenarios
nSc = numel(scenarios);
assert(iter <= nSc);
% In case of batch execution (running on a cluster)
if batchStartupOptionUsed()
% Unpack scenario configurations:
jScenario = scenarios(iter);
iAlgorithm = algorithms(iter);
algType = algorithmType(iter);
algParam = algorithmParameters(iter);
building = BUILDINGS_TO_TEST(buildingTypes(jScenario));
priceInd = priceIndices(jScenario);
% Determine if we need to compute the current file:
fName = makeFilename(iter, algType, algParam, building, priceInd, psf);
fPath = fullfile(OUTPUT_FOLDER, fName);
if isfile(fPath)
tsdisp(fName + " already exists - stopping.");
return
else
tsdisp("Starting work on: " + fName);
% Create an empty file (so that other workers will skip it):
emptyFile(fPath);
end
% Perform the computation
tv = tic();
switch algType
case -1
out = runBenchmarkAlgorithm('PriceIndex',priceInd,...
'BuildingType', building,...
'powerScalingFactor', psf);
case 0
out = runNominalAlgorithm('PriceIndex',priceInd,...
'BuildingType', building,...
'powerScalingFactor', psf);
case 1
out = runRobustLinftyAlgorithm('PriceIndex',priceInd,...
'BuildingType', building,...
'alpha', LinftyAlphas(algParam),...
'powerScalingFactor', psf);
case 2
out = runRobustMixedAlgorithm('PriceIndex',priceInd,...
'BuildingType',building,...
'alphaMixed', mixedAlphas(algParam),...
'alphaSpikes', mixedSpikeAlphas(algParam),...
'powerScalingFactor', psf);
end
parsave(fPath, out, iter, algType, algParam, building, priceInd, psf);
%{
outputData(iter).Data = out;
outputData(iter).BuildingType = building;
outputData(iter).PriceIndex = priceInd;
%}
tsdisp("Iteration #" + iter + " took " + round(toc(tv)) + "sec.");
end
end
function emptyFile(fPath)
% Method 1:
fclose(fopen(fPath, 'w'));
% Method 2:
% parsave(fPath, [], [], [], [], [], []);
end
function parsave(fName, outputData, iter, algType, algParam, buildingId, fuelPriceId, psf)
save(fName, 'outputData', 'iter', 'algType', 'algParam', 'buildingId', 'fuelPriceId', 'psf');
end
function [buildIdx, priceIdx] = getCaseIndices(nBuildings, nFuelPrices)
buildIdx = repmat(1:nBuildings, 1, nFuelPrices);
priceIdx = repelem(1:nFuelPrices, 1, nBuildings);
end
function [nameStr] = makeFilename(iter, algType, algParams, bldgTypeId, fuelPriceId, powerScalingFactor)
% Returns the name of the file into which to save intermediate results.
% NOTE: The formatspec may need to be modified if the simulation grid is modified
% significantly (e.g., more than 1E4-1 iterations).
nameStr = compose("I%04u_AT%02d_AP%03u_B%1u_F%1u_PSF%4.2f.mat", ...
iter, algType, algParams, bldgTypeId, fuelPriceId, powerScalingFactor);
end
function [] = tsdisp(msg, ts)
arguments
msg (1,1) string
ts (1,1) datetime = datetime('now')
end
disp(datestr(ts,"[dd-mm-yyyy HH:MM:SS.FFF] ") + msg);
end
function PBSinfo()
id = getenv('PBS_JOBID');
name = getenv('PBS_JOBNAME');
ncpus = getenv('NCPUS');
tsdisp("PBS job info: ID=" + id + ", Name=" + name + ", #CPUs=" + ncpus);
end