Bigger values for testing results.
[genetic.git] / include / 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         /**
20          * Value of the begining of the Fitness function domain.
21          * By default it is equal to 0.
22          */
23         float span_start = 0;
24
25         /**
26          * Value of the end of the Fitness function domain.
27          * By default it is equal to 0.
28          */
29         float span_end = 0;
30
31         /**
32          * Calculates fenotype of the current Chromosome
33          */
34         _Value fenotype() {
35             int _fenotype = 0;
36             int ratio = 1;
37             unsigned int chromosomeSize = this->chromosome.size();
38
39             for (unsigned int i = 0; i < chromosomeSize; i++) {
40                 _fenotype = _fenotype + this->chromosome[i].get() * ratio;
41                 ratio = ratio * 2;
42             }
43             return _fenotype;
44         }
45
46         /**
47          * Calculates fenotype of the current Chromosome
48          */
49         _Value calculateFenotype() {
50             const unsigned int power2N = 1 << this->chromosome.size();
51             return span_start + (span_end - span_start) * this->fenotype() / power2N;
52         }
53
54         /**
55          * Calculates fitness value of the current Chromosome
56          */
57         _Value do_calculate() {
58             _Value fenotype = this->calculateFenotype();
59             return (exp(fenotype) * sin(3.1415 * fenotype) + 1) / fenotype;
60         }
61     public:
62         /**
63          * Class constructor. Initializes class with required values.
64          *
65          * @param _chromosome Chromosome, for which value will be calculated
66          */
67         WSTI(_Chromosome& _chromosome)
68             : Fitness<_Chromosome>(_chromosome) {
69         }
70
71         /**
72          * Class constructor. Initializes class with required values.
73          *
74          * @param start begining of the Fitness function domain
75          * @param end end of the Fitness function domain
76          */
77         WSTI(float start, float end)
78             : span_start(start), span_end(end) {
79         }
80
81         /**
82          * Class constructor. Initializes class with required values.
83          *
84          * @param _chromosome Chromosome, for which value will be calculated
85          * @param start begining of the Fitness function domain
86          * @param end end of the Fitness function domain
87          */
88         WSTI(_Chromosome& _chromosome, float start, float end)
89             : Fitness<_Chromosome>(_chromosome), span_start(start), span_end(end) {
90         }
91
92         /**
93          * Method used to pass additional arguments needed by the function to
94          * run correctly.
95          * Required map keys are:
96          *  - _span_start_ - with value equal to begining of the Fitness function domain
97          *  - _span_end_ - with value equal to end of the Fitness function domain
98          * Their values should be floats.
99          * 
100          * @param args map containing span_start and span_end as a keys and
101          *      their values (_double_) passed as strings
102          */
103         virtual void parseArguments(std::map<string, string> args) {
104             std::map<string, string>::iterator it;
105             for (it = args.begin(); it != args.end(); it++) {
106                 if (it->first == "span_start") {
107                     this->span_start = std::stod(it->second);
108                 }
109                 else if (it->first == "span_end") {
110                     this->span_end = std::stod(it->second);
111                 }
112             }
113         }
114
115         /**
116          * Method used to get additional arguments needed by the function to
117          * run correctly.
118          *
119          * @return map containing span_start and span_end as a map keys, with
120          *      their values passed as string
121          */
122         std::map<string, string> getArguments() {
123             std::map<string, string> fit_args;
124             fit_args.insert(std::pair<string, string>("span_start", std::to_string(span_start)));
125             fit_args.insert(std::pair<string, string>("span_end", std::to_string(span_end)));
126             return fit_args;
127         }
128     };
129 }
130
131 #endif /* __FITNESS_WSTI_H */