From: Rafał Długołęcki Date: Thu, 30 Apr 2015 16:54:13 +0000 (+0200) Subject: Small optimizations on performance and memory. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=917440678dc4e920dfd5810da1274665acb17138;p=genetic.git Small optimizations on performance and memory. --- diff --git a/include/algorithm.h b/include/algorithm.h index 9386607..cb6acf3 100644 --- a/include/algorithm.h +++ b/include/algorithm.h @@ -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; } diff --git a/include/condition/condition.h b/include/condition/condition.h index 3562129..eea77d5 100644 --- a/include/condition/condition.h +++ b/include/condition/condition.h @@ -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); } }; diff --git a/include/condition/generationLimitCondition.h b/include/condition/generationLimitCondition.h index ffe977f..455fda4 100644 --- a/include/condition/generationLimitCondition.h +++ b/include/condition/generationLimitCondition.h @@ -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. */ diff --git a/include/crossover/crossover.h b/include/crossover/crossover.h index d2eada8..4d38ba0 100644 --- a/include/crossover/crossover.h +++ b/include/crossover/crossover.h @@ -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; diff --git a/include/fitness/fitness.h b/include/fitness/fitness.h index a975034..01bddd7 100644 --- a/include/fitness/fitness.h +++ b/include/fitness/fitness.h @@ -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 friend class Selection; public: diff --git a/include/fitness/wsti.h b/include/fitness/wsti.h index 7021f59..408cfad 100644 --- a/include/fitness/wsti.h +++ b/include/fitness/wsti.h @@ -13,7 +13,7 @@ namespace genetic { /** * Just an example Fitness function based on WSTI version. */ - template + template class WSTI : public Fitness<_Chromosome, _Value> { protected: /** diff --git a/include/mutation/mutation.h b/include/mutation/mutation.h index a35b312..ffd7d63 100644 --- a/include/mutation/mutation.h +++ b/include/mutation/mutation.h @@ -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; diff --git a/include/selection/linearRankSelection.h b/include/selection/linearRankSelection.h index 8bbd3ce..de303b6 100644 --- a/include/selection/linearRankSelection.h +++ b/include/selection/linearRankSelection.h @@ -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) { } }; diff --git a/include/selection/rouletteSelection.h b/include/selection/rouletteSelection.h index 009c8a6..859e253 100644 --- a/include/selection/rouletteSelection.h +++ b/include/selection/rouletteSelection.h @@ -43,7 +43,7 @@ namespace genetic { vector calculateGenerationFitness( Generation<_Chromosome> generation) { vector 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; diff --git a/include/selection/selection.h b/include/selection/selection.h index dd6e846..02573eb 100644 --- a/include/selection/selection.h +++ b/include/selection/selection.h @@ -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) { }