Add copy constructors. Make example working.
[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 fen = 0;
24             int rat = 1;
25             for (unsigned int i = 0; i < chromosome.get().size(); i++) {
26                 fen = fen + chromosome.get()[i].get() * rat;
27                 rat = rat * 2;
28             }
29             return fen;
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             this->chromosome = chromosome;
39             this->span_start = start;
40             this->span_end = end;
41         }
42
43         double calculate() {
44             double fenotype = this->calculateFenotype();
45             return (exp(fenotype) * sin(3.1415 * fenotype) + 1) / fenotype;
46         }
47     };
48 }
49
50 #endif /* __FITNESS_WSTI_H */