Optimize code. (from 10s to 6s for 1000 of generations with 200 chromosomes with 10 genes each).
* \attention Method is currently not in use.
*/
void showGeneration() {
- for (unsigned int i = 0; i < this->generation.get().size(); i++) {
+ for (unsigned int i = 0; i < this->generation.size(); i++) {
cout << "# " << i << ") ";
- for (unsigned int j = 0; j < this->generation.get()[i].get().size(); j++) {
- cout << this->generation.get()[i].get()[j].get();
+ for (unsigned int j = 0; j < this->generation[i].size(); j++) {
+ cout << this->generation[i][j].get();
}
cout << "\n";
}
*/
void showAvgFitness() {
double avg = 0;
- for (unsigned int i = 0; i < this->generation.get().size(); i++) {
- _Fitness fit(this->generation.get()[i]);
+ for (unsigned int i = 0; i < this->generation.size(); i++) {
+ _Fitness fit(this->generation[i]);
fit.parseArguments(fitness.getArguments());
avg += fit.calculate();
}
- cout << " " << avg / this->generation.get().size();
+ cout << " " << avg / this->generation.size();
}
/**
*/
void showBestFitness() {
double best = -100000;
- for (unsigned int i = 0; i < this->generation.get().size(); i++) {
- _Fitness fit(this->generation.get()[i]);
+ for (unsigned int i = 0; i < this->generation.size(); i++) {
+ _Fitness fit(this->generation[i]);
fit.parseArguments(fitness.getArguments());
double tmp = fit.calculate();
*/
vector<_Gene> genes;
+ template<typename> friend class Chromosome;
public:
/**
* Default constructor
/** Copy constructor */
Chromosome(const Chromosome& chromosome)
- : genes(chromosome.get()) {
+ : genes(chromosome.genes) {
}
/**
* @return Chromosome instance containing copied Genes
*/
Chromosome& operator=(const Chromosome& chromosome) {
- this->genes = chromosome.get();
+ this->genes = chromosome.genes;
return *this;
}
/**
- * Allows read-only access to Chromosome Genes
+ * Returns number of Genes in current Chromosome
+ *
+ * @return number of Genes in current Chromosome
+ */
+ unsigned int size() const {
+ return this->genes.size();
+ }
+
+ /**
+ * Returns i-th Gene in the current Chromosome
+ *
+ * @return i-th Gene in the current Chromosome
*/
- vector<_Gene> get() const {
- return this->genes;
+ _Gene& operator[](unsigned int i) {
+ return this->genes[i];
}
};
}
* @return new Chromosome crossed between given two
*/
_Chromosome do_cross(_Chromosome first, _Chromosome second, unsigned int splitPlace) {
- const unsigned int chromosomeSize = first.get().size();
+ const unsigned int chromosomeSize = first.size();
// cout << " ";
// for (unsigned int i = 0; i < chromosomeSize; i++) {
-// cout << first.get()[i].get();
+// cout << first[i].get();
// }
// cout << "\n x ";
// for (unsigned int i = 0; i < chromosomeSize; i++) {
-// cout << second.get()[i].get();
+// cout << second[i].get();
// }
// cout << "\n--------";
// for (unsigned int i = 0; i < chromosomeSize; i++) {
vector<GeneType> crossedChromosome;
for (unsigned int i = 0; i < chromosomeSize; i++) {
if (i < splitPlace) {
- crossedChromosome.push_back(first.get()[i].get());
+ crossedChromosome.push_back(first[i].get());
}
else {
- crossedChromosome.push_back(second.get()[i].get());
+ crossedChromosome.push_back(second[i].get());
}
}
// cout << "\n ";
// for (unsigned int i = 0; i < chromosomeSize; i++) {
-// cout << crossedChromosome[i].get();
+// cout << crossedChromosome[i];
// }
// cout << "\n";
* @return new Generation of Chromosome's after the Crossover
*/
Generation<_Chromosome> cross(Generation<_Chromosome> _generation) {
- const unsigned int generationSize = _generation.get().size();
+ const unsigned int generationSize = _generation.size();
vector<_Chromosome> newGeneration;
for (unsigned int i = 0; i < generationSize; i++) {
CrossoverChanceType random = (rand() + 1 % 10000) / 10000.0;
- _Chromosome chromosome = _generation.get()[i];
+ _Chromosome chromosome = _generation[i];
if (random < this->chance) {
- const unsigned int chromosomeSize = chromosome.get().size();
+ const unsigned int chromosomeSize = chromosome.size();
const unsigned int splitPlace = rand() % chromosomeSize;
unsigned int pairedChromosome = 0;
pairedChromosome = rand() % generationSize;
} while(pairedChromosome == i);
- chromosome = do_cross(chromosome, _generation.get()[pairedChromosome], splitPlace);
+ chromosome = do_cross(chromosome, _generation[pairedChromosome], splitPlace);
}
newGeneration.push_back(chromosome);
}
* Copy constructor
*/
Fitness(_Chromosome& _chromosome)
- : chromosome(_chromosome.get()) {
+ : chromosome(_chromosome) {
}
/**
class WSTI : public Fitness<_Chromosome, _Value> {
protected:
/**
- * Value of the begining of the Fitness function domain
+ * Value of the begining of the Fitness function domain.
+ * By default it is equal to 0.
*/
- float span_start;
+ float span_start = 0;
/**
- * Value of the end of the Fitness function domain
+ * Value of the end of the Fitness function domain.
+ * By default it is equal to 0.
*/
- float span_end;
+ float span_end = 0;
/**
* Calculates fenotype of the current Chromosome
_Value fenotype() {
int _fenotype = 0;
int ratio = 1;
- for (unsigned int i = 0; i < this->chromosome.get().size(); i++) {
- _fenotype = _fenotype + this->chromosome.get()[i].get() * ratio;
+ for (unsigned int i = 0; i < this->chromosome.size(); i++) {
+ _fenotype = _fenotype + this->chromosome[i].get() * ratio;
ratio = ratio * 2;
}
-// cout << "Fenotyp: " << _fenotype << "\n";
return _fenotype;
}
* Calculates fenotype of the current Chromosome
*/
_Value calculateFenotype() {
- const unsigned int power2N = 1 << this->chromosome.get().size();
+ const unsigned int power2N = 1 << this->chromosome.size();
return span_start + (span_end - span_start) * this->fenotype() / power2N;
}
this->chromosomes = generation.get();
return *this;
}
+
+ /**
+ * Returns number of Chromosomes in current Generation
+ *
+ * @return number of Chromosomes in current Generation
+ */
+ unsigned int size() const {
+ return this->chromosomes.size();
+ }
+
+ /**
+ * Returns i-th Chromosome in the current Generation
+ *
+ * @return i-th Chromosome in the current Generation
+ */
+ _Chromosome& operator[](unsigned int i) {
+ return this->chromosomes[i];
+ }
};
}
const int chromosomeSize = 10;
const int generationSize = 200;
const double crossoverChance = 0.75;
- const double mutationChance = 0.01;
+ const double mutationChance = 0.1;
_Fitness fitness(0.5, 2.5);
_Generator generationGenerator(generationSize, chromosomeSize);
* @return new Generation of Chromosome's that passed the mutation
*/
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;
- _Chromosome chromosome = _generation.get()[i];
+ _Chromosome chromosome = _generation[i];
if (random < this->chance) {
unsigned int mutatedGene = (rand() % chromosomeSize);
- vector<GeneType> newChromosome = chromosome.get();
- newChromosome[mutatedGene] = GeneType(!newChromosome[mutatedGene].get());
-
- chromosome = _Chromosome(newChromosome);
+ chromosome[mutatedGene] = GeneType(!chromosome[mutatedGene].get());
}
newGeneration.push_back(chromosome);
}
vector<_Chromosome> selected;
multimap<FitnessValueType, _Chromosome> probabilities;
- unsigned int size = this->generation.get().size();
- const unsigned int power2N = 1 << this->generation.get()[0].get().size();
+ unsigned int size = this->generation.size();
+ const unsigned int power2N = 1 << this->generation[0].size();
/** Random value used to draw chromosome */
FitnessValueType random = 0;
normalizedFitness = this->normalizeFitness(
this->calculateGenerationFitness(this->generation),
- this->generation.get()[0].get().size()
+ this->generation[0].size()
);
return spinRoulette(normalizedFitness);