Big optimizations and securing code.
[genetic.git] / include / selection / rouletteSelection.h
index 859e2531926fd8048f1890b4c57a75478b4cec29..afb3ac7f74828a4c0dfe8c1e28ef6b29aac354e3 100644 (file)
@@ -4,8 +4,6 @@
 #include <vector>
 #include <utility>      // std::pair
 #include <map>
-#include <cstdlib>
-#include <iostream>
 
 #include "chromosome.h"
 #include "selection.h"
@@ -40,9 +38,9 @@ namespace genetic {
              *      Generation. Values are set in the same order as they are in
              *      the Generation.
              */
-            vector<FitnessValueType> calculateGenerationFitness(
-                Generation<_Chromosome> generation) {
-                vector<FitnessValueType> generationFitness;
+            std::vector<FitnessValueType> calculateGenerationFitness(
+                const Generation<_Chromosome>& generation) {
+                std::vector<FitnessValueType> generationFitness;
                 const unsigned int generationSize = generation.size();
 
                 for (unsigned int i = 0; i < generationSize; i++) {
@@ -61,19 +59,19 @@ namespace genetic {
              * @return multimap containing normalized Fitness as a key and its
              *      Chromosome as the value
              */
-            multimap<FitnessValueType, _Chromosome> normalizeFitness(
-                vector<FitnessValueType> generationFitness,
+            std::multimap<FitnessValueType, _Chromosome> normalizeFitness(
+                const std::vector<FitnessValueType>& generationFitness,
                 unsigned int chromosomeSize) {
                 FitnessValueType min;
                 FitnessValueType max;
                 FitnessValueType offset;
 
-                unsigned int fitnessSize = generationFitness.size();
+                const unsigned int fitnessSize = generationFitness.size();
 
                 /* we use multimap because it stores multiple values with the
                  * same key
                  */
-                multimap<FitnessValueType, _Chromosome> normalizedFitness;
+                std::multimap<FitnessValueType, _Chromosome> normalizedFitness;
 
                 min = max = generationFitness[0];
                 offset = 0;
@@ -105,12 +103,12 @@ namespace genetic {
              * @return new Generation of Chromosome's that passed the Selection
              */
             Generation<_Chromosome> spinRoulette(
-                multimap<FitnessValueType, _Chromosome> normalizedFitness) {
+                const std::multimap<FitnessValueType, _Chromosome>& normalizedFitness) {
 
                 typedef typename std::multimap<FitnessValueType, _Chromosome>::iterator FitnessIterator;
 
-                vector<_Chromosome> selected;
-                multimap<FitnessValueType, _Chromosome> probabilities;
+                std::vector<_Chromosome> selected;
+                std::multimap<FitnessValueType, _Chromosome> probabilities;
 
                 unsigned int size = this->generation.size();
                 const unsigned int power2N = 1 << this->generation[0].size();
@@ -169,7 +167,7 @@ namespace genetic {
              * @return new Generation of Chromosome's that passed the Selection
              */
             Generation<_Chromosome> do_draw() {
-                multimap<FitnessValueType, _Chromosome> normalizedFitness;
+                std::multimap<FitnessValueType, _Chromosome> normalizedFitness;
 
                 normalizedFitness = this->normalizeFitness(
                     this->calculateGenerationFitness(this->generation),
@@ -187,10 +185,8 @@ namespace genetic {
              *      Selection
              * @param _fitness Fitness method to calculate fitness of Chromosomes
              */
-            RouletteSelection(Generation<_Chromosome>& _generation, genetic::Fitness<_Chromosome>& _fitness) :
+            RouletteSelection(const Generation<_Chromosome>& _generation, genetic::Fitness<_Chromosome>& _fitness) :
                 Selection<_Chromosome>(_generation, _fitness) {
-                this->generation = _generation;
-                this->fitness = _fitness;
             }
         };
 //     }