dlugolecki.net.pl
Dziennik
Polecane
Software
projects
/
genetic.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
9174406
)
Big optimizations and securing code.
author
Rafał Długołęcki
<rafal@dlugolecki.net.pl>
Thu, 30 Apr 2015 20:35:58 +0000
(22:35 +0200)
committer
Rafał Długołęcki
<rafal@dlugolecki.net.pl>
Thu, 30 Apr 2015 20:35:58 +0000
(22:35 +0200)
13 files changed:
include/algorithm.h
patch
|
blob
|
history
include/chromosome.h
patch
|
blob
|
history
include/condition/condition.h
patch
|
blob
|
history
include/condition/generationLimitCondition.h
patch
|
blob
|
history
include/crossover/crossover.h
patch
|
blob
|
history
include/fitness/fitness.h
patch
|
blob
|
history
include/fitness/wsti.h
patch
|
blob
|
history
include/gene.h
patch
|
blob
|
history
include/generation.h
patch
|
blob
|
history
include/mutation/mutation.h
patch
|
blob
|
history
include/selection/linearRankSelection.h
patch
|
blob
|
history
include/selection/rouletteSelection.h
patch
|
blob
|
history
include/selection/selection.h
patch
|
blob
|
history
diff --git
a/include/algorithm.h
b/include/algorithm.h
index cb6acf3cf30a9fbf8227748355e215d4608785d8..54f51fbda8574d1b12b78db5ee2bbb427d017270 100644
(file)
--- a/
include/algorithm.h
+++ b/
include/algorithm.h
@@
-79,7
+79,7
@@
namespace genetic {
*/
Algorithm(
generator::Generator<_Chromosome>& _generator,
*/
Algorithm(
generator::Generator<_Chromosome>& _generator,
- _Fitness& _fitness,
+
_Fitness& _fitness,
float crossoverChance,
float mutationChance) :
generator(_generator),
float crossoverChance,
float mutationChance) :
generator(_generator),
@@
-119,8
+119,8
@@
namespace genetic {
* \attention Method is currently not in use.
*/
void showGeneration() {
* \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 << ") ";
for (unsigned int i = 0; i < generationSize; i++) {
cout << "# " << i << ") ";
@@
-136,11
+136,12
@@
namespace genetic {
*/
void showAvgFitness() {
float avg = 0;
*/
void showAvgFitness() {
float avg = 0;
- unsigned int generationSize = this->generation.size();
+ const unsigned int generationSize = this->generation.size();
+ std::map<string, string> arguments = fitness.getArguments();
for (unsigned int i = 0; i < generationSize; i++) {
_Fitness fit(this->generation[i]);
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;
avg += fit.calculate();
}
cout << " " << avg / generationSize;
@@
-151,18
+152,19
@@
namespace genetic {
*/
void showBestFitness() {
float best = -100000;
*/
void showBestFitness() {
float best = -100000;
- unsigned int generationSize = this->generation.size();
+ const unsigned int generationSize = this->generation.size();
+ std::map<string, string> arguments = fitness.getArguments();
for (unsigned int i = 0; i < generationSize; i++) {
_Fitness fit(this->generation[i]);
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;
}
}
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 08d24c92c5b422290c0c3198ebf0be700b418963..378300aa5621f138992d2d06e50b71ce096eb86f 100644
(file)
--- a/
include/chromosome.h
+++ b/
include/chromosome.h
@@
-22,7
+22,7
@@
namespace genetic {
/**
* Genes of the chromosome
*/
/**
* Genes of the chromosome
*/
- vector<_Gene> genes;
+
std::
vector<_Gene> genes;
template<typename> friend class Chromosome;
public:
template<typename> friend class Chromosome;
public:
@@
-36,7
+36,7
@@
namespace genetic {
*
* @param genes vector containing Genes to use in Chromosome
*/
*
* @param genes vector containing Genes to use in Chromosome
*/
- Chromosome(
vector<_Gene>
genes) {
+ Chromosome(
const std::vector<_Gene>&
genes) {
this->genes = genes;
}
this->genes = genes;
}
@@
-70,7
+70,7
@@
namespace genetic {
*
* @return i-th Gene in the current Chromosome
*/
*
* @return i-th Gene in the current Chromosome
*/
- _Gene& operator[](unsigned int i) {
+ _Gene& operator[](
const
unsigned int i) {
return this->genes[i];
}
};
return this->genes[i];
}
};
diff --git
a/include/condition/condition.h
b/include/condition/condition.h
index eea77d593accd1210719ce14db78882042505394..2b175751fb66d15510ae3380d36d28b7c0a8102c 100644
(file)
--- 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.
*/
* @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:
/**
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.
*/
* @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);
}
};
return do_check(generation);
}
};
diff --git
a/include/condition/generationLimitCondition.h
b/include/condition/generationLimitCondition.h
index 455fda49a92f67c39468df993651b54ebec00e48..e120a9655a99bbf1f3b04409677c44a733ac2c6a 100644
(file)
--- 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
*/
* @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.
*/
/* 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
*/
*
* @param limit number of generations after which algorithm should stop
*/
- GenerationLimitCondition(unsigned int limit)
+ GenerationLimitCondition(
const
unsigned int limit)
: maxNumberOfGenerations(limit) {
}
};
: maxNumberOfGenerations(limit) {
}
};
diff --git
a/include/crossover/crossover.h
b/include/crossover/crossover.h
index 4d38ba0ea824da0b94bd20622c0b4a540a93e181..395ac60636c21fc788a969736babcb6d4f70228b 100644
(file)
--- 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
*/
* @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 << " ";
const unsigned int chromosomeSize = first.size();
// cout << " ";
@@
-80,7
+80,7
@@
namespace genetic {
*
* @param chance probability of Crossover (0 = 0%, 1 = 100%)
*/
*
* @param chance probability of Crossover (0 = 0%, 1 = 100%)
*/
- Crossover(
CrossoverChanceType
chance) :
+ Crossover(
const CrossoverChanceType&
chance) :
chance(chance) {
}
chance(chance) {
}
@@
-92,7
+92,7
@@
namespace genetic {
*/
Generation<_Chromosome> cross(Generation<_Chromosome>& _generation) {
const unsigned int generationSize = _generation.size();
*/
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;
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 01bddd77e166021e53351eecd4cf478db03efb9a..1ab804afd43f5911dd229f0fcfb85d510e25aec0 100644
(file)
--- a/
include/fitness/fitness.h
+++ b/
include/fitness/fitness.h
@@
-46,7
+46,7
@@
namespace genetic {
/**
* Copy constructor
*/
/**
* Copy constructor
*/
- Fitness(_Chromosome& _chromosome)
+ Fitness(
const
_Chromosome& _chromosome)
: chromosome(_chromosome) {
}
: chromosome(_chromosome) {
}
@@
-63,7
+63,7
@@
namespace genetic {
* Method used to pass additional arguments needed by the function to
* run correctly.
*/
* Method used to pass additional arguments needed by the function to
* run correctly.
*/
- virtual void parseArguments(std::map<string, string>) { }
+ virtual void parseArguments(std::map<string, string>
&
) { }
/**
* Method used to get additional arguments needed by the function to
/**
* Method used to get additional arguments needed by the function to
diff --git
a/include/fitness/wsti.h
b/include/fitness/wsti.h
index 408cfada3e8f0f60ff75fc50e6c7abbc3bd1957a..e33aa798c316b3c94483dbf208ec6c6639fff9dd 100644
(file)
--- 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
*/
*
* @param _chromosome Chromosome, for which value will be calculated
*/
- WSTI(_Chromosome& _chromosome)
+ WSTI(
const
_Chromosome& _chromosome)
: Fitness<_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
*/
* @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) {
}
: 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
*/
* @param args map containing span_start and span_end as a keys and
* their values (_double_) passed as strings
*/
- virtual void parseArguments(std::map<string, string> args) {
+ virtual void parseArguments(std::map<string, string>
&
args) {
std::map<string, string>::iterator it;
for (it = args.begin(); it != args.end(); it++) {
if (it->first == "span_start") {
std::map<string, string>::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 781cd3e29d67978dc2b27e965746a1fe53a6f645..61a6b462e21566bc8649b21a11a6cbda9c19860a 100644
(file)
--- a/
include/gene.h
+++ b/
include/gene.h
@@
-26,7
+26,7
@@
namespace genetic {
/**
* Class constructor, initializes Gene with default value.
*/
/**
* Class constructor, initializes Gene with default value.
*/
- Gene(
Type
value) {
+ Gene(
const Type&
value) {
this->value = value;
}
this->value = value;
}
diff --git
a/include/generation.h
b/include/generation.h
index 88942ec0d9fd2a0cac8e19b3762bee8cef227da8..f460a7fbe62cc408a6034b8ad42bf84420d9f78d 100644
(file)
--- a/
include/generation.h
+++ b/
include/generation.h
@@
-32,7
+32,7
@@
namespace genetic {
*
* @param chromosomes vector containing Chromosomes to use in Generation
*/
*
* @param chromosomes vector containing Chromosomes to use in Generation
*/
- Generation(
vector<_Chromosome>
chromosomes) {
+ Generation(
const std::vector<_Chromosome>&
chromosomes) {
this->chromosomes = chromosomes;
}
this->chromosomes = chromosomes;
}
@@
-66,7
+66,7
@@
namespace genetic {
*
* @return i-th Chromosome in the current Generation
*/
*
* @return i-th Chromosome in the current Generation
*/
- _Chromosome& operator[](unsigned int i) {
+ _Chromosome& operator[](
const
unsigned int i) {
return this->chromosomes[i];
}
};
return this->chromosomes[i];
}
};
diff --git
a/include/mutation/mutation.h
b/include/mutation/mutation.h
index ffd7d6384c696dd3ef9461d7e24a8389c0510c87..49b6094649c9bc09b15003df1f89593846746d51 100644
(file)
--- a/
include/mutation/mutation.h
+++ b/
include/mutation/mutation.h
@@
-37,7
+37,7
@@
namespace genetic {
*
* @param chance Chance on which Mutation can occur.
*/
*
* @param chance Chance on which Mutation can occur.
*/
- Mutation(
MutationChanceType
chance) :
+ Mutation(
const MutationChanceType&
chance) :
chance(chance) {
}
chance(chance) {
}
diff --git
a/include/selection/linearRankSelection.h
b/include/selection/linearRankSelection.h
index de303b62ca827c2e1b36f8f282749d10b4758bff..ee119d60543490f41bd932f94ba9e2c255c46e45 100644
(file)
--- a/
include/selection/linearRankSelection.h
+++ b/
include/selection/linearRankSelection.h
@@
-61,7
+61,7
@@
namespace genetic {
ChromosomeMap rankedGeneration;
unsigned int rank = generationSize;
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));
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();
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;
std::vector<_Chromosome> selected;
@@
-88,7
+88,7
@@
namespace genetic {
while (size > 0) {
bool found = false;
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) {
for (ChromosomeMapIterator it = probabilities.begin(); it != probabilities.end(); it++) {
if (random < it->first) {
@@
-112,7
+112,7
@@
namespace genetic {
return Generation<_Chromosome>(selected);
}
public:
return Generation<_Chromosome>(selected);
}
public:
- LinearRankSelection(Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
+ LinearRankSelection(
const
Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
Selection<_Chromosome>(_generation, _fitness) {
}
};
Selection<_Chromosome>(_generation, _fitness) {
}
};
diff --git
a/include/selection/rouletteSelection.h
b/include/selection/rouletteSelection.h
index 859e2531926fd8048f1890b4c57a75478b4cec29..afb3ac7f74828a4c0dfe8c1e28ef6b29aac354e3 100644
(file)
--- a/
include/selection/rouletteSelection.h
+++ b/
include/selection/rouletteSelection.h
@@
-4,8
+4,6
@@
#include <vector>
#include <utility> // std::pair
#include <map>
#include <vector>
#include <utility> // std::pair
#include <map>
-#include <cstdlib>
-#include <iostream>
#include "chromosome.h"
#include "selection.h"
#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.
*/
* 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++) {
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
*/
* @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 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
*/
/* 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;
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(
* @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;
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();
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() {
* @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),
normalizedFitness = this->normalizeFitness(
this->calculateGenerationFitness(this->generation),
@@
-187,10
+185,8
@@
namespace genetic {
* Selection
* @param _fitness Fitness method to calculate fitness of Chromosomes
*/
* 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) {
Selection<_Chromosome>(_generation, _fitness) {
- this->generation = _generation;
- this->fitness = _fitness;
}
};
// }
}
};
// }
diff --git
a/include/selection/selection.h
b/include/selection/selection.h
index 02573eb59177682ab260d33b803ba6c4d22487d7..187db12802fc22ee152e211a96e27ad2d33e2362 100644
(file)
--- a/
include/selection/selection.h
+++ b/
include/selection/selection.h
@@
-43,7
+43,7
@@
namespace genetic {
* checked.
* @return Value of the Fitness function
*/
* checked.
* @return Value of the Fitness function
*/
- FitnessValueType checkChromosomeFitness(
_Chromosome
chromosome) {
+ FitnessValueType checkChromosomeFitness(
const _Chromosome&
chromosome) {
this->fitness.chromosome = chromosome;
return fitness.calculate();
}
this->fitness.chromosome = chromosome;
return fitness.calculate();
}
@@
-63,7
+63,7
@@
namespace genetic {
* applied
* @param _fitness Fitness function to use in Selection
*/
* 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) {
}
generation(_generation), fitness(_fitness) {
}