From 78b37584a9c5791669fbb776dc2144cfd6e1d4c3 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 27 Jun 2024 01:35:29 -0400 Subject: [PATCH] multi type test, it's possible for infinite recursion. --- CMakeLists.txt | 2 +- examples/gp_test_2.cpp | 17 +++++++++++++---- examples/gp_test_3.cpp | 11 +++++++++++ src/generators.cpp | 18 ++++++++++++------ 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 79cf22e..ea6c3b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.36) +project(blt-gp VERSION 0.0.37) include(CTest) diff --git a/examples/gp_test_2.cpp b/examples/gp_test_2.cpp index a2dd137..3abbdd8 100644 --- a/examples/gp_test_2.cpp +++ b/examples/gp_test_2.cpp @@ -26,10 +26,19 @@ static constexpr long SEED = 41912; blt::gp::type_system type_system; blt::gp::gp_program program(type_system, std::mt19937_64{SEED}); // NOLINT -blt::gp::operation_t add([](float a, float b) { return a + b; }); -blt::gp::operation_t sub([](float a, float b) { return a - b; }); -blt::gp::operation_t mul([](float a, float b) { return a * b; }); -blt::gp::operation_t pro_div([](float a, float b) { return b == 0 ? 0.0f : a / b; }); +blt::gp::operation_t add([](float a, float b) { + BLT_TRACE("a: %f + b: %f = %f", a, b, a + b); + return a + b; +}); +blt::gp::operation_t sub([](float a, float b) { + BLT_TRACE("a: %f - b: %f = %f", a, b, a - b); + return a - b; }); +blt::gp::operation_t mul([](float a, float b) { + BLT_TRACE("a: %f * b: %f = %f", a, b, a * b); + return a * b; }); +blt::gp::operation_t pro_div([](float a, float b) { + BLT_TRACE("a: %f / b: %f = %f", a, b, (b == 0 ? 0.0f : a / b)); + return b == 0 ? 0.0f : a / b; }); blt::gp::operation_t lit([]() { //static std::uniform_real_distribution dist(-32000, 32000); static std::uniform_real_distribution dist(0.0f, 10.0f); diff --git a/examples/gp_test_3.cpp b/examples/gp_test_3.cpp index de0b3ac..5eee62d 100644 --- a/examples/gp_test_3.cpp +++ b/examples/gp_test_3.cpp @@ -59,6 +59,17 @@ int main() silly.add_operator(sub); silly.add_operator(mul); silly.add_operator(pro_div); + + silly.add_operator(op_if); + silly.add_operator(eq_f); + silly.add_operator(eq_b); + silly.add_operator(lt); + silly.add_operator(gt); + silly.add_operator(op_and); + silly.add_operator(op_or); + silly.add_operator(op_xor); + silly.add_operator(op_not); + silly.add_operator(lit, true); program.set_operations(std::move(silly)); diff --git a/src/generators.cpp b/src/generators.cpp index b61a5e9..57016e3 100644 --- a/src/generators.cpp +++ b/src/generators.cpp @@ -61,10 +61,10 @@ namespace blt::gp tree_generator.pop(); tree.get_operations().emplace_back( - args.program.get_operation(top.id), - args.program.get_transfer_func(top.id), - args.program.is_static(top.id) - ); + args.program.get_operation(top.id), + args.program.get_transfer_func(top.id), + args.program.is_static(top.id) + ); max_depth = std::max(max_depth, top.depth); if (args.program.is_static(top.id)) @@ -89,7 +89,10 @@ namespace blt::gp return create_tree([args](gp_program& program, std::stack& tree_generator, const type& type, blt::size_t new_depth) { if (new_depth >= args.max_depth) { - tree_generator.push({program.select_terminal(type.id()), new_depth}); + if (program.get_type_terminals(type.id()).empty()) + tree_generator.push({program.select_non_terminal(type.id()), new_depth}); + else + tree_generator.push({program.select_terminal(type.id()), new_depth}); return; } if (program.choice() || new_depth < args.min_depth) @@ -104,7 +107,10 @@ namespace blt::gp return create_tree([args](gp_program& program, std::stack& tree_generator, const type& type, blt::size_t new_depth) { if (new_depth >= args.max_depth) { - tree_generator.push({program.select_terminal(type.id()), new_depth}); + if (program.get_type_terminals(type.id()).empty()) + tree_generator.push({program.select_non_terminal(type.id()), new_depth}); + else + tree_generator.push({program.select_terminal(type.id()), new_depth}); return; } tree_generator.push({program.select_non_terminal(type.id()), new_depth});