Fixed Mutation.
[genetic.git] / src / mutation / mutation.h
index 20d78730b16b5af2b97696801d1879979c6c1951..69c23e7fde54c4d47334a806d61299235e71ce15 100644 (file)
@@ -14,6 +14,7 @@ namespace genetic {
         class Mutation {
         public:
             typedef double MutationChanceType;
+            typedef typename _Chromosome::GeneType GeneType;
         protected:
             MutationChanceType chance;
 
@@ -29,14 +30,17 @@ namespace genetic {
 
                 for (unsigned int i = 0; i < generationSize; i++) {
                     MutationChanceType random = (rand() % 10000) / 10000.0;
-//                     cout << " Mutation chance: " << chance << ", random: " << random << "\n";
-                    newGeneration.push_back(_generation.get()[i]);
 
+                    _Chromosome chromosome = _generation.get()[i];
                     if (random < this->chance) {
-//                         cout << " > Mutated!\n";
-                        unsigned int which = (rand() % chromosomeSize);
-                        newGeneration[i].get()[which] = !newGeneration[i].get()[which].get();
+                        unsigned int mutatedGene = (rand() % chromosomeSize);
+                        vector<GeneType> newChromosome = chromosome.get();
+
+                        newChromosome[mutatedGene] = GeneType(!newChromosome[mutatedGene].get());
+
+                        chromosome = _Chromosome(newChromosome);
                     }
+                    newGeneration.push_back(chromosome);
                 }
                 return Generation<_Chromosome>(newGeneration);
             }