-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perturbation.cpp
39 lines (29 loc) · 944 Bytes
/
Perturbation.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
//
// Created by osmangokalp on 4.06.2020.
//
#include <vector>
#include <random>
#include "Perturbation.h"
int *Perturbation::perturb(Problem &problem, int l, int *sol, int k, std::default_random_engine generator) const {
int t = problem.getT();
int n = problem.getN();
int *solPerturbed = new int[t];
for (int i = 0; i < t; ++i) {
solPerturbed[i] = sol[i];
}
std::vector<int> v(t);
for (int i = 0; i < v.size(); ++i) {
v[i] = i;
}
for (int j = 0; j < k; ++j) {
int randRowIndex = generator() % (t); //random row index
int beforeStartIndex = solPerturbed[randRowIndex];
int randStartIndex = beforeStartIndex;
while (beforeStartIndex == randStartIndex) {
randStartIndex = generator() % (n - l + 1);
}
solPerturbed[randRowIndex] = randStartIndex;
v.erase(v.begin() + randRowIndex);
}
return solPerturbed;
}