-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromosome.h
66 lines (55 loc) · 1.18 KB
/
Chromosome.h
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
#include <vector>
#include "Gene.h"
namespace EvoLisa {
using std::vector;
class Chromosome {
public:
vector <Gene *> genes;
int ChrSize;
void Init (int size) {
ChrSize = size;
genes.reserve (ChrSize);
for (int i = 0; i < ChrSize; i++) {
Gene *g = new Gene ();
genes.push_back (g);
}
}
Chromosome () {
Init (6000);
}
Chromosome (int ChrSize) {
Init (ChrSize);
}
Chromosome (Chromosome *c) {
for each (Gene *g in c->genes) {
this->genes.push_back (new Gene (g));
}
this->ChrSize = c->ChrSize;
}
~Chromosome () {
for (int i = 0; i < genes.size (); i++)
delete genes[i];
genes.clear ();
}
void Mutate (bool mode) {
for (int i = 0; i < ChrSize; i++) {
genes[i]->Mutate (mode);
}
if (Tools::GenerateRandFloat (0.0, 1.0) < Tools::SwapProb) {
int i1 = Tools::GenerateRandInt (0, ChrSize);
int i2;
do {
i2 = Tools::GenerateRandInt (0, ChrSize);
} while (i1 == i2);
std::swap (genes[i1], genes[i2]);
}
}
void Copy (Chromosome *c) {
for (int i = 0; i < c->ChrSize; i++) {
delete genes[i];
genes[i] = new Gene (c->genes[i]);
}
this->ChrSize = c->ChrSize;
}
};
}