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.
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.get().size();
53 const unsigned int chromosomeSize = _generation.get()[0].get().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.get()[i];
60 if (random < this->chance) {
61 unsigned int mutatedGene = (rand() % chromosomeSize);
62 vector<GeneType> newChromosome = chromosome.get();
64 newChromosome[mutatedGene] = GeneType(!newChromosome[mutatedGene].get());
66 chromosome = _Chromosome(newChromosome);
68 newGeneration.push_back(chromosome);
70 return Generation<_Chromosome>(newGeneration);
76 #endif /* __MUTATION_MUTATION_H */