-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·356 lines (263 loc) · 8.87 KB
/
main.cpp
File metadata and controls
executable file
·356 lines (263 loc) · 8.87 KB
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
#include <bits/stdc++.h>
#include "mobccwr.h"
#include "dataset.h"
#include "baseline.h"
using namespace std;
/** To check Time Limit **/
class Timer {
private:
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1> >;
std::chrono::time_point<clock_t> m_beg;
public:
Timer() : m_beg(clock_t::now()) {}
void reset() { m_beg = clock_t::now(); }
double elapsed() const {
return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
}
}timer;
/** END **/
void readDataset(ifstream &in) {
int numItems, numOutfits;
in >> numItems >> numOutfits;
map <string, PITEM_NODE> M;
for(int i = 0; i < numItems; i++) {
string info;
TPROFIT profit;
TCOST cost;
int type;
in >> info >> type >> profit >> cost;
if(!(cost > 0)) {
cout << info << endl;
}
assert(cost > 0);
assert(type == 0 || type == 1);
if(!i) MIN_ITEM_COST = cost;
else MIN_ITEM_COST = min(MIN_ITEM_COST, cost);
TITEM itemType;
if(!type) itemType = TopWear;
else itemType = BottomWear;
PITEM_NODE itemNode = new ITEM_NODE;
ITEM_NODE_INIT(itemNode, itemType, profit, cost, info);
M[info] = itemNode;
}
for(int i = 0; i < numOutfits; i++) {
PITEMS_EDGE itemEdge = new ITEMS_EDGE;
string u, v; in >> u >> v;
PITEM_NODE item1 = M[u];
PITEM_NODE item2 = M[v];
if(item1->cost >= 2500 - 1e-6 || item2->cost >= 2500 - 1e-6) {}
else NUM_OUTFITS++;
if(!i) MIN_OUTFIT_COST = item1->cost + item2->cost;
else MIN_OUTFIT_COST = min(MIN_OUTFIT_COST, item1->cost + item2->cost);
ITEMS_EDGE_INIT(itemEdge, item1, item2);
}
}
void readDatasetProb(ifstream &in) {
int numItems, numOutfits;
in >> numItems >> numOutfits;
map <string, PITEM_NODE> M;
for(int i = 0; i < numItems; i++) {
string info;
TPROFIT profit;
TCOST cost;
int type;
in >> info >> type >> profit >> cost;
if(!(cost > 0)) {
cout << info << endl;
}
assert(cost > 0);
assert(type == 0 || type == 1);
if(!i) MIN_ITEM_COST = cost;
else MIN_ITEM_COST = min(MIN_ITEM_COST, cost);
TITEM itemType;
if(!type) itemType = TopWear;
else itemType = BottomWear;
PITEM_NODE itemNode = new ITEM_NODE;
ITEM_NODE_INIT(itemNode, itemType, profit, cost, info);
M[info] = itemNode;
}
for(int i = 0; i < numOutfits; i++) {
PITEMS_EDGE itemEdge = new ITEMS_EDGE;
TPROFIT profit;
string u, v;
in >> u >> v >> profit;
assert(abs(profit) > 1e-9);
PITEM_NODE item1 = M[u];
PITEM_NODE item2 = M[v];
if(item1->cost >= 2500 - 1e-6 || item2->cost >= 2500 - 1e-6) {}
else NUM_OUTFITS++;
if(!i) MIN_OUTFIT_COST = item1->cost + item2->cost;
else MIN_OUTFIT_COST = min(MIN_OUTFIT_COST, item1->cost + item2->cost);
ITEMS_EDGE_INIT(itemEdge, item1, item2, profit);
}
}
PRESULT solve(TPARAM alpha, TPARAM beta, TCOST budget) {
GRAPH G(alpha, beta);
PRESULT result = new RESULT;
double executionTimeRatio = 0.0;
double executionTimePseudo = 0.0;
timer.reset();
cout << "Ratio part is running...\n";
long long numIterations = 0;
TCOST remBudget = budget;
RETURN_SET ratioReturnSet;
RETURN_SET_INIT(&ratioReturnSet);
while(true) {
numIterations++;
RETURN_SET returnSet = G.BEST_RATIO(remBudget);
if(IS_RETURN_SET_EMPTY(&returnSet)) {
break;
}
assert(returnSet.totalCost <= remBudget + 1e-6);
remBudget -= returnSet.totalCost;
ADD_RETURN_SET_TO_RETURN_SET(&ratioReturnSet, &returnSet);
}
executionTimeRatio = timer.elapsed();
timer.reset();
cout << "Ratio calculated in " << fixed << setprecision(2) << executionTimeRatio << "s.\n\n";
cout << "Pseudo Profit part is running...\n";
RETURN_SET pseudoReturnSet;
RETURN_SET_INIT(&pseudoReturnSet);
// pseudoReturnSet = G.BEST_PSEUDO_PROFIT_EDGE_PROB(budget);
pseudoReturnSet = G.BEST_PSEUDO_PROFIT_EDGE(budget);
executionTimePseudo = timer.elapsed();
cout << "Pseudo Profit calculated in " << fixed << setprecision(2) << executionTimePseudo << "s.\n\n";
if(abs(ratioReturnSet.totalProfit - pseudoReturnSet.totalProfit) <= 1e-9) {
if(ratioReturnSet.totalCost < pseudoReturnSet.totalCost)
result->returnSetAns = ratioReturnSet;
else
result->returnSetAns = pseudoReturnSet;
}
else if(ratioReturnSet.totalProfit > pseudoReturnSet.totalProfit) result->returnSetAns = ratioReturnSet;
else result->returnSetAns = pseudoReturnSet;
result->ALPHA = alpha;
result->BETA = beta;
result->BUDGET = budget;
result->returnSetRatio = ratioReturnSet;
result->returnSetPseudo = pseudoReturnSet;
result->executionTimeRatio = executionTimeRatio;
result->executionTimePseudo = executionTimePseudo;
result->executionTimeTotal = executionTimeRatio + executionTimePseudo;
return result;
}
PRESULT solveRandomBaseline(TPARAM alpha, TPARAM beta, TCOST budget) {
// RANDOM1 R(alpha, beta);
// HIGHPROFIT R(alpha, beta);
LOWCOST R(alpha, beta);
PRESULT result = new RESULT;
RETURN_SET returnSet = R.GET_RESULT(budget);
result->returnSetAns = returnSet;
result->ALPHA = alpha;
result->BETA = beta;
result->BUDGET = budget;
result->returnSetRatio = returnSet;
result->returnSetPseudo = returnSet;
result->executionTimeRatio = 0;
result->executionTimePseudo = 0;
result->executionTimeTotal = 0;
return result;
}
bool VERIFY_RESULT(PRESULT result) {
TPROFIT totalProfit = 0.0;
TCOST totalCost = 0.0;
set<long long> top, bottom;
long long numOutfits = 0;
TPROFIT PS = 0, CS = 0, VS = 0;
if(result->returnSetAns.totalCost > result->BUDGET)
return false;
for(PITEM_NODE item : result->returnSetAns.returnItems) {
assert(top.find(item->itemHash) == top.end());
assert(bottom.find(item->itemHash) == bottom.end());
// assert(item->cost <= 2500);
if(item->type == TopWear) top.insert(item->itemHash);
else bottom.insert(item->itemHash);
totalCost += item->cost;
totalProfit += result->BETA * item->profit;
VS += item->profit;
}
cout << "Topwears:\n";
for(long long u : top) {
cout << AllItems[u]->itemHash << " " << AllItems[u]->cost << " " << AllItems[u]->profit << "\n";
}
cout << "\n";
cout << "Bottomwears:\n";
for(long long u : bottom) {
cout << AllItems[u]->itemHash << " " << AllItems[u]->cost << " " << AllItems[u]->profit << "\n";
}
cout << "\n";
cout << "Outfits:\n";
for(long long u : top) {
for(long long v : bottom) {
if(hashEdges.find({u, v}) != hashEdges.end()) {
long long edgeHash = hashEdges[{u, v}];
totalProfit += result->ALPHA * AllEdges[edgeHash]->profit;
numOutfits++;
CS += AllEdges[edgeHash]->profit;
cout << "[" << AllItems[u]->itemHash << " " << AllItems[v]->itemHash << "]\n";
}
}
}
// cout << "\n";
PS = totalProfit;
if(abs(result->returnSetAns.totalProfit - totalProfit) < 1e-6 && abs(result->returnSetAns.totalCost - totalCost) < 1e-6) {
result->numTop = (long long)top.size();
result->numBottom = (long long)bottom.size();
result->numCompOutfits = numOutfits;
result->PS = PS;
result->CS = CS;
result->VS = VS;
return true;
}
return false;
}
int main()
{
srand(time(NULL));
cout << "Enter Dataset File Path: ";
string inPath;
cin >> inPath;
ifstream in;
try {
in = ifstream(inPath);
}
catch(...) {
cerr << "Invalid path\n";
return 0;
}
cout << "Reading the Dataset....\n";
readDataset(in);
// readDatasetProb(in);
cout << "Done reading...\n\n";
cout << "Preprocessing started.\n";
timer.reset();
PREPROCESS_INIT();
cout << "Done Preprocessing in " << fixed << setprecision(2) << timer.elapsed() << "s.\n\n";
string outPath = "./result/result.csv";
ofstream out(outPath);
cout << "Analyzing the data...\n";
ANALYZE_DATASET(inPath, out);
cout << "Done Analyzing.\n\n";
TPARAM beta[5] = {0, 0.25, 0.5, 0.75, 1.0};
TPARAM alpha[5] = {1, 0.75, 0.5, 0.25, 0};
out << "MAIN RESULT\n";
out << "Note: Execution time is in milliseconds and prices are in USD.\n\n";
out << "Budget\t" << "Alpha\t" << "Beta\t" << "TotalProfitReturn\t" << "ExecutionTime (in ms)\t" << "NumTopMain\t" << "NumBottomMain\t" << "NumCompOutfitsMain\t" << "ProfitBestRatioEdge\t" << "ProfitBestPseudoNodes\n";
cout << "Running algorithm...\n";
for(int i = 0; i < 5; i++) {
for(TCOST budget = 1000.0; budget <= 5000.0; budget += 500.0) {
// PRESULT result = solveRandomBaseline(alpha[i], beta[i], budget);
PRESULT result = solve(alpha[i], beta[i], budget);
assert(VERIFY_RESULT(result));
// PRINT_RESULT_ROW_BASELINE(result, out);
// PRINT_RESULT_RUNTIME(result, out);
PRINT_RESULT_ROW(result, out);
// PRINT_RESULT_ROW_PSCSVS(result, out);
}
}
// PRESULT result = solve(0.75, 0.25, 1000);
// assert(VERIFY_RESULT(result));
// PRINT_RESULT_ROW(result, out);
cout << "Done !!!\n\n";
return 0;
}