dlugolecki.net.pl
Dziennik
Polecane
Software
projects
/
genetic.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added LinearRankSelection.
[genetic.git]
/
src
/
mutation
/
mutation.h
diff --git
a/src/mutation/mutation.h
b/src/mutation/mutation.h
index 20d78730b16b5af2b97696801d1879979c6c1951..a35b312b60d637359e7e5d028c79be6bdd5a730e 100644
(file)
--- a/
src/mutation/mutation.h
+++ b/
src/mutation/mutation.h
@@
-10,33
+10,59
@@
using namespace std;
namespace genetic {
// namespace mutation {
namespace genetic {
// namespace mutation {
+ /**
+ * Mutations class. Applies mutation on Chromosome's.
+ */
template < typename _Chromosome>
class Mutation {
public:
template < typename _Chromosome>
class Mutation {
public:
+ /**
+ * Type of probability of mutation chance
+ */
typedef double MutationChanceType;
typedef double MutationChanceType;
+
+ /**
+ * Type representing Chromosome Gene
+ */
+ typedef typename _Chromosome::GeneType GeneType;
protected:
protected:
+ /**
+ * Chance with which mutation can occur. (0 = 0%, 1 = 100%)
+ */
MutationChanceType chance;
public:
MutationChanceType chance;
public:
+ /**
+ * Class constructor. Initializes class variables.
+ *
+ * @param chance Chance on which Mutation can occur.
+ */
Mutation(MutationChanceType chance) :
chance(chance) {
}
Mutation(MutationChanceType chance) :
chance(chance) {
}
+ /**
+ * Applies with defined probability mutation on the given generation
+ * of Chromosome's.
+ *
+ * @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.
get().
size();
- const unsigned int chromosomeSize = _generation
.get()[0].get()
.size();
+ const unsigned int generationSize = _generation.size();
+ const unsigned int chromosomeSize = _generation
[0]
.size();
vector<_Chromosome> newGeneration;
for (unsigned int i = 0; i < generationSize; i++) {
MutationChanceType random = (rand() % 10000) / 10000.0;
vector<_Chromosome> newGeneration;
for (unsigned int i = 0; i < generationSize; i++) {
MutationChanceType random = (rand() % 10000) / 10000.0;
-// cout << " Mutation chance: " << chance << ", random: " << random << "\n";
- newGeneration.push_back(_generation.get()[i]);
+ _Chromosome chromosome = _generation[i];
if (random < this->chance) {
if (random < this->chance) {
-
// cout << " > Mutated!\n"
;
- unsigned int which = (rand() % chromosomeSize);
-
newGeneration[i].get()[which] = !newGeneration[i].get()[which].get(
);
+
unsigned int mutatedGene = (rand() % chromosomeSize)
;
+
+
chromosome[mutatedGene] = GeneType(!chromosome[mutatedGene].get()
);
}
}
+ newGeneration.push_back(chromosome);
}
return Generation<_Chromosome>(newGeneration);
}
}
return Generation<_Chromosome>(newGeneration);
}