Added functions to control crossover rate and type

This commit is contained in:
Mike 2018-04-14 01:07:56 -07:00
parent 5f0b8a4c13
commit b0da515139
5 changed files with 92 additions and 10 deletions

View File

@ -28,6 +28,7 @@ namespace BitEvolver
std::shared_ptr<Chromosome> Breeder::Breed(
std::shared_ptr<Chromosome> mama,
std::shared_ptr<Chromosome> papa,
Enums::CrossoverType crossover_type,
double crossover_rate,
double mutation_rate
)
@ -36,18 +37,15 @@ namespace BitEvolver
std::shared_ptr<Chromosome> kiddo;
int bit_length;
// For now, don't crossover unless the bit lengths are identical
//
bit_length = mama->GetBitCount();
if ( papa->GetBitCount() != bit_length ) {
throw std::runtime_error("Breeder::Breed() - Incompatible parents");
}
// Directly copy the mama
kiddo = std::shared_ptr<Chromosome>(new Chromosome(this->random, bit_length));
*kiddo = *mama;
// Apply crossover
this->ApplyCrossover(kiddo, papa, crossover_rate);
this->ApplyCrossover(kiddo, papa, crossover_type, crossover_rate);
// Apply mutation
this->Mutate(kiddo, mutation_rate);
@ -115,7 +113,12 @@ namespace BitEvolver
}
//
void Breeder::ApplyCrossover(std::shared_ptr<Chromosome> kiddo, std::shared_ptr<Chromosome> parent, double crossover_rate)
void Breeder::ApplyCrossover(
std::shared_ptr<Chromosome> kiddo,
std::shared_ptr<Chromosome> parent,
Enums::CrossoverType crossover_type,
double crossover_rate
)
{
//
int
@ -124,7 +127,12 @@ namespace BitEvolver
i
;
//
// Only proceed if using sexual crossover
if (crossover_type != Enums::CrossoverType::Sexual) {
return;
}
// For now, don't crossover unless the bit lengths are identical
bits_count = kiddo->GetBitCount();
if ( parent->GetBitCount() != bits_count ) {
throw std::runtime_error("Breeder::ApplyCrossover() - Parent incompatible with Kiddo (bit lengths don't match)");

View File

@ -20,6 +20,7 @@ namespace BitEvolver
std::shared_ptr<class Chromosome> Breed(
std::shared_ptr<class Chromosome> mama,
std::shared_ptr<class Chromosome> papa,
Enums::CrossoverType crossover_type,
double crossover_rate,
double mutation_rate
);
@ -35,7 +36,12 @@ namespace BitEvolver
//
int PickRandomCrossoverPoint(std::shared_ptr<class Chromosome> chromosome, double crossover_rate);
void ApplyCrossover(std::shared_ptr<class Chromosome> kiddo, std::shared_ptr<class Chromosome> parent, double crossover_rate);
void ApplyCrossover(
std::shared_ptr<class Chromosome> kiddo,
std::shared_ptr<class Chromosome> parent,
Enums::CrossoverType crossover_type,
double crossover_rate
);
};
};

15
Enums.h
View File

@ -3,7 +3,20 @@
//
namespace BitEvolver
{
//
namespace Enums
{
//
enum class CrossoverType
{
//
None,
Sexual
};
};
};
#endif

View File

@ -149,6 +149,34 @@ namespace BitEvolver
return fitness_average;
}
//
void Population::SetCrossoverPoint(double p)
{
//
this->crossover_point = p;
}
//
double Population::GetCrossoverPoint()
{
//
return this->crossover_point;
}
//
void Population::SetCrossoverType(Enums::CrossoverType t)
{
//
this->crossover_type = t;
}
//
Enums::CrossoverType Population::GetCrossoverType()
{
//
return this->crossover_type;
}
//
void Population::SetMutationRate(double r)
{
@ -156,6 +184,13 @@ namespace BitEvolver
this->mutation_rate = r;
}
//
double Population::GetMutationRate()
{
//
return this->mutation_rate;
}
//
void Population::Evolve()
{
@ -332,7 +367,12 @@ namespace BitEvolver
papa = this->PickChromosomeForBreeding();
//
kiddo = this->breeder->Breed(mama, papa, BIT_EVOLVER_POPULATION_DEFAULT_CROSSOVER, this->mutation_rate);
kiddo = this->breeder->Breed(
mama, papa,
this->crossover_type,
this->crossover_point,
this->mutation_rate
);
return kiddo;
}

View File

@ -46,8 +46,14 @@ namespace BitEvolver
double GetAverageFitness();
double GetAverageFitness(std::vector<std::shared_ptr<class Chromosome>> _chromosomes);
//
void SetCrossoverPoint(double p);
double GetCrossoverPoint();
void SetCrossoverType(Enums::CrossoverType t);
Enums::CrossoverType GetCrossoverType();
//
void SetMutationRate(double r);
double GetMutationRate();
//
void Evolve();
@ -57,6 +63,13 @@ namespace BitEvolver
void PrintPopulation();
void PrintPopulation(std::vector<std::shared_ptr<class Chromosome>> _chromosomes);
// Constants
const static int DEFAULT_POPULATION_SIZE = 100;
const static int DEFAULT_MUTATION_RATE = 0.01;
//
const static Enums::CrossoverType DEFAULT_CROSSOVER_TYPE = Enums::CrossoverType::Sexual;
const static int DEFAULT_CROSSOVER_POINT = 0.7;
//
private:
@ -68,6 +81,8 @@ namespace BitEvolver
std::vector<std::shared_ptr<class Chromosome>> chromosomes;
int population_size;
bool population_needs_sorting;
Enums::CrossoverType crossover_type;
double crossover_point;
double mutation_rate;
int evolution_number;