Make generators be more Generic.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Sun, 5 Apr 2015 21:02:41 +0000 (23:02 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Sun, 5 Apr 2015 21:02:41 +0000 (23:02 +0200)
README
src/algorithm.h
src/generator/bit.h [new file with mode: 0644]
src/generator/generator.h [moved from src/generator/generation.h with 66% similarity]
src/main.cpp

diff --git a/README b/README
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..39dd7c3abab7e790f5111da8dca84b29eea62e52 100644 (file)
--- a/README
+++ b/README
@@ -0,0 +1,2 @@
+# Genetic Algorithm Library
+
index bc928506b08846567ad6ab81dba4dc454e909269..9386607347fa9039a999ebf6b9dbd222ce6499f7 100644 (file)
@@ -6,7 +6,7 @@
 #include "chromosome.h"
 #include "generation.h"
 #include "fitness/fitness.h"
-#include "generator/generation.h"
+#include "generator/generator.h"
 #include "selection/selection.h"
 #include "crossover/crossover.h"
 #include "mutation/mutation.h"
@@ -30,7 +30,7 @@ namespace genetic {
         /**
          * Generator which draws initial population
          */
-        generator::Generation<_Chromosome>& generator;
+        generator::Generator<_Chromosome>& generator;
 
         /**
          * Fitness function used in selection
@@ -78,7 +78,7 @@ namespace genetic {
          * @param mutationChance probability of fitness (0 = 0%, 1 = 100%)
          */
         Algorithm(
-            generator::Generation<_Chromosome>& _generator,
+            generator::Generator<_Chromosome>& _generator,
            _Fitness& _fitness,
             double crossoverChance,
             double mutationChance) :
diff --git a/src/generator/bit.h b/src/generator/bit.h
new file mode 100644 (file)
index 0000000..7bd5007
--- /dev/null
@@ -0,0 +1,62 @@
+#ifndef __GENERATOR_BIT_H
+#define __GENERATOR_BIT_H
+
+#include <vector>
+#include <cstdlib>
+
+#include "gene.h"
+#include "chromosome.h"
+#include "generation.h"
+
+#include "generator/generator.h"
+
+using namespace std;
+
+namespace genetic {
+    namespace generator {
+        /**
+         * Generator for generating entire Generation of Chromosomes (individuals)
+         */
+        template < typename _Chromosome >
+        class Bit : public Generator<_Chromosome> {
+        public:
+            /**
+             * Type representing Chromosome Gene
+             */
+            typedef typename _Chromosome::GeneType GeneType;
+
+        protected:
+            /**
+             * Breeds new generation of chromosomes.
+             *
+             * @return ::genetic::Generation Generation of Chromosomes
+             */
+            virtual ::genetic::Generation<_Chromosome> do_breed() {
+                vector<_Chromosome> chromosomes;
+
+                for (unsigned int i = 0; i < this->generationSize; i++) {
+                    vector<GeneType> genes;
+                    for (unsigned int j = 0; j < this->chromosomeSize; j++) {
+                        GeneType gene(rand() % 2);
+                        genes.push_back(gene);
+                    }
+                    chromosomes.push_back(genes);
+                }
+
+                return ::genetic::Generation<_Chromosome>(chromosomes);
+            }
+        public:
+            /**
+             * Constructor. Initializes required variables and constants
+             *
+             * @param generationSize Indicates size of the generation
+             * @param chromosomeSize Indicates size of the chromosome
+             */
+            Bit(unsigned int generationSize, unsigned int chromosomeSize)
+                : Generator<_Chromosome>(generationSize, chromosomeSize) {
+            }
+        };
+    }
+}
+
+#endif /* __GENERATOR_GENERATOR_H */
similarity index 66%
rename from src/generator/generation.h
rename to src/generator/generator.h
index 91e605d7e28035d15d073860c1c054e8c5d9ddcf..64f2720820530fadb84d1b1fabc94deb76598864 100644 (file)
@@ -1,7 +1,6 @@
-#ifndef __GENERATOR_GENERATION_H
-#define __GENERATOR_GENERATION_H
+#ifndef __GENERATOR_GENERATOR_H
+#define __GENERATOR_GENERATOR_H
 
-#include <vector>
 #include <cstdlib>
 
 #include "../gene.h"
@@ -16,7 +15,7 @@ namespace genetic {
          * Generator for generating entire Generation of Chromosomes (individuals)
          */
         template < typename _Chromosome >
-        class Generation {
+        class Generator {
         public:
             /**
              * Type representing Chromosome Gene
@@ -33,6 +32,13 @@ namespace genetic {
              */
             unsigned int chromosomeSize;
 
+            /**
+             * Breeding calculations should be done here...
+             *
+             * @return newly breeded Generation
+             */
+            virtual ::genetic::Generation<_Chromosome> do_breed() = 0;
+
         public:
             /**
              * Constructor. Initializes required variables and constants
@@ -40,7 +46,7 @@ namespace genetic {
              * @param generationSize Indicates size of the generation
              * @param chromosomeSize Indicates size of the chromosome
              */
-            Generation(unsigned int generationSize, unsigned int chromosomeSize) {
+            Generator(unsigned int generationSize, unsigned int chromosomeSize) {
                 this->generationSize = generationSize;
                 this->chromosomeSize = chromosomeSize;
 
@@ -54,21 +60,10 @@ namespace genetic {
              * @return ::genetic::Generation Generation of Chromosomes
              */
             ::genetic::Generation<_Chromosome> breed() {
-                vector<_Chromosome> chromosomes;
-
-                for (unsigned int i = 0; i < generationSize; i++) {
-                    vector<GeneType> genes;
-                    for (unsigned int j = 0; j < chromosomeSize; j++) {
-                        GeneType gene(rand() % 2);
-                        genes.push_back(gene);
-                    }
-                    chromosomes.push_back(genes);
-                }
-
-                return ::genetic::Generation<_Chromosome>(chromosomes);
+                return do_breed();
             }
         };
     }
 }
 
-#endif /* __GENERATOR_GENERATION_H */
+#endif /* __GENERATOR_GENERATOR_H */
index f2aec1380acfd130482c57cc364305fdb6da4876..f82cc9221370ef8a2efce8b16e0e1d6f59648b1d 100644 (file)
@@ -6,7 +6,7 @@
 #include "gene.h"
 #include "chromosome.h"
 #include "generation.h"
-#include "generator/generation.h"
+#include "generator/bit.h"
 
 #include "selection/roulette.h"
 #include "crossover/crossover.h"
@@ -28,7 +28,7 @@ int main() {
     typedef Crossover<_Chromosome> _Crossover;
     typedef Mutation<_Chromosome> _Mutation;
 
-    typedef generator::Generation<_Chromosome> _Generator;
+    typedef generator::Bit<_Chromosome> _Generator;
     typedef Algorithm<_Chromosome, _Selection, _Crossover, _Mutation, _Fitness> _Algorithm;
     typedef Condition<_Chromosome> _Condition;