2018-04-13 23:59:20 -07:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
#include "BitEvolver/Random.h"
|
|
|
|
#include "BitEvolver/Chromosome.h"
|
|
|
|
#include "BitEvolver/Breeder.h"
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
namespace BitEvolver
|
|
|
|
{
|
|
|
|
//
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
//
|
|
|
|
Breeder::Breeder(std::shared_ptr<Random> _random)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
this->random = _random;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
std::shared_ptr<Chromosome> Breeder::Breed(
|
|
|
|
std::shared_ptr<Chromosome> mama,
|
|
|
|
std::shared_ptr<Chromosome> papa,
|
2018-04-14 01:07:56 -07:00
|
|
|
Enums::CrossoverType crossover_type,
|
2018-04-14 19:58:13 -07:00
|
|
|
Enums::CrossoverOrder crossover_order,
|
|
|
|
Enums::CrossoverBounds crossover_bounds,
|
|
|
|
double crossover_point,
|
|
|
|
double crossover_point_std,
|
2018-04-13 23:59:20 -07:00
|
|
|
double mutation_rate
|
|
|
|
)
|
|
|
|
{
|
|
|
|
//
|
2018-04-14 19:58:13 -07:00
|
|
|
std::shared_ptr<Chromosome>
|
|
|
|
parent_primary,
|
|
|
|
parent_secondary,
|
|
|
|
kiddo
|
|
|
|
;
|
2018-04-13 23:59:20 -07:00
|
|
|
|
2018-04-14 19:58:13 -07:00
|
|
|
// Choose primary / secondary parents
|
|
|
|
switch( crossover_order )
|
|
|
|
{
|
|
|
|
//
|
|
|
|
case Enums::CrossoverOrder::MamaPapa:
|
|
|
|
parent_primary = mama;
|
|
|
|
parent_secondary = papa;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//
|
|
|
|
case Enums::CrossoverOrder::ByFitness:
|
|
|
|
if ( mama->GetFitness() > papa->GetFitness() ) {
|
|
|
|
parent_primary = mama;
|
|
|
|
parent_secondary = papa;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
parent_primary = papa;
|
|
|
|
parent_secondary = mama;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-13 23:59:20 -07:00
|
|
|
|
2018-04-14 19:58:13 -07:00
|
|
|
// Directly copy the primary parent first
|
|
|
|
kiddo = std::shared_ptr<Chromosome>(new Chromosome(this->random, parent_primary->GetBitCount()));
|
|
|
|
*kiddo = *parent_primary;
|
2018-04-13 23:59:20 -07:00
|
|
|
|
2018-04-14 19:58:13 -07:00
|
|
|
// Apply crossover with the secondary parent
|
|
|
|
this->ApplyCrossover(
|
|
|
|
kiddo, parent_secondary,
|
|
|
|
crossover_type, crossover_bounds, crossover_point, crossover_point_std
|
|
|
|
);
|
2018-04-13 23:59:20 -07:00
|
|
|
|
|
|
|
// Apply mutation
|
|
|
|
this->Mutate(kiddo, mutation_rate);
|
|
|
|
|
2018-04-14 07:18:47 -07:00
|
|
|
// Reset kiddo's fitness
|
|
|
|
kiddo->ResetFitness();
|
|
|
|
|
2018-05-02 03:12:08 -07:00
|
|
|
// Increment kiddo's generation number
|
|
|
|
kiddo->IncrementGenerationNumber();
|
|
|
|
|
2018-04-13 23:59:20 -07:00
|
|
|
return kiddo;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
void Breeder::Mutate(std::shared_ptr<Chromosome> chromosome, double mutation_rate)
|
|
|
|
{
|
|
|
|
//
|
2018-04-14 19:15:31 -07:00
|
|
|
int
|
|
|
|
i,
|
|
|
|
size
|
|
|
|
;
|
2018-04-13 23:59:20 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
size = chromosome->GetBitCount();
|
|
|
|
for ( i=0; i<size; i++ ) {
|
|
|
|
|
|
|
|
//
|
|
|
|
if ( this->random->RollBool(mutation_rate) ) {
|
|
|
|
chromosome->FlipBit(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
chromosome->ResetFitness();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-04-14 19:58:13 -07:00
|
|
|
int Breeder::PickRandomCrossoverPoint(
|
|
|
|
std::shared_ptr<Chromosome> chromosome,
|
|
|
|
Enums::CrossoverBounds crossover_bounds,
|
|
|
|
double crossover_point,
|
|
|
|
double crossover_point_std
|
|
|
|
)
|
2018-04-13 23:59:20 -07:00
|
|
|
{
|
|
|
|
//
|
2018-04-14 19:58:13 -07:00
|
|
|
int crossover_point_index;
|
2018-04-13 23:59:20 -07:00
|
|
|
int bit_count;
|
2018-04-14 19:58:13 -07:00
|
|
|
double random_double;
|
2018-04-13 23:59:20 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
bit_count = chromosome->GetBitCount();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Choose a double between [0.0,1.0] for the crossover point.
|
2018-04-14 19:58:13 -07:00
|
|
|
Use normal distribution, with the mean and std from the parameters.
|
2018-04-13 23:59:20 -07:00
|
|
|
That way, there is still randomness to the crossover point,
|
2018-04-14 19:58:13 -07:00
|
|
|
but it still generally sticks near the chosen point.
|
2018-04-13 23:59:20 -07:00
|
|
|
*/
|
2018-04-14 19:58:13 -07:00
|
|
|
random_double = this->random->GetNormal(crossover_point, crossover_point_std);
|
2018-04-13 23:59:20 -07:00
|
|
|
|
|
|
|
// Apply to the actual int length
|
2018-04-14 19:58:13 -07:00
|
|
|
crossover_point_index = floor(random_double * bit_count);
|
2018-04-13 23:59:20 -07:00
|
|
|
|
2018-04-14 19:58:13 -07:00
|
|
|
// Loop around to keep in bounds?
|
|
|
|
if ( crossover_bounds == Enums::CrossoverBounds::Wrap ) {
|
|
|
|
while ( crossover_point_index < 0 )
|
|
|
|
{
|
|
|
|
crossover_point_index += bit_count;
|
|
|
|
}
|
|
|
|
while ( crossover_point_index >= bit_count)
|
|
|
|
{
|
|
|
|
crossover_point_index -= bit_count;
|
|
|
|
}
|
2018-04-13 23:59:20 -07:00
|
|
|
}
|
2018-04-14 19:58:13 -07:00
|
|
|
else if ( crossover_bounds == Enums::CrossoverBounds::Clip ) {
|
|
|
|
if ( crossover_point_index < 0 ) {
|
|
|
|
crossover_point_index = 0;
|
|
|
|
}
|
|
|
|
else if ( crossover_point_index >= bit_count ) {
|
|
|
|
crossover_point_index = bit_count-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
throw std::runtime_error("Breeder::PickRandomCrossoverPoint() - Invalid crossover_bounds");
|
2018-04-13 23:59:20 -07:00
|
|
|
}
|
|
|
|
|
2018-04-14 19:58:13 -07:00
|
|
|
return crossover_point_index;
|
2018-04-13 23:59:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-04-14 01:07:56 -07:00
|
|
|
void Breeder::ApplyCrossover(
|
|
|
|
std::shared_ptr<Chromosome> kiddo,
|
|
|
|
std::shared_ptr<Chromosome> parent,
|
|
|
|
Enums::CrossoverType crossover_type,
|
2018-04-14 19:58:13 -07:00
|
|
|
Enums::CrossoverBounds crossover_bounds,
|
|
|
|
double crossover_point,
|
|
|
|
double crossover_point_std
|
2018-04-14 01:07:56 -07:00
|
|
|
)
|
2018-04-13 23:59:20 -07:00
|
|
|
{
|
|
|
|
//
|
|
|
|
int
|
|
|
|
bits_count,
|
2018-04-14 19:58:13 -07:00
|
|
|
crossover_point_index,
|
2018-04-13 23:59:20 -07:00
|
|
|
i
|
|
|
|
;
|
|
|
|
|
2018-04-14 01:07:56 -07:00
|
|
|
// Only proceed if using sexual crossover
|
|
|
|
if (crossover_type != Enums::CrossoverType::Sexual) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now, don't crossover unless the bit lengths are identical
|
2018-04-13 23:59:20 -07:00
|
|
|
bits_count = kiddo->GetBitCount();
|
|
|
|
if ( parent->GetBitCount() != bits_count ) {
|
|
|
|
throw std::runtime_error("Breeder::ApplyCrossover() - Parent incompatible with Kiddo (bit lengths don't match)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick random crossover point
|
2018-04-14 19:58:13 -07:00
|
|
|
crossover_point_index = this->PickRandomCrossoverPoint(kiddo, crossover_bounds, crossover_point, crossover_point_std);
|
2018-04-13 23:59:20 -07:00
|
|
|
|
|
|
|
// Begin copying the parent at the crossover point and beyond
|
|
|
|
// (not before)
|
2018-04-14 19:58:13 -07:00
|
|
|
for ( i=crossover_point_index; i<bits_count; i++) {
|
2018-04-13 23:59:20 -07:00
|
|
|
kiddo->SetBit( i, parent->GetBit(i) );
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
kiddo->ResetFitness();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|