X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=src%2Ffitness%2Fwsti.h;h=fa303b0eb97fd13d706e8206ec8c609ca47e86e9;hb=12625bb2d19ee89abacc2456bff57dc22aa3a526;hp=068590d94c815868b0d2bf6c04401932a47ef566;hpb=971fed67ac2e5a01b5232853c08e03ed05bebbe0;p=genetic.git diff --git a/src/fitness/wsti.h b/src/fitness/wsti.h index 068590d..fa303b0 100644 --- a/src/fitness/wsti.h +++ b/src/fitness/wsti.h @@ -5,45 +5,46 @@ #include "../gene.h" +#include "fitness.h" + using namespace std; namespace genetic { /** * Just an example Fitness function based on WSTI version. */ - template < typename _Chromosome > - class WSTI { + template + class WSTI : public Fitness<_Chromosome, _Value> { protected: - _Chromosome chromosome; - float span_start; float span_end; - double fenotype() { - int fen = 0; - int rat = 1; - for (unsigned int i = 0; i < chromosome.get().size(); i++) { - fen = fen + chromosome.get()[i].get() * rat; - rat = rat * 2; + _Value fenotype() { + int _fenotype = 0; + int ratio = 1; + for (unsigned int i = 0; i < this->chromosome.get().size(); i++) { + _fenotype = _fenotype + this->chromosome.get()[i].get() * ratio; + ratio = ratio * 2; } - return fen; + return _fenotype; } - double calculateFenotype() { + _Value calculateFenotype() { const unsigned int power2N = 1 << this->chromosome.get().size(); return span_start + (span_end - span_start) * this->fenotype() / power2N; } - public: - WSTI(_Chromosome& chromosome, float start, float end) { - this->chromosome = chromosome; - this->span_start = start; - this->span_end = end; - } - double calculate() { - double fenotype = this->calculateFenotype(); + _Value do_calculate() { + _Value fenotype = this->calculateFenotype(); return (exp(fenotype) * sin(3.1415 * fenotype) + 1) / fenotype; } + public: + WSTI(float start, float end) + : span_start(start), span_end(end) { + } + WSTI(_Chromosome& _chromosome, float start, float end) + : Fitness<_Chromosome>(_chromosome), span_start(start), span_end(end) { + } }; }