-
Notifications
You must be signed in to change notification settings - Fork 0
/
cromossome.cpp
75 lines (67 loc) · 1.71 KB
/
cromossome.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
#include "cromossome.hpp"
Cromossome::Cromossome(int len_) : len(len_) {
score = 0.0;
n_forbidden = 0;
gene = vector<int>(len, 0);
acc = vector<int>(len, 0);
}
void Cromossome::add(int u) {
if (gene[u]) return;
double increment = 0.0;
gene[u] = 1;
for (int i = 0; i < len; i++) {
if (i == u) {
increment += weights[i][i];
} else {
increment += (weights[u][i] + weights[i][u]) * gene[i];
}
}
score += increment;
for (int i = 0; i < reversed_triples[u].size(); i++) {
int val = reversed_triples[u][i];
acc[val]++;
if (acc[val] == 3) {
n_forbidden++;
}
}
}
void Cromossome::sub(int u) {
if (!gene[u]) return;
double decrement = 0.0;
for (int i = 0; i < len; i++) {
if (i == u) {
decrement += weights[i][i];
} else {
decrement += (weights[u][i] + weights[i][u]) * gene[i];
}
}
gene[u] = 1;
score -= decrement;
for (int i = 0; i < reversed_triples[u].size(); i++) {
int val = reversed_triples[u][i];
if (acc[val] == 3) {
n_forbidden--;
}
acc[val]--;
}
}
double Cromossome::getScore() {
return score;
}
int Cromossome::getNForbidden() {
return n_forbidden;
}
bool Cromossome::isGeneForbidden(int u) {
for (int i = 0; i < reversed_triples[u].size(); i++) {
int val = reversed_triples[u][i];
if (acc[val] == 3) return true;
}
return false;
}
bool Cromossome::hasGene(int u) {
return gene[u] ? true : false;
}
void Cromossome::reset() {
gene.clear();
acc.clear();
}