-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitialSolver.cpp
More file actions
87 lines (70 loc) · 1.75 KB
/
Copy pathInitialSolver.cpp
File metadata and controls
87 lines (70 loc) · 1.75 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
#include "InitialSolver.hpp"
#include <ctime>
#include "Utility.hpp"
std::pair<int, bool> InitialSolver::firstPossiblePosition(G::Graph *g, int* res, int* indexexams, int n, int tmax) {
int i, j;
bool* available = new bool[n];
std::pair<G::Edge, bool> e;
std::pair<int, bool> max;
max.first = 0;
max.second = true;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
available[j] = true;
for (j = 0; j < i; j++) {
e = edge(indexexams[i], indexexams[j], *g);
if (e.second) {
available[res[j] - 1] = false;
}
}
for (j = 0; j < n; j++) {
if (available[j]) {
res[i] = j + 1;
if (j > max.first)
max.first = j;
if (j >= tmax)
max.second = false;
break;
}
}
}
delete[] available;
max.first++;
return max;
}
int InitialSolver::squeakyWheel(G::Graph* g, Solution* sol) {
int* blame = new int[sol->n];
int* indexexams = new int[sol->n];
int* soltemp = new int[sol->n];
int i, overblame = sol->tmax / 2;
std::pair<int, bool> temp;
temp.second = false;
srand(time(NULL));
int* weight = new int[sol->n];
for (int i = 0; i < sol->n; i++) {
blame[i] = 0;
indexexams[i] = i;
weight[i] = rand();
}
mergeSort(indexexams, weight, sol->n);
delete[] weight;
while (!temp.second) {
temp = InitialSolver::firstPossiblePosition(g, soltemp, indexexams, sol->n, sol->tmax);
if (!temp.second) {
for (i = 0; i < sol->n; i++) {
blame[i] += soltemp[i]; // blame[i] += i;
if (sol->sol[i] > sol->tmax)
blame[i] += overblame;
}
mergeSort(indexexams, blame, sol->n);
reverseVector(indexexams, sol->n);
reverseVector(blame, sol->n);
}
}
for (i = 0; i < sol->n; i++)
sol->sol[indexexams[i]] = soltemp[i];
delete[] soltemp;
delete[] indexexams;
delete[] blame;
return temp.first;
}