Finished Roulette Selection.
[genetic.git] / src / fitness / wsti.h
index 068590d94c815868b0d2bf6c04401932a47ef566..fa303b0eb97fd13d706e8206ec8c609ca47e86e9 100644 (file)
@@ -5,45 +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() {
-            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;
+        _Value fenotype() {
+            int _fenotype = 0;
+            int ratio = 1;
+            for (unsigned int i = 0; i < this->chromosome.get().size(); i++) {
+                _fenotype = _fenotype + this->chromosome.get()[i].get() * ratio;
+                ratio = ratio * 2;
             }
-            return fen;
+            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) {
-            this->chromosome = chromosome;
-            this->span_start = start;
-            this->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) {
+        }
     };
 }