cleanup and structure

thread
Brett 2024-06-21 22:04:57 -04:00
parent c8dbaef7d3
commit 0ecfa3750c
11 changed files with 405 additions and 201 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.0.24)
project(blt-gp VERSION 0.0.25)
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)

View File

@ -107,6 +107,7 @@ void test()
tree_generator.push(op::LIT);
}
// print out the tree / operators
for (const auto& v : operations)
std::cout << to_string(v) << " ";
std::cout << std::endl;
@ -270,37 +271,20 @@ void test()
std::cout << process.top() << std::endl;
}
//
//struct silly
//{
// long nyah = 50;
// int bruh = 10;
//
// friend std::ostream& operator<<(std::ostream& stream, const silly& silly)
// {
// stream << "[" << silly.nyah << " " << silly.bruh << "]";
// return stream;
// }
//};
//
//struct large
//{
// unsigned char data[2048];
//};
//
//struct super_large
//{
// unsigned char data[9582];
//};
blt::gp::type_system type_system;
blt::gp::gp_program program(type_system);
std::random_device dev;
std::mt19937_64 engine{dev()};
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 lit([]() { return 0.0f; });
blt::gp::operation_t lit([]() {
static std::uniform_real_distribution<float> dist(-32000, 32000);
return dist(engine);
});
int main()

View File

@ -19,9 +19,18 @@
#ifndef BLT_GP_FWDECL_H
#define BLT_GP_FWDECL_H
namespace blt::gp
{
class gp_program;
class type;
class type_system;
class tree_generator_t;
class grow_generator_t;
class full_generator_t;
}
#endif //BLT_GP_FWDECL_H

View File

@ -0,0 +1,49 @@
#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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_GP_GENERATORS_H
#define BLT_GP_GENERATORS_H
#include <blt/gp/fwdecl.h>
#include <blt/gp/tree.h>
namespace blt::gp
{
// base class for any kind of tree generator
class tree_generator_t
{
public:
virtual tree_t generate(gp_program& program, blt::size_t min_depth, blt::size_t max_depth) = 0;
};
class grow_generator_t : public tree_generator_t
{
public:
tree_t generate(gp_program& program, blt::size_t min_depth, blt::size_t max_depth) final;
};
class full_generator_t : public tree_generator_t
{
public:
tree_t generate(gp_program& program, blt::size_t min_depth, blt::size_t max_depth) final;
};
}
#endif //BLT_GP_GENERATORS_H

122
include/blt/gp/operations.h Normal file
View File

@ -0,0 +1,122 @@
#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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_GP_OPERATIONS_H
#define BLT_GP_OPERATIONS_H
#include <blt/std/types.h>
#include <blt/gp/typesystem.h>
#include <blt/gp/stack.h>
#include <functional>
namespace blt::gp
{
template<typename Signature>
class operation_t;
template<typename Return, typename... Args>
class operation_t<Return(Args...)>
{
public:
using function_t = std::function<Return(Args...)>;
constexpr operation_t(const operation_t& copy) = default;
constexpr operation_t(operation_t&& move) = default;
template<typename Functor>
constexpr explicit operation_t(const Functor& functor): func(functor)
{}
template<blt::u64 index>
[[nodiscard]] inline constexpr static blt::size_t getByteOffset()
{
blt::size_t offset = 0;
blt::size_t current_index = 0;
((offset += (current_index++ > index ? stack_allocator::aligned_size<Args>() : 0)), ...);
return offset;
}
template<blt::u64... indices>
inline constexpr Return exec_sequence_to_indices(stack_allocator& allocator, std::integer_sequence<blt::u64, indices...>) const
{
// expands Args and indices, providing each argument with its index calculating the current argument byte offset
return func(allocator.from<Args>(getByteOffset<indices>())...);
}
[[nodiscard]] constexpr inline Return operator()(stack_allocator& allocator) const
{
if constexpr (sizeof...(Args) == 0)
{
return func();
} else
{
constexpr auto seq = std::make_integer_sequence<blt::u64, sizeof...(Args)>();
Return ret = exec_sequence_to_indices(allocator, seq);
allocator.call_destructors<Args...>();
allocator.pop_bytes((stack_allocator::aligned_size<Args>() + ...));
return ret;
}
}
[[nodiscard]] std::function<void(stack_allocator&)> make_callable() const
{
return [this](stack_allocator& values) {
values.push(this->operator()(values));
};
}
[[nodiscard]] inline constexpr blt::size_t get_argc() const
{
return sizeof...(Args);
}
private:
function_t func;
};
template<typename Return, typename Class, typename... Args>
class operation_t<Return (Class::*)(Args...) const> : public operation_t<Return(Args...)>
{
public:
using operation_t<Return(Args...)>::operation_t;
};
template<typename Lambda>
operation_t(Lambda) -> operation_t<decltype(&Lambda::operator())>;
template<typename Return, typename... Args>
operation_t(Return (*)(Args...)) -> operation_t<Return(Args...)>;
// templat\e<typename Return, typename Class, typename... Args>
// operation_t<Return(Args...)> make_operator(Return (Class::*)(Args...) const lambda)
// {
// // https://ventspace.wordpress.com/2022/04/11/quick-snippet-c-type-trait-templates-for-lambda-details/
// }
//
// template<typename Lambda>
// operation_t<decltype(&Lambda::operator())> make_operator(Lambda&& lambda)
// {
// return operation_t<decltype(&Lambda::operator())>(std::forward(lambda));
// }
//
// template<typename Return, typename... Args>
// operation(std::function<Return(Args...)>) -> operation<Return(Args...)>;
}
#endif //BLT_GP_OPERATIONS_H

View File

@ -28,187 +28,20 @@
#include <utility>
#include <iostream>
#include <random>
#include <blt/std/ranges.h>
#include <blt/std/hashmap.h>
#include <blt/std/types.h>
#include <blt/std/utility.h>
#include <blt/std/memory.h>
#include <blt/gp/fwdecl.h>
#include <blt/gp/typesystem.h>
#include <blt/gp/operations.h>
#include <blt/gp/tree.h>
#include <blt/gp/stack.h>
namespace blt::gp
{
template<typename T>
struct integer_type
{
T id;
integer_type() = default;
integer_type(T id): id(id) // NOLINT
{}
inline operator T() const // NOLINT
{
return id;
}
};
struct operator_id : integer_type<blt::size_t>
{
using integer_type<blt::size_t>::integer_type;
};
struct type_id : integer_type<blt::size_t>
{
using integer_type<blt::size_t>::integer_type;
};
class type
{
public:
type() = default;
template<typename T>
static type make_type(type_id id)
{
return type(sizeof(T), id, blt::type_string<T>());
}
[[nodiscard]] blt::size_t size() const
{
return size_;
}
[[nodiscard]] type_id id() const
{
return id_;
}
[[nodiscard]] std::string_view name() const
{
return name_;
}
private:
type(size_t size, type_id id, std::string_view name): size_(size), id_(id), name_(name)
{}
blt::size_t size_{};
type_id id_{};
std::string name_{};
};
class type_system
{
public:
type_system() = default;
template<typename T>
inline type register_type()
{
types.insert({blt::type_string_raw<T>(), type::make_type<T>(types.size())});
return types[blt::type_string_raw<T>()];
}
template<typename T>
inline type get_type()
{
return types[blt::type_string_raw<T>()];
}
private:
blt::hashmap_t<std::string, type> types;
};
template<typename Signature>
class operation_t;
template<typename Return, typename... Args>
class operation_t<Return(Args...)>
{
public:
using function_t = std::function<Return(Args...)>;
constexpr operation_t(const operation_t& copy) = default;
constexpr operation_t(operation_t&& move) = default;
template<typename Functor>
constexpr explicit operation_t(const Functor& functor): func(functor)
{}
template<blt::u64 index>
[[nodiscard]] inline constexpr static blt::size_t getByteOffset()
{
blt::size_t offset = 0;
blt::size_t current_index = 0;
((offset += (current_index++ > index ? stack_allocator::aligned_size<Args>() : 0)), ...);
return offset;
}
template<blt::u64... indices>
inline constexpr Return exec_sequence_to_indices(stack_allocator& allocator, std::integer_sequence<blt::u64, indices...>) const
{
// expands Args and indices, providing each argument with its index calculating the current argument byte offset
return func(allocator.from<Args>(getByteOffset<indices>())...);
}
[[nodiscard]] constexpr inline Return operator()(stack_allocator& allocator) const
{
if constexpr (sizeof...(Args) == 0)
return func();
constexpr auto seq = std::make_integer_sequence<blt::u64, sizeof...(Args)>();
Return ret = exec_sequence_to_indices(allocator, seq);
allocator.call_destructors<Args...>();
allocator.pop_bytes((stack_allocator::aligned_size<Args>() + ...));
return ret;
}
[[nodiscard]] std::function<void(stack_allocator&)> make_callable() const
{
return [this](stack_allocator& values) {
values.push(this->operator()(values));
};
}
[[nodiscard]] blt::size_t get_argc() const
{
return sizeof...(Args);
}
private:
function_t func;
};
template<typename Return, typename Class, typename... Args>
class operation_t<Return (Class::*)(Args...) const> : public operation_t<Return(Args...)>
{
public:
using operation_t<Return(Args...)>::operation_t;
};
template<typename Lambda>
operation_t(Lambda) -> operation_t<decltype(&Lambda::operator())>;
template<typename Return, typename... Args>
operation_t(Return (*)(Args...)) -> operation_t<Return(Args...)>;
// templat\e<typename Return, typename Class, typename... Args>
// operation_t<Return(Args...)> make_operator(Return (Class::*)(Args...) const lambda)
// {
// // https://ventspace.wordpress.com/2022/04/11/quick-snippet-c-type-trait-templates-for-lambda-details/
// }
//
// template<typename Lambda>
// operation_t<decltype(&Lambda::operator())> make_operator(Lambda&& lambda)
// {
// return operation_t<decltype(&Lambda::operator())>(std::forward(lambda));
// }
//
// template<typename Return, typename... Args>
// operation(std::function<Return(Args...)>) -> operation<Return(Args...)>;
class gp_program
{
public:
@ -226,14 +59,38 @@ namespace blt::gp
(argument_types[operator_index].push_back(system.get_type<Args>()), ...);
operators.push_back(op.make_callable());
}
[[nodiscard]] inline type_system& get_typesystem()
{
return system;
}
void generate_tree();
inline operator_id select_terminal(type_id id, std::mt19937_64& engine)
{
std::uniform_int_distribution<blt::size_t> dist(0, terminals[id].size() - 1);
return terminals[id][dist(engine)];
}
inline operator_id select_non_terminal(type_id id, std::mt19937_64& engine)
{
std::uniform_int_distribution<blt::size_t> dist(0, non_terminals[id].size() - 1);
return non_terminals[id][dist(engine)];
}
inline std::vector<type>& get_argument_types(operator_id id)
{
return argument_types[id];
}
private:
type_system system;
blt::gp::stack_allocator alloc;
// indexed from return TYPE ID, returns index of operator
blt::expanding_buffer<std::vector<blt::size_t>> terminals;
blt::expanding_buffer<std::vector<blt::size_t>> non_terminals;
// indexed from OPERATOR NUMBER
blt::expanding_buffer<std::vector<operator_id>> terminals;
blt::expanding_buffer<std::vector<operator_id>> non_terminals;
// indexed from OPERATOR ID (operator number)
blt::expanding_buffer<std::vector<type>> argument_types;
std::vector<std::function<void(stack_allocator&)>> operators;
};

47
include/blt/gp/tree.h Normal file
View File

@ -0,0 +1,47 @@
#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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_GP_TREE_H
#define BLT_GP_TREE_H
#include <blt/gp/typesystem.h>
#include <blt/gp/stack.h>
#include <blt/gp/fwdecl.h>
namespace blt::gp
{
class tree_t
{
public:
[[nodiscard]] inline std::vector<blt::gp::operator_id>& get_operations()
{
return operations;
}
[[nodiscard]] inline blt::gp::stack_allocator& get_values()
{
return values;
}
private:
std::vector<blt::gp::operator_id> operations;
blt::gp::stack_allocator values;
};
}
#endif //BLT_GP_TREE_H

View File

@ -0,0 +1,96 @@
#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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_GP_TYPESYSTEM_H
#define BLT_GP_TYPESYSTEM_H
#include <blt/std/hashmap.h>
#include <blt/std/types.h>
#include <blt/gp/fwdecl.h>
namespace blt::gp
{
struct operator_id : integer_type<blt::size_t>
{
using integer_type<blt::size_t>::integer_type;
};
struct type_id : integer_type<blt::size_t>
{
using integer_type<blt::size_t>::integer_type;
};
class type
{
public:
type() = default;
template<typename T>
static type make_type(type_id id)
{
return type(sizeof(T), id, blt::type_string<T>());
}
[[nodiscard]] inline blt::size_t size() const
{
return size_;
}
[[nodiscard]] inline type_id id() const
{
return id_;
}
[[nodiscard]] inline std::string_view name() const
{
return name_;
}
private:
type(size_t size, type_id id, std::string_view name): size_(size), id_(id), name_(name)
{}
blt::size_t size_{};
type_id id_{};
std::string name_{};
};
class type_system
{
public:
type_system() = default;
template<typename T>
inline type register_type()
{
types.insert({blt::type_string_raw<T>(), type::make_type<T>(types.size())});
return types[blt::type_string_raw<T>()];
}
template<typename T>
inline type get_type()
{
return types[blt::type_string_raw<T>()];
}
private:
blt::hashmap_t<std::string, type> types;
};
}
#endif //BLT_GP_TYPESYSTEM_H

@ -1 +1 @@
Subproject commit 9ad96191ff91ec7efa6aa142e377201fe81b4c40
Subproject commit 1ca46b9d7bb9621a2f7d02a9ca1eff99e91e3f1a

34
src/generators.cpp Normal file
View File

@ -0,0 +1,34 @@
/*
* <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 <blt/gp/generators.h>
namespace blt::gp
{
tree_t grow_generator_t::generate(gp_program& program, blt::size_t min_depth, blt::size_t max_depth)
{
tree_t tree;
return tree;
}
tree_t full_generator_t::generate(gp_program& program, blt::size_t min_depth, blt::size_t max_depth)
{
tree_t tree;
return tree;
}
}

View File

@ -14,4 +14,10 @@
*
* 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 <blt/gp/typesystem.h>
namespace blt::gp
{
}