-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevaluate.cpp
399 lines (361 loc) · 13.3 KB
/
evaluate.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (c) 2016 Simon Racz <[email protected]>
#include <iostream>
#include <algorithm>
#include <tuple>
#include <functional>
#include <sstream>
#include <fstream>
#include <vector>
#include <exception>
#include <memory>
#include <numeric>
#include <iterator>
#include <iomanip>
#include "cxxopts.hpp"
/**
Handles command line options.
*/
class Options {
private:
std::string mPathTr;
std::string mPathPr;
int mDimension = 0;
int mLength = 0;
bool mHelp = false;
cxxopts::Options options;
void ensureConsistency() {
if (mHelp) {
return;
}
if (mDimension == 0) {
throw cxxopts::OptionException("Please add the set's dimension.");
}
if (mLength == 0) {
throw cxxopts::OptionException("Please add the set's queue length.");
}
if (mPathTr.length() == 0) {
throw cxxopts::OptionException("Path to training set can't be empty.");
}
if (mPathPr.length() == 0) {
throw cxxopts::OptionException("Path to predicted solutions can't be empty.");
}
}
public:
Options() : options("evaluate", "Compares given algorithm result to the First Fit algorithm.") {
options.add_options()
("t,file_tr", "Training set file with optimal solutions", cxxopts::value<std::string>(mPathTr))
("p,file_pr", "Predicted solutions", cxxopts::value<std::string>(mPathPr))
("d,dim", "Dimension of the items (required)", cxxopts::value<int>(mDimension))
("l,length", "Length of the queues (required)", cxxopts::value<int>(mLength))
("h,help", "Prints help", cxxopts::value<bool>(mHelp))
;
}
bool parseCMDLine(int argc, char* argv[]) {
try {
options.parse(argc, argv);
ensureConsistency();
} catch(const cxxopts::OptionException& e) {
std::cerr << "error parsing options: " << e.what() << std::endl;
return false;
}
return true;
}
std::string getPathTr() const {return mPathTr;}
std::string getPathPr() const {return mPathPr;}
int getDimension() const {return mDimension;}
int getLength() const {return mLength;}
bool isHelp() const {return mHelp;}
std::string helpMessage() const {return options.help({""});}
void print() const {
std::cout << "options = {"
<< "\n file_tr: " << mPathTr
<< ",\n file_pr: " << mPathPr
<< ",\n dimension: " << mDimension
<< ",\n length: " << mLength
<< ",\n help: " << mHelp
<< "\n}" << std::endl;
}
};
std::vector<int> readInput(const std::string& path, int totalLength) {
std::vector<int> ret;
auto fs = std::ifstream(path, std::ios::in);
std::string line;
while (std::getline(fs, line)) {
std::istringstream ss{line};
int input;
for (int i = 0; i < totalLength; ++i) {
if (ss >> input) {
ret.push_back(input);
}
}
}
return ret;
}
std::vector<int> wasteForOptimum(const std::vector<int>& queues, int length, int dimension) {
int sampleMargin = 2 * length * dimension + length * (length + 1);
int assignmentMargin = 2 * length * dimension;
int sampleSize = queues.size() / sampleMargin;
std::vector<int> ret;
ret.reserve(sampleSize);
// samples
for (int k = 0; k < sampleSize; ++k) {
int waste = 0;
// tasks
for (int i = 0; i < length; ++i) {
// assigned to Node 0
if (queues[sampleMargin * k + assignmentMargin + i * (length + 1)] == 1) {
for (int d = 0; d < dimension; ++d) {
waste += queues[sampleMargin * k + length * dimension + i * dimension + d];
}
}
}
ret.push_back(waste);
}
return ret;
}
std::vector<int> wasteForSet(const std::vector<int>& queues,
const std::vector<int>& prediction,
int length,
int dimension) {
int sampleMarginInQueue = 2 * length * dimension + length * (length + 1);
int sampleMargin = length * (length + 1);
int sampleSize = prediction.size() / sampleMargin;
std::vector<int> ret;
ret.reserve(sampleSize);
// samples
for (int k = 0; k < sampleSize; ++k) {
int waste = 0;
// tasks
for (int i = 0; i < length; ++i) {
// assigned to Node 0
if (prediction[sampleMargin * k + i * (length + 1)] == 1) {
for (int d = 0; d < dimension; ++d) {
waste += queues[sampleMarginInQueue * k + length * dimension + i * dimension + d];
}
}
}
ret.push_back(waste);
}
return ret;
}
std::vector<int> extractResources(const std::vector<int>& queues, int sample, int length, int dimension) {
std::vector<int> ret;
ret.reserve(length * dimension);
int sampleMargin = 2 * length * dimension + length * (length + 1);
for (int i = 0; i < length; ++i) {
for (int d = 0; d < dimension; ++d) {
ret.push_back(queues[sample * sampleMargin + i * dimension + d]);
}
}
return ret;
}
struct SortableTask {
int originalIndex = 0;
int assignment = 0;
std::vector<int> resources;
};
double harmonicMean(const std::vector<int>& nums) {
if (nums[0] == 0) {
return 0;
}
double sum = 0;
for (auto& item : nums) {
sum += 1. / (double)item;
}
return nums.size() / sum;
}
std::vector<SortableTask> sortedTasks(const std::vector<int>& queues, int sample, int length, int dimension) {
std::vector<SortableTask> ret;
ret.reserve(length);
int sampleMargin = 2 * length * dimension + length * (length + 1);
for (int i = 0; i < length; ++i) {
SortableTask task;
task.originalIndex = i;
for (int d = 0; d < dimension; ++d) {
task.resources.push_back(queues[sample * sampleMargin + length * dimension + i * dimension + d]);
}
ret.push_back(task);
}
std::sort(ret.begin(), ret.end(), [] (const SortableTask& left, const SortableTask& right) -> bool {
return harmonicMean(left.resources) > harmonicMean(right.resources);
});
return ret;
}
std::vector<int> checkPrediction(const std::vector<int>& queues,
const std::vector<int>& prediction,
int length,
int dimension) {
std::vector<int> ret;
ret.reserve(prediction.size());
int sampleMarginInQueue = 2 * length * dimension + length * (length + 1);
int sampleMargin = length * (length + 1);
int sampleSize = prediction.size() / sampleMargin;
// samples
for (int k = 0; k < sampleSize; ++ k) {
auto resources = extractResources(queues, k, length, dimension);
// tasks
for (int i = 0; i < length; ++i) {
int assignment = 0;
// assignment
for (int j = 0; j < (length + 1); ++j) {
if (prediction[sampleMargin * k + i * (length + 1) + j] == 1) {
assignment = j;
break;
}
}
if (assignment != 0) {
bool valid = true;
for (int d = 0; d < dimension; ++d) {
resources[(assignment - 1) * dimension + d]
-= queues[sampleMarginInQueue * k + length * dimension + i * dimension + d];
if (resources[(assignment - 1) * dimension + d] < 0) {
valid = false;
}
}
if (!valid) {
for (int d = 0; d < dimension; ++d) {
resources[i * dimension + d]
+= queues[sampleMarginInQueue * k + length * dimension + i * dimension + d];
}
ret.push_back(1);
for (int m = 0; m < length; ++m) {
ret.push_back(0);
}
continue;
}
}
for (int m = 0; m < (length + 1); ++m) {
ret.push_back(prediction[sampleMargin * k + i * (length + 1) + m]);
}
}
}
return ret;
}
std::vector<int> worstPrediction(int sampleSize, int length) {
std::vector<int> ret;
ret.reserve(sampleSize * length * (length + 1));
for (int k = 0; k < sampleSize; ++k) {
for (int i = 0; i < length; ++i) {
ret.push_back(1);
for (int j = 0; j < length; ++j) {
ret.push_back(0);
}
}
}
return ret;
}
bool tryAssignTaskToNode(std::vector<int>& workQueue, const SortableTask& task, int nodeId) {
bool valid = true;
int dimension = task.resources.size();
for (int d = 0; d < dimension; ++d) {
workQueue[nodeId * dimension + d] -= task.resources[d];
if (workQueue[nodeId * dimension + d] < 0) {
valid = false;
}
}
if (!valid) {
for (int d = 0; d < dimension; ++d) {
workQueue[nodeId * dimension + d] += task.resources[d];
}
}
return valid;
}
std::vector<int> calculateFirstFit(const std::vector<int>& queues, int length, int dimension) {
std::vector<int> ret;
int sampleSize = queues.size() / (2 * length * dimension + length * (length + 1));
ret.reserve(sampleSize * length * (length + 1));
for (int k = 0; k < sampleSize; ++k) {
auto resources = extractResources(queues, k, length, dimension);
auto tasks = sortedTasks(queues, k, length, dimension);
for (int i = 0; i < length; ++i) {
for (int j = 0; j < length; ++j) {
if (tryAssignTaskToNode(resources, tasks[i], j)) {
tasks[i].assignment = j + 1;
break;
}
}
}
for (int i = 0; i < length; ++i) {
// linear search, could do better
for (auto& task : tasks) {
if (task.originalIndex == i) {
for (int d = 0; d < (length + 1); ++d) {
if (task.assignment == d) {
ret.push_back(1);
} else {
ret.push_back(0);
}
}
break;
}
}
}
}
return ret;
}
struct Stats {
double mean = 0;
double variance = 0;
};
Stats calculateStatistics(const std::vector<int>& wasteWorstPred,
const std::vector<int>& wasteOpt,
const std::vector<int>& wastePred) {
int sampleSize = wasteOpt.size();
Stats stats;
double prevMean = 0;
double mean = 0;
double variance = 0;
for (int i = 0; i < sampleSize; ++i) {
prevMean = mean;
// normalize
double divider = wasteWorstPred[i] - wasteOpt[i];
divider = std::max(1., divider);
double shift = wasteOpt[i];
double item = (wastePred[i] - shift) / divider;
mean = prevMean + (item - prevMean) / (i + 1);
variance = variance + (item - prevMean) * (item - mean);
}
stats.mean = mean;
stats.variance = variance;
return stats;
}
void printStatistics(const std::vector<int>& wasteWorstPred,
const std::vector<int>& wasteOpt,
const std::vector<int>& wastePred,
const std::vector<int>& wasteFF) {
int sampleSize = wasteOpt.size();
auto predStats = calculateStatistics(wasteWorstPred, wasteOpt, wastePred);
auto ffStats = calculateStatistics(wasteWorstPred, wasteOpt, wasteFF);
std::cout << "\nComparison of wasted resources for First Fit (FF) and provided prediction.\n";
std::cout << "\nThe waste is normalized. Optimal solution has mean 0 and the worst solution has mean 1.\n\n";
std::cout << "First Fit\n\nMean: " << ffStats.mean << "\nStandard deviation: " << std::sqrt(ffStats.variance / sampleSize);
std::cout << "\n\nCustom Algorithm\n\nMean: " << predStats.mean
<< "\nStandard deviation: " << std::sqrt(predStats.variance / sampleSize) << std::endl;
std::cout << std::endl;
}
int main(int argc, char* argv[]) {
Options opts;
if (!opts.parseCMDLine(argc, argv)) {
return 1;
}
if (opts.isHelp())
{
std::cout << opts.helpMessage() << std::endl;
return 0;
}
// opts.print();
int length = opts.getLength();
int dim = opts.getDimension();
auto queues = readInput(opts.getPathTr(), 2 * length * dim + length * (length + 1));
auto prediction = readInput(opts.getPathPr(), length * (length + 1));
prediction = checkPrediction(queues, prediction, length, dim);
int sampleSize = prediction.size() / (length * (length + 1));
auto firstFit = calculateFirstFit(queues, length, dim);
auto worstPred = worstPrediction(sampleSize, length);
auto wasteOpt = wasteForOptimum(queues, length, dim);
auto wastePred = wasteForSet(queues, prediction, length, dim);
auto wasteWorstPred = wasteForSet(queues, worstPred, length, dim);
auto wasteFF = wasteForSet(queues, firstFit, length, dim);
printStatistics(wasteWorstPred, wasteOpt, wastePred, wasteFF);
return 0;
}