From d3124f8516474afdb06e0b7cdaa6b426308cef1c Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 30 Jun 2024 03:20:56 -0400 Subject: [PATCH] crossover --- CMakeLists.txt | 2 +- include/blt/gp/program.h | 5 +++++ include/blt/gp/stack.h | 11 +++++++---- include/blt/gp/transformers.h | 22 ++++++++++++++++++++-- include/blt/gp/tree.h | 16 ---------------- lib/blt | 2 +- src/generators.cpp | 5 ----- src/transformers.cpp | 9 ++++++++- 8 files changed, 42 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a245a1..cc244f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.42) +project(blt-gp VERSION 0.0.43) include(CTest) diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index a8ef710..0b167a4 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -53,6 +53,11 @@ namespace blt::gp blt::size_t argc_context = 0; }; + struct operator_info + { + + }; + struct operator_storage { // indexed from return TYPE ID, returns index of operator diff --git a/include/blt/gp/stack.h b/include/blt/gp/stack.h index 9e5762f..b1f1888 100644 --- a/include/blt/gp/stack.h +++ b/include/blt/gp/stack.h @@ -21,11 +21,13 @@ #include #include +#include #include #include #include #include #include +#include namespace blt::gp { @@ -52,7 +54,8 @@ namespace blt::gp template T pop() { - static_assert(std::is_trivially_copyable_v> && "Type must be bitwise copyable!"); + using NO_REF_T = std::remove_reference_t; + static_assert(std::is_trivially_copyable_v && "Type must be bitwise copyable!"); constexpr static auto TYPE_SIZE = aligned_size(); if (head == nullptr) throw std::runtime_error("Silly boi the stack is empty!"); @@ -116,7 +119,8 @@ namespace blt::gp free(old); if (diff == 0) break; - } else { + } else + { // otherwise update the offset pointer head->metadata.offset -= bytes; break; @@ -185,8 +189,7 @@ namespace blt::gp stack_allocator& operator=(stack_allocator&& move) noexcept { - head = move.head; - move.head = nullptr; + move.head = std::exchange(head, move.head); return *this; } diff --git a/include/blt/gp/transformers.h b/include/blt/gp/transformers.h index d2c8a24..31c3781 100644 --- a/include/blt/gp/transformers.h +++ b/include/blt/gp/transformers.h @@ -22,6 +22,7 @@ #include #include #include +#include namespace blt::gp { @@ -29,8 +30,25 @@ namespace blt::gp class crossover_t { public: - BLT_ATTRIB_CONST virtual std::pair apply(gp_program& program, const tree_t& p1, const tree_t& p2); - virtual void apply_in_place(gp_program& program, tree_t& p1, tree_t& p2); + enum class error_t + { + NO_VALID_TYPE + }; + struct result_t + { + tree_t child1; + tree_t child2; + }; + + /** + * child1 and child2 are copies of the parents, the result of selecting a crossover point and performing standard subtree crossover. + * the parents are not modified during this process + * @param program reference to the global program container responsible for managing these trees + * @param p1 reference to the first parent + * @param p2 reference to the second parent + * @return expected pair of child otherwise returns error enum + */ + virtual blt::expected apply(gp_program& program, const tree_t& p1, const tree_t& p2); // NOLINT }; } diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index c78398d..00cdc46 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -57,7 +57,6 @@ namespace blt::gp public: [[nodiscard]] inline std::vector& get_operations() { - valid = false; return operations; } @@ -71,20 +70,6 @@ namespace blt::gp return values; } - void setDepth(blt::size_t d) - { - depth = d; - valid = true; - } - - blt::size_t getDepth() - { - if (valid) - return depth; - valid = true; - return 0; - } - evaluation_context evaluate(void* context); /** @@ -116,7 +101,6 @@ namespace blt::gp } private: - bool valid = false; std::vector operations; blt::gp::stack_allocator values; blt::size_t depth; diff --git a/lib/blt b/lib/blt index cdb91d8..f3451b5 160000 --- a/lib/blt +++ b/lib/blt @@ -1 +1 @@ -Subproject commit cdb91d800781d2c2a8ad3b9a829ca6d52abaa6ea +Subproject commit f3451b57ab249fdcf5ccedd64cbd56b2a64e2de2 diff --git a/src/generators.cpp b/src/generators.cpp index 9fd5bc0..4e20e99 100644 --- a/src/generators.cpp +++ b/src/generators.cpp @@ -59,7 +59,6 @@ namespace blt::gp { auto top = tree_generator.top(); tree_generator.pop(); - //BLT_INFO("%ld D: %ld (%ld left)", top.id, top.depth, tree_generator.size()); tree.get_operations().emplace_back( args.program.get_operation(top.id), @@ -74,13 +73,9 @@ namespace blt::gp } for (const auto& child : args.program.get_argument_types(top.id)) - { std::forward(perChild)(args.program, tree_generator, child, top.depth + 1); - } } - tree.setDepth(max_depth); - return tree; } diff --git a/src/transformers.cpp b/src/transformers.cpp index 6a3bd24..0c53011 100644 --- a/src/transformers.cpp +++ b/src/transformers.cpp @@ -19,5 +19,12 @@ namespace blt::gp { - + blt::expected crossover_t::apply(gp_program& program, const tree_t& p1, const tree_t& p2) // NOLINT + { + result_t result{p1, p2}; + + + + return result; + } } \ No newline at end of file