-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSampleDecoder.cpp
More file actions
executable file
·37 lines (25 loc) · 978 Bytes
/
Copy pathSampleDecoder.cpp
File metadata and controls
executable file
·37 lines (25 loc) · 978 Bytes
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
/*
* SampleDecoder.cpp
*
* Created on: May 11, 2015
* Author: Celso
*/
#include "SampleDecoder.h"
SampleDecoder::SampleDecoder() { }
SampleDecoder::~SampleDecoder() { }
// Runs in Theta(n log n)
double SampleDecoder::decode(const std::vector< double >& chromosome) const {
std::vector< std::pair< double, unsigned > > ranking(chromosome.size());
for(unsigned i = 0; i < chromosome.size(); ++i)
ranking[i] = std::pair< double, unsigned >(chromosome[i], i);
// Here we sort 'permutation', which will then produce a permutation of [n] in pair::second:
std::sort(ranking.begin(), ranking.end());
// permutation[i].second is in {0, ..., n - 1}; a permutation can be obtained as follows
std::list< unsigned > permutation;
for(std::vector< std::pair< double, unsigned > >::const_iterator i = ranking.begin();
i != ranking.end(); ++i) {
permutation.push_back(i->second);
}
// sample fitness is the first allele
return chromosome.front();
}