From: Rafał Długołęcki Date: Sun, 5 Apr 2015 16:44:21 +0000 (+0200) Subject: Apply small optimizations. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=cb1db4f6f0e4d6c65e09013c21df12b67d05d0b0;p=genetic.git Apply small optimizations. --- diff --git a/src/algorithm.h b/src/algorithm.h index 67b7160..bc92850 100644 --- a/src/algorithm.h +++ b/src/algorithm.h @@ -119,9 +119,12 @@ namespace genetic { * \attention Method is currently not in use. */ void showGeneration() { - for (unsigned int i = 0; i < this->generation.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[i].size(); j++) { + for (unsigned int j = 0; j < chromosomeSize; j++) { cout << this->generation[i][j].get(); } cout << "\n"; @@ -133,12 +136,14 @@ namespace genetic { */ void showAvgFitness() { double avg = 0; - for (unsigned int i = 0; i < this->generation.size(); i++) { + 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.size(); + cout << " " << avg / generationSize; } /** @@ -146,7 +151,9 @@ namespace genetic { */ void showBestFitness() { double best = -100000; - for (unsigned int i = 0; i < this->generation.size(); i++) { + unsigned int generationSize = this->generation.size(); + + for (unsigned int i = 0; i < generationSize; i++) { _Fitness fit(this->generation[i]); fit.parseArguments(fitness.getArguments()); diff --git a/src/fitness/wsti.h b/src/fitness/wsti.h index 1724936..7021f59 100644 --- a/src/fitness/wsti.h +++ b/src/fitness/wsti.h @@ -34,7 +34,9 @@ namespace genetic { _Value fenotype() { int _fenotype = 0; int ratio = 1; - for (unsigned int i = 0; i < this->chromosome.size(); i++) { + unsigned int chromosomeSize = this->chromosome.size(); + + for (unsigned int i = 0; i < chromosomeSize; i++) { _fenotype = _fenotype + this->chromosome[i].get() * ratio; ratio = ratio * 2; } diff --git a/src/gene.h b/src/gene.h index f3b3c55..781cd3e 100644 --- a/src/gene.h +++ b/src/gene.h @@ -15,6 +15,8 @@ namespace genetic { * with additional changes complex struct. */ Type value; + + template friend class Gene; public: /** * Default constructor @@ -29,7 +31,7 @@ namespace genetic { } /** Copy constructor */ - Gene(const Gene& gene) : value(gene.get()) {} + Gene(const Gene& gene) : value(gene.value) {} /** * Copy operator. @@ -38,7 +40,7 @@ namespace genetic { * @return Gene instance containing copied value */ Gene& operator=(const Gene& gene) { - this->value = gene.get(); + this->value = gene.value; return *this; } diff --git a/src/main.cpp b/src/main.cpp index 8002052..f2aec13 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,7 +35,7 @@ int main() { const int chromosomeSize = 10; const int generationSize = 200; const double crossoverChance = 0.75; - const double mutationChance = 0.1; + const double mutationChance = 0.01; _Fitness fitness(0.5, 2.5); _Generator generationGenerator(generationSize, chromosomeSize); diff --git a/src/selection/roulette.h b/src/selection/roulette.h index f1b10c5..4371df6 100644 --- a/src/selection/roulette.h +++ b/src/selection/roulette.h @@ -43,8 +43,9 @@ namespace genetic { vector calculateGenerationFitness( Generation<_Chromosome> generation) { vector generationFitness; + unsigned int generationSize = generation.size(); - for (unsigned int i = 0; i < generation.size(); i++) { + for (unsigned int i = 0; i < generationSize; i++) { generationFitness.push_back(this->checkChromosomeFitness(generation[i])); } @@ -67,6 +68,8 @@ namespace genetic { FitnessValueType max; FitnessValueType offset; + unsigned int fitnessSize = generationFitness.size(); + /* we use multimap because it stores multiple values with the * same key */ @@ -75,7 +78,7 @@ namespace genetic { min = max = generationFitness[0]; offset = 0; - for (unsigned int i = 0; i < generationFitness.size(); i++) { + for (unsigned int i = 0; i < fitnessSize; i++) { if(generationFitness[i] < min) { min = generationFitness[i]; } @@ -87,7 +90,7 @@ namespace genetic { offset = (max - min) / (chromosomeSize - 1) - min; - for (unsigned int i = 0; i < generationFitness.size(); i++) { + for (unsigned int i = 0; i < fitnessSize; i++) { normalizedFitness.insert(std::make_pair(generationFitness[i] + offset, this->generation[i])); }