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