X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;ds=sidebyside;f=include%2Ffitness%2Fwsti.h;fp=include%2Ffitness%2Fwsti.h;h=7021f590824665b9f22dcf963704240f7c22bf31;hb=2a8fc81203107eb3495a50fb2666803fda3e0517;hp=0000000000000000000000000000000000000000;hpb=169219566d08e4940d25b66ecdb68e80b90c090d;p=genetic.git diff --git a/include/fitness/wsti.h b/include/fitness/wsti.h new file mode 100644 index 0000000..7021f59 --- /dev/null +++ b/include/fitness/wsti.h @@ -0,0 +1,131 @@ +#ifndef __FITNESS_WSTI_H +#define __FITNESS_WSTI_H + +#include + +#include "../gene.h" + +#include "fitness.h" + +using namespace std; + +namespace genetic { + /** + * Just an example Fitness function based on WSTI version. + */ + template + class WSTI : public Fitness<_Chromosome, _Value> { + protected: + /** + * Value of the begining of the Fitness function domain. + * By default it is equal to 0. + */ + float span_start = 0; + + /** + * Value of the end of the Fitness function domain. + * By default it is equal to 0. + */ + float span_end = 0; + + /** + * Calculates fenotype of the current Chromosome + */ + _Value fenotype() { + int _fenotype = 0; + int ratio = 1; + unsigned int chromosomeSize = this->chromosome.size(); + + for (unsigned int i = 0; i < chromosomeSize; i++) { + _fenotype = _fenotype + this->chromosome[i].get() * ratio; + ratio = ratio * 2; + } + return _fenotype; + } + + /** + * Calculates fenotype of the current Chromosome + */ + _Value calculateFenotype() { + const unsigned int power2N = 1 << this->chromosome.size(); + return span_start + (span_end - span_start) * this->fenotype() / power2N; + } + + /** + * Calculates fitness value of the current Chromosome + */ + _Value do_calculate() { + _Value fenotype = this->calculateFenotype(); + return (exp(fenotype) * sin(3.1415 * fenotype) + 1) / fenotype; + } + public: + /** + * Class constructor. Initializes class with required values. + * + * @param _chromosome Chromosome, for which value will be calculated + */ + WSTI(_Chromosome& _chromosome) + : Fitness<_Chromosome>(_chromosome) { + } + + /** + * Class constructor. Initializes class with required values. + * + * @param start begining of the Fitness function domain + * @param end end of the Fitness function domain + */ + WSTI(float start, float end) + : span_start(start), span_end(end) { + } + + /** + * Class constructor. Initializes class with required values. + * + * @param _chromosome Chromosome, for which value will be calculated + * @param start begining of the Fitness function domain + * @param end end of the Fitness function domain + */ + WSTI(_Chromosome& _chromosome, float start, float end) + : Fitness<_Chromosome>(_chromosome), span_start(start), span_end(end) { + } + + /** + * Method used to pass additional arguments needed by the function to + * run correctly. + * Required map keys are: + * - _span_start_ - with value equal to begining of the Fitness function domain + * - _span_end_ - with value equal to end of the Fitness function domain + * Their values should be floats. + * + * @param args map containing span_start and span_end as a keys and + * their values (_double_) passed as strings + */ + virtual void parseArguments(std::map args) { + std::map::iterator it; + for (it = args.begin(); it != args.end(); it++) { + if (it->first == "span_start") { + this->span_start = std::stod(it->second); + } + else if (it->first == "span_end") { + this->span_end = std::stod(it->second); + } + } + } + + /** + * Method used to get additional arguments needed by the function to + * run correctly. + * + * @return map containing span_start and span_end as a map keys, with + * their values passed as string + */ + std::map getArguments() { + std::map fit_args; + fit_args.insert(std::pair("span_start", std::to_string(span_start))); + fit_args.insert(std::pair("span_end", std::to_string(span_end))); + return fit_args; + } + }; +} + +#endif /* __FITNESS_WSTI_H */