From 7810a212265e01d3f24d9b24350233bd4b4f2a35 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 21 Jul 2024 23:23:50 -0400 Subject: [PATCH] fix fitness proportinate --- CMakeLists.txt | 4 ++-- src/selection.cpp | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 23de5e5..e685b9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.95) +project(blt-gp VERSION 0.0.96) include(CTest) @@ -87,7 +87,7 @@ endmacro() if (${BUILD_EXAMPLES}) - blt_add_project(blt-SR-playground examples/symbolic_regression.cpp example) + blt_add_project(blt-symbolic-regression examples/symbolic_regression.cpp example) endif () diff --git a/src/selection.cpp b/src/selection.cpp index 779cb5e..34343ed 100644 --- a/src/selection.cpp +++ b/src/selection.cpp @@ -80,12 +80,17 @@ namespace blt::gp tree_t& select_fitness_proportionate_t::select(gp_program& program, population_t& pop, population_stats& stats) { auto choice = program.get_random().get_double(); - for (const auto& ind : blt::enumerate(pop)) + for (const auto& [index, ref] : blt::enumerate(pop)) { - if (ind.first == 0 && choice <= stats.normalized_fitness[ind.first]) - return ind.second.tree; - if (choice > stats.normalized_fitness[ind.first - 1] && choice <= stats.normalized_fitness[ind.first]) - return ind.second.tree; + if (index == 0) + { + if (choice <= stats.normalized_fitness[index]) + return ref.tree; + } else + { + if (choice > stats.normalized_fitness[index - 1] && choice <= stats.normalized_fitness[index]) + return ref.tree; + } } BLT_WARN("Unable to find individual with fitness proportionate. This should not be a possible code path! (%lf)", choice); return pop.get_individuals()[0].tree;