Duplicated EvaluateFitness() to EvaluateError(), reversed
This commit is contained in:
parent
b256a47cde
commit
a233802f6c
@ -237,6 +237,45 @@ namespace BitEvolver
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
void Population::EvaluateError(std::function<double(std::shared_ptr<Chromosome>)> evaluation_callback)
|
||||
{
|
||||
//
|
||||
std::shared_ptr<std::vector<std::shared_ptr<Chromosome>>> _chromosomes_copy;
|
||||
std::vector<std::shared_ptr<std::thread>> threads;
|
||||
std::shared_ptr<std::thread> thread;
|
||||
int
|
||||
threads_count,
|
||||
i
|
||||
;
|
||||
|
||||
// Make a new vector containing all current chromosomes
|
||||
this->population_modification_mutex.lock();
|
||||
_chromosomes_copy = std::shared_ptr<std::vector<std::shared_ptr<Chromosome>>>(
|
||||
new std::vector<std::shared_ptr<Chromosome>>()
|
||||
);
|
||||
for ( i=0; i<(int)this->chromosomes.size(); i++ ) {
|
||||
_chromosomes_copy->push_back( this->chromosomes[i] );
|
||||
}
|
||||
this->population_modification_mutex.unlock();
|
||||
|
||||
// Spawn threads
|
||||
threads_count = this->GetThreadCountSuggestion();
|
||||
for ( i=0; i<threads_count; i++) {
|
||||
|
||||
//
|
||||
thread = std::shared_ptr<std::thread>(
|
||||
new std::thread(&Population::EvaluateError_Thread, this, _chromosomes_copy, evaluation_callback)
|
||||
);
|
||||
threads.push_back(thread);
|
||||
}
|
||||
|
||||
// Wait for threads to finish
|
||||
for ( i=0; i<threads_count; i++ ) {
|
||||
threads[i]->join();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
void Population::Evolve()
|
||||
{
|
||||
@ -448,6 +487,41 @@ namespace BitEvolver
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
void Population::EvaluateError_Thread(
|
||||
std::shared_ptr<std::vector<std::shared_ptr<Chromosome>>> _chromosomes,
|
||||
std::function<double(std::shared_ptr<Chromosome>)> evaluation_callback
|
||||
)
|
||||
{
|
||||
//
|
||||
std::shared_ptr<Chromosome> chromosome;
|
||||
double error;
|
||||
|
||||
//
|
||||
while (true)
|
||||
{
|
||||
// Grab a free chromosome
|
||||
this->evaluate_fitness_mutex.lock();
|
||||
chromosome = nullptr;
|
||||
if ( _chromosomes->size() ) {
|
||||
chromosome = _chromosomes->at(_chromosomes->size()-1);
|
||||
_chromosomes->pop_back();
|
||||
}
|
||||
this->evaluate_fitness_mutex.unlock();
|
||||
|
||||
// Call the evaluation callback
|
||||
if ( chromosome != nullptr ) {
|
||||
error = evaluation_callback(chromosome);
|
||||
chromosome->SetError(error);
|
||||
}
|
||||
|
||||
// We're done if there was nothing to grab
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
std::shared_ptr<Chromosome> Population::BreedChild()
|
||||
{
|
||||
|
@ -58,6 +58,7 @@ namespace BitEvolver
|
||||
|
||||
//
|
||||
void EvaluateFitness(std::function<double(std::shared_ptr<Chromosome>)> evaluation_callback);
|
||||
void EvaluateError(std::function<double(std::shared_ptr<Chromosome>)> evaluation_callback);
|
||||
|
||||
//
|
||||
void Evolve();
|
||||
@ -117,6 +118,10 @@ namespace BitEvolver
|
||||
std::shared_ptr<std::vector<std::shared_ptr<Chromosome>>> _chromosomes,
|
||||
std::function<double(std::shared_ptr<Chromosome>)> evaluation_callback
|
||||
);
|
||||
void EvaluateError_Thread(
|
||||
std::shared_ptr<std::vector<std::shared_ptr<Chromosome>>> _chromosomes,
|
||||
std::function<double(std::shared_ptr<Chromosome>)> evaluation_callback
|
||||
);
|
||||
|
||||
//
|
||||
std::shared_ptr<Chromosome> BreedChild();
|
||||
|
Loading…
Reference in New Issue
Block a user