Added functions to control crossover rate and type
This commit is contained in:
parent
5f0b8a4c13
commit
b0da515139
22
Breeder.cpp
22
Breeder.cpp
@ -28,6 +28,7 @@ namespace BitEvolver
|
|||||||
std::shared_ptr<Chromosome> Breeder::Breed(
|
std::shared_ptr<Chromosome> Breeder::Breed(
|
||||||
std::shared_ptr<Chromosome> mama,
|
std::shared_ptr<Chromosome> mama,
|
||||||
std::shared_ptr<Chromosome> papa,
|
std::shared_ptr<Chromosome> papa,
|
||||||
|
Enums::CrossoverType crossover_type,
|
||||||
double crossover_rate,
|
double crossover_rate,
|
||||||
double mutation_rate
|
double mutation_rate
|
||||||
)
|
)
|
||||||
@ -36,18 +37,15 @@ namespace BitEvolver
|
|||||||
std::shared_ptr<Chromosome> kiddo;
|
std::shared_ptr<Chromosome> kiddo;
|
||||||
int bit_length;
|
int bit_length;
|
||||||
|
|
||||||
// For now, don't crossover unless the bit lengths are identical
|
//
|
||||||
bit_length = mama->GetBitCount();
|
bit_length = mama->GetBitCount();
|
||||||
if ( papa->GetBitCount() != bit_length ) {
|
|
||||||
throw std::runtime_error("Breeder::Breed() - Incompatible parents");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Directly copy the mama
|
// Directly copy the mama
|
||||||
kiddo = std::shared_ptr<Chromosome>(new Chromosome(this->random, bit_length));
|
kiddo = std::shared_ptr<Chromosome>(new Chromosome(this->random, bit_length));
|
||||||
*kiddo = *mama;
|
*kiddo = *mama;
|
||||||
|
|
||||||
// Apply crossover
|
// Apply crossover
|
||||||
this->ApplyCrossover(kiddo, papa, crossover_rate);
|
this->ApplyCrossover(kiddo, papa, crossover_type, crossover_rate);
|
||||||
|
|
||||||
// Apply mutation
|
// Apply mutation
|
||||||
this->Mutate(kiddo, mutation_rate);
|
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
|
int
|
||||||
@ -124,7 +127,12 @@ namespace BitEvolver
|
|||||||
i
|
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();
|
bits_count = kiddo->GetBitCount();
|
||||||
if ( parent->GetBitCount() != bits_count ) {
|
if ( parent->GetBitCount() != bits_count ) {
|
||||||
throw std::runtime_error("Breeder::ApplyCrossover() - Parent incompatible with Kiddo (bit lengths don't match)");
|
throw std::runtime_error("Breeder::ApplyCrossover() - Parent incompatible with Kiddo (bit lengths don't match)");
|
||||||
|
@ -20,6 +20,7 @@ namespace BitEvolver
|
|||||||
std::shared_ptr<class Chromosome> Breed(
|
std::shared_ptr<class Chromosome> Breed(
|
||||||
std::shared_ptr<class Chromosome> mama,
|
std::shared_ptr<class Chromosome> mama,
|
||||||
std::shared_ptr<class Chromosome> papa,
|
std::shared_ptr<class Chromosome> papa,
|
||||||
|
Enums::CrossoverType crossover_type,
|
||||||
double crossover_rate,
|
double crossover_rate,
|
||||||
double mutation_rate
|
double mutation_rate
|
||||||
);
|
);
|
||||||
@ -35,7 +36,12 @@ namespace BitEvolver
|
|||||||
|
|
||||||
//
|
//
|
||||||
int PickRandomCrossoverPoint(std::shared_ptr<class Chromosome> chromosome, double crossover_rate);
|
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
15
Enums.h
@ -3,7 +3,20 @@
|
|||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
|
namespace BitEvolver
|
||||||
|
{
|
||||||
|
//
|
||||||
|
namespace Enums
|
||||||
|
{
|
||||||
|
//
|
||||||
|
enum class CrossoverType
|
||||||
|
{
|
||||||
|
//
|
||||||
|
None,
|
||||||
|
Sexual
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -149,6 +149,34 @@ namespace BitEvolver
|
|||||||
return fitness_average;
|
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)
|
void Population::SetMutationRate(double r)
|
||||||
{
|
{
|
||||||
@ -156,6 +184,13 @@ namespace BitEvolver
|
|||||||
this->mutation_rate = r;
|
this->mutation_rate = r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
double Population::GetMutationRate()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
return this->mutation_rate;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
void Population::Evolve()
|
void Population::Evolve()
|
||||||
{
|
{
|
||||||
@ -332,7 +367,12 @@ namespace BitEvolver
|
|||||||
papa = this->PickChromosomeForBreeding();
|
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;
|
return kiddo;
|
||||||
}
|
}
|
||||||
|
15
Population.h
15
Population.h
@ -46,8 +46,14 @@ namespace BitEvolver
|
|||||||
double GetAverageFitness();
|
double GetAverageFitness();
|
||||||
double GetAverageFitness(std::vector<std::shared_ptr<class Chromosome>> _chromosomes);
|
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);
|
void SetMutationRate(double r);
|
||||||
|
double GetMutationRate();
|
||||||
|
|
||||||
//
|
//
|
||||||
void Evolve();
|
void Evolve();
|
||||||
@ -57,6 +63,13 @@ namespace BitEvolver
|
|||||||
void PrintPopulation();
|
void PrintPopulation();
|
||||||
void PrintPopulation(std::vector<std::shared_ptr<class Chromosome>> _chromosomes);
|
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:
|
private:
|
||||||
|
|
||||||
@ -68,6 +81,8 @@ namespace BitEvolver
|
|||||||
std::vector<std::shared_ptr<class Chromosome>> chromosomes;
|
std::vector<std::shared_ptr<class Chromosome>> chromosomes;
|
||||||
int population_size;
|
int population_size;
|
||||||
bool population_needs_sorting;
|
bool population_needs_sorting;
|
||||||
|
Enums::CrossoverType crossover_type;
|
||||||
|
double crossover_point;
|
||||||
double mutation_rate;
|
double mutation_rate;
|
||||||
int evolution_number;
|
int evolution_number;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user