Fix building and installing package.
[genetic.git] / include / mutation / mutation.h
diff --git a/include/mutation/mutation.h b/include/mutation/mutation.h
new file mode 100644 (file)
index 0000000..a35b312
--- /dev/null
@@ -0,0 +1,73 @@
+#ifndef __MUTATION_MUTATION_H
+#define __MUTATION_MUTATION_H
+
+#include <iostream>
+
+#include "chromosome.h"
+#include "generation.h"
+
+using namespace std;
+
+namespace genetic {
+//     namespace mutation {
+        /**
+         * Mutations class. Applies mutation on Chromosome's.
+         */
+        template < typename _Chromosome>
+        class Mutation {
+        public:
+            /**
+             * Type of probability of mutation chance
+             */
+            typedef double MutationChanceType;
+
+            /**
+             * Type representing Chromosome Gene
+             */
+            typedef typename _Chromosome::GeneType GeneType;
+        protected:
+            /**
+             * Chance with which mutation can occur. (0 = 0%, 1 = 100%)
+             */
+            MutationChanceType chance;
+
+        public:
+            /**
+             * Class constructor. Initializes class variables.
+             *
+             * @param chance Chance on which Mutation can occur.
+             */
+            Mutation(MutationChanceType chance) :
+                chance(chance) {
+            }
+
+            /**
+             * Applies with defined probability mutation on the given generation
+             * of Chromosome's.
+             *
+             * @param _generation Generation for which the mutation should be applied
+             * @return new Generation of Chromosome's that passed the mutation
+             */
+            Generation<_Chromosome> mutate(Generation<_Chromosome> _generation) {
+                const unsigned int generationSize = _generation.size();
+                const unsigned int chromosomeSize = _generation[0].size();
+                vector<_Chromosome> newGeneration;
+
+                for (unsigned int i = 0; i < generationSize; i++) {
+                    MutationChanceType random = (rand() % 10000) / 10000.0;
+
+                    _Chromosome chromosome = _generation[i];
+                    if (random < this->chance) {
+                        unsigned int mutatedGene = (rand() % chromosomeSize);
+
+                        chromosome[mutatedGene] = GeneType(!chromosome[mutatedGene].get());
+                    }
+                    newGeneration.push_back(chromosome);
+                }
+                return Generation<_Chromosome>(newGeneration);
+            }
+        };
+//     }
+}
+
+#endif /* __MUTATION_MUTATION_H */