1 #ifndef __MUTATION_MUTATION_H
2 #define __MUTATION_MUTATION_H
6 #include "chromosome.h"
7 #include "generation.h"
10 // namespace mutation {
12 * Mutations class. Applies mutation on Chromosome's.
14 template < typename _Chromosome>
18 * Type of probability of mutation chance
20 typedef float MutationChanceType;
23 * Type representing Chromosome Gene
25 typedef typename _Chromosome::GeneType GeneType;
28 * Chance with which mutation can occur. (0 = 0%, 1 = 100%)
30 MutationChanceType chance;
34 * Class constructor. Initializes class variables.
36 * @param chance Chance on which Mutation can occur.
38 Mutation(const MutationChanceType& chance) :
43 * Applies with defined probability mutation on the given generation
46 * @param _generation Generation for which the mutation should be applied
47 * @return new Generation of Chromosome's that passed the mutation
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;
54 for (unsigned int i = 0; i < generationSize; i++) {
55 MutationChanceType random = (rand() % 10000) / 10000.0;
57 _Chromosome chromosome = _generation[i];
58 if (random < this->chance) {
59 unsigned int mutatedGene = (rand() % chromosomeSize);
61 chromosome[mutatedGene] = GeneType(!chromosome[mutatedGene].get());
63 newGeneration.push_back(chromosome);
65 return Generation<_Chromosome>(newGeneration);
71 #endif /* __MUTATION_MUTATION_H */