-
Notifications
You must be signed in to change notification settings - Fork 0
/
endgame.cpp
559 lines (452 loc) · 16 KB
/
endgame.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include <iostream>
#include <cstdlib>
#include "endgame.h"
#include <iomanip>
#include <fstream>
#include <vector>
#include <queue>
#ifdef DEBUG
#define INPUT_FILE "../inputs/input9.txt"
#define OUTPUT_FILE "../outputs/output9.txt"
#endif // DEBUG
#ifndef DEBUG
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#endif // DEBUG
using namespace std;
struct Rock {
int mass;
int energy;
float ratio;
bool toTake = false;
short availability;
short *cities;
};
struct RockRatio {
short id;
float ratio;
};
bool operator>(const Rock &r1, const Rock &r2);
struct City {
short nRocks = 0;
short *rocks;
short addedRocks = 0;
};
struct CitySol {
short cityId;
short rockId;
};
int getDistance(short city1, short city2);
void input();
void output(CitySol *solution, short *orderedRocks, double gloveEnergy, double rocksEnergy, double time);
void bbTsp(vector<short> *path, int cost, vector<short> *remaining, int n, int i);
void removeVector(vector<short> *v, short x);
int simpleMinPath(CitySol *sol, short *rocksSol);
void printMinPath(vector<short> *minSol);
void printMinCost(int minCost);
void printSolution(CitySol *solution, short *orderedRocks, double gloveEnergy, double rocksEnergy, double time);
double calcSpeed(int W);
double calcEnergy(int rocksEnergy, double totalTime);
void findRocks(CitySol *citySol, short *rocksSol, int *capacityLeft);
void calcFinalResources(CitySol *sol, double *totalTime, double *totalEnergy, int *rocksEnergy);
void chooseRocksToTake();
short nCitiesTot;
short nDifferentRocks;
short source;
int C;
double R;
double vmax;
double vmin;
Rock *rocks;
City *cities;
int **weights;
RockRatio *rocksRatio;
//int globalMinWeight;
//int minCost; //costo di un percorso minimo a caso (il primo percorso che prendo)
//vector<short>* minSol = nullptr;
//vector<short>* remainingBbTsp = nullptr;
//bool operator>(const Rock &r1, const Rock &r2) {
// return r1.ratio < r2.ratio;
//}
int minRockRatioComparator(const RockRatio *r1, const RockRatio *r2) {
return r1->ratio < r2->ratio
? -1
: 1;
}
int main() {
// Rock test[5];
//
// test[0].id = 0; test[0].ratio = 4.6;
// test[1].id = 1; test[1].ratio = 2.3;
// test[2].id = 2; test[2].ratio = 10.55;
// test[3].id = 3; test[3].ratio = 4.9;
// test[4].id = 4; test[4].ratio = 6.8;
//
// for (int i = 0; i < 5; ++i) {
// cout << test[i].ratio << " ";
// }
// cout << endl;
//
// qsort(test, 10, sizeof(Rock),
// reinterpret_cast<int (*)(const void *, const void *)>(minRockRatioComparator));
CitySol *solution;
short *rocksSolution;
int cost, rocksEnergy;
double totalTime = 0, totalEnergy = 0;
#ifdef DEBUG
clock_t startTime, afterInput, afterSolution, afterFinalCalc;
startTime = clock();
#endif // DEBUG
input();
chooseRocksToTake();
#ifdef DEBUG
afterInput = clock();
#endif // DEBUG
solution = new CitySol[nCitiesTot + 1]; // Last cityId is the source
rocksSolution = new short[nDifferentRocks];
cost = simpleMinPath(solution, rocksSolution);
#ifdef DEBUG
afterSolution = clock();
#endif // DEBUG
calcFinalResources(solution, &totalTime, &totalEnergy, &rocksEnergy);
//bbTsp(&sol, 0, remainingBbTsp, nCitiesTot-1, 1);
#ifdef DEBUG
afterFinalCalc = clock();
#endif // DEBUG
output(solution, rocksSolution, totalEnergy, rocksEnergy, totalTime);
#ifdef DEBUG
cout << "Costo: " << cost << endl;
printSolution(solution, rocksSolution, totalEnergy, rocksEnergy, totalTime);
cout << endl << endl;
cout << "Input duration: " << afterInput - startTime << endl;
cout << "Solution creation duration: " << afterSolution - afterInput << endl;
cout << "Solution calc duration: " << afterFinalCalc - afterSolution << endl;
#endif // DEBUG
return 0;
}
void input() {
#ifdef DEBUG
clock_t startTime = clock(), afterRocksInput, afterRocksPerCity, afterRocksSort, afterWeightInput;
#endif // DEBUG
ifstream in(INPUT_FILE);
in >> nCitiesTot >> source;
in >> nDifferentRocks >> C >> R >> vmin >> vmax;
rocks = new Rock[nDifferentRocks];
rocksRatio = new RockRatio[nDifferentRocks];
for (short i = 0; i < nDifferentRocks; ++i) {
in >> rocks[i].mass >> rocks[i].energy; //mass and energy for each rockId
rocks[i].ratio = (float) rocks[i].mass / (float) rocks[i].energy;
//vettore rocce id ratio
rocksRatio[i].id = i;
rocksRatio[i].ratio = rocks[i].ratio;
// cout << "rockId n " << rocks[i].id << " mass: " << rocks[i].mass << " energy: " << rocks[i].energy << " ratio: " << rocks[i].ratio << endl;
}
#ifdef DEBUG
for (int i = 0; i < nDifferentRocks; ++i) {
cout << rocksRatio[i].ratio << " ";
}
cout << endl;
afterRocksInput = clock();
#endif // DEBUG
short city;
cities = new City[nCitiesTot];
// for (int i = 0; i < nDifferentRocks; ++i) { //i-th rockId
// in >> rocks[i].availability;
//
// for (int j = 0; j < rocks[i].availability; ++j) {
// in >> city;
// }
// }
for (short i = 0; i < nDifferentRocks; ++i) { //i-th rockId
in >> rocks[i].availability;
rocks[i].cities = new short[rocks[i].availability];
for (short j = 0; j < rocks[i].availability; ++j) {
in >> city;
rocks[i].cities[j] = city;
cities[city].nRocks++;
}
}
for (short i = 0; i < nCitiesTot; ++i) {
cities[i].rocks = new short[cities[i].nRocks];
}
for (short i = 0; i < nDifferentRocks; ++i) {
for (short j = 0; j < rocks[i].availability; ++j) {
city = rocks[i].cities[j];
cities[city].rocks[cities[city].addedRocks++] = i;
}
delete[] rocks[i].cities;
}
#ifdef DEBUG
afterRocksPerCity = clock();
#endif // DEBUG
// for (int i = 0; i < nCitiesTot; ++i) {
// qsort(cities[i].rocks, cities[i].nRocks, sizeof(short),
// reinterpret_cast<int (*)(const void *, const void *)>(minRockRatioComparator));
// }
#ifdef DEBUG
afterRocksSort = clock();
#endif // DEBUG
weights = new int *[nCitiesTot - 1]; //1 is 0
//inserimento dei pesi tra città
for (short i = 0; i < nCitiesTot - 1; i++) {
weights[i] = new int[i + 1];
for (short j = 0; j < i + 1; j++) {
in >> weights[i][j];
// if(weights[i][j] < globalMinWeight)
// globalMinWeight = weights[i][j];
}
}
#ifdef DEBUG
afterWeightInput = clock();
cout << "Input rocks: " << afterRocksInput - startTime << endl;
cout << "Rocks per cityId: " << afterRocksPerCity - afterRocksInput << endl;
cout << "Rock sort: " << afterRocksSort - afterRocksPerCity << endl;
cout << "Weight input: " << afterWeightInput - afterRocksSort << endl;
cout << endl << endl;
#endif // DEBUG
}
void output(CitySol *solution, short *orderedRocks, double gloveEnergy, double rocksEnergy, double time) {
ofstream out(OUTPUT_FILE);
// //final glove's energy, rocks' energy and time for the journey
out << scientific << setprecision(10) << gloveEnergy << " "; //total glove's energy
out << scientific << setprecision(10) << rocksEnergy << " "; //total rocks energy
out << scientific << setprecision(10) << time << endl; //time used
for (int i = 0; i < nDifferentRocks; ++i) {
out << orderedRocks[i] << " ";
}
out << endl;
for (int i = 0; i <= nCitiesTot; ++i) {
out << solution[i].cityId << " ";
}
out << "\n***";
}
void printSolution(CitySol *solution, short *orderedRocks, double gloveEnergy, double rocksEnergy, double time) {
// ofstream out(OUTPUT_FILE);
// //final glove's energy, rocks' energy and time for the journey
// cout << scientific << setprecision(10) << gloveEnergy << " "; //total glove's energy
// cout << scientific << setprecision(10) << rocksEnergy << " "; //total rocks energy
// cout << scientific << setprecision(10) << time << endl; //time used
cout << "Total energy: " << gloveEnergy << endl;
cout << "Rocks energy: " << rocksEnergy << endl;
cout << "Time: " << time << endl;
cout << "Rocce: " << nDifferentRocks << endl;
for (int i = 0; i < nDifferentRocks; ++i) {
cout << orderedRocks[i] << " ";
}
cout << "\nPercorso: " << nCitiesTot << endl;
for (int i = 0; i <= nCitiesTot; ++i) {
cout << solution[i].cityId << " ";
}
cout << "\n***";
}
void chooseRocksToTake() {
int capacityLeft = C, taken = 0;
qsort(rocksRatio, nDifferentRocks, sizeof(RockRatio),
reinterpret_cast<int (*)(const void *, const void *)>(minRockRatioComparator));
for (int i = 0; i < nDifferentRocks && capacityLeft != 0; ++i) {
if(rocks[rocksRatio[i].id].mass > capacityLeft)
continue;
capacityLeft -= rocks[rocksRatio[i].id].mass;
rocks[rocksRatio[i].id].toTake = true;
taken++;
}
cout << "Taken rocks: " << taken << endl;
}
int simpleMinPath(CitySol *sol, short *rocksSol) { //int rocksWeight, int C
short i, j, swap, minCityIndex;
int minDistance, localDistance, cost = 0, capacityLeft = C;
// double speed, energy, time;
for (i = 0; i < nCitiesTot; i++) {
sol[i].cityId = i;
}
sol[0].cityId = source;
sol[source].cityId = 0;
sol[nCitiesTot].cityId = source;
for (i = 0; i < nDifferentRocks; ++i) {
rocksSol[i] = -1;
}
for (i = nCitiesTot - 1; i >= 0; i--) {
minCityIndex = i;
minDistance = getDistance(sol[i + 1].cityId, sol[i].cityId); //minTime = getDistance(sol[i+1].cityId, sol[i].cityId)/calcSpeed(W);
for (j = i - 1; j > 0; j--) {
localDistance = getDistance(sol[i + 1].cityId, sol[j].cityId);
if (localDistance < minDistance) {
minCityIndex = j;
minDistance = localDistance;
}
}
cost += minDistance;
swap = sol[i].cityId;
sol[i].cityId = sol[minCityIndex].cityId;
sol[minCityIndex].cityId = swap;
// Take rocks
findRocks(&sol[i], rocksSol, &capacityLeft);
}
return cost;
}
void findRocks(CitySol *citySol, short *rocksSol, int *capacityLeft) { //vector<Rock>* cityRocks
auto cityRocks = cities[citySol->cityId].rocks;
auto nRocks = cities[citySol->cityId].nRocks;
if (nRocks == 0 || *capacityLeft == 0) { // cityRocks->empty()
citySol->rockId = -1;
return;
}
// Find maximum ratio for all rockId of the cityId
// for (int i = 0; i < nRocks; ++i) {
// if ((*capacityLeft - rocks[cityRocks[i]].mass) < 0 || rocksSol[cityRocks[i]] != -1)
// continue;
//
// citySol->rockId = cityRocks[i];
// rocksSol[citySol->rockId] = citySol->cityId;
// *capacityLeft -= rocks[citySol->rockId].mass;
// return;
// }
// citySol->rockId = -1;
// for (int i = 0; i < cityRocks->size(); ++i) {
// if (*capacityLeft - ((*cityRocks).top()).mass < 0 || rocksSol[(*cityRocks).top().id] != -1)
// continue;
//
// cityId->rockId = (*cityRocks).top().id;
// rocksSol[cityId->rockId] = cityId->cityId;
// *capacityLeft -= rocks[cityId->rockId].mass;
// return;
// }
Rock* localRock;
float localRatio;
short maxRockId;
float maxRockRatio = 0;
for (int i = 0; i < nRocks; ++i){
localRock = rocks + cityRocks[i];
// if(*capacityLeft - rocks[cityRocks[i]].mass < 0 || rocksSol[cityRocks[i]] != -1)
if(!localRock->toTake)
continue;
if(localRock->availability < 20) {
maxRockId = cityRocks[i];
maxRockRatio = localRatio;
break;
}
localRatio = localRock->ratio / (float) localRock->availability;
localRock->availability--;
if(localRatio > maxRockRatio) {
maxRockId = cityRocks[i];
maxRockRatio = localRatio;
}
}
if(maxRockRatio == 0)
citySol->rockId = -1;
else {
citySol->rockId = maxRockId;
rocksSol[maxRockId] = citySol->cityId;
*capacityLeft -= rocks[maxRockId].mass;
rocks[maxRockId].toTake = false;
}
}
//int fullMinPathSearch(CitySol* sol, short* rocksSol) {
//
//}
void calcFinalResources(CitySol *sol, double *totalTime, double *totalEnergy, int *rocksEnergy) {
int rocksWeight = 0, distance;
double speed, time;
*rocksEnergy = 0;
*totalTime = 0;
for (short i = 0; i < nCitiesTot; i++) {
if (sol[i].rockId != -1) {
rocksWeight += rocks[sol[i].rockId].mass;
*rocksEnergy += rocks[sol[i].rockId].energy;
}
distance = getDistance(sol[i].cityId, sol[i + 1].cityId);
speed = calcSpeed(rocksWeight);
time = distance / speed;
*totalTime += time;
}
*totalEnergy = calcEnergy(*rocksEnergy, *totalTime);
}
int getDistance(short city1, short city2) {
return city1 < city2
? weights[city2 - 1][city1]
: weights[city1 - 1][city2];
}
double calcSpeed(int W) {
if (C != 0)
return vmax - W * ((vmax - vmin) / C);
return vmax;
}
double calcEnergy(int rocksEnergy, double totalTime) {
return rocksEnergy - R * totalTime;
}
void removeVector(vector<short> *v, short x) {
for (auto it = v->begin(); it != v->end(); it++) {
if (*it == x) {
v->erase(it);
return;
}
}
}
int calcLb(short origin, vector<short> *remaining, int cost) {
int out, back, transfer, costLocal, minTransferOut, localTransferOut;
//out = INT_MAX;
//back = INT_MAX;
// transfer = INT_MAX;
// out = getDistance(origin, (*remaining)[0]);
// back = getDistance((*remaining)[0], source);
// transfer = out + getDistance((*remainingBbTsp)[0], (*remainingBbTsp)[1]);
for (int i = 0; i < remaining->size(); i++) {
// calculate minimum cost of exiting edges at this point
costLocal = getDistance(origin, (*remaining)[i]);
if (costLocal < out) {
out = costLocal;
}
// Calculate for each cityId the best way to pass into it (best duo entry + exit)
// origin --> qualsiasi nodo + qualsiasi nodo
// minTransferOut = INT_MAX;
// for (int j = 0; j < remaining->size(); j++){ //consider the remainingBbTsp nodes
// if(i != j){
// localTransferOut = getDistance((*remaining)[i], (*remaining)[j]);
// if(localTransferOut < minTransferOut)
// minTransferOut = localTransferOut;
// }
// }
// transfer += minTransferOut;
//calculate lb cost to go back to source of the nearest cities
costLocal = getDistance((*remaining)[i], source);
if (costLocal < back) {
back = costLocal;
}
}
// transfer = (globalMinWeight * remaining->size());
// TODO: Try to remove /2 for remainingBbTsp nodes > 10 or 15 for example
// return cost + ((out + back + transfer) / 2);
return cost + out + back + transfer;
}
void bbTsp(vector<short> *path, int cost, vector<short> *remaining, int n, int i) {
// short origin = path->back();
// vector<short> choices(*remaining);
// int lb, localCost;
//
// for (auto it = choices.begin() ; it != choices.end(); ++it) {
// localCost = cost + getDistance(origin, *it);
// path->push_back(*it);
// removeVector(remaining, *it);
//
// if(i < n) {
// lb = calcLb(*it, remaining, localCost);
// if(lb < minCost){
// bbTsp(path, localCost, remaining, n, i + 1);
// }
// } else {
// cost = localCost;
// cost += getDistance(*it, (*path)[0]); //path->front() Returns a reference to the first element in the vector.
// if(cost < minCost) {
// if(minSol) {
// delete minSol;
// minSol = nullptr;
// }
// minSol = new vector<short>(*path);
// minCost = cost;
// }
// }
//
// path->pop_back();
// remaining->push_back(*it);
// }
}