From b6110fd66678214cc2c55e15242ab4dbcc89d59c Mon Sep 17 00:00:00 2001 From: "Mr.Robb" Date: Sun, 21 Jan 2018 00:07:25 +0100 Subject: [PATCH] Documenting DNA class --- Tetris AI/src/DNA.cpp | 52 +++++++++++++++++++++------------ Tetris AI/src/DNA.hpp | 68 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 88 insertions(+), 32 deletions(-) diff --git a/Tetris AI/src/DNA.cpp b/Tetris AI/src/DNA.cpp index 9f4e2183..cdbf7c5d 100644 --- a/Tetris AI/src/DNA.cpp +++ b/Tetris AI/src/DNA.cpp @@ -8,43 +8,54 @@ #include "DNA.hpp" -DNA::DNA(){ - -} +DNA::DNA(){} -// constructor -DNA::DNA(int length) { +DNA::DNA(int length) +{ + this->my_fitness = 0; this->genes = vector (length); - for (int i = 0; i < length; i++) { - this->genes[i] = (float(rand()) - float(RAND_MAX / 2)) / float(RAND_MAX); + + for (int i = 0; i < length; i++) + { + // Assigns a random number between -1 and 1 + this->genes[i] = (float(rand()) - float(RAND_MAX / 2)) / float(RAND_MAX / 2); } return *this; } -float DNA::operator[](int i) { +float& DNA::operator[](int i) +{ return this->genes[i]; } -vector DNA::getValues() { +void DNA::operator=(const vector &v) +{ + this->genes = v; +} + +vector DNA::getValues() +{ return genes; } -// fitness function -void DNA::fitness (int score, int max_score) { +void DNA::fitness (int score, int max_score) +{ if (max_score != 0) { my_fitness = float(score) / float(max_score); } + else { my_fitness = 0; } } -float DNA::getFitness() { +float DNA::getFitness() +{ return my_fitness; } -// crossover -DNA DNA::crossover(DNA &partner) { +DNA DNA::crossover(DNA &partner) +{ DNA child(int(partner.genes.size())); int midpoint = int(rand() % genes.size()); @@ -56,12 +67,15 @@ DNA DNA::crossover(DNA &partner) { return child; } -// mutation -void DNA::mutate(float mutationRate, int generation) { - for (int i = 0; i < genes.size(); i++) { +void DNA::mutate(float mutationRate) +{ + for (int i = 0; i < genes.size(); i++) + { float r = ((double) rand() / (RAND_MAX)); - if (r < mutationRate) { - genes[i] = (float(float(rand()) - float(RAND_MAX)/2) / float(RAND_MAX)); + if (r < mutationRate) + { + // Assigns a random number between -1 and 1 + genes[i] = (float(float(rand()) - float(RAND_MAX)/2) / float(RAND_MAX / 2)); } } } diff --git a/Tetris AI/src/DNA.hpp b/Tetris AI/src/DNA.hpp index b79f9baf..e2786830 100644 --- a/Tetris AI/src/DNA.hpp +++ b/Tetris AI/src/DNA.hpp @@ -10,38 +10,80 @@ #define DNA_hpp #include -#include #include using namespace std; class DNA { -private: - vector genes; - float my_fitness; - public: + + /** + @brief Creates an empty DNA. + */ DNA(); - // constructor + /** + @brief Creates a DNA with a length with random genes between -1 and 1. + @param length Size of the genes container. + */ DNA(int length); - float operator[](int i); + /** + @brief Set custom values to the genes. + @pre Must have the same lenght as the initial genes. + @param v It will be the genes of the individual. + */ + void operator=(const vector& v); + + /** + @brief Access the genes directly. + @pre i must be between 0 and lenght-1. + @param i Position of the gene. + */ + float& operator[](int i); - // array --> string + /** + @brief Get all of the genes at once. + @return A vector containing all of its genes. + */ vector getValues(); - // fitness function - void fitness (int score, int max_score); + /** + @brief Calculates the relative fitness of the individual. + @param score The score of the individual. + @param max_score Is the maximum score possible + or the maximum score of the generation. + */ + void fitness(int score, int max_score); + /** + @brief Get the fitness of the individual. + @pre Fitness function has been already executed. + @see DNA::fitness + */ float getFitness(); - // crossover + /** + @brief Creates a child with mixed genes via crossover. + @f$ itself + partner = child @f$ + @param partner It will provide some genetic information. + */ DNA crossover(DNA &partner); - // mutation - void mutate(float mutationRate, int generation); + /** + @brief May mutate some gene of the individual. + @param mutationRate probability of mutation between 0 and 1. + */ + void mutate(float mutationRate); + +private: + + /// @brief Contains the genetic information + vector genes; + + /// @brief Contains the fitness of the individual. + float my_fitness; }; #endif /* DNA_hpp */