main
Brett 2025-04-02 01:07:52 -04:00
parent 08e44fb4bd
commit 9cc4b9949b
5 changed files with 34 additions and 20 deletions

View File

@ -27,7 +27,7 @@ macro(compile_options target_name)
sanitizers(${target_name})
endmacro()
project(blt-gp VERSION 0.4.0)
project(blt-gp VERSION 0.4.1)
include(CTest)

View File

@ -194,10 +194,10 @@ namespace blt::gp::example
// TODO: make stats helper
const auto& stats = program.get_population_stats();
BLT_INFO("Stats:");
BLT_INFO("Average fitness: %lf", stats.average_fitness.load());
BLT_INFO("Best fitness: %lf", stats.best_fitness.load());
BLT_INFO("Worst fitness: %lf", stats.worst_fitness.load());
BLT_INFO("Overall fitness: %lf", stats.overall_fitness.load());
BLT_INFO("Average fitness: {:0.6f}", stats.average_fitness.load());
BLT_INFO("Best fitness: {:0.6f}", stats.best_fitness.load());
BLT_INFO("Worst fitness: {:0.6f}", stats.worst_fitness.load());
BLT_INFO("Overall fitness: {:0.6f}", stats.overall_fitness.load());
}
void execute()

View File

@ -462,7 +462,7 @@ namespace blt::gp
[this, &fitness_function, &crossover_selection, &mutation_selection, &reproduction_selection](const size_t id) {
thread_helper.barrier.wait();
multi_threaded_fitness_eval<FitnessFunc>()(fitness_function);
multi_threaded_fitness_eval<FitnessFunc>()(fitness_function, id);
if (thread_helper.next_gen_left > 0)
{
@ -574,7 +574,7 @@ namespace blt::gp
const size_t id) {
thread_helper.barrier.wait();
multi_threaded_fitness_eval<FitnessFunc>()(fitness_function);
multi_threaded_fitness_eval<FitnessFunc>()(fitness_function, id);
if (thread_helper.next_gen_left > 0)
{
@ -804,6 +804,10 @@ namespace blt::gp
current_stats.normalized_fitness.push_back(sum_of_prob + prob);
sum_of_prob += prob;
}
std::sort(current_pop.begin(), current_pop.end(), [](const auto& a, const auto& b)
{
return a.fitness.adjusted_fitness > b.fitness.adjusted_fitness;
});
thread_helper.evaluation_left = 0;
}
};
@ -812,9 +816,10 @@ namespace blt::gp
template <typename FitnessFunc>
auto multi_threaded_fitness_eval()
{
return [this](FitnessFunc& fitness_function) {
return [this](FitnessFunc& fitness_function, size_t thread_id) {
if (thread_helper.evaluation_left > 0)
{
thread_helper.barrier.wait();
while (thread_helper.evaluation_left > 0)
{
size_t size = 0;
@ -828,6 +833,15 @@ namespace blt::gp
std::memory_order::memory_order_relaxed));
perform_fitness_function(begin, end, fitness_function);
}
thread_helper.barrier.wait();
if (thread_id == 0)
{
std::sort(current_pop.begin(), current_pop.end(), [](const auto& a, const auto& b)
{
return a.fitness.adjusted_fitness > b.fitness.adjusted_fitness;
});
}
thread_helper.barrier.wait();
}
};
}

@ -1 +1 @@
Subproject commit 9a05c86b02c9c45c2b384c531007416148ec4b56
Subproject commit 0ebbc198c5b80ca99e537460ef92fd806864fa3e

View File

@ -20,12 +20,12 @@
namespace blt::gp
{
void select_best_t::pre_process(gp_program&, population_t& pop)
void select_best_t::pre_process(gp_program&, population_t&)
{
std::sort(pop.begin(), pop.end(), [](const auto& a, const auto& b)
{
return a.fitness.adjusted_fitness > b.fitness.adjusted_fitness;
});
// std::sort(pop.begin(), pop.end(), [](const auto& a, const auto& b)
// {
// return a.fitness.adjusted_fitness > b.fitness.adjusted_fitness;
// });
index = 0;
}
@ -35,19 +35,19 @@ namespace blt::gp
return pop.get_individuals()[index.fetch_add(1, std::memory_order_relaxed) % size].tree;
}
void select_worst_t::pre_process(gp_program&, population_t& pop)
void select_worst_t::pre_process(gp_program&, population_t&)
{
std::sort(pop.begin(), pop.end(), [](const auto& a, const auto& b)
{
return a.fitness.adjusted_fitness < b.fitness.adjusted_fitness;
});
// std::sort(pop.begin(), pop.end(), [](const auto& a, const auto& b)
// {
// return a.fitness.adjusted_fitness < b.fitness.adjusted_fitness;
// });
index = 0;
}
const tree_t& select_worst_t::select(gp_program&, const population_t& pop)
{
const auto size = pop.get_individuals().size();
return pop.get_individuals()[index.fetch_add(1, std::memory_order_relaxed) % size].tree;
return pop.get_individuals()[(size - 1) - (index.fetch_add(1, std::memory_order_relaxed) % size)].tree;
}
const tree_t& select_random_t::select(gp_program& program, const population_t& pop)