fix fitness proportinate

thread
Brett 2024-07-21 23:23:50 -04:00
parent 32c5c6abf6
commit 7810a21226
2 changed files with 12 additions and 7 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.0.95) project(blt-gp VERSION 0.0.96)
include(CTest) include(CTest)
@ -87,7 +87,7 @@ endmacro()
if (${BUILD_EXAMPLES}) 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 () endif ()

View File

@ -80,12 +80,17 @@ namespace blt::gp
tree_t& select_fitness_proportionate_t::select(gp_program& program, population_t& pop, population_stats& stats) tree_t& select_fitness_proportionate_t::select(gp_program& program, population_t& pop, population_stats& stats)
{ {
auto choice = program.get_random().get_double(); 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]) if (index == 0)
return ind.second.tree; {
if (choice > stats.normalized_fitness[ind.first - 1] && choice <= stats.normalized_fitness[ind.first]) if (choice <= stats.normalized_fitness[index])
return ind.second.tree; 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); 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; return pop.get_individuals()[0].tree;