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:
c7bf950
)
Small optimizations on performance and memory.
author
Rafał Długołęcki
<rafal@dlugolecki.net.pl>
Thu, 30 Apr 2015 16:54:13 +0000
(18:54 +0200)
committer
Rafał Długołęcki
<rafal@dlugolecki.net.pl>
Thu, 30 Apr 2015 16:54:13 +0000
(18:54 +0200)
include/algorithm.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/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 9386607347fa9039a999ebf6b9dbd222ce6499f7..cb6acf3cf30a9fbf8227748355e215d4608785d8 100644
(file)
--- a/
include/algorithm.h
+++ b/
include/algorithm.h
@@
-80,8
+80,8
@@
namespace genetic {
Algorithm(
generator::Generator<_Chromosome>& _generator,
_Fitness& _fitness,
Algorithm(
generator::Generator<_Chromosome>& _generator,
_Fitness& _fitness,
-
double
crossoverChance,
-
double
mutationChance) :
+
float
crossoverChance,
+
float
mutationChance) :
generator(_generator),
fitness(_fitness),
crossover(crossoverChance),
generator(_generator),
fitness(_fitness),
crossover(crossoverChance),
@@
-135,7
+135,7
@@
namespace genetic {
* Displays average fitness value of the entire generation.
*/
void showAvgFitness() {
* 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++) {
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() {
* 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());
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;
}
if (tmp > best) {
best = tmp;
}
diff --git
a/include/condition/condition.h
b/include/condition/condition.h
index 3562129a1666999eaad86ea0249029a80012c569..eea77d593accd1210719ce14db78882042505394 100644
(file)
--- 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.
*
* 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.
*/
*
* @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.
*
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.
*/
*
* @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);
}
};
return do_check(generation);
}
};
diff --git
a/include/condition/generationLimitCondition.h
b/include/condition/generationLimitCondition.h
index ffe977f3430a494411944cbecce1e764662b1b2e..455fda49a92f67c39468df993651b54ebec00e48 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(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.
*/
diff --git
a/include/crossover/crossover.h
b/include/crossover/crossover.h
index d2eada8efb0bfe46b2d0a3edbdf6311c8bd7907b..4d38ba0ea824da0b94bd20622c0b4a540a93e181 100644
(file)
--- a/
include/crossover/crossover.h
+++ b/
include/crossover/crossover.h
@@
-14,7
+14,7
@@
namespace genetic {
/**
* Type of probability of crossover chance
*/
/**
* Type of probability of crossover chance
*/
- typedef
double
CrossoverChanceType;
+ typedef
float
CrossoverChanceType;
/**
* Type representing Chromosome Gene
/**
* 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
*/
* @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 << " ";
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
*/
* @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;
const unsigned int generationSize = _generation.size();
vector<_Chromosome> newGeneration;
diff --git
a/include/fitness/fitness.h
b/include/fitness/fitness.h
index a9750341f32271db145a514c42b5d8691a3ae931..01bddd77e166021e53351eecd4cf478db03efb9a 100644
(file)
--- 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.
*/
* 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<typename> friend class Selection;
public:
class Fitness {
template<typename> friend class Selection;
public:
diff --git
a/include/fitness/wsti.h
b/include/fitness/wsti.h
index 7021f590824665b9f22dcf963704240f7c22bf31..408cfada3e8f0f60ff75fc50e6c7abbc3bd1957a 100644
(file)
--- 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.
*/
/**
* Just an example Fitness function based on WSTI version.
*/
- template <typename _Chromosome, typename _Value =
double
>
+ template <typename _Chromosome, typename _Value =
float
>
class WSTI : public Fitness<_Chromosome, _Value> {
protected:
/**
class WSTI : public Fitness<_Chromosome, _Value> {
protected:
/**
diff --git
a/include/mutation/mutation.h
b/include/mutation/mutation.h
index a35b312b60d637359e7e5d028c79be6bdd5a730e..ffd7d6384c696dd3ef9461d7e24a8389c0510c87 100644
(file)
--- a/
include/mutation/mutation.h
+++ b/
include/mutation/mutation.h
@@
-19,7
+19,7
@@
namespace genetic {
/**
* Type of probability of mutation chance
*/
/**
* Type of probability of mutation chance
*/
- typedef
double
MutationChanceType;
+ typedef
float
MutationChanceType;
/**
* Type representing Chromosome Gene
/**
* 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
*/
* @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;
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 8bbd3cea323013f721ded44fed6e0bec4c5ef7c5..de303b62ca827c2e1b36f8f282749d10b4758bff 100644
(file)
--- a/
include/selection/linearRankSelection.h
+++ b/
include/selection/linearRankSelection.h
@@
-112,7
+112,7
@@
namespace genetic {
return Generation<_Chromosome>(selected);
}
public:
return Generation<_Chromosome>(selected);
}
public:
- LinearRankSelection(Generation<_Chromosome> _generation, GeneticFitness& _fitness) :
+ LinearRankSelection(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 009c8a66e47247d91ca7fdf3819fc2341ce6b07b..859e2531926fd8048f1890b4c57a75478b4cec29 100644
(file)
--- a/
include/selection/rouletteSelection.h
+++ b/
include/selection/rouletteSelection.h
@@
-43,7
+43,7
@@
namespace genetic {
vector<FitnessValueType> calculateGenerationFitness(
Generation<_Chromosome> generation) {
vector<FitnessValueType> generationFitness;
vector<FitnessValueType> calculateGenerationFitness(
Generation<_Chromosome> generation) {
vector<FitnessValueType> 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]));
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
*/
* 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;
Selection<_Chromosome>(_generation, _fitness) {
this->generation = _generation;
this->fitness = _fitness;
diff --git
a/include/selection/selection.h
b/include/selection/selection.h
index dd6e846299b0ae34c159843e883b63a5b6c43082..02573eb59177682ab260d33b803ba6c4d22487d7 100644
(file)
--- 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
*/
* 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) {
}
generation(_generation), fitness(_fitness) {
}