From 246f305957d14d3a51605e4948f4f050fb91a163 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 10 Jul 2024 02:54:58 -0400 Subject: [PATCH] depth to trees --- CMakeLists.txt | 2 +- examples/gp_test_7.cpp | 10 +++++----- include/blt/gp/program.h | 5 ++--- include/blt/gp/selection.h | 6 +++--- include/blt/gp/tree.h | 4 +++- src/tree.cpp | 40 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index de611dd..40a8972 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.54) +project(blt-gp VERSION 0.0.55) include(CTest) diff --git a/examples/gp_test_7.cpp b/examples/gp_test_7.cpp index a7bad5d..f4214ae 100644 --- a/examples/gp_test_7.cpp +++ b/examples/gp_test_7.cpp @@ -56,11 +56,11 @@ void print_best() auto best = program.get_best<10>(); for (auto& v : best) - BLT_TRACE(v.get().get_evaluation_value(nullptr)); - std::string small("--------------------------"); - for (blt::size_t i = 0; i < std::to_string(program.get_current_generation()).size(); i++) - small += "-"; - BLT_TRACE(small); + BLT_TRACE("%lf (depth: %ld)", v.get().get_evaluation_value(nullptr), v.get().get_depth(program)); + //std::string small("--------------------------"); + //for (blt::size_t i = 0; i < std::to_string(program.get_current_generation()).size(); i++) + // small += "-"; + //BLT_TRACE(small); } /** diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index ad3b704..e43ca0f 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -36,7 +36,6 @@ #include #include #include -#include #include #include #include @@ -280,8 +279,8 @@ namespace blt::gp template void evaluate_fitness(Callable&& fitness_function, Container& result_storage) { - for (const auto& ind : blt::enumerate(current_pop.get_individuals())) - ind.second.raw_fitness = static_cast(fitness_function(ind.second.tree, result_storage, ind.first)); + for (const auto& ind : blt::enumerate(current_pop.get_individuals())) + ind.second.raw_fitness = static_cast(fitness_function(ind.second.tree, result_storage, ind.first)); double min = 0; for (auto& ind : current_pop.get_individuals()) { diff --git a/include/blt/gp/selection.h b/include/blt/gp/selection.h index 68fed2c..83f6d26 100644 --- a/include/blt/gp/selection.h +++ b/include/blt/gp/selection.h @@ -50,13 +50,13 @@ namespace blt::gp if (config.elites > 0) { std::vector> values; - + for (blt::size_t i = 0; i < config.elites; i++) values.emplace_back(i, current_pop.get_individuals()[i].adjusted_fitness); - + for (auto& ind : current_pop.get_individuals()) { - + } } diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index e5d8059..1d33edb 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -74,6 +74,8 @@ namespace blt::gp evaluation_context evaluate(void* context); + blt::size_t get_depth(gp_program& program); + /** * Helper template for returning the result of the last evaluation */ @@ -118,7 +120,7 @@ namespace blt::gp individual() = default; - explicit individual(tree_t&& tree): tree(tree) + explicit individual(tree_t&& tree): tree(std::move(tree)) {} explicit individual(const tree_t& tree): tree(tree) diff --git a/src/tree.cpp b/src/tree.cpp index 759a24b..5eae548 100644 --- a/src/tree.cpp +++ b/src/tree.cpp @@ -149,4 +149,44 @@ namespace blt::gp out << '\n'; } + + blt::size_t tree_t::get_depth(gp_program& program) + { + blt::size_t depth = 0; + + auto operations_stack = operations; + std::vector values_process; + std::vector value_stack; + + for (const auto& op : operations_stack) + { + if (op.is_value) + value_stack.push_back(1); + } + + while (!operations_stack.empty()) + { + auto operation = operations_stack.back(); + // keep the last value in the stack on the process stack stored in the eval context, this way it can be accessed easily. + operations_stack.pop_back(); + if (operation.is_value) + { + auto d = value_stack.back(); + depth = std::max(depth, d); + values_process.push_back(d); + value_stack.pop_back(); + continue; + } + blt::size_t local_depth = 0; + for (blt::size_t i = 0; i < program.get_operator_info(operation.id).argc.argc; i++) + { + local_depth = std::max(local_depth, values_process.back()); + values_process.pop_back(); + } + value_stack.push_back(local_depth + 1); + operations_stack.emplace_back(empty_callable, operation.transfer, operation.id, true); + } + + return depth; + } } \ No newline at end of file