my heads doing a special kind of hurt. needs context

thread
Brett 2024-06-23 22:01:48 -04:00
parent 5d8f4c00cd
commit 1dc90f9c13
8 changed files with 106 additions and 60 deletions

View File

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

View File

@ -37,9 +37,11 @@ int main()
type_system.register_type<float>(); type_system.register_type<float>();
type_system.register_type<bool>(); type_system.register_type<bool>();
type_system.add_operator(add); program.add_operator(add);
type_system.add_operator(sub); program.add_operator(sub);
type_system.add_operator(mul); program.add_operator(mul);
type_system.add_operator(pro_div); program.add_operator(pro_div);
type_system.add_operator(lit); program.add_operator(lit, true);
return 0;
} }

View File

@ -219,6 +219,7 @@ void test()
std::cout << std::endl; std::cout << std::endl;
} }
// run the tree
std::stack<float> process; std::stack<float> process;
std::stack<op> operators; std::stack<op> operators;
@ -398,4 +399,6 @@ int main_old()
std::cout << silly_op.operator()(alloc) << std::endl; std::cout << silly_op.operator()(alloc) << std::endl;
std::cout << "Hello World!" << std::endl; std::cout << "Hello World!" << std::endl;
return 0;
} }

View File

@ -26,7 +26,7 @@
namespace blt::gp namespace blt::gp
{ {
template<typename Signature> template<typename>
class operation_t; class operation_t;
template<typename Return, typename... Args> template<typename Return, typename... Args>

View File

@ -42,28 +42,96 @@
namespace blt::gp namespace blt::gp
{ {
static constexpr blt::size_t NONE_T = 0x0;
static constexpr blt::size_t STATIC_T = 0x1;
static constexpr blt::size_t TERMINAL_T = 0x2;
class gp_program class gp_program
{ {
public: public:
explicit gp_program(type_system& system, std::mt19937_64 engine): system(system), engine(engine) /**
* Note about context size: This is required as context is passed to every operator in the GP tree, this context will be provided by your
* call to one of the evaluator functions. This was the nicest way to provide this as C++ lacks reflection
*
* @param system type system to use in tree generation
* @param engine random engine to use throughout the program. TODO replace this with something better
* @param context_size number of arguments which are always present as "context" to the GP system / operators
*/
explicit gp_program(type_system& system, std::mt19937_64 engine):
system(system), engine(engine)
{} {}
void generate_tree();
[[nodiscard]] inline std::mt19937_64& get_random()
{
return engine;
}
[[nodiscard]] inline type_system& get_typesystem() [[nodiscard]] inline type_system& get_typesystem()
{ {
return system; return system;
} }
void generate_tree(); inline operator_id select_terminal(type_id id)
inline std::mt19937_64& get_random()
{ {
return 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::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];
}
inline std::vector<operator_id>& get_type_terminals(type_id id)
{
return terminals[id];
}
inline std::vector<operator_id>& get_type_non_terminals(type_id id)
{
return non_terminals[id];
}
inline bool is_static(operator_id id)
{
return static_types.contains(static_cast<blt::size_t>(id));
}
template<typename Return, typename... Args>
void add_operator(const operation_t<Return(Args...)>& op, bool is_static = false)
{
auto return_type_id = system.get_type<Return>().id();
auto operator_id = blt::gp::operator_id(operators.size());
auto& operator_list = op.get_argc() == 0 ? terminals : non_terminals;
operator_list[return_type_id].push_back(operator_id);
(argument_types[operator_id].push_back(system.get_type<Args>()), ...);
operators.push_back(op.make_callable());
if (is_static)
static_types.insert(operator_id);
} }
private: private:
type_system& system; type_system& system;
blt::gp::stack_allocator alloc; blt::gp::stack_allocator alloc;
std::mt19937_64 engine; std::mt19937_64 engine;
// indexed from return TYPE ID, returns index of operator
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;
blt::hashset_t<operator_id> static_types;
std::vector<std::function<void(stack_allocator&)>> operators;
}; };
} }

View File

@ -113,54 +113,27 @@ namespace blt::gp
return itr->second; return itr->second;
} }
inline operator_id select_terminal(std::mt19937_64& engine, type_id id)
{
std::uniform_int_distribution<blt::size_t> dist(0, terminals[id].size() - 1);
return terminals[id][dist(engine)];
}
inline operator_id select_non_terminal(std::mt19937_64& engine, type_id id)
{
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];
}
inline std::vector<operator_id>& get_type_terminals(type_id id)
{
return terminals[id];
}
inline std::vector<operator_id>& get_type_non_terminals(type_id id)
{
return non_terminals[id];
}
template<typename Return, typename... Args>
void add_operator(const operation_t<Return(Args...)>& op)
{
auto return_type_id = get_type<Return>().id();
auto& operator_list = op.get_argc() == 0 ? terminals : non_terminals;
operator_list[return_type_id].push_back(operators.size());
auto operator_index = operators.size();
(argument_types[operator_index].push_back(get_type<Args>()), ...);
operators.push_back(op.make_callable());
}
private: private:
blt::hashmap_t<std::string, type> types; blt::hashmap_t<std::string, type> types;
// indexed from return TYPE ID, returns index of operator
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;
}; };
} }
template<>
struct std::hash<blt::gp::operator_id>
{
std::size_t operator()(const blt::gp::operator_id& s) const noexcept
{
return std::hash<blt::gp::operator_id::value_type>{}(s);
}
};
template<>
struct std::hash<blt::gp::type_id>
{
std::size_t operator()(const blt::gp::type_id& s) const noexcept
{
return std::hash<blt::gp::type_id::value_type>{}(s);
}
};
#endif //BLT_GP_TYPESYSTEM_H #endif //BLT_GP_TYPESYSTEM_H

@ -1 +1 @@
Subproject commit 1ca46b9d7bb9621a2f7d02a9ca1eff99e91e3f1a Subproject commit cc788e98f4a739f450f487f6a7c758990128a698

View File

@ -38,9 +38,9 @@ namespace blt::gp
do do
{ {
base_type = system.select_type(program.get_random()); base_type = system.select_type(program.get_random());
} while (system.get_type_non_terminals(base_type.id()).empty()); } while (program.get_type_non_terminals(base_type.id()).empty());
tree_generator.emplace(system.select_non_terminal(program.get_random(), base_type.id()), 0); tree_generator.emplace(program.select_non_terminal(base_type.id()), 0);
return tree_generator; return tree_generator;
} }