-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRASP.cpp
116 lines (95 loc) · 3.48 KB
/
GRASP.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
//
// Created by osmangokalp on 5/18/2020.
//
#include "GRASP.h"
#include "LocalSearch.h"
#include <iostream>
#include <vector>
Solution *
GRASP::GRASPMotifSearch(Problem &problem, int l, double alpha, double candidateRatio, bool firstImp,
std::default_random_engine generator, int MAX_EVAL) const {
int n = problem.getN();
int t = problem.getT();
Solution *bestSolution = nullptr; //best solution
int numEval = 0;
int candidateCount = (n - l + 1) * candidateRatio;
int *CL = new int[candidateCount]; //candidate list
int *RCL = new int[candidateCount]; //restricted candidate list (max size: candidate list)
double *scores = new double[candidateCount];
LocalSearch ls;
int GRASPIter = -1;
while (numEval < MAX_EVAL) {
GRASPIter++;
//init solution
int s0 = generator() % (n - l + 1); //random start index
Solution *solution = new Solution(t); //incumbent solution
solution->startIndices[0] = s0;
for (int rowIndex = 1; rowIndex < t; ++rowIndex) {
//construct candidate list CL
std::vector<int> v(n - l + 1);
for (int i = 0; i < v.size(); ++i) {
v[i] = i;
}
for (int i = 0; i < candidateCount; ++i) {
int selectedIndex = generator() % v.size();
int candidate = v[selectedIndex];
CL[i] = candidate;
v.erase(v.begin() + selectedIndex);
}
double max = 0.0;
double min = 1.0;
for (int j = 0; j < candidateCount; ++j) {
solution->startIndices[rowIndex] = CL[j];
problem.evaluateSolution(solution, rowIndex + 1, l);
numEval++;
scores[j] = solution->similarityScore;
if (scores[j] > max) {
max = scores[j];
}
if (scores[j] < min) {
min = scores[j];
}
}
//construct RCL
int RCLSize = 0;
for (int k = 0; k < candidateCount; ++k) {
if (scores[k] >= min + alpha * (max - min)) {
RCL[RCLSize++] = CL[k];
}
}
//select random from RCL
int selectedIndex = generator() % RCLSize;
int selected = RCL[selectedIndex];
solution->startIndices[rowIndex] = selected;
}
//final evaluation of the produced solution before local search
problem.evaluateSolution(solution, t, l);
numEval++;
bool imp;
do {
imp = false;
double before = solution->similarityScore;
ls.oneExchange(problem, l, firstImp, solution, numEval);
double after = solution->similarityScore;
if (after - before > 0) {
imp = true;
}
} while (imp);
//std::cout << "GRASP iteration " << GRASPIter << ", Sol: " << solution->similarityScore << std::endl;
if (bestSolution == nullptr) {
bestSolution = solution;
} else {
if (solution->similarityScore > bestSolution->similarityScore) {
delete bestSolution;
bestSolution = solution;
//std::cout << "\tNEW BEST" << std::endl;
} else {
delete solution;
}
}
}
delete[] CL;
delete[] RCL;
delete[] scores;
return bestSolution;
}