From ef46bc3189740a042a66977714157267b74871dd Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 11 Jul 2024 00:52:36 -0400 Subject: [PATCH] i think elites are working now --- CMakeLists.txt | 2 +- examples/gp_test_7.cpp | 8 ++++---- include/blt/gp/program.h | 26 +++++++++++++------------- include/blt/gp/selection.h | 27 ++++++++++++++------------- include/blt/gp/tree.h | 3 ++- src/selection.cpp | 20 ++++++++++---------- 6 files changed, 44 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bcc7586..c0b4d2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.57) +project(blt-gp VERSION 0.0.58) include(CTest) diff --git a/examples/gp_test_7.cpp b/examples/gp_test_7.cpp index 651baca..c60a91b 100644 --- a/examples/gp_test_7.cpp +++ b/examples/gp_test_7.cpp @@ -60,8 +60,8 @@ void print_best() auto& v = program.get_current_pop().get_individuals()[i]; auto& tree = v.tree; auto size = tree.get_values().size(); - BLT_TRACE("%lf (fitness: %lf, raw: %lf) (depth: %ld) (blocks: %ld) (size: t: %ld m: %ld u: %ld r: %ld) filled: %f%%", - tree.get_evaluation_value(nullptr), v.adjusted_fitness, v.raw_fitness, + BLT_TRACE("%lf [index %ld] (fitness: %lf, raw: %lf) (depth: %ld) (blocks: %ld) (size: t: %ld m: %ld u: %ld r: %ld) filled: %f%%", + tree.get_evaluation_value(nullptr), i, v.standardized_fitness, v.raw_fitness, tree.get_depth(program), size.blocks, size.total_size_bytes, size.total_no_meta_bytes, size.total_used_bytes, size.total_remaining_bytes, static_cast(size.total_used_bytes) / static_cast(size.total_no_meta_bytes)); @@ -110,7 +110,7 @@ int main() current_tree.get_depth(program), size.blocks, size.total_size_bytes, size.total_no_meta_bytes, size.total_used_bytes, size.total_remaining_bytes, static_cast(size.total_used_bytes) / static_cast(size.total_no_meta_bytes));*/ container[index] = current_tree.get_evaluation_value(nullptr); - return container[index]; + return container[index] / 1000000000.0; }, result_container); print_best(); program.create_next_generation(blt::gp::select_tournament_t{}, blt::gp::select_tournament_t{}, @@ -120,7 +120,7 @@ int main() program.evaluate_fitness([](blt::gp::tree_t& current_tree, decltype(result_container)& container, blt::size_t index) { container[index] = current_tree.get_evaluation_value(nullptr); - return container[index]; + return container[index] / 1000000000.0; }, result_container); print_best(); diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index 6b5316b..88550d7 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -287,7 +287,7 @@ namespace blt::gp { if (ind.raw_fitness < min) min = ind.raw_fitness; - if (larger_better && ind.raw_fitness > max) + if (ind.raw_fitness > max) max = ind.raw_fitness; } @@ -301,29 +301,29 @@ namespace blt::gp for (auto& ind : current_pop.get_individuals()) { // make standardized fitness [0, +inf) - auto standardized_fitness = ind.raw_fitness + diff; - BLT_WARN(standardized_fitness); + ind.standardized_fitness = ind.raw_fitness + diff; + //BLT_WARN(ind.standardized_fitness); if (larger_better) - standardized_fitness = (max + diff) - standardized_fitness; - BLT_WARN(standardized_fitness); - ind.adjusted_fitness = (1.0 / (1.0 + standardized_fitness)); + ind.standardized_fitness = (max + diff) - ind.standardized_fitness; + //BLT_WARN(ind.standardized_fitness); + //ind.adjusted_fitness = (1.0 / (1.0 + ind.standardized_fitness)); - if (ind.adjusted_fitness > worst_fitness) + if (ind.standardized_fitness > worst_fitness) { - worst_fitness = ind.adjusted_fitness; + worst_fitness = ind.standardized_fitness; worst = &ind; } - if (ind.adjusted_fitness < best_fitness) + if (ind.standardized_fitness < best_fitness) { - best_fitness = ind.adjusted_fitness; + best_fitness = ind.standardized_fitness; best = &ind; } - overall_fitness += ind.adjusted_fitness; + overall_fitness += ind.standardized_fitness / static_cast(config.population_size); } - current_stats = {overall_fitness, overall_fitness / static_cast(config.population_size), best_fitness, worst_fitness, best, + current_stats = {overall_fitness, overall_fitness, best_fitness, worst_fitness, best, worst}; } @@ -347,7 +347,7 @@ namespace blt::gp values.reserve(current_pop.get_individuals().size()); for (const auto& ind : blt::enumerate(current_pop.get_individuals())) - values.emplace_back(ind.first, ind.second.adjusted_fitness); + values.emplace_back(ind.first, ind.second.standardized_fitness); std::sort(values.begin(), values.end(), [](const auto& a, const auto& b) { return a.second < b.second; diff --git a/include/blt/gp/selection.h b/include/blt/gp/selection.h index e8897a5..df3d91b 100644 --- a/include/blt/gp/selection.h +++ b/include/blt/gp/selection.h @@ -52,31 +52,32 @@ namespace blt::gp std::vector> values; for (blt::size_t i = 0; i < config.elites; i++) - values.emplace_back(i, current_pop.get_individuals()[i].adjusted_fitness); + values.emplace_back(i, current_pop.get_individuals()[i].standardized_fitness); for (const auto& ind : blt::enumerate(current_pop.get_individuals())) { - blt::i64 largest = -1; - double largest_fit = 0; for (blt::size_t i = 0; i < config.elites; i++) { - BLT_INFO("%lf < %lf? // %lf", ind.second.adjusted_fitness, values[i].second, ind.second.raw_fitness); - if (ind.second.adjusted_fitness < values[i].second && values[i].second > largest_fit) +// BLT_INFO("%lf < %lf? // %lf", ind.second.standardized_fitness, values[i].second, ind.second.raw_fitness); + if (ind.second.standardized_fitness < values[i].second) { - largest = static_cast(i); - largest_fit = values[i].second; + bool doesnt_contain = true; + for (blt::size_t j = 0; j < config.elites; j++) + { + if (ind.first == values[j].first) + doesnt_contain = false; + } + if (doesnt_contain) + values[i] = {ind.first, ind.second.standardized_fitness}; + break; } } - if (largest > 0) - { - BLT_INFO("%ld %lf", largest, largest_fit); - values[largest] = {ind.first, ind.second.adjusted_fitness}; - } } for (blt::size_t i = 0; i < config.elites; i++) { - BLT_DEBUG("%lf %lf", values[i].second, current_pop.get_individuals()[values[i].first].tree.get_evaluation_value(nullptr)); +// BLT_DEBUG("%lf at %ld produces %lf", values[i].second, values[i].first, +// current_pop.get_individuals()[values[i].first].tree.get_evaluation_value(nullptr)); next_pop.get_individuals().push_back(current_pop.get_individuals()[values[i].first]); } } diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index 1d33edb..f737aaa 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -115,7 +115,8 @@ namespace blt::gp { tree_t tree; double raw_fitness = 0; - double adjusted_fitness = 0; + double standardized_fitness = 0; + //double adjusted_fitness = 0; double probability = 0; individual() = default; diff --git a/src/selection.cpp b/src/selection.cpp index b5b78f1..7a159ae 100644 --- a/src/selection.cpp +++ b/src/selection.cpp @@ -24,13 +24,13 @@ namespace blt::gp tree_t& select_best_t::select(gp_program&, population_t& pop, population_stats&) { auto& first = pop.get_individuals()[0]; - double best_fitness = first.adjusted_fitness; + double best_fitness = first.standardized_fitness; tree_t* tree = &first.tree; for (auto& ind : pop.get_individuals()) { - if (ind.adjusted_fitness < best_fitness) + if (ind.standardized_fitness < best_fitness) { - best_fitness = ind.adjusted_fitness; + best_fitness = ind.standardized_fitness; tree = &ind.tree; } } @@ -40,13 +40,13 @@ namespace blt::gp tree_t& select_worst_t::select(gp_program&, population_t& pop, population_stats&) { auto& first = pop.get_individuals()[0]; - double worst_fitness = first.adjusted_fitness; + double worst_fitness = first.standardized_fitness; tree_t* tree = &first.tree; for (auto& ind : pop.get_individuals()) { - if (ind.adjusted_fitness > worst_fitness) + if (ind.standardized_fitness > worst_fitness) { - worst_fitness = ind.adjusted_fitness; + worst_fitness = ind.standardized_fitness; tree = &ind.tree; } } @@ -63,13 +63,13 @@ namespace blt::gp auto& first = pop.get_individuals()[program.get_random().get_size_t(0ul, pop.get_individuals().size())]; individual* ind = &first; - double best_guy = first.adjusted_fitness; + double best_guy = first.standardized_fitness; for (blt::size_t i = 0; i < selection_size - 1; i++) { auto& sel = pop.get_individuals()[program.get_random().get_size_t(0ul, pop.get_individuals().size())]; - if (sel.adjusted_fitness < best_guy) + if (sel.standardized_fitness < best_guy) { - best_guy = sel.adjusted_fitness; + best_guy = sel.standardized_fitness; ind = &sel; } } @@ -97,7 +97,7 @@ namespace blt::gp double sum_of_prob = 0; for (auto& ind : pop) { - ind.probability = sum_of_prob + (ind.adjusted_fitness / stats.overall_fitness); + ind.probability = sum_of_prob + (ind.standardized_fitness / stats.overall_fitness); sum_of_prob += ind.probability; } }