Additional optimization. Removed get() method from Generation.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Sun, 5 Apr 2015 15:46:54 +0000 (17:46 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Sun, 5 Apr 2015 15:46:54 +0000 (17:46 +0200)
src/generation.h
src/selection/roulette.h

index f195d7e5d367a4d84bfe879b51154fc1c0b5739a..88942ec0d9fd2a0cac8e19b3762bee8cef227da8 100644 (file)
@@ -20,6 +20,7 @@ namespace genetic {
          */
         vector<_Chromosome> chromosomes;
 
+        template<typename> friend class Generation;
     public:
         /**
          * Default constructor
@@ -37,14 +38,7 @@ namespace genetic {
 
         /** Copy constructor */
         Generation(const Generation& generation)
-            : chromosomes(generation.get()) {
-        }
-
-        /**
-         * Allows read-only access to Generation Chromosomes
-         */
-        vector<_Chromosome> get() const {
-            return this->chromosomes;
+            : chromosomes(generation.chromosomes) {
         }
 
         /**
@@ -54,7 +48,7 @@ namespace genetic {
          * @return Generation instance containing copied Chromosomes
          */
         Generation& operator=(const Generation& generation){
-            this->chromosomes = generation.get();
+            this->chromosomes = generation.chromosomes;
             return *this;
         }
 
index b5c1f33e1b5c55701c8d7452320f3520f84f753d..f1b10c5d393864fd53d52ceb2673e8eb217e4d27 100644 (file)
@@ -44,8 +44,8 @@ namespace genetic {
                 Generation<_Chromosome> generation) {
                 vector<FitnessValueType> generationFitness;
 
-                for (unsigned int i = 0; i < generation.get().size(); i++) {
-                    generationFitness.push_back(this->checkChromosomeFitness(generation.get()[i]));
+                for (unsigned int i = 0; i < generation.size(); i++) {
+                    generationFitness.push_back(this->checkChromosomeFitness(generation[i]));
                 }
 
                 return generationFitness;
@@ -88,7 +88,7 @@ namespace genetic {
                 offset = (max - min) / (chromosomeSize - 1) - min;
 
                 for (unsigned int i = 0; i < generationFitness.size(); i++) {
-                    normalizedFitness.insert(std::make_pair(generationFitness[i] + offset, this->generation.get()[i]));
+                    normalizedFitness.insert(std::make_pair(generationFitness[i] + offset, this->generation[i]));
                 }
 
                 return normalizedFitness;