-
Notifications
You must be signed in to change notification settings - Fork 0
/
Organism.cpp
63 lines (52 loc) · 1.73 KB
/
Organism.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
/**
* DNA.cpp
*
* Created on: Jul 31, 2011
* Author: Jeroen Vlek
* Email: [email protected]
* Website: www.perceptivebits.com
* License: Free Beer (Feel free to use it in every
* way possible and if you like it, make
* sure to give me credit and buy me a drink
* if we ever meet ;) )
*/
#include <algorithm>
#include <cassert>
#include <iostream>
#include <boost/bind.hpp>
#include "Config.h"
#include "Organism.h"
#include "TargetImage.h"
Organism::Organism(const unsigned int genomeLength) : m_score(-1.0) {
m_genome.reserve(genomeLength);
for (unsigned int i = 0; i < genomeLength; ++i) {
Gene randomGene = Factory::Instance().makeRandomGene();
m_genome.push_back(randomGene);
}
createPhenotype();
}
Organism::Organism(const Organism& parentA, const Organism& parentB,
const bool doMutation) : m_score(-1.0) {
m_genome.reserve(parentA.m_genome.size());
unsigned int floorHalf = parentA.m_genome.size() / 2;
m_genome.insert(m_genome.begin(), parentA.m_genome.begin(), parentA.m_genome.begin() + floorHalf);
m_genome.insert(m_genome.begin() + floorHalf, parentB.m_genome.begin() + floorHalf, parentB.m_genome.end());
if (doMutation) {
unsigned int index = Factory::Instance().randGenomeIndex();
Gene randomGene = Factory::Instance().makeRandomGene();
m_genome[index] = randomGene;
}
createPhenotype();
}
Organism::~Organism() {
}
void Organism::createPhenotype() {
m_phenotype.clear();
for_each(m_genome.begin(), m_genome.end(), boost::bind(&PhenotypeImage::drawGene, &m_phenotype, _1));
}
PhenotypeImage& Organism::getPhenotype() {
return m_phenotype;
}
bool Organism::compareScores(const Organism* a, const Organism* b) {
return a->getScore() < b->getScore();
}