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