2024-03-11 17:59:08 -04:00
|
|
|
#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 LILFBTF5_TREE_H
|
|
|
|
#define LILFBTF5_TREE_H
|
|
|
|
|
2024-03-11 19:21:38 -04:00
|
|
|
#include <blt/std/any.h>
|
|
|
|
#include <functional>
|
|
|
|
#include "blt/std/ranges.h"
|
|
|
|
#include "blt/std/allocator.h"
|
2024-03-14 08:36:06 -04:00
|
|
|
#include "type.h"
|
2024-03-11 19:21:38 -04:00
|
|
|
#include <lilfbtf/fwddecl.h>
|
2024-03-14 15:13:30 -04:00
|
|
|
#include <lilfbtf/random.h>
|
2024-03-11 19:21:38 -04:00
|
|
|
|
2024-03-11 17:59:08 -04:00
|
|
|
namespace fb
|
|
|
|
{
|
2024-03-11 19:21:38 -04:00
|
|
|
|
|
|
|
class func_t
|
|
|
|
{
|
|
|
|
private:
|
2024-03-13 12:16:25 -04:00
|
|
|
arg_c_t argc_ = 0;
|
2024-03-12 16:53:27 -04:00
|
|
|
type_id type;
|
2024-03-13 12:16:25 -04:00
|
|
|
function_id function;
|
2024-03-11 19:21:38 -04:00
|
|
|
const func_t_call_t& func;
|
|
|
|
protected:
|
|
|
|
blt::unsafe::any_t value;
|
|
|
|
public:
|
2024-03-13 12:16:25 -04:00
|
|
|
explicit func_t(arg_c_t argc, const func_t_call_t& func, type_id output_type, function_id function_type);
|
2024-03-11 19:21:38 -04:00
|
|
|
|
2024-03-13 12:16:25 -04:00
|
|
|
[[nodiscard]] inline arg_c_t argc() const
|
2024-03-11 19:21:38 -04:00
|
|
|
{ return argc_; }
|
|
|
|
|
|
|
|
[[nodiscard]] inline blt::unsafe::any_t getValue() const
|
2024-03-13 12:16:25 -04:00
|
|
|
{ return value; }
|
2024-03-11 19:21:38 -04:00
|
|
|
|
|
|
|
inline func_t& setValue(blt::unsafe::any_t val)
|
|
|
|
{
|
|
|
|
this->value = val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:16:25 -04:00
|
|
|
/**
|
|
|
|
* @return the type_id that this function container will output
|
|
|
|
*/
|
2024-03-12 16:53:27 -04:00
|
|
|
[[nodiscard]] inline type_id getType() const
|
2024-03-13 12:16:25 -04:00
|
|
|
{ return type; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the function id of this function container
|
|
|
|
*/
|
2024-03-14 15:13:30 -04:00
|
|
|
[[nodiscard]] inline function_id getFunction() const
|
2024-03-13 12:16:25 -04:00
|
|
|
{ return function; }
|
2024-03-11 19:21:38 -04:00
|
|
|
|
2024-03-22 19:38:50 -04:00
|
|
|
inline void call(blt::span<detail::node_t*> args, blt::unsafe::buffer_any_t extra_args)
|
|
|
|
{ func({*this, args, extra_args}); };
|
2024-03-11 19:21:38 -04:00
|
|
|
|
|
|
|
~func_t() = default;
|
|
|
|
};
|
|
|
|
|
2024-03-19 11:46:18 -04:00
|
|
|
enum class tree_init_t
|
|
|
|
{
|
2024-03-19 13:00:25 -04:00
|
|
|
// standard koza grow method
|
2024-03-19 11:46:18 -04:00
|
|
|
GROW,
|
2024-03-19 13:00:25 -04:00
|
|
|
|
|
|
|
BRETT_GROW,
|
2024-03-19 11:46:18 -04:00
|
|
|
// standard full method
|
2024-03-19 13:00:25 -04:00
|
|
|
FULL
|
2024-03-19 11:46:18 -04:00
|
|
|
};
|
|
|
|
|
2024-03-11 19:21:38 -04:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
class node_t
|
|
|
|
{
|
|
|
|
friend tree_t;
|
|
|
|
private:
|
|
|
|
blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc;
|
|
|
|
func_t type;
|
|
|
|
node_t** children = nullptr;
|
|
|
|
public:
|
|
|
|
explicit node_t(const func_t& type, blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc): alloc(alloc), type(type)
|
|
|
|
{
|
|
|
|
children = alloc.emplace_many<node_t*>(this->type.argc());
|
|
|
|
for (blt::size_t i = 0; i < this->type.argc(); i++)
|
|
|
|
children[i] = nullptr;
|
|
|
|
}
|
|
|
|
|
2024-03-22 19:38:50 -04:00
|
|
|
inline void evaluate(blt::unsafe::buffer_any_t extra_args)
|
2024-03-11 19:21:38 -04:00
|
|
|
{
|
2024-03-22 19:38:50 -04:00
|
|
|
type.call(blt::span<node_t*>{children, type.argc()}, extra_args);
|
2024-03-11 19:21:38 -04:00
|
|
|
}
|
|
|
|
|
2024-03-14 08:47:14 -04:00
|
|
|
inline blt::unsafe::any_t value()
|
|
|
|
{
|
|
|
|
return type.getValue();
|
|
|
|
}
|
|
|
|
|
2024-03-11 19:21:38 -04:00
|
|
|
~node_t()
|
|
|
|
{
|
|
|
|
for (blt::size_t i = 0; i < type.argc(); i++)
|
|
|
|
{
|
|
|
|
alloc.destroy(children[i]);
|
|
|
|
alloc.deallocate(children[i]);
|
|
|
|
}
|
|
|
|
alloc.deallocate(children, type.argc());
|
|
|
|
}
|
|
|
|
};
|
2024-03-15 12:06:47 -04:00
|
|
|
|
2024-03-19 12:13:33 -04:00
|
|
|
struct tree_construction_info_t
|
2024-03-15 12:06:47 -04:00
|
|
|
{
|
2024-03-19 12:13:33 -04:00
|
|
|
tree_init_t tree_type;
|
2024-03-15 12:06:47 -04:00
|
|
|
random& engine;
|
|
|
|
type_engine_t& types;
|
2024-03-19 12:13:33 -04:00
|
|
|
double terminal_chance = 0.5;
|
2024-03-15 12:06:47 -04:00
|
|
|
};
|
2024-03-19 11:46:18 -04:00
|
|
|
|
2024-03-19 12:13:33 -04:00
|
|
|
struct node_construction_info_t
|
2024-03-19 11:46:18 -04:00
|
|
|
{
|
2024-03-19 12:13:33 -04:00
|
|
|
tree_t& tree;
|
2024-03-19 11:46:18 -04:00
|
|
|
random& engine;
|
|
|
|
type_engine_t& types;
|
2024-03-19 12:13:33 -04:00
|
|
|
double terminal_chance;
|
|
|
|
|
|
|
|
node_construction_info_t(tree_t& tree, const tree_construction_info_t& info):
|
|
|
|
tree(tree), engine(info.engine), types(info.types), terminal_chance(info.terminal_chance)
|
|
|
|
{}
|
2024-03-19 11:46:18 -04:00
|
|
|
};
|
2024-03-19 13:40:14 -04:00
|
|
|
|
|
|
|
struct tree_eval_t
|
|
|
|
{
|
|
|
|
blt::unsafe::any_t value;
|
|
|
|
type_id contained_type;
|
|
|
|
};
|
2024-03-11 19:21:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class tree_t
|
|
|
|
{
|
2024-03-23 02:57:01 -04:00
|
|
|
friend gp_population_t;
|
2024-03-11 19:21:38 -04:00
|
|
|
private:
|
2024-03-14 15:13:30 -04:00
|
|
|
inline blt::bump_allocator<blt::BLT_2MB_SIZE, false>& get_allocator()
|
|
|
|
{ return alloc; }
|
2024-03-15 12:06:47 -04:00
|
|
|
|
2024-03-19 13:40:14 -04:00
|
|
|
void recalculate_cache();
|
|
|
|
|
2024-03-19 11:46:18 -04:00
|
|
|
static detail::node_t* allocate_non_terminal(detail::node_construction_info_t info, type_id type);
|
|
|
|
|
|
|
|
static detail::node_t* allocate_non_terminal_restricted(detail::node_construction_info_t info, type_id type);
|
|
|
|
|
|
|
|
static detail::node_t* allocate_terminal(detail::node_construction_info_t info, type_id type);
|
|
|
|
|
2024-03-19 12:13:33 -04:00
|
|
|
static void grow(detail::node_construction_info_t info, blt::size_t min_depth, blt::size_t max_depth);
|
2024-03-19 13:40:14 -04:00
|
|
|
|
2024-03-19 13:00:25 -04:00
|
|
|
static void brett_grow(detail::node_construction_info_t info, blt::size_t min_depth, blt::size_t max_depth);
|
2024-03-15 12:06:47 -04:00
|
|
|
|
2024-03-19 12:13:33 -04:00
|
|
|
static void full(detail::node_construction_info_t info, blt::size_t depth);
|
2024-03-23 02:57:01 -04:00
|
|
|
|
2024-03-14 15:13:30 -04:00
|
|
|
explicit tree_t(type_engine_t& types);
|
2024-03-23 02:57:01 -04:00
|
|
|
public:
|
2024-03-14 08:36:06 -04:00
|
|
|
|
2024-03-19 11:46:18 -04:00
|
|
|
static tree_t make_tree(detail::tree_construction_info_t tree_info, blt::size_t min_depth, blt::size_t max_depth,
|
2024-03-15 12:06:47 -04:00
|
|
|
std::optional<type_id> starting_type = {});
|
2024-03-14 15:39:14 -04:00
|
|
|
|
2024-03-22 19:38:50 -04:00
|
|
|
detail::tree_eval_t evaluate(blt::unsafe::buffer_any_t extra_args, const fitness_eval_func_t& fitnessEvalFunc);
|
2024-03-19 13:40:14 -04:00
|
|
|
|
|
|
|
blt::size_t depth();
|
|
|
|
|
|
|
|
// invalidates the internal cache, as the result of tree modification
|
|
|
|
inline void invalidate()
|
|
|
|
{
|
|
|
|
cache.dirty = true;
|
|
|
|
}
|
2024-03-23 02:57:01 -04:00
|
|
|
|
|
|
|
inline blt::unsafe::any_t& data()
|
|
|
|
{
|
|
|
|
return extra_data;
|
|
|
|
}
|
2024-03-19 13:40:14 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
blt::bump_allocator<blt::BLT_2MB_SIZE, false> alloc;
|
|
|
|
type_engine_t& types;
|
|
|
|
detail::node_t* root = nullptr;
|
2024-03-23 02:57:01 -04:00
|
|
|
// any extra data associated with this tree / individual
|
|
|
|
blt::unsafe::any_t extra_data;
|
2024-03-19 13:40:14 -04:00
|
|
|
struct cache_t
|
|
|
|
{
|
|
|
|
blt::size_t depth = 0;
|
|
|
|
blt::size_t node_count = 0;
|
2024-03-22 11:52:24 -04:00
|
|
|
detail::fitness_results fitness;
|
2024-03-19 13:40:14 -04:00
|
|
|
bool dirty = true;
|
|
|
|
} cache;
|
2024-03-11 19:21:38 -04:00
|
|
|
};
|
2024-03-11 17:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //LILFBTF5_TREE_H
|