Remove some not used namespace declarations.
[genetic.git] / include / mutation / mutation.h
1 #ifndef __MUTATION_MUTATION_H
2 #define __MUTATION_MUTATION_H
3
4 #include <vector>
5
6 #include "chromosome.h"
7 #include "generation.h"
8
9 namespace genetic {
10 //     namespace mutation {
11         /**
12          * Mutations class. Applies mutation on Chromosome's.
13          */
14         template < typename _Chromosome>
15         class Mutation {
16         public:
17             /**
18              * Type of probability of mutation chance
19              */
20             typedef float MutationChanceType;
21
22             /**
23              * Type representing Chromosome Gene
24              */
25             typedef typename _Chromosome::GeneType GeneType;
26         protected:
27             /**
28              * Chance with which mutation can occur. (0 = 0%, 1 = 100%)
29              */
30             MutationChanceType chance;
31
32         public:
33             /**
34              * Class constructor. Initializes class variables.
35              *
36              * @param chance Chance on which Mutation can occur.
37              */
38             Mutation(const MutationChanceType& chance) :
39                 chance(chance) {
40             }
41
42             /**
43              * Applies with defined probability mutation on the given generation
44              * of Chromosome's.
45              *
46              * @param _generation Generation for which the mutation should be applied
47              * @return new Generation of Chromosome's that passed the mutation
48              */
49             Generation<_Chromosome> mutate(Generation<_Chromosome>& _generation) {
50                 const unsigned int generationSize = _generation.size();
51                 const unsigned int chromosomeSize = _generation[0].size();
52                 std::vector<_Chromosome> newGeneration;
53
54                 for (unsigned int i = 0; i < generationSize; i++) {
55                     MutationChanceType random = (rand() % 10000) / 10000.0;
56
57                     _Chromosome chromosome = _generation[i];
58                     if (random < this->chance) {
59                         unsigned int mutatedGene = (rand() % chromosomeSize);
60
61                         chromosome[mutatedGene] = GeneType(!chromosome[mutatedGene].get());
62                     }
63                     newGeneration.push_back(chromosome);
64                 }
65                 return Generation<_Chromosome>(newGeneration);
66             }
67         };
68 //     }
69 }
70
71 #endif /* __MUTATION_MUTATION_H */