Fix test text.
[genetic.git] / src / algorithm.h
index 894f4880019034d0e2004bca4e45cfff4d49ecfd..9386607347fa9039a999ebf6b9dbd222ce6499f7 100644 (file)
@@ -6,7 +6,7 @@
 #include "chromosome.h"
 #include "generation.h"
 #include "fitness/fitness.h"
-#include "generator/generation.h"
+#include "generator/generator.h"
 #include "selection/selection.h"
 #include "crossover/crossover.h"
 #include "mutation/mutation.h"
@@ -19,7 +19,7 @@ namespace genetic {
     /**
      * Genetic Algorithm itself
      */
-    template < typename _Chromosome, typename _Selection, typename _Crossover, typename _Mutation >
+    template < typename _Chromosome, typename _Selection, typename _Crossover, typename _Mutation, typename _Fitness = Fitness<_Chromosome, typename _Selection::FitnessValueType > >
     class Algorithm {
     public:
         /**
@@ -30,7 +30,7 @@ namespace genetic {
         /**
          * Generator which draws initial population
          */
-        generator::Generation<_Chromosome>& generator;
+        generator::Generator<_Chromosome>& generator;
 
         /**
          * Fitness function used in selection
@@ -38,7 +38,7 @@ namespace genetic {
          * It is a reference, because Fitness is purely virtual class and we
          * can't have instance of it. It must be one of derived classes.
          */
-        Fitness<_Chromosome, FitnessValueType>& fitness;
+        _Fitness& fitness;
 
         /**
          * Crossover function
@@ -78,8 +78,8 @@ namespace genetic {
          * @param mutationChance probability of fitness (0 = 0%, 1 = 100%)
          */
         Algorithm(
-            generator::Generation<_Chromosome>& _generator,
-            Fitness<_Chromosome, FitnessValueType>& _fitness,
+            generator::Generator<_Chromosome>& _generator,
+           _Fitness& _fitness,
             double crossoverChance,
             double mutationChance) :
                 generator(_generator),
@@ -119,10 +119,13 @@ namespace genetic {
          * \attention Method is currently not in use.
          */
         void showGeneration() {
-            for (unsigned int i = 0; i < this->generation.get().size(); i++) {
+            unsigned int generationSize = this->generation.size();
+            unsigned int chromosomeSize = this->generation[0].size();
+
+            for (unsigned int i = 0; i < generationSize; i++) {
                 cout << "# " << i << ") ";
-                for (unsigned int j = 0; j < this->generation.get()[i].get().size(); j++) {
-                    cout << this->generation.get()[i].get()[j].get();
+                for (unsigned int j = 0; j < chromosomeSize; j++) {
+                    cout << this->generation[i][j].get();
                 }
                 cout << "\n";
             }
@@ -130,26 +133,30 @@ namespace genetic {
 
         /**
          * Displays average fitness value of the entire generation.
-         * @todo make it more generic
          */
         void showAvgFitness() {
             double avg = 0;
-            for (unsigned int i = 0; i < this->generation.get().size(); i++) {
-                WSTI<_Chromosome, FitnessValueType> fit(this->generation.get()[i], 0.5, 2.5);
+            unsigned int generationSize = this->generation.size();
+
+            for (unsigned int i = 0; i < generationSize; i++) {
+                _Fitness fit(this->generation[i]);
+                fit.parseArguments(fitness.getArguments());
                 avg += fit.calculate();
             }
-            cout << " " << avg / this->generation.get().size();
+            cout << " " << avg / generationSize;
         }
 
         /**
          * Displays best fitness value of the entire generation.
-         * @todo make it more generic, currently has hardcoded WSTI Fitness
-         *      class, due its additional arguments
          */
         void showBestFitness() {
             double best = -100000;
-            for (unsigned int i = 0; i < this->generation.get().size(); i++) {
-                WSTI<_Chromosome, FitnessValueType> fit(this->generation.get()[i], 0.5, 2.5);
+            unsigned int generationSize = this->generation.size();
+
+            for (unsigned int i = 0; i < generationSize; i++) {
+                _Fitness fit(this->generation[i]);
+                fit.parseArguments(fitness.getArguments());
+
                 double tmp = fit.calculate();
                 if (tmp > best) {
                     best = tmp;