From: Rafał Długołęcki Date: Sun, 22 Mar 2015 11:50:09 +0000 (+0100) Subject: Add copy constructors. Make example working. X-Git-Url: https://git.dlugolecki.net.pl/?p=genetic.git;a=commitdiff_plain;h=971fed67ac2e5a01b5232853c08e03ed05bebbe0 Add copy constructors. Make example working. --- diff --git a/src/chromosome.h b/src/chromosome.h index b7e17df..7582af0 100644 --- a/src/chromosome.h +++ b/src/chromosome.h @@ -8,17 +8,26 @@ using namespace std; namespace genetic { - template < typename _Gene > + template < typename _Chromosome > class Chromosome { protected: - vector<_Gene> genes; + vector<_Chromosome> genes; public: - Chromosome(vector<_Gene> genes) { + Chromosome() {} + + Chromosome(vector<_Chromosome> genes) { this->genes = genes; } - vector<_Gene> get() { + /** Copy constructor */ + Chromosome(const Chromosome& chromosome) : genes(chromosome.get()) {} + + Chromosome& operator=(const Chromosome&){ + return *this; + } + + vector<_Chromosome> get() { return this->genes; } }; diff --git a/src/fitness/wsti.h b/src/fitness/wsti.h new file mode 100644 index 0000000..068590d --- /dev/null +++ b/src/fitness/wsti.h @@ -0,0 +1,50 @@ +#ifndef __FITNESS_WSTI_H +#define __FITNESS_WSTI_H + +#include + +#include "../gene.h" + +using namespace std; + +namespace genetic { + /** + * Just an example Fitness function based on WSTI version. + */ + template < typename _Chromosome > + class WSTI { + 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; + } + return fen; + } + + double 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(); + return (exp(fenotype) * sin(3.1415 * fenotype) + 1) / fenotype; + } + }; +} + +#endif /* __FITNESS_WSTI_H */ diff --git a/src/gene.h b/src/gene.h index 499ca80..618f887 100644 --- a/src/gene.h +++ b/src/gene.h @@ -6,14 +6,23 @@ namespace genetic { template < typename Type > class Gene { protected: - Type fenotype; + Type value; public: - Gene(Type fenotype) { - this->fenotype = fenotype; + Gene() {} + + Gene(Type value) { + this->value = value; + } + + /** Copy constructor */ + Gene(const Gene& gene) : value(gene.get()) {} + + Gene& operator=(const Gene&){ + return *this; } - Type get() { - return this->fenotype; + Type get() const { + return value; } }; } diff --git a/src/generation.h b/src/generation.h new file mode 100644 index 0000000..c1adc73 --- /dev/null +++ b/src/generation.h @@ -0,0 +1,28 @@ +#ifndef __GENERATION_H +#define __GENERATION_H + +#include + +#include +#include + +using namespace std; + +namespace genetic { + template < typename _Chromosome > + class Generation { + protected: + vector<_Chromosome> chromosomes; + + public: + Generation(vector<_Chromosome> chromosomes) { + this->chromosomes = chromosomes; + } + + vector<_Chromosome> get() { + return this->chromosomes; + } + }; +} + +#endif /* __GENERATION_H */ diff --git a/src/main.cpp b/src/main.cpp index 8d30c3b..958c241 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,11 +6,14 @@ #include "gene.h" #include "chromosome.h" +#include "fitness/wsti.h" + using namespace std; using namespace genetic; int main() { typedef Gene _Gene; + typedef Chromosome<_Gene> _Chromosome; time_t t; srand((unsigned)time(&t)); @@ -18,11 +21,21 @@ int main() { vector<_Gene> genes; for (int i = 0; i < 20; i++) { - _Gene gene(rand() % 255); - cout << "Generated gene: " << gene.get() << "\n"; + _Gene gene(rand() % 2); + cout << "Generated gene: " << (int)gene.get() << "\n"; genes.push_back(gene); } - Chromosome<_Gene> chromosome(genes); + + _Chromosome chromosome(genes); + + cout << "Entire chromosome: "; + for (int i = 0; i < 20; i++) { + cout << chromosome.get()[i].get(); + } + cout << endl; + WSTI<_Chromosome> fitness(chromosome, 0.5, 2.5); + + cout << "Fitness is equal to: " << (double)fitness.calculate() << "\n"; return 0; }