From 1c17bb8413d40dfae2c1ed7e67f210529caf5bb3 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 19 Feb 2024 15:55:54 -0500 Subject: [PATCH] some function types --- libs/BLT | 2 +- tests/include/lilfbtf/symbol_regression.h | 142 ++++++++++++++++++++++ tests/include/lilfbtf/tests.h | 73 +++++++++++ tests/src/main.cpp | 14 ++- tests/src/tests.cpp | 52 ++++++++ 5 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 tests/include/lilfbtf/symbol_regression.h create mode 100644 tests/include/lilfbtf/tests.h create mode 100644 tests/src/tests.cpp diff --git a/libs/BLT b/libs/BLT index 8af1db4..1fdf6f6 160000 --- a/libs/BLT +++ b/libs/BLT @@ -1 +1 @@ -Subproject commit 8af1db43c3dd8a260729030365a4eabc8246ccf6 +Subproject commit 1fdf6f6e894ab223e856d2b750cb6b9daa95ecdd diff --git a/tests/include/lilfbtf/symbol_regression.h b/tests/include/lilfbtf/symbol_regression.h new file mode 100644 index 0000000..2ddbb5f --- /dev/null +++ b/tests/include/lilfbtf/symbol_regression.h @@ -0,0 +1,142 @@ +/* + * + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LILFBTF5_SYMBOL_REGRESSION_H +#define LILFBTF5_SYMBOL_REGRESSION_H + +#include + +namespace fb +{ + + enum class symbolic_regress_function_t + { + ADD, SUB, MUL, DIV, EXP, LOG, SIN, COS + }; + + class test_add_function_t : public function_base_t + { + public: + test_add_function_t(): function_base_t(symbolic_regress_function_t::ADD, 2) + {} + + template + inline static T call(T a, T b) + { + return a + b; + } + }; + + class test_sub_function_t : public function_base_t + { + public: + test_sub_function_t(): function_base_t(symbolic_regress_function_t::SUB, 2) + {} + + template + inline static T call(T a, T b) + { + return a - b; + } + }; + + class test_mul_function_t : public function_base_t + { + public: + test_mul_function_t(): function_base_t(symbolic_regress_function_t::MUL, 2) + {} + + template + inline static T call(T a, T b) + { + return a * b; + } + }; + + class test_div_function_t : public function_base_t + { + public: + test_div_function_t(): function_base_t(symbolic_regress_function_t::DIV, 2) + {} + + template + inline static T call(T a, T b) + { + if (b == 0) + return 0; + return a / b; + } + }; + + class test_exp_function_t : public function_base_t + { + public: + test_exp_function_t(): function_base_t(symbolic_regress_function_t::EXP, 1) + {} + + template + inline static T call(T a) + { + return std::exp(a); + } + }; + + class test_log_function_t : public function_base_t + { + public: + test_log_function_t(): function_base_t(symbolic_regress_function_t::LOG, 1) + {} + + template + inline static T call(T a) + { + if (a == 0) + return 0; + return std::log(a); + } + }; + + class test_sin_function_t : public function_base_t + { + public: + test_sin_function_t(): function_base_t(symbolic_regress_function_t::SIN, 1) + {} + + template + inline static T call(T a) + { + return std::sin(a); + } + }; + + class test_cos_function_t : public function_base_t + { + public: + test_cos_function_t(): function_base_t(symbolic_regress_function_t::COS, 1) + {} + + template + inline static T call(T a) + { + return std::cos(a); + } + }; + +} + +#endif //LILFBTF5_SYMBOL_REGRESSION_H diff --git a/tests/include/lilfbtf/tests.h b/tests/include/lilfbtf/tests.h new file mode 100644 index 0000000..71b9f1e --- /dev/null +++ b/tests/include/lilfbtf/tests.h @@ -0,0 +1,73 @@ +/* + * + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LILFBTF5_TESTS_H +#define LILFBTF5_TESTS_H + +#include "blt/std/types.h" +#include +#include +#include + + +namespace fb +{ + + using arg_count_t = blt::size_t; + template + using arg_t = std::variant; + + template, bool> = true> + class function_base_t + { + protected: + 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) + {} + + template + auto operator()(arg_types&& ... args) + { + return BASE::call(std::forward(args)...); + } + + ENUM_TYPE type() + { return type_; } + + arg_count_t argCount() + { return args_count_; } + }; + + void run_tree_type_tests(blt::size_t population_size, blt::size_t tree_min_size, blt::size_t tree_max_size); + + inline void execute_tests() + { + run_tree_type_tests(1, 5, 17); + run_tree_type_tests(10, 5, 17); + run_tree_type_tests(100, 5, 17); + run_tree_type_tests(1000, 5, 17); + run_tree_type_tests(10000, 5, 17); + run_tree_type_tests(100000, 5, 17); + run_tree_type_tests(1000000, 5, 17); + } + +} + +#endif //LILFBTF5_TESTS_H diff --git a/tests/src/main.cpp b/tests/src/main.cpp index 74c9cef..e31f7b7 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include struct data { float f; @@ -9,7 +11,7 @@ struct data { char c; }; -int main() +int main(int argc, const char** argv) { size_t size = 32; size_t remaining_bytes = size; @@ -35,5 +37,15 @@ int main() delete[](buffer); std::cout << "Hello, World!" << std::endl; + + blt::arg_parse parser; + + parser.addArgument(blt::arg_builder("--tests").setHelp("Run the tests").setAction(blt::arg_action_t::STORE_TRUE).build()); + + auto args = parser.parse_args(argc, argv); + + if (args.contains("--tests")) + fb::execute_tests(); + return 0; } diff --git a/tests/src/tests.cpp b/tests/src/tests.cpp new file mode 100644 index 0000000..990c67e --- /dev/null +++ b/tests/src/tests.cpp @@ -0,0 +1,52 @@ +/* + * + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include +#include "blt/std/utility.h" + +namespace fb +{ + + /* + * Classes + */ + test_add_function_t add_base; + test_sub_function_t sub_base; + test_mul_function_t mul_base; + test_div_function_t div_base; + test_exp_function_t exp_base; + test_log_function_t log_base; + test_sin_function_t sin_base; + test_cos_function_t cos_base; + + + /* + * Functions + */ + + void run_tree_type_tests(blt::size_t population_size, blt::size_t tree_min_size, blt::size_t tree_max_size) + { + (void) population_size; + (void) tree_min_size; + (void) tree_max_size; + + std::cout << exp_base(population_size) << std::endl; + } +} \ No newline at end of file