-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndividual.cpp
More file actions
59 lines (49 loc) · 1.25 KB
/
Individual.cpp
File metadata and controls
59 lines (49 loc) · 1.25 KB
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
#include <iostream>
#include "Individual.h"
#include "Coin.h"
using namespace std;
Individual::Individual() {
Individual(5);
}
Individual::Individual(int lenght) {
Coin c = Coin(0.5);
this->lenght = lenght;
this->chromosome = vector<bool>(lenght);
this->evaluation = 0;
for(int i=0; i<this->lenght;i++)
this->setBit(i, c.toss());
}
void Individual::setBit(int n, bool value){ this->chromosome[n] = value; }
void Individual::toogleBit(int n) {
if(this->chromosome[n])
this->chromosome[n] = false;
else
this->chromosome[n] = true;
}
int Individual::size(){ return this->lenght; }
bool Individual::getBit(int n){ return this->chromosome[n]; }
Individual Individual::copy() {
Individual copy = Individual(this->lenght);
for(int i=0; i<this->lenght;i++)
copy.setBit(i, this->getBit(i));
return copy;
}
vector<Individual> Individual::cross(int n, Individual another) {
vector<Individual> sons (2);
sons[0] = this->copy();
sons[1] = another.copy();
for(int i=n; i<this->lenght; i++) {
sons[0].setBit(i, another.getBit(i));
sons[1].setBit(i, this->getBit(i));
}
return sons;
}
string Individual::print() {
string out = "";
for(int i=0; i<this->lenght;i++)
if(this->chromosome[i])
out += "1";
else
out += "0";
return out;
}