some function types

main
Brett 2024-02-19 15:55:54 -05:00
parent d6f180ee05
commit 1c17bb8413
5 changed files with 281 additions and 2 deletions

@ -1 +1 @@
Subproject commit 8af1db43c3dd8a260729030365a4eabc8246ccf6 Subproject commit 1fdf6f6e894ab223e856d2b750cb6b9daa95ecdd

View File

@ -0,0 +1,142 @@
/*
* <Short Description>
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef LILFBTF5_SYMBOL_REGRESSION_H
#define LILFBTF5_SYMBOL_REGRESSION_H
#include <lilfbtf/tests.h>
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<test_add_function_t, symbolic_regress_function_t>
{
public:
test_add_function_t(): function_base_t<test_add_function_t, symbolic_regress_function_t>(symbolic_regress_function_t::ADD, 2)
{}
template<typename T>
inline static T call(T a, T b)
{
return a + b;
}
};
class test_sub_function_t : public function_base_t<test_sub_function_t, symbolic_regress_function_t>
{
public:
test_sub_function_t(): function_base_t<test_sub_function_t, symbolic_regress_function_t>(symbolic_regress_function_t::SUB, 2)
{}
template<typename T>
inline static T call(T a, T b)
{
return a - b;
}
};
class test_mul_function_t : public function_base_t<test_mul_function_t, symbolic_regress_function_t>
{
public:
test_mul_function_t(): function_base_t<test_mul_function_t, symbolic_regress_function_t>(symbolic_regress_function_t::MUL, 2)
{}
template<typename T>
inline static T call(T a, T b)
{
return a * b;
}
};
class test_div_function_t : public function_base_t<test_div_function_t, symbolic_regress_function_t>
{
public:
test_div_function_t(): function_base_t<test_div_function_t, symbolic_regress_function_t>(symbolic_regress_function_t::DIV, 2)
{}
template<typename T>
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<test_exp_function_t, symbolic_regress_function_t>
{
public:
test_exp_function_t(): function_base_t<test_exp_function_t, symbolic_regress_function_t>(symbolic_regress_function_t::EXP, 1)
{}
template<typename T>
inline static T call(T a)
{
return std::exp(a);
}
};
class test_log_function_t : public function_base_t<test_log_function_t, symbolic_regress_function_t>
{
public:
test_log_function_t(): function_base_t<test_log_function_t, symbolic_regress_function_t>(symbolic_regress_function_t::LOG, 1)
{}
template<typename T>
inline static T call(T a)
{
if (a == 0)
return 0;
return std::log(a);
}
};
class test_sin_function_t : public function_base_t<test_sin_function_t, symbolic_regress_function_t>
{
public:
test_sin_function_t(): function_base_t<test_sin_function_t, symbolic_regress_function_t>(symbolic_regress_function_t::SIN, 1)
{}
template<typename T>
inline static T call(T a)
{
return std::sin(a);
}
};
class test_cos_function_t : public function_base_t<test_cos_function_t, symbolic_regress_function_t>
{
public:
test_cos_function_t(): function_base_t<test_cos_function_t, symbolic_regress_function_t>(symbolic_regress_function_t::COS, 1)
{}
template<typename T>
inline static T call(T a)
{
return std::cos(a);
}
};
}
#endif //LILFBTF5_SYMBOL_REGRESSION_H

View File

@ -0,0 +1,73 @@
/*
* <Short Description>
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef LILFBTF5_TESTS_H
#define LILFBTF5_TESTS_H
#include "blt/std/types.h"
#include <type_traits>
#include <variant>
#include <cmath>
namespace fb
{
using arg_count_t = blt::size_t;
template<typename... allowed_arg_types>
using arg_t = std::variant<allowed_arg_types...>;
template<typename BASE, typename ENUM_TYPE, std::enable_if_t<std::is_enum_v<ENUM_TYPE>, 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<typename... arg_types>
auto operator()(arg_types&& ... args)
{
return BASE::call(std::forward<arg_types>(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

View File

@ -2,6 +2,8 @@
#include <utility> #include <utility>
#include <memory> #include <memory>
#include <blt/std/logging.h> #include <blt/std/logging.h>
#include <blt/parse/argparse.h>
#include <lilfbtf/tests.h>
struct data { struct data {
float f; float f;
@ -9,7 +11,7 @@ struct data {
char c; char c;
}; };
int main() int main(int argc, const char** argv)
{ {
size_t size = 32; size_t size = 32;
size_t remaining_bytes = size; size_t remaining_bytes = size;
@ -35,5 +37,15 @@ int main()
delete[](buffer); delete[](buffer);
std::cout << "Hello, World!" << std::endl; 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; return 0;
} }

52
tests/src/tests.cpp Normal file
View File

@ -0,0 +1,52 @@
/*
* <Short Description>
* 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 <https://www.gnu.org/licenses/>.
*/
#include <lilfbtf/tests.h>
#include <lilfbtf/symbol_regression.h>
#include <blt/std/logging.h>
#include <iostream>
#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;
}
}