From 665e019bad489257542aea320076d0833ee014e0 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 15 Jul 2025 14:07:32 -0400 Subject: [PATCH] fix stats --- include/blt/gp/program.h | 70 ++++++++++++++------------------ include/blt/gp/tree.h | 21 ++++++++++ include/blt/gp/util/statistics.h | 12 +++--- lib/blt | 2 +- 4 files changed, 58 insertions(+), 47 deletions(-) diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index 4183cc9..4bf6f94 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -374,7 +374,11 @@ namespace blt::gp { current_generation = 0; current_pop = config.pop_initializer.get().generate({ - *this, root_type, config.population_size, config.initial_min_tree_size, config.initial_max_tree_size + *this, + root_type, + config.population_size, + config.initial_min_tree_size, + config.initial_max_tree_size }); next_pop = population_t(current_pop); BLT_ASSERT_MSG(current_pop.get_individuals().size() == config.population_size, @@ -393,7 +397,11 @@ namespace blt::gp void generate_initial_population(const type_id root_type) { current_pop = config.pop_initializer.get().generate({ - *this, root_type, config.population_size, config.initial_min_tree_size, config.initial_max_tree_size + *this, + root_type, + config.population_size, + config.initial_min_tree_size, + config.initial_max_tree_size }); next_pop = population_t(current_pop); BLT_ASSERT_MSG(current_pop.get_individuals().size() == config.population_size, @@ -753,25 +761,13 @@ namespace blt::gp storage = std::move(op); } - template - std::array get_best_indexes() + template + std::array get_best_indexes() const { - std::array arr; + std::array arr; - tracked_vector> values; - values.reserve(current_pop.get_individuals().size()); - - for (const auto& [index, value] : blt::enumerate(current_pop.get_individuals())) - values.emplace_back(index, value.fitness.adjusted_fitness); - - std::sort(values.begin(), values.end(), [](const auto& a, const auto& b) { - return a.second > b.second; - }); - - for (blt::size_t i = 0; i < std::min(size, config.population_size); i++) - arr[i] = values[i].first; - for (blt::size_t i = std::min(size, config.population_size); i < size; i++) - arr[i] = 0; + for (auto [i, a] : enumerate(arr)) + a = std::min(i, config.population_size); return arr; } @@ -810,10 +806,6 @@ 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; } }; @@ -840,14 +832,6 @@ namespace blt::gp 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(); } }; } @@ -870,15 +854,15 @@ namespace blt::gp fitness_function(ind.tree, ind.fitness, i); } - auto old_best = current_stats.best_fitness.load(std::memory_order_relaxed); - while (ind.fitness.adjusted_fitness > old_best && !current_stats.best_fitness.compare_exchange_weak( - old_best, ind.fitness.adjusted_fitness, std::memory_order_relaxed, std::memory_order_relaxed)) - {} - - auto old_worst = current_stats.worst_fitness.load(std::memory_order_relaxed); - while (ind.fitness.adjusted_fitness < old_worst && !current_stats.worst_fitness.compare_exchange_weak( - old_worst, ind.fitness.adjusted_fitness, std::memory_order_relaxed, std::memory_order_relaxed)) - {} + // auto old_best = current_stats.best_fitness.load(std::memory_order_relaxed); + // while (ind.fitness.adjusted_fitness > old_best && !current_stats.best_fitness.compare_exchange_weak( + // old_best, ind.fitness.adjusted_fitness, std::memory_order_relaxed, std::memory_order_relaxed)) + // {} + // + // auto old_worst = current_stats.worst_fitness.load(std::memory_order_relaxed); + // while (ind.fitness.adjusted_fitness < old_worst && !current_stats.worst_fitness.compare_exchange_weak( + // old_worst, ind.fitness.adjusted_fitness, std::memory_order_relaxed, std::memory_order_relaxed)) + // {} auto old_overall = current_stats.overall_fitness.load(std::memory_order_relaxed); while (!current_stats.overall_fitness.compare_exchange_weak(old_overall, ind.fitness.adjusted_fitness + old_overall, @@ -1003,6 +987,12 @@ namespace blt::gp thread_helper.evaluation_left.store(config.population_size, std::memory_order_release); (*thread_execution_service)(0); + std::sort(current_pop.begin(), current_pop.end(), [](const auto& a, const auto& b) { + return a.fitness.adjusted_fitness > b.fitness.adjusted_fitness; + }); + + current_stats.best_fitness = current_pop.get_individuals()[0].fitness.adjusted_fitness; + current_stats.worst_fitness = current_pop.get_individuals()[current_pop.get_individuals().size() - 1].fitness.adjusted_fitness; current_stats.average_fitness = current_stats.overall_fitness / static_cast(config.population_size); } diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index 18e75be..f25fba0 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -713,6 +713,27 @@ namespace blt::gp double standardized_fitness = 0; double adjusted_fitness = 0; i64 hits = 0; + + /** + * Sets fitness such that larger values of raw_fitness are worse + */ + void set_normal(const double raw_fit) + { + raw_fitness = raw_fit; + standardized_fitness = raw_fit; + adjusted_fitness = 1 / (1 + raw_fit); + } + + /** + * Sets fitness such that smaller values of raw_fitness are worse + * Note: this function loses accuracy the larger the number. if your desired fitness is around 0-20 (raw) then this will work + */ + void set_inverted_normal(const double raw_fit) + { + raw_fitness = raw_fit; + standardized_fitness = raw_fit; + adjusted_fitness = 1 - (1 / (1 + raw_fit)); + } }; struct individual_t diff --git a/include/blt/gp/util/statistics.h b/include/blt/gp/util/statistics.h index 3fd868d..bf79733 100644 --- a/include/blt/gp/util/statistics.h +++ b/include/blt/gp/util/statistics.h @@ -178,22 +178,22 @@ namespace blt::gp { move.overall_fitness = 0; move.average_fitness = 0; - move.best_fitness = 0; - move.worst_fitness = 0; + move.best_fitness = std::numeric_limits::min(); + move.worst_fitness = std::numeric_limits::max(); } std::atomic overall_fitness = 0; std::atomic average_fitness = 0; - std::atomic best_fitness = 0; - std::atomic worst_fitness = 1; + std::atomic best_fitness = std::numeric_limits::min(); + std::atomic worst_fitness = std::numeric_limits::max(); tracked_vector normalized_fitness{}; void clear() { overall_fitness = 0; average_fitness = 0; - best_fitness = 0; - worst_fitness = 0; + best_fitness = std::numeric_limits::min(); + worst_fitness = std::numeric_limits::max(); normalized_fitness.clear(); } }; diff --git a/lib/blt b/lib/blt index 729a16a..284743c 160000 --- a/lib/blt +++ b/lib/blt @@ -1 +1 @@ -Subproject commit 729a16ab574e31bf1b44446a777e4ee834518c6e +Subproject commit 284743c683aebae3277f64a07cdd0deedf629039