Created Generator for breeding Generations.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Sun, 22 Mar 2015 14:24:23 +0000 (15:24 +0100)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Sun, 22 Mar 2015 14:24:23 +0000 (15:24 +0100)
src/chromosome.h
src/fitness/wsti.h
src/generation.h
src/generator/generation.h [new file with mode: 0644]
src/main.cpp

index a20c793db43d1852f1a75e79d3757594746888dd..d37569fdde192d82ab60bfbee6834ca8336639ed 100644 (file)
@@ -3,20 +3,22 @@
 
 #include <vector>
 
-#include <gene.h>
+#include "gene.h"
 
 using namespace std;
 
 namespace genetic {
-    template < typename _Chromosome >
+    template < typename _Gene >
     class Chromosome {
+    public:
+        typedef _Gene GeneType;
     protected:
-        vector<_Chromosome> genes;
+        vector<_Gene> genes;
 
     public:
         Chromosome() {}
 
-        Chromosome(vector<_Chromosome> genes) {
+        Chromosome(vector<_Gene> genes) {
             this->genes = genes;
         }
 
@@ -29,7 +31,7 @@ namespace genetic {
             return *this;
         }
 
-        vector<_Chromosome> get() {
+        vector<_Gene> get() const {
             return this->genes;
         }
     };
index 028c32220098425fd836d1c18580845501aad136..b93439f204466028c6d6ba021a15d47ae2bcae7f 100644 (file)
@@ -20,13 +20,13 @@ namespace genetic {
         float span_end;
 
         double fenotype() {
-            int fen = 0;
-            int rat = 1;
+            int _fenotype = 0;
+            int ratio = 1;
             for (unsigned int i = 0; i < chromosome.get().size(); i++) {
-                fen = fen + chromosome.get()[i].get() * rat;
-                rat = rat * 2;
+                _fenotype = _fenotype + chromosome.get()[i].get() * ratio;
+                ratio = ratio * 2;
             }
-            return fen;
+            return _fenotype;
         }
 
         double calculateFenotype() {
index c1adc73cb4fcb6299a2fcffb371839e858e1bb4e..8ca0eb841a975cb1c9fc2dfe3f8cc8bbf66d181c 100644 (file)
@@ -15,11 +15,18 @@ namespace genetic {
         vector<_Chromosome> chromosomes;
 
     public:
+        Generation() {}
+
         Generation(vector<_Chromosome> chromosomes) {
             this->chromosomes = chromosomes;
         }
 
-        vector<_Chromosome> get() {
+        /** Copy constructor */
+        Generation(const Generation& generation)
+            : chromosomes(generation.get()) {
+        }
+
+        vector<_Chromosome> get() const {
             return this->chromosomes;
         }
     };
diff --git a/src/generator/generation.h b/src/generator/generation.h
new file mode 100644 (file)
index 0000000..c90a376
--- /dev/null
@@ -0,0 +1,70 @@
+#ifndef __GENERATOR_GENERATION_H
+#define __GENERATOR_GENERATION_H
+
+#include <vector>
+
+#include "../gene.h"
+#include "../chromosome.h"
+#include "../generation.h"
+
+using namespace std;
+
+namespace genetic {
+    namespace generator {
+        /**
+         * Generator for generating entire Generation of Chromosomes (individuals)
+         */
+        template < typename _Chromosome >
+        class Generation {
+        public:
+            /**
+             * Type of used Genes in Chromosome
+             */
+            typedef typename _Chromosome::GeneType GeneType;
+        protected:
+            /**
+             * Size of the generation to generate
+             */
+            unsigned int generationSize;
+
+            /**
+             * Size of the chromosome to generate
+             */
+            unsigned int chromosomeSize;
+
+        public:
+            /**
+             * Constructor.
+             *
+             * @param generationSize Indicates size of the generation
+             * @param generationSize Indicates size of the chromosome
+             */
+            Generation(unsigned int generationSize, unsigned int chromosomeSize) {
+                this->generationSize = generationSize;
+                this->chromosomeSize = chromosomeSize;
+            }
+
+            /**
+             * Breeds new generation of chromosomes.
+             *
+             * @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);
+            }
+        };
+    }
+}
+
+#endif /* __GENERATOR_GENERATION_H */
index 60ea330e26472e87d88fab8058eeaff5d3df8d59..38bbf42cabfddfa9bf8ba507935c72192083a64f 100644 (file)
@@ -5,6 +5,8 @@
 
 #include "gene.h"
 #include "chromosome.h"
+#include "generation.h"
+#include "generator/generation.h"
 
 #include "fitness/wsti.h"
 
@@ -14,28 +16,21 @@ using namespace genetic;
 int main() {
     typedef Gene<int> _Gene;
     typedef Chromosome<_Gene> _Chromosome;
+    
+    const int chromosomeSize = 11;
+    const int generationSize = 20;
 
     time_t t;
     srand((unsigned)time(&t));
 
-    vector<_Gene> genes;
+    generator::Generation<_Chromosome> generationGenerator(generationSize, chromosomeSize);
+    Generation<_Chromosome> generation = generationGenerator.breed();
 
-    for (int i = 0; i < 11; i++) {
-        _Gene gene(rand() % 2);
-        cout << "Generated gene: " << (int)gene.get() << "\n";
-        genes.push_back(gene);
-    }
-
-    _Chromosome chromosome(genes);
+    for (unsigned int i = 0; i < generationSize; i++) {
+        WSTI<_Chromosome> fitness(generation.get()[i], 0.5, 2.5);
 
-    cout << "Entire chromosome: ";
-    for (unsigned int i = 0; i < chromosome.get().size(); i++) {
-        cout << chromosome.get()[i].get();
+        cout << "Fitness is equal to: " << fitness.calculate() << "\n";
     }
-    cout << endl;
-    WSTI<_Chromosome> fitness(chromosome, 0.5, 2.5);
-
-    cout << "Fitness is equal to: " << fitness.calculate() << "\n";
 
     return 0;
 }