Add copy constructors. Make example working.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Sun, 22 Mar 2015 11:50:09 +0000 (12:50 +0100)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Sun, 22 Mar 2015 11:50:09 +0000 (12:50 +0100)
src/chromosome.h
src/fitness/wsti.h [new file with mode: 0644]
src/gene.h
src/generation.h [new file with mode: 0644]
src/main.cpp

index b7e17df852ff489673969cb2736d02ef747951a3..7582af05a12146f5f243472b00f15760fa102903 100644 (file)
@@ -8,17 +8,26 @@
 using namespace std;
 
 namespace genetic {
-    template < typename _Gene >
+    template < typename _Chromosome >
     class Chromosome {
     protected:
-        vector<_Gene> genes;
+        vector<_Chromosome> genes;
 
     public:
-        Chromosome(vector<_Gene> genes) {
+        Chromosome() {}
+
+        Chromosome(vector<_Chromosome> genes) {
             this->genes = genes;
         }
 
-        vector<_Gene> get() {
+        /** Copy constructor */
+        Chromosome(const Chromosome& chromosome) : genes(chromosome.get()) {}
+
+        Chromosome& operator=(const Chromosome&){
+            return *this;
+        }
+
+        vector<_Chromosome> get() {
             return this->genes;
         }
     };
diff --git a/src/fitness/wsti.h b/src/fitness/wsti.h
new file mode 100644 (file)
index 0000000..068590d
--- /dev/null
@@ -0,0 +1,50 @@
+#ifndef __FITNESS_WSTI_H
+#define __FITNESS_WSTI_H
+
+#include <cmath>
+
+#include "../gene.h"
+
+using namespace std;
+
+namespace genetic {
+    /**
+     * Just an example Fitness function based on WSTI version.
+     */
+    template < typename _Chromosome >
+    class WSTI {
+    protected:
+        _Chromosome chromosome;
+
+        float span_start;
+        float span_end;
+
+        double fenotype() {
+            int fen = 0;
+            int rat = 1;
+            for (unsigned int i = 0; i < chromosome.get().size(); i++) {
+                fen = fen + chromosome.get()[i].get() * rat;
+                rat = rat * 2;
+            }
+            return fen;
+        }
+
+        double calculateFenotype() {
+            const unsigned int power2N = 1 << this->chromosome.get().size();
+            return span_start + (span_end - span_start) * this->fenotype() / power2N;
+        }
+    public:
+        WSTI(_Chromosome& chromosome, float start, float end) {
+            this->chromosome = chromosome;
+            this->span_start = start;
+            this->span_end = end;
+        }
+
+        double calculate() {
+            double fenotype = this->calculateFenotype();
+            return (exp(fenotype) * sin(3.1415 * fenotype) + 1) / fenotype;
+        }
+    };
+}
+
+#endif /* __FITNESS_WSTI_H */
index 499ca80643a9f5a428ae167321fa5d2089af67ac..618f88723f1d4cf8a152d964441d8ea8d46e2e98 100644 (file)
@@ -6,14 +6,23 @@ namespace genetic {
     template < typename Type >
     class Gene {
     protected:
-        Type fenotype;
+        Type value;
     public:
-        Gene(Type fenotype) {
-            this->fenotype = fenotype;
+        Gene() {}
+
+        Gene(Type value) {
+            this->value = value;
+        }
+
+        /** Copy constructor */
+        Gene(const Gene& gene) : value(gene.get()) {}
+
+        Gene& operator=(const Gene&){
+            return *this;
         }
 
-        Type get() {
-            return this->fenotype;
+        Type get() const {
+            return value;
         }
     };
 }
diff --git a/src/generation.h b/src/generation.h
new file mode 100644 (file)
index 0000000..c1adc73
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef __GENERATION_H
+#define __GENERATION_H
+
+#include <vector>
+
+#include <gene.h>
+#include <chromosome.h>
+
+using namespace std;
+
+namespace genetic {
+    template < typename _Chromosome >
+    class Generation {
+    protected:
+        vector<_Chromosome> chromosomes;
+
+    public:
+        Generation(vector<_Chromosome> chromosomes) {
+            this->chromosomes = chromosomes;
+        }
+
+        vector<_Chromosome> get() {
+            return this->chromosomes;
+        }
+    };
+}
+
+#endif /* __GENERATION_H */
index 8d30c3b94171b1584aa6653076714349e3e2e3bb..958c2410403fe2017250815208a807a0179e0875 100644 (file)
@@ -6,11 +6,14 @@
 #include "gene.h"
 #include "chromosome.h"
 
+#include "fitness/wsti.h"
+
 using namespace std;
 using namespace genetic;
 
 int main() {
     typedef Gene<int> _Gene;
+    typedef Chromosome<_Gene> _Chromosome;
 
     time_t t;
     srand((unsigned)time(&t));
@@ -18,11 +21,21 @@ int main() {
     vector<_Gene> genes;
 
     for (int i = 0; i < 20; i++) {
-        _Gene gene(rand() % 255);
-        cout << "Generated gene: " << gene.get() << "\n";
+        _Gene gene(rand() % 2);
+        cout << "Generated gene: " << (int)gene.get() << "\n";
         genes.push_back(gene);
     }
-    Chromosome<_Gene> chromosome(genes);
+
+    _Chromosome chromosome(genes);
+
+    cout << "Entire chromosome: ";
+    for (int i = 0; i < 20; i++) {
+        cout << chromosome.get()[i].get();
+    }
+    cout << endl;
+    WSTI<_Chromosome> fitness(chromosome, 0.5, 2.5);
+
+    cout << "Fitness is equal to: " << (double)fitness.calculate() << "\n";
 
     return 0;
 }