From d5dea3b31ecc02207af6bc8ecfbd715c149febe5 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 6 Aug 2024 18:58:04 -0400 Subject: [PATCH] cleanup tree evaluation --- CMakeLists.txt | 2 +- include/blt/gp/transformers.h | 2 ++ include/blt/gp/tree.h | 3 --- lib/blt | 2 +- src/transformers.cpp | 15 ++++++++++----- src/tree.cpp | 7 +------ 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bae24a8..e0806e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.122) +project(blt-gp VERSION 0.0.123) include(CTest) diff --git a/include/blt/gp/transformers.h b/include/blt/gp/transformers.h index 60042cb..e055237 100644 --- a/include/blt/gp/transformers.h +++ b/include/blt/gp/transformers.h @@ -108,6 +108,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); + virtual ~mutation_t() = default; protected: diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index 8e02a61..afe145d 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -34,9 +34,6 @@ namespace blt::gp struct op_container_t { -// op_container_t(detail::callable_t& func, detail::transfer_t& transfer, operator_id id, bool is_value): -// func(func), transfer(transfer), id(id), is_value(is_value) -// {} op_container_t(detail::callable_t& func, blt::size_t type_size, operator_id id, bool is_value): func(func), type_size(type_size), id(id), is_value(is_value) {} diff --git a/lib/blt b/lib/blt index 3e8b616..42fa378 160000 --- a/lib/blt +++ b/lib/blt @@ -1 +1 @@ -Subproject commit 3e8b616bf9d0513824d4452e3e77fccac910f0fb +Subproject commit 42fa3782007e92119f550fcc8c871398ec1310e8 diff --git a/src/transformers.cpp b/src/transformers.cpp index 66b6e2f..4379ece 100644 --- a/src/transformers.cpp +++ b/src/transformers.cpp @@ -210,10 +210,17 @@ namespace blt::gp { auto c = p; + mutate_point(program, c, program.get_random().get_size_t(0ul, c.get_operations().size())); + + return c; + } + + void 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(program.get_random().get_size_t(0ul, ops_r.size())); + auto begin_point = static_cast(node); auto end_point = find_endpoint(program, ops_r, begin_point); auto begin_operator_id = ops_r[begin_point].id; const auto& type_info = program.get_operator_info(begin_operator_id); @@ -276,8 +283,8 @@ namespace blt::gp << "\n"; std::cout << "now Named: " << (program.get_name(ops_r[begin_point].id) ? *program.get_name(ops_r[begin_point].id) : "Unnamed") << "\n"; std::cout << "Was named: " << (program.get_name(begin_operator_id) ? *program.get_name(begin_operator_id) : "Unnamed") << "\n"; - std::cout << "Parent:" << std::endl; - p.print(program, std::cout, false, true); + //std::cout << "Parent:" << std::endl; + //p.print(program, std::cout, false, true); std::cout << "Child:" << std::endl; c.print(program, std::cout, false, true); std::cout << std::endl; @@ -286,8 +293,6 @@ namespace blt::gp throw std::exception(); } #endif - - return c; } blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, const std::vector& container, blt::ptrdiff_t index) diff --git a/src/tree.cpp b/src/tree.cpp index 6d32d3b..5bb1eff 100644 --- a/src/tree.cpp +++ b/src/tree.cpp @@ -47,20 +47,15 @@ namespace blt::gp auto value_stack = values; auto& values_process = results.values; - auto operations_stack = operations; - while (!operations_stack.empty()) + for (const auto& operation : blt::reverse_iterate(operations.begin(), operations.end())) { - 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) { value_stack.transfer_bytes(values_process, operation.type_size); continue; } operation.func(context, values_process, values_process); - //operations_stack.emplace_back(empty_callable, operation.type_size, operation.id, true); } return results;