-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalSearch.cpp
45 lines (33 loc) · 1.14 KB
/
LocalSearch.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
//
// Created by osmangokalp on 5/31/2020.
//
#include "LocalSearch.h"
void LocalSearch::oneExchange(Problem &problem, int l, bool firstImp, Solution *sol, int &numEval) const {
int n = problem.getN();
int t = problem.getT();
int bestRow = -1, bestStartIndex = -1;
double startScore = sol->similarityScore;
double bestScore = startScore;
for (int row = 0; row < t; ++row) {
int temp = sol->startIndices[row];
for (int si = 0; si < n - l + 1; ++si) {
sol->startIndices[row] = si;
problem.evaluateSolution(sol, t, l);
numEval++;
if (sol->similarityScore > bestScore) {
if (firstImp) { //first imp.
return;
}
bestScore = sol->similarityScore;
bestRow = row;
bestStartIndex = si;
}
}
sol->startIndices[row] = temp; //restore original value
}
if (bestScore > startScore) { //there is an improvement
sol->startIndices[bestRow] = bestStartIndex;
}
problem.evaluateSolution(sol, t, l); //final evaluation
numEval++;
}