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