1 #ifndef __MUTATION_MUTATION_H
2 #define __MUTATION_MUTATION_H
6 #include "chromosome.h"
7 #include "generation.h"
12 // namespace mutation {
13 template < typename _Chromosome>
16 typedef double MutationChanceType;
18 MutationChanceType chance;
21 Mutation(MutationChanceType chance) :
25 Generation<_Chromosome> mutate(Generation<_Chromosome> _generation) {
26 const unsigned int generationSize = _generation.get().size();
27 const unsigned int chromosomeSize = _generation.get()[0].get().size();
28 vector<_Chromosome> newGeneration;
30 for (unsigned int i = 0; i < generationSize; i++) {
31 MutationChanceType random = (rand() % 10000) / 10000.0;
32 // cout << " Mutation chance: " << chance << ", random: " << random << "\n";
33 newGeneration.push_back(_generation.get()[i]);
35 if (random < this->chance) {
36 // cout << " > Mutated!\n";
37 unsigned int which = (rand() % chromosomeSize);
38 newGeneration[i].get()[which] = !newGeneration[i].get()[which].get();
41 return Generation<_Chromosome>(newGeneration);
47 #endif /* __MUTATION_MUTATION_H */