From: Rafał Długołęcki Date: Thu, 30 Apr 2015 20:35:58 +0000 (+0200) Subject: Big optimizations and securing code. X-Git-Url: https://git.dlugolecki.net.pl/?p=genetic.git;a=commitdiff_plain;h=0c268e96035b65112e449c1c0df49f2be3eb184f Big optimizations and securing code. --- diff --git a/include/algorithm.h b/include/algorithm.h index cb6acf3..54f51fb 100644 --- a/include/algorithm.h +++ b/include/algorithm.h @@ -79,7 +79,7 @@ namespace genetic { */ Algorithm( generator::Generator<_Chromosome>& _generator, - _Fitness& _fitness, + _Fitness& _fitness, float crossoverChance, float mutationChance) : generator(_generator), @@ -119,8 +119,8 @@ namespace genetic { * \attention Method is currently not in use. */ void showGeneration() { - unsigned int generationSize = this->generation.size(); - unsigned int chromosomeSize = this->generation[0].size(); + const unsigned int generationSize = this->generation.size(); + const unsigned int chromosomeSize = this->generation[0].size(); for (unsigned int i = 0; i < generationSize; i++) { cout << "# " << i << ") "; @@ -136,11 +136,12 @@ namespace genetic { */ void showAvgFitness() { float avg = 0; - unsigned int generationSize = this->generation.size(); + const unsigned int generationSize = this->generation.size(); + std::map arguments = fitness.getArguments(); for (unsigned int i = 0; i < generationSize; i++) { _Fitness fit(this->generation[i]); - fit.parseArguments(fitness.getArguments()); + fit.parseArguments(arguments); avg += fit.calculate(); } cout << " " << avg / generationSize; @@ -151,18 +152,19 @@ namespace genetic { */ void showBestFitness() { float best = -100000; - unsigned int generationSize = this->generation.size(); + const unsigned int generationSize = this->generation.size(); + std::map arguments = fitness.getArguments(); for (unsigned int i = 0; i < generationSize; i++) { _Fitness fit(this->generation[i]); - fit.parseArguments(fitness.getArguments()); + fit.parseArguments(arguments); float tmp = fit.calculate(); if (tmp > best) { best = tmp; } } - cout << " " << best << endl; + cout << " " << best << "\n"; } }; } diff --git a/include/chromosome.h b/include/chromosome.h index 08d24c9..378300a 100644 --- a/include/chromosome.h +++ b/include/chromosome.h @@ -22,7 +22,7 @@ namespace genetic { /** * Genes of the chromosome */ - vector<_Gene> genes; + std::vector<_Gene> genes; template friend class Chromosome; public: @@ -36,7 +36,7 @@ namespace genetic { * * @param genes vector containing Genes to use in Chromosome */ - Chromosome(vector<_Gene> genes) { + Chromosome(const std::vector<_Gene>& genes) { this->genes = genes; } @@ -70,7 +70,7 @@ namespace genetic { * * @return i-th Gene in the current Chromosome */ - _Gene& operator[](unsigned int i) { + _Gene& operator[](const unsigned int i) { return this->genes[i]; } }; diff --git a/include/condition/condition.h b/include/condition/condition.h index eea77d5..2b17575 100644 --- a/include/condition/condition.h +++ b/include/condition/condition.h @@ -24,7 +24,7 @@ namespace genetic { * @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(const Generation<_Chromosome>&) = 0; public: /** @@ -36,7 +36,7 @@ namespace genetic { * @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(const Generation<_Chromosome>& generation) { return do_check(generation); } }; diff --git a/include/condition/generationLimitCondition.h b/include/condition/generationLimitCondition.h index 455fda4..e120a96 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(const Generation<_Chromosome> &) { /* Initial population is never checked, as method is invoked after * selection, crossover and mutation. It is safe to increment it now. */ @@ -52,7 +52,7 @@ namespace genetic { * * @param limit number of generations after which algorithm should stop */ - GenerationLimitCondition(unsigned int limit) + GenerationLimitCondition(const unsigned int limit) : maxNumberOfGenerations(limit) { } }; diff --git a/include/crossover/crossover.h b/include/crossover/crossover.h index 4d38ba0..395ac60 100644 --- a/include/crossover/crossover.h +++ b/include/crossover/crossover.h @@ -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, const unsigned int splitPlace) { const unsigned int chromosomeSize = first.size(); // cout << " "; @@ -80,7 +80,7 @@ namespace genetic { * * @param chance probability of Crossover (0 = 0%, 1 = 100%) */ - Crossover(CrossoverChanceType chance) : + Crossover(const CrossoverChanceType& chance) : chance(chance) { } @@ -92,7 +92,7 @@ namespace genetic { */ Generation<_Chromosome> cross(Generation<_Chromosome>& _generation) { const unsigned int generationSize = _generation.size(); - vector<_Chromosome> newGeneration; + std::vector<_Chromosome> newGeneration; for (unsigned int i = 0; i < generationSize; i++) { CrossoverChanceType random = (rand() + 1 % 10000) / 10000.0; diff --git a/include/fitness/fitness.h b/include/fitness/fitness.h index 01bddd7..1ab804a 100644 --- a/include/fitness/fitness.h +++ b/include/fitness/fitness.h @@ -46,7 +46,7 @@ namespace genetic { /** * Copy constructor */ - Fitness(_Chromosome& _chromosome) + Fitness(const _Chromosome& _chromosome) : chromosome(_chromosome) { } @@ -63,7 +63,7 @@ namespace genetic { * Method used to pass additional arguments needed by the function to * run correctly. */ - virtual void parseArguments(std::map) { } + virtual void parseArguments(std::map&) { } /** * Method used to get additional arguments needed by the function to diff --git a/include/fitness/wsti.h b/include/fitness/wsti.h index 408cfad..e33aa79 100644 --- a/include/fitness/wsti.h +++ b/include/fitness/wsti.h @@ -64,7 +64,7 @@ namespace genetic { * * @param _chromosome Chromosome, for which value will be calculated */ - WSTI(_Chromosome& _chromosome) + WSTI(const _Chromosome& _chromosome) : Fitness<_Chromosome>(_chromosome) { } @@ -85,7 +85,7 @@ namespace genetic { * @param start begining of the Fitness function domain * @param end end of the Fitness function domain */ - WSTI(_Chromosome& _chromosome, float start, float end) + WSTI(const _Chromosome& _chromosome, float start, float end) : Fitness<_Chromosome>(_chromosome), span_start(start), span_end(end) { } @@ -100,7 +100,7 @@ namespace genetic { * @param args map containing span_start and span_end as a keys and * their values (_double_) passed as strings */ - virtual void parseArguments(std::map args) { + virtual void parseArguments(std::map& args) { std::map::iterator it; for (it = args.begin(); it != args.end(); it++) { if (it->first == "span_start") { diff --git a/include/gene.h b/include/gene.h index 781cd3e..61a6b46 100644 --- a/include/gene.h +++ b/include/gene.h @@ -26,7 +26,7 @@ namespace genetic { /** * Class constructor, initializes Gene with default value. */ - Gene(Type value) { + Gene(const Type& value) { this->value = value; } diff --git a/include/generation.h b/include/generation.h index 88942ec..f460a7f 100644 --- a/include/generation.h +++ b/include/generation.h @@ -32,7 +32,7 @@ namespace genetic { * * @param chromosomes vector containing Chromosomes to use in Generation */ - Generation(vector<_Chromosome> chromosomes) { + Generation(const std::vector<_Chromosome>& chromosomes) { this->chromosomes = chromosomes; } @@ -66,7 +66,7 @@ namespace genetic { * * @return i-th Chromosome in the current Generation */ - _Chromosome& operator[](unsigned int i) { + _Chromosome& operator[](const unsigned int i) { return this->chromosomes[i]; } }; diff --git a/include/mutation/mutation.h b/include/mutation/mutation.h index ffd7d63..49b6094 100644 --- a/include/mutation/mutation.h +++ b/include/mutation/mutation.h @@ -37,7 +37,7 @@ namespace genetic { * * @param chance Chance on which Mutation can occur. */ - Mutation(MutationChanceType chance) : + Mutation(const MutationChanceType& chance) : chance(chance) { } diff --git a/include/selection/linearRankSelection.h b/include/selection/linearRankSelection.h index de303b6..ee119d6 100644 --- a/include/selection/linearRankSelection.h +++ b/include/selection/linearRankSelection.h @@ -61,7 +61,7 @@ namespace genetic { ChromosomeMap rankedGeneration; unsigned int rank = generationSize; - double denominator = generationSize * (generationSize - 1); + const double denominator = generationSize * (generationSize - 1); for (ChromosomeMapIterator it = generationFitness.begin(); it != generationFitness.end(); it++) { rankedGeneration.insert(std::make_pair(rank--/denominator, it->second)); @@ -79,7 +79,7 @@ namespace genetic { ChromosomeMap probabilities = rankChromosomes(); unsigned int size = probabilities.size(); - unsigned int denominator = size * (size - 1); + const unsigned int denominator = size * (size - 1); std::vector<_Chromosome> selected; @@ -88,7 +88,7 @@ namespace genetic { while (size > 0) { bool found = false; - random = (rand() % size) / (double)denominator; + random = (rand() % size) / (float)denominator; for (ChromosomeMapIterator it = probabilities.begin(); it != probabilities.end(); it++) { if (random < it->first) { @@ -112,7 +112,7 @@ namespace genetic { return Generation<_Chromosome>(selected); } public: - LinearRankSelection(Generation<_Chromosome>& _generation, GeneticFitness& _fitness) : + LinearRankSelection(const Generation<_Chromosome>& _generation, GeneticFitness& _fitness) : Selection<_Chromosome>(_generation, _fitness) { } }; diff --git a/include/selection/rouletteSelection.h b/include/selection/rouletteSelection.h index 859e253..afb3ac7 100644 --- a/include/selection/rouletteSelection.h +++ b/include/selection/rouletteSelection.h @@ -4,8 +4,6 @@ #include #include // std::pair #include -#include -#include #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 calculateGenerationFitness( - Generation<_Chromosome> generation) { - vector generationFitness; + std::vector calculateGenerationFitness( + const Generation<_Chromosome>& generation) { + std::vector 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 normalizeFitness( - vector generationFitness, + std::multimap normalizeFitness( + const std::vector& 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 normalizedFitness; + std::multimap 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 normalizedFitness) { + const std::multimap& normalizedFitness) { typedef typename std::multimap::iterator FitnessIterator; - vector<_Chromosome> selected; - multimap probabilities; + std::vector<_Chromosome> selected; + std::multimap 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 normalizedFitness; + std::multimap 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; } }; // } diff --git a/include/selection/selection.h b/include/selection/selection.h index 02573eb..187db12 100644 --- a/include/selection/selection.h +++ b/include/selection/selection.h @@ -43,7 +43,7 @@ namespace genetic { * checked. * @return Value of the Fitness function */ - FitnessValueType checkChromosomeFitness(_Chromosome chromosome) { + FitnessValueType checkChromosomeFitness(const _Chromosome& chromosome) { this->fitness.chromosome = chromosome; return fitness.calculate(); } @@ -63,7 +63,7 @@ namespace genetic { * applied * @param _fitness Fitness function to use in Selection */ - Selection(Generation<_Chromosome>& _generation, GeneticFitness& _fitness) : + Selection(const Generation<_Chromosome>& _generation, GeneticFitness& _fitness) : generation(_generation), fitness(_fitness) { }