From b10b4388897b1673b0dbe84b1db34584db0e9660 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 6 Aug 2024 21:34:03 -0400 Subject: [PATCH] cleanup and more functions --- CMakeLists.txt | 2 +- include/blt/gp/operations.h | 5 +++++ include/blt/gp/transformers.h | 5 ++--- include/blt/gp/tree.h | 2 ++ src/transformers.cpp | 27 +++++---------------------- src/tree.cpp | 18 ++++++++++++++++++ 6 files changed, 33 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e0806e8..a0aeff0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.123) +project(blt-gp VERSION 0.0.124) include(CTest) diff --git a/include/blt/gp/operations.h b/include/blt/gp/operations.h index b80d9fe..a7b6828 100644 --- a/include/blt/gp/operations.h +++ b/include/blt/gp/operations.h @@ -177,6 +177,11 @@ namespace blt::gp return name; } + inline constexpr const auto& get_function() const + { + return func; + } + operator_id id = -1; private: function_t func; diff --git a/include/blt/gp/transformers.h b/include/blt/gp/transformers.h index e055237..da011a3 100644 --- a/include/blt/gp/transformers.h +++ b/include/blt/gp/transformers.h @@ -32,8 +32,6 @@ namespace blt::gp using op_iter = std::vector::iterator; } - blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, const std::vector& container, blt::ptrdiff_t start); - class crossover_t { public: @@ -108,7 +106,8 @@ namespace blt::gp virtual tree_t apply(gp_program& program, const tree_t& p); // NOLINT - void mutate_point(gp_program& program, tree_t& c, blt::size_t node); + // returns the point after the mutation + blt::size_t mutate_point(gp_program& program, tree_t& c, blt::size_t node); virtual ~mutation_t() = default; diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index afe145d..5838fe1 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -112,6 +112,8 @@ namespace blt::gp } void print(gp_program& program, std::ostream& output, bool print_literals = true, bool pretty_indent = false, bool include_types = false) const; + + blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, blt::ptrdiff_t start); private: std::vector operations; diff --git a/src/transformers.cpp b/src/transformers.cpp index 4379ece..e30e034 100644 --- a/src/transformers.cpp +++ b/src/transformers.cpp @@ -71,10 +71,10 @@ namespace blt::gp return blt::unexpected(point.error()); auto crossover_point_begin_itr = c1_ops.begin() + point->p1_crossover_point; - auto crossover_point_end_itr = c1_ops.begin() + find_endpoint(program, c1_ops, point->p1_crossover_point); + auto crossover_point_end_itr = c1_ops.begin() + c1.find_endpoint(program, point->p1_crossover_point); auto found_point_begin_itr = c2_ops.begin() + point->p2_crossover_point; - auto found_point_end_itr = c2_ops.begin() + find_endpoint(program, c2_ops, point->p2_crossover_point); + auto found_point_end_itr = c2_ops.begin() + c2.find_endpoint(program, point->p2_crossover_point); stack_allocator& c1_stack = c1.get_values(); stack_allocator& c2_stack = c2.get_values(); @@ -215,13 +215,13 @@ namespace blt::gp return c; } - void mutation_t::mutate_point(gp_program& program, tree_t& c, blt::size_t node) + blt::size_t mutation_t::mutate_point(gp_program& program, tree_t& c, blt::size_t node) { auto& ops_r = c.get_operations(); auto& vals_r = c.get_values(); auto begin_point = static_cast(node); - auto end_point = find_endpoint(program, ops_r, begin_point); + auto end_point = c.find_endpoint(program, begin_point); auto begin_operator_id = ops_r[begin_point].id; const auto& type_info = program.get_operator_info(begin_operator_id); @@ -293,23 +293,6 @@ namespace blt::gp throw std::exception(); } #endif - } - - blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, const std::vector& container, blt::ptrdiff_t index) - { - blt::i64 children_left = 0; - - do - { - const auto& type = program.get_operator_info(container[index].id); - // this is a child to someone - if (children_left != 0) - children_left--; - if (type.argc.argc > 0) - children_left += type.argc.argc; - index++; - } while (children_left > 0); - - return index; + return begin_point + new_ops_r.size(); } } \ No newline at end of file diff --git a/src/tree.cpp b/src/tree.cpp index 5bb1eff..28270c6 100644 --- a/src/tree.cpp +++ b/src/tree.cpp @@ -201,4 +201,22 @@ namespace blt::gp return depth; } + + blt::ptrdiff_t tree_t::find_endpoint(gp_program& program, blt::ptrdiff_t index) + { + blt::i64 children_left = 0; + + do + { + const auto& type = program.get_operator_info(operations[index].id); + // this is a child to someone + if (children_left != 0) + children_left--; + if (type.argc.argc > 0) + children_left += type.argc.argc; + index++; + } while (children_left > 0); + + return index; + } } \ No newline at end of file