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;
}
};
--- /dev/null
+#ifndef __FITNESS_WSTI_H
+#define __FITNESS_WSTI_H
+
+#include <cmath>
+
+#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 */
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;
}
};
}
--- /dev/null
+#ifndef __GENERATION_H
+#define __GENERATION_H
+
+#include <vector>
+
+#include <gene.h>
+#include <chromosome.h>
+
+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 */
#include "gene.h"
#include "chromosome.h"
+#include "fitness/wsti.h"
+
using namespace std;
using namespace genetic;
int main() {
typedef Gene<int> _Gene;
+ typedef Chromosome<_Gene> _Chromosome;
time_t t;
srand((unsigned)time(&t));
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;
}