1 #ifndef __MUTATION_MUTATION_H
2 #define __MUTATION_MUTATION_H
6 #include "chromosome.h"
7 #include "generation.h"
12 // namespace mutation {
14 * Mutations class. Applies mutation on Chromosome's.
16 template < typename _Chromosome>
20 * Type of probability of mutation chance
22 typedef double MutationChanceType;
25 * Type representing Chromosome Gene
27 typedef typename _Chromosome::GeneType GeneType;
30 * Chance with which mutation can occur. (0 = 0%, 1 = 100%)
32 MutationChanceType chance;
36 * Class constructor. Initializes class variables.
38 * @param chance Chance on which Mutation can occur.
40 Mutation(MutationChanceType chance) :
45 * Applies with defined probability mutation on the given generation
48 * @param _generation Generation for which the mutation should be applied
49 * @return new Generation of Chromosome's that passed the mutation
51 Generation<_Chromosome> mutate(Generation<_Chromosome> _generation) {
52 const unsigned int generationSize = _generation.size();
53 const unsigned int chromosomeSize = _generation[0].size();
54 vector<_Chromosome> newGeneration;
56 for (unsigned int i = 0; i < generationSize; i++) {
57 MutationChanceType random = (rand() % 10000) / 10000.0;
59 _Chromosome chromosome = _generation[i];
60 if (random < this->chance) {
61 unsigned int mutatedGene = (rand() % chromosomeSize);
63 chromosome[mutatedGene] = GeneType(!chromosome[mutatedGene].get());
65 newGeneration.push_back(chromosome);
67 return Generation<_Chromosome>(newGeneration);
73 #endif /* __MUTATION_MUTATION_H */