Finished Roulette Selection.
[genetic.git] / src / fitness / wsti.h
index b93439f204466028c6d6ba021a15d47ae2bcae7f..fa303b0eb97fd13d706e8206ec8c609ca47e86e9 100644 (file)
@@ -5,43 +5,46 @@
 
 #include "../gene.h"
 
+#include "fitness.h"
+
 using namespace std;
 
 namespace genetic {
     /**
      * Just an example Fitness function based on WSTI version.
      */
-    template < typename _Chromosome >
-    class WSTI {
+    template <typename _Chromosome, typename _Value = double>
+    class WSTI : public Fitness<_Chromosome, _Value> {
     protected:
-        _Chromosome chromosome;
-
         float span_start;
         float span_end;
 
-        double fenotype() {
+        _Value fenotype() {
             int _fenotype = 0;
             int ratio = 1;
-            for (unsigned int i = 0; i < chromosome.get().size(); i++) {
-                _fenotype = _fenotype + chromosome.get()[i].get() * ratio;
+            for (unsigned int i = 0; i < this->chromosome.get().size(); i++) {
+                _fenotype = _fenotype + this->chromosome.get()[i].get() * ratio;
                 ratio = ratio * 2;
             }
             return _fenotype;
         }
 
-        double calculateFenotype() {
+        _Value 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)
-            : chromosome(_chromosome.get()), span_start(start), span_end(end) {
-        }
 
-        double calculate() {
-            double fenotype = this->calculateFenotype();
+        _Value do_calculate() {
+            _Value fenotype = this->calculateFenotype();
             return (exp(fenotype) * sin(3.1415 * fenotype) + 1) / fenotype;
         }
+    public:
+        WSTI(float start, float end)
+            : span_start(start), span_end(end) {
+        }
+        WSTI(_Chromosome& _chromosome, float start, float end)
+            : Fitness<_Chromosome>(_chromosome), span_start(start), span_end(end) {
+        }
     };
 }