Small optimizations on performance and memory.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Thu, 30 Apr 2015 16:54:13 +0000 (18:54 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Thu, 30 Apr 2015 16:54:13 +0000 (18:54 +0200)
include/algorithm.h
include/condition/condition.h
include/condition/generationLimitCondition.h
include/crossover/crossover.h
include/fitness/fitness.h
include/fitness/wsti.h
include/mutation/mutation.h
include/selection/linearRankSelection.h
include/selection/rouletteSelection.h
include/selection/selection.h

index 9386607347fa9039a999ebf6b9dbd222ce6499f7..cb6acf3cf30a9fbf8227748355e215d4608785d8 100644 (file)
@@ -80,8 +80,8 @@ namespace genetic {
         Algorithm(
             generator::Generator<_Chromosome>& _generator,
            _Fitness& _fitness,
-            double crossoverChance,
-            double mutationChance) :
+            float crossoverChance,
+            float mutationChance) :
                 generator(_generator),
                 fitness(_fitness),
                 crossover(crossoverChance),
@@ -135,7 +135,7 @@ namespace genetic {
          * Displays average fitness value of the entire generation.
          */
         void showAvgFitness() {
-            double avg = 0;
+            float avg = 0;
             unsigned int generationSize = this->generation.size();
 
             for (unsigned int i = 0; i < generationSize; i++) {
@@ -150,14 +150,14 @@ namespace genetic {
          * Displays best fitness value of the entire generation.
          */
         void showBestFitness() {
-            double best = -100000;
+            float best = -100000;
             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();
+                float tmp = fit.calculate();
                 if (tmp > best) {
                     best = tmp;
                 }
index 3562129a1666999eaad86ea0249029a80012c569..eea77d593accd1210719ce14db78882042505394 100644 (file)
@@ -19,24 +19,24 @@ namespace genetic {
          * If condition is satisfied, we can check another generation.
          * All the custom Condition checks should be invoked in this method.
          *
-         * @param generation current generation to check
+         * @param generation reference of current generation to check
          *
          * @return true if condition is satisfied and another generation can checked;
          *      false if condition is not satisfied and algorithm should stop.
          */
-        virtual bool do_check(Generation<_Chromosome>) = 0;
+        virtual bool do_check(Generation<_Chromosome>&) = 0;
 
     public:
         /**
          * Checks if current generation passes stop condition.
          * If condition is satisfied, we can check another generation.
          *
-         * @param generation current generation to check
+         * @param generation reference of current generation to check
          *
          * @return true if condition is satisfied and another generation can checked;
          *      false if condition is not satisfied and algorithm should stop.
          */
-        bool check(Generation<_Chromosome> generation) {
+        bool check(Generation<_Chromosome>& generation) {
             return do_check(generation);
         }
     };
index ffe977f3430a494411944cbecce1e764662b1b2e..455fda49a92f67c39468df993651b54ebec00e48 100644 (file)
@@ -34,7 +34,7 @@ namespace genetic {
          * @return true if limit is not reached and another iteration of
          *      calculations should be started, false otherwise
          */
-        bool do_check(Generation<_Chromosome>) {
+        bool do_check(Generation<_Chromosome> &) {
             /* Initial population is never checked, as method is invoked after
              * selection, crossover and mutation. It is safe to increment it now.
              */
index d2eada8efb0bfe46b2d0a3edbdf6311c8bd7907b..4d38ba0ea824da0b94bd20622c0b4a540a93e181 100644 (file)
@@ -14,7 +14,7 @@ namespace genetic {
             /**
              * Type of probability of crossover chance
              */
-            typedef double CrossoverChanceType;
+            typedef float CrossoverChanceType;
 
             /**
              * Type representing Chromosome Gene
@@ -34,7 +34,7 @@ namespace genetic {
              * @param splitPlace Gene number on which the Genes should be swapped
              * @return new Chromosome crossed between given two
              */
-            _Chromosome do_cross(_Chromosome first, _Chromosome second, unsigned int splitPlace) {
+            _Chromosome do_cross(_Chromosome& first, _Chromosome& second, unsigned int splitPlace) {
                 const unsigned int chromosomeSize = first.size();
 
 //                 cout << "        ";
@@ -90,7 +90,7 @@ namespace genetic {
              * @param _generation Generation for which the crossover should be applied
              * @return new Generation of Chromosome's after the Crossover
              */
-            Generation<_Chromosome> cross(Generation<_Chromosome> _generation) {
+            Generation<_Chromosome> cross(Generation<_Chromosome>& _generation) {
                 const unsigned int generationSize = _generation.size();
                 vector<_Chromosome> newGeneration;
 
index a9750341f32271db145a514c42b5d8691a3ae931..01bddd77e166021e53351eecd4cf478db03efb9a 100644 (file)
@@ -11,7 +11,7 @@ namespace genetic {
      * Base Fitness template class. It should be a base class for any custom
      * fitness functions.
      */
-    template < typename _Chromosome, typename _Value = double >
+    template < typename _Chromosome, typename _Value = float >
     class Fitness {
         template<typename> friend class Selection;
     public:
index 7021f590824665b9f22dcf963704240f7c22bf31..408cfada3e8f0f60ff75fc50e6c7abbc3bd1957a 100644 (file)
@@ -13,7 +13,7 @@ namespace genetic {
     /**
      * Just an example Fitness function based on WSTI version.
      */
-    template <typename _Chromosome, typename _Value = double>
+    template <typename _Chromosome, typename _Value = float>
     class WSTI : public Fitness<_Chromosome, _Value> {
     protected:
         /**
index a35b312b60d637359e7e5d028c79be6bdd5a730e..ffd7d6384c696dd3ef9461d7e24a8389c0510c87 100644 (file)
@@ -19,7 +19,7 @@ namespace genetic {
             /**
              * Type of probability of mutation chance
              */
-            typedef double MutationChanceType;
+            typedef float MutationChanceType;
 
             /**
              * Type representing Chromosome Gene
@@ -48,7 +48,7 @@ namespace genetic {
              * @param _generation Generation for which the mutation should be applied
              * @return new Generation of Chromosome's that passed the mutation
              */
-            Generation<_Chromosome> mutate(Generation<_Chromosome> _generation) {
+            Generation<_Chromosome> mutate(Generation<_Chromosome>& _generation) {
                 const unsigned int generationSize = _generation.size();
                 const unsigned int chromosomeSize = _generation[0].size();
                 vector<_Chromosome> newGeneration;
index 8bbd3cea323013f721ded44fed6e0bec4c5ef7c5..de303b62ca827c2e1b36f8f282749d10b4758bff 100644 (file)
@@ -112,7 +112,7 @@ namespace genetic {
                 return Generation<_Chromosome>(selected);
             }
         public:
-            LinearRankSelection(Generation<_Chromosome> _generation, GeneticFitness& _fitness) :
+            LinearRankSelection(Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
                 Selection<_Chromosome>(_generation, _fitness) {
             }
         };
index 009c8a66e47247d91ca7fdf3819fc2341ce6b07b..859e2531926fd8048f1890b4c57a75478b4cec29 100644 (file)
@@ -43,7 +43,7 @@ namespace genetic {
             vector<FitnessValueType> calculateGenerationFitness(
                 Generation<_Chromosome> generation) {
                 vector<FitnessValueType> generationFitness;
-                unsigned int generationSize = generation.size();
+                const unsigned int generationSize = generation.size();
 
                 for (unsigned int i = 0; i < generationSize; i++) {
                     generationFitness.push_back(this->checkChromosomeFitness(generation[i]));
@@ -187,7 +187,7 @@ namespace genetic {
              *      Selection
              * @param _fitness Fitness method to calculate fitness of Chromosomes
              */
-            RouletteSelection(Generation<_Chromosome> _generation, genetic::Fitness<_Chromosome>& _fitness) :
+            RouletteSelection(Generation<_Chromosome>& _generation, genetic::Fitness<_Chromosome>& _fitness) :
                 Selection<_Chromosome>(_generation, _fitness) {
                 this->generation = _generation;
                 this->fitness = _fitness;
index dd6e846299b0ae34c159843e883b63a5b6c43082..02573eb59177682ab260d33b803ba6c4d22487d7 100644 (file)
@@ -63,7 +63,7 @@ namespace genetic {
              *      applied
              * @param _fitness Fitness function to use in Selection
              */
-            Selection(Generation<_Chromosome> _generation, GeneticFitness& _fitness) :
+            Selection(Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
                 generation(_generation), fitness(_fitness) {
             }