From a40031beabdfa32fd7e7d722bbf4f448b1733c1b Mon Sep 17 00:00:00 2001 From: Brett Date: Sat, 23 Mar 2024 02:57:01 -0400 Subject: [PATCH] init pop --- CMakeLists.txt | 2 +- include/lilfbtf/fwddecl.h | 1 + include/lilfbtf/system.h | 15 ++++++++-- include/lilfbtf/tree.h | 12 ++++++-- libs/BLT | 2 +- src/system.cpp | 59 ++++++++++++++++++++++++++++++++++++--- src/tree.cpp | 4 ++- tests/src/main.cpp | 10 +++++++ 8 files changed, 93 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f2ff7a..4c5c764 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(lilfbtf5 VERSION 0.1.38) +project(lilfbtf5 VERSION 0.2.0) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/include/lilfbtf/fwddecl.h b/include/lilfbtf/fwddecl.h index f9cccc7..9ace168 100644 --- a/include/lilfbtf/fwddecl.h +++ b/include/lilfbtf/fwddecl.h @@ -65,6 +65,7 @@ namespace fb using func_t_call_t = std::function; using func_t_init_t = std::function; using fitness_eval_func_t = std::function; + using individual_eval_func_t = std::function; using function_name = const std::string&; using type_name = const std::string&; } diff --git a/include/lilfbtf/system.h b/include/lilfbtf/system.h index 90061e4..862b5fa 100644 --- a/include/lilfbtf/system.h +++ b/include/lilfbtf/system.h @@ -27,22 +27,31 @@ namespace fb { + enum class population_init_t + { + GROW, FULL, BRETT_GROW, RAMPED_HALF_HALF, RAMPED_TRI_HALF + }; + class gp_population_t { private: blt::thread_pool& pool; std::vector population; + fb::random& engine; + type_engine_t& types; void crossover(); void mutate(); + public: - explicit gp_population_t(blt::thread_pool& pool): pool(pool) + explicit gp_population_t(blt::thread_pool& pool, type_engine_t& types, fb::random& engine): pool(pool), engine(engine), types(types) {} - void init_pop(); + void init_pop(population_init_t init_type, blt::size_t pop_size, blt::size_t min_depth, blt::size_t max_depth, + std::optional starting_type = {}, double terminal_chance = 0.5); - void execute(const fitness_eval_func_t& fitnessEvalFunc); + void execute(const individual_eval_func_t& individualEvalFunc, const fitness_eval_func_t& fitnessEvalFunc); void breed_new_pop(); }; diff --git a/include/lilfbtf/tree.h b/include/lilfbtf/tree.h index 484896e..fcbc7a9 100644 --- a/include/lilfbtf/tree.h +++ b/include/lilfbtf/tree.h @@ -149,6 +149,7 @@ namespace fb class tree_t { + friend gp_population_t; private: inline blt::bump_allocator& get_allocator() { return alloc; } @@ -166,9 +167,9 @@ namespace fb static void brett_grow(detail::node_construction_info_t info, blt::size_t min_depth, blt::size_t max_depth); static void full(detail::node_construction_info_t info, blt::size_t depth); - - public: + explicit tree_t(type_engine_t& types); + public: static tree_t make_tree(detail::tree_construction_info_t tree_info, blt::size_t min_depth, blt::size_t max_depth, std::optional starting_type = {}); @@ -182,11 +183,18 @@ namespace fb { cache.dirty = true; } + + inline blt::unsafe::any_t& data() + { + return extra_data; + } private: blt::bump_allocator alloc; type_engine_t& types; detail::node_t* root = nullptr; + // any extra data associated with this tree / individual + blt::unsafe::any_t extra_data; struct cache_t { blt::size_t depth = 0; diff --git a/libs/BLT b/libs/BLT index 1256ec2..26e606a 160000 --- a/libs/BLT +++ b/libs/BLT @@ -1 +1 @@ -Subproject commit 1256ec201cdb3a8d3d9bb4ebe9c25637a718465e +Subproject commit 26e606afb18c44ad2fbe982edbdf2811da4d8ad1 diff --git a/src/system.cpp b/src/system.cpp index 87a96a5..fcb6980 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -29,14 +29,65 @@ namespace fb } - void fb::gp_population_t::init_pop() + void fb::gp_population_t::init_pop(const population_init_t init_type, blt::size_t pop_size, blt::size_t min_depth, blt::size_t max_depth, + std::optional starting_type, double terminal_chance) { - + for (blt::size_t i = 0; i < pop_size; i++) + { + switch (init_type) + { + case population_init_t::GROW: + population.push_back( + fb::tree_t::make_tree({fb::tree_init_t::GROW, engine, types, terminal_chance}, min_depth, max_depth, starting_type)); + break; + case population_init_t::FULL: + population.push_back( + fb::tree_t::make_tree({fb::tree_init_t::FULL, engine, types, terminal_chance}, min_depth, max_depth, starting_type)); + break; + case population_init_t::BRETT_GROW: + population.push_back( + fb::tree_t::make_tree({fb::tree_init_t::BRETT_GROW, engine, types, terminal_chance}, min_depth, max_depth, + starting_type)); + break; + case population_init_t::RAMPED_HALF_HALF: + if (engine.choice()) + { + population.push_back( + fb::tree_t::make_tree({fb::tree_init_t::GROW, engine, types, terminal_chance}, min_depth, max_depth, starting_type)); + } else + { + // will select between min and max + population.push_back( + fb::tree_t::make_tree({fb::tree_init_t::FULL, engine, types, terminal_chance}, min_depth, max_depth, starting_type)); + } + break; + case population_init_t::RAMPED_TRI_HALF: + if (engine.choice(0.3)) + { + population.push_back( + fb::tree_t::make_tree({fb::tree_init_t::GROW, engine, types, terminal_chance}, min_depth, max_depth, starting_type)); + } else if (engine.choice(0.3)) + { + population.push_back( + fb::tree_t::make_tree({fb::tree_init_t::FULL, engine, types, terminal_chance}, min_depth, max_depth, starting_type)); + } else + { + population.push_back( + fb::tree_t::make_tree({fb::tree_init_t::BRETT_GROW, engine, types, terminal_chance}, min_depth, max_depth, + starting_type)); + } + break; + } + } } - void fb::gp_population_t::execute(const fitness_eval_func_t& fitnessEvalFunc) + void fb::gp_population_t::execute(const individual_eval_func_t& individualEvalFunc, const fitness_eval_func_t& fitnessEvalFunc) { - + for (auto& individual : population) + { + individualEvalFunc(individual); + individual.cache.fitness = fitnessEvalFunc(individual.root); + } } void fb::gp_population_t::breed_new_pop() diff --git a/src/tree.cpp b/src/tree.cpp index acc96b0..dd3a503 100644 --- a/src/tree.cpp +++ b/src/tree.cpp @@ -25,7 +25,9 @@ namespace fb {} tree_t::tree_t(type_engine_t& types): alloc(), types(types) - {} + { + extra_data = nullptr; + } tree_t tree_t::make_tree(detail::tree_construction_info_t tree_info, blt::size_t min_depth, blt::size_t max_depth, std::optional starting_type) diff --git a/tests/src/main.cpp b/tests/src/main.cpp index dd2b14d..f971a8d 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -88,6 +88,16 @@ const fb::func_t_call_t or_n_f = [](const fb::detail::func_t_arguments& args) { args.self.setValue(args.arguments[0]->value().any_cast() | args.arguments[1]->value().any_cast()); }; +const fb::individual_eval_func_t image_gp_eval = [](fb::tree_t& tree) { + for (blt::size_t i = 0; i < image_width; i++) + { + for (blt::size_t j = 0; j < image_width; j++) + { + + } + } +}; + int main(int argc, const char** argv) { size_t size = 32;