Created Generator for breeding Generations.
[genetic.git] / src / fitness / wsti.h
1 #ifndef __FITNESS_WSTI_H
2 #define __FITNESS_WSTI_H
3
4 #include <cmath>
5
6 #include "../gene.h"
7
8 using namespace std;
9
10 namespace genetic {
11     /**
12      * Just an example Fitness function based on WSTI version.
13      */
14     template < typename _Chromosome >
15     class WSTI {
16     protected:
17         _Chromosome chromosome;
18
19         float span_start;
20         float span_end;
21
22         double fenotype() {
23             int _fenotype = 0;
24             int ratio = 1;
25             for (unsigned int i = 0; i < chromosome.get().size(); i++) {
26                 _fenotype = _fenotype + chromosome.get()[i].get() * ratio;
27                 ratio = ratio * 2;
28             }
29             return _fenotype;
30         }
31
32         double calculateFenotype() {
33             const unsigned int power2N = 1 << this->chromosome.get().size();
34             return span_start + (span_end - span_start) * this->fenotype() / power2N;
35         }
36     public:
37         WSTI(_Chromosome& _chromosome, float start, float end)
38             : chromosome(_chromosome.get()), span_start(start), span_end(end) {
39         }
40
41         double calculate() {
42             double fenotype = this->calculateFenotype();
43             return (exp(fenotype) * sin(3.1415 * fenotype) + 1) / fenotype;
44         }
45     };
46 }
47
48 #endif /* __FITNESS_WSTI_H */