From bf4394cb0d78e2cf6016b512ed79608d5233ce4e Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 27 Jun 2024 03:01:39 -0400 Subject: [PATCH] silly trees, there's a memory leak --- CMakeLists.txt | 3 +- examples/gp_test_3.cpp | 2 - examples/gp_test_4.cpp | 89 +++++++++++++++++++++++++++++++++++ include/blt/gp/program.h | 14 ++++++ include/blt/gp/transformers.h | 27 +++++++++++ src/transformers.cpp | 23 +++++++++ 6 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 examples/gp_test_4.cpp create mode 100644 include/blt/gp/transformers.h create mode 100644 src/transformers.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ea6c3b9..307da13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.37) +project(blt-gp VERSION 0.0.38) include(CTest) @@ -72,5 +72,6 @@ if (${BUILD_EXAMPLES}) blt_add_example(blt-gp1 examples/gp_test_1.cpp) blt_add_example(blt-gp2 examples/gp_test_2.cpp) blt_add_example(blt-gp3 examples/gp_test_3.cpp) + blt_add_example(blt-gp4 examples/gp_test_4.cpp) endif () \ No newline at end of file diff --git a/examples/gp_test_3.cpp b/examples/gp_test_3.cpp index 5eee62d..32c2e6d 100644 --- a/examples/gp_test_3.cpp +++ b/examples/gp_test_3.cpp @@ -82,6 +82,4 @@ int main() BLT_TRACE(value); return 0; - - return 0; } diff --git a/examples/gp_test_4.cpp b/examples/gp_test_4.cpp new file mode 100644 index 0000000..df5d389 --- /dev/null +++ b/examples/gp_test_4.cpp @@ -0,0 +1,89 @@ +/* + * + * 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 + +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 op_if([](bool b, float a, float c) { return b ? a : c; }); +blt::gp::operation_t eq_f([](float a, float b) { return a == b; }); +blt::gp::operation_t eq_b([](bool a, bool b) { return a == b; }); +blt::gp::operation_t lt([](float a, float b) { return a < b; }); +blt::gp::operation_t gt([](float a, float b) { return a > b; }); +blt::gp::operation_t op_and([](bool a, bool b) { return a && b; }); +blt::gp::operation_t op_or([](bool a, bool b) { return a || b; }); +blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast(a ^ b); }); +blt::gp::operation_t op_not([](bool b) { return !b; }); + +blt::gp::operation_t lit([]() { + //static std::uniform_real_distribution dist(-32000, 32000); + static std::uniform_real_distribution dist(0.0f, 10.0f); + return dist(program.get_random()); +}); + +/** + * This is a test using multiple types with blt::gp + */ +int main() +{ + type_system.register_type(); + type_system.register_type(); + + blt::gp::gp_operations silly{type_system}; + silly.add_operator(add); + 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)); + + blt::gp::ramped_half_initializer_t pop_init; + + auto pop = pop_init.generate(blt::gp::initializer_arguments{program, type_system.get_type().id(), 500, 3, 10}); + + for (auto& tree : pop.getIndividuals()) + { + auto value = tree.get_evaluation_value(nullptr); + + BLT_TRACE(value); + } + + return 0; +} diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index f1b1c37..6987f75 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -173,6 +173,20 @@ namespace blt::gp return non_terminals[id][dist(engine)]; } +// inline operator_id select_non_terminal_too_deep(type_id id) +// { +// std::uniform_int_distribution dist(0, non_terminals[id].size() - 1); +// operator_id sel; +// do +// { +// sel = non_terminals[id][dist(engine)]; +// } while (std::find_if(argument_types[sel].begin(), argument_types[sel].end(), +// [id](const auto& v) { +// return v.id() == id; +// }) != argument_types[sel].end()); +// return sel; +// } + inline std::vector& get_argument_types(operator_id id) { return argument_types[id]; diff --git a/include/blt/gp/transformers.h b/include/blt/gp/transformers.h new file mode 100644 index 0000000..faa5cac --- /dev/null +++ b/include/blt/gp/transformers.h @@ -0,0 +1,27 @@ +#pragma once +/* + * 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 BLT_GP_TRANSFORMERS_H +#define BLT_GP_TRANSFORMERS_H + +namespace blt::gp +{ + +} + +#endif //BLT_GP_TRANSFORMERS_H diff --git a/src/transformers.cpp b/src/transformers.cpp new file mode 100644 index 0000000..6a3bd24 --- /dev/null +++ b/src/transformers.cpp @@ -0,0 +1,23 @@ +/* + * + * 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 + +namespace blt::gp +{ + +} \ No newline at end of file