From 6c14bbc9416924219fa399dbdbff70dc9a6a9b20 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 20 Feb 2024 15:18:19 -0500 Subject: [PATCH] working on it --- libs/BLT | 2 +- tests/include/lilfbtf/symbol_regression.h | 34 +++--- tests/include/lilfbtf/tests.h | 12 +- tests/src/tests.cpp | 131 +++++++++++++++++++++- 4 files changed, 154 insertions(+), 25 deletions(-) diff --git a/libs/BLT b/libs/BLT index 1fdf6f6..0b6b6ae 160000 --- a/libs/BLT +++ b/libs/BLT @@ -1 +1 @@ -Subproject commit 1fdf6f6e894ab223e856d2b750cb6b9daa95ecdd +Subproject commit 0b6b6aed9b1fa2428bd665a72149f21f9e0e5d0d diff --git a/tests/include/lilfbtf/symbol_regression.h b/tests/include/lilfbtf/symbol_regression.h index 2ddbb5f..739bc7f 100644 --- a/tests/include/lilfbtf/symbol_regression.h +++ b/tests/include/lilfbtf/symbol_regression.h @@ -26,17 +26,17 @@ namespace fb enum class symbolic_regress_function_t { - ADD, SUB, MUL, DIV, EXP, LOG, SIN, COS + ADD, SUB, MUL, DIV, EXP, LOG, SIN, COS, SIZE }; class test_add_function_t : public function_base_t { public: - test_add_function_t(): function_base_t(symbolic_regress_function_t::ADD, 2) + constexpr test_add_function_t(): function_base_t(symbolic_regress_function_t::ADD, 2) {} template - inline static T call(T a, T b) + constexpr inline static T call(T a, T b) { return a + b; } @@ -45,11 +45,11 @@ namespace fb class test_sub_function_t : public function_base_t { public: - test_sub_function_t(): function_base_t(symbolic_regress_function_t::SUB, 2) + constexpr test_sub_function_t(): function_base_t(symbolic_regress_function_t::SUB, 2) {} template - inline static T call(T a, T b) + constexpr inline static T call(T a, T b) { return a - b; } @@ -58,11 +58,11 @@ namespace fb class test_mul_function_t : public function_base_t { public: - test_mul_function_t(): function_base_t(symbolic_regress_function_t::MUL, 2) + constexpr test_mul_function_t(): function_base_t(symbolic_regress_function_t::MUL, 2) {} template - inline static T call(T a, T b) + constexpr inline static T call(T a, T b) { return a * b; } @@ -71,11 +71,11 @@ namespace fb class test_div_function_t : public function_base_t { public: - test_div_function_t(): function_base_t(symbolic_regress_function_t::DIV, 2) + constexpr test_div_function_t(): function_base_t(symbolic_regress_function_t::DIV, 2) {} template - inline static T call(T a, T b) + constexpr inline static T call(T a, T b) { if (b == 0) return 0; @@ -86,11 +86,11 @@ namespace fb class test_exp_function_t : public function_base_t { public: - test_exp_function_t(): function_base_t(symbolic_regress_function_t::EXP, 1) + constexpr test_exp_function_t(): function_base_t(symbolic_regress_function_t::EXP, 1) {} template - inline static T call(T a) + constexpr inline static T call(T a) { return std::exp(a); } @@ -99,11 +99,11 @@ namespace fb class test_log_function_t : public function_base_t { public: - test_log_function_t(): function_base_t(symbolic_regress_function_t::LOG, 1) + constexpr test_log_function_t(): function_base_t(symbolic_regress_function_t::LOG, 1) {} template - inline static T call(T a) + constexpr inline static T call(T a) { if (a == 0) return 0; @@ -114,11 +114,11 @@ namespace fb class test_sin_function_t : public function_base_t { public: - test_sin_function_t(): function_base_t(symbolic_regress_function_t::SIN, 1) + constexpr test_sin_function_t(): function_base_t(symbolic_regress_function_t::SIN, 1) {} template - inline static T call(T a) + constexpr inline static T call(T a) { return std::sin(a); } @@ -127,11 +127,11 @@ namespace fb class test_cos_function_t : public function_base_t { public: - test_cos_function_t(): function_base_t(symbolic_regress_function_t::COS, 1) + constexpr test_cos_function_t(): function_base_t(symbolic_regress_function_t::COS, 1) {} template - inline static T call(T a) + constexpr inline static T call(T a) { return std::cos(a); } diff --git a/tests/include/lilfbtf/tests.h b/tests/include/lilfbtf/tests.h index 71b9f1e..b101cbc 100644 --- a/tests/include/lilfbtf/tests.h +++ b/tests/include/lilfbtf/tests.h @@ -29,8 +29,8 @@ namespace fb { using arg_count_t = blt::size_t; - template - using arg_t = std::variant; + //template + //using arg_t = std::variant; template, bool> = true> class function_base_t @@ -39,19 +39,19 @@ namespace fb ENUM_TYPE type_; arg_count_t args_count_; public: - function_base_t(ENUM_TYPE type, arg_count_t args_count): type_(type), args_count_(args_count) + constexpr function_base_t(ENUM_TYPE type, arg_count_t args_count): type_(type), args_count_(args_count) {} template - auto operator()(arg_types&& ... args) + constexpr auto operator()(arg_types&& ... args) const { return BASE::call(std::forward(args)...); } - ENUM_TYPE type() + [[nodiscard]] constexpr ENUM_TYPE type() const noexcept { return type_; } - arg_count_t argCount() + [[nodiscard]] constexpr arg_count_t argCount() const noexcept { return args_count_; } }; diff --git a/tests/src/tests.cpp b/tests/src/tests.cpp index 990c67e..69f685b 100644 --- a/tests/src/tests.cpp +++ b/tests/src/tests.cpp @@ -18,11 +18,16 @@ #include #include #include +#include #include #include "blt/std/utility.h" +#include +#include +#include namespace fb { + using arg_t = double; /* * Classes @@ -35,7 +40,131 @@ namespace fb test_log_function_t log_base; test_sin_function_t sin_base; test_cos_function_t cos_base; + +#define STATIC_FUNCTION_LIST \ + STATIC_FUNCTION_APPLY(test_add_function_t, args[0], args[1]) \ + STATIC_FUNCTION_APPLY(test_sub_function_t, args[0], args[1]) \ + STATIC_FUNCTION_APPLY(test_mul_function_t, args[0], args[1]) \ + STATIC_FUNCTION_APPLY(test_div_function_t, args[0], args[1]) \ + STATIC_FUNCTION_APPLY(test_exp_function_t, args[0]) \ + STATIC_FUNCTION_APPLY(test_log_function_t, args[0]) \ + STATIC_FUNCTION_APPLY(test_sin_function_t, args[0]) \ + STATIC_FUNCTION_APPLY(test_cos_function_t, args[0]) + +#define FUNCTION_LIST \ + FUNCTION_APPLY(add_base, add_t, args[0], args[1]) \ + FUNCTION_APPLY(sub_base, sub_t, args[0], args[1]) \ + FUNCTION_APPLY(mul_base, mul_t, args[0], args[1]) \ + FUNCTION_APPLY(div_base, div_t, args[0], args[1]) \ + FUNCTION_APPLY(exp_base, exp_t, args[0]) \ + FUNCTION_APPLY(log_base, log_t, args[0]) \ + FUNCTION_APPLY(sin_base, sin_t, args[0]) \ + FUNCTION_APPLY(cos_base, cos_t, args[0]) + class function_virtual_t + { + private: + arg_count_t args_count_; + public: + explicit function_virtual_t(arg_count_t args_count): args_count_(args_count) + {} + + virtual arg_t operator()(blt::span args) = 0; + + [[nodiscard]] arg_count_t argCount() const noexcept + { + return args_count_; + } + + virtual ~function_virtual_t() = default; + }; + + // create virtual function variants + +#define FUNCTION_APPLY(type, name, ...) \ + class function_virtual_##name : public function_virtual_t{ \ + public: \ + function_virtual_##name(): function_virtual_t(type.argCount())\ + {} \ + \ + arg_t operator()(blt::span args) final \ + { \ + return type(__VA_ARGS__);\ + }\ + }; + + FUNCTION_LIST + +#undef FUNCTION_APPLY +#define FUNCTION_APPLY(type, name, ...) std::make_unique(), + + std::array, static_cast(symbolic_regress_function_t::SIZE)> virtual_functions = { + FUNCTION_LIST + }; + +#undef FUNCTION_APPLY + + using function_variant = std::variant; + +#define STATIC_FUNCTION_APPLY(type, ...) \ + constexpr auto operator()(const type& func){ \ + return func(__VA_ARGS__); \ + } + + + struct function_variant_visitor_apply + { + private: + blt::span args; + public: + constexpr explicit function_variant_visitor_apply(blt::span args): args(args) + {} + + STATIC_FUNCTION_LIST + }; + +#undef STATIC_FUNCTION_APPLY +#define STATIC_FUNCTION_APPLY(type, ...) \ + constexpr auto operator()(const type& func){ \ + return func.argCount(); \ + } + + struct function_variant_visitor_argc + { + STATIC_FUNCTION_LIST + }; + + + + class flat_tree + { + + }; + + struct node_container + { + [[nodiscard]] constexpr arg_count_t argCount() const + { return 0; } + + [[nodiscard]] static inline constexpr arg_count_t determine_max_argc() + { + return 2; + } + }; + + template + class node_tree + { + private: + static ALLOC allocator; + struct node + { + std::array children; + NODE_CONTAINER caller; + }; + public: + + }; /* * Functions @@ -47,6 +176,6 @@ namespace fb (void) tree_min_size; (void) tree_max_size; - std::cout << exp_base(population_size) << std::endl; + std::cout << log_base(population_size) << std::endl; } } \ No newline at end of file