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