Skip to content

Commit

Permalink
Documenting DNA class
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRobb committed Jan 20, 2018
1 parent 5be4895 commit b6110fd
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 32 deletions.
52 changes: 33 additions & 19 deletions Tetris AI/src/DNA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> (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<float> DNA::getValues() {
void DNA::operator=(const vector<float> &v)
{
this->genes = v;
}

vector<float> 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());
Expand All @@ -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));
}
}
}
68 changes: 55 additions & 13 deletions Tetris AI/src/DNA.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,80 @@
#define DNA_hpp

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

class DNA {

private:
vector<float> 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<float>& 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<float> 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<float> genes;

/// @brief Contains the fitness of the individual.
float my_fitness;
};

#endif /* DNA_hpp */

0 comments on commit b6110fd

Please sign in to comment.