init pop
parent
d8b4f1ec42
commit
a40031beab
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.25)
|
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_ADDRSAN "Enable the address sanitizer" OFF)
|
||||||
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)
|
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)
|
||||||
|
|
|
@ -65,6 +65,7 @@ namespace fb
|
||||||
using func_t_call_t = std::function<void(const detail::func_t_arguments&)>;
|
using func_t_call_t = std::function<void(const detail::func_t_arguments&)>;
|
||||||
using func_t_init_t = std::function<void(func_t&)>;
|
using func_t_init_t = std::function<void(func_t&)>;
|
||||||
using fitness_eval_func_t = std::function<detail::fitness_results(detail::node_t*)>;
|
using fitness_eval_func_t = std::function<detail::fitness_results(detail::node_t*)>;
|
||||||
|
using individual_eval_func_t = std::function<void(tree_t&)>;
|
||||||
using function_name = const std::string&;
|
using function_name = const std::string&;
|
||||||
using type_name = const std::string&;
|
using type_name = const std::string&;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,22 +27,31 @@
|
||||||
namespace fb
|
namespace fb
|
||||||
{
|
{
|
||||||
|
|
||||||
|
enum class population_init_t
|
||||||
|
{
|
||||||
|
GROW, FULL, BRETT_GROW, RAMPED_HALF_HALF, RAMPED_TRI_HALF
|
||||||
|
};
|
||||||
|
|
||||||
class gp_population_t
|
class gp_population_t
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
blt::thread_pool<true>& pool;
|
blt::thread_pool<true>& pool;
|
||||||
std::vector<tree_t> population;
|
std::vector<tree_t> population;
|
||||||
|
fb::random& engine;
|
||||||
|
type_engine_t& types;
|
||||||
|
|
||||||
void crossover();
|
void crossover();
|
||||||
|
|
||||||
void mutate();
|
void mutate();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit gp_population_t(blt::thread_pool<true>& pool): pool(pool)
|
explicit gp_population_t(blt::thread_pool<true>& 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<type_id> 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();
|
void breed_new_pop();
|
||||||
};
|
};
|
||||||
|
|
|
@ -149,6 +149,7 @@ namespace fb
|
||||||
|
|
||||||
class tree_t
|
class tree_t
|
||||||
{
|
{
|
||||||
|
friend gp_population_t;
|
||||||
private:
|
private:
|
||||||
inline blt::bump_allocator<blt::BLT_2MB_SIZE, false>& get_allocator()
|
inline blt::bump_allocator<blt::BLT_2MB_SIZE, false>& get_allocator()
|
||||||
{ return alloc; }
|
{ 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 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);
|
static void full(detail::node_construction_info_t info, blt::size_t depth);
|
||||||
|
|
||||||
public:
|
|
||||||
explicit tree_t(type_engine_t& types);
|
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,
|
static tree_t make_tree(detail::tree_construction_info_t tree_info, blt::size_t min_depth, blt::size_t max_depth,
|
||||||
std::optional<type_id> starting_type = {});
|
std::optional<type_id> starting_type = {});
|
||||||
|
@ -182,11 +183,18 @@ namespace fb
|
||||||
{
|
{
|
||||||
cache.dirty = true;
|
cache.dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline blt::unsafe::any_t& data()
|
||||||
|
{
|
||||||
|
return extra_data;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
blt::bump_allocator<blt::BLT_2MB_SIZE, false> alloc;
|
blt::bump_allocator<blt::BLT_2MB_SIZE, false> alloc;
|
||||||
type_engine_t& types;
|
type_engine_t& types;
|
||||||
detail::node_t* root = nullptr;
|
detail::node_t* root = nullptr;
|
||||||
|
// any extra data associated with this tree / individual
|
||||||
|
blt::unsafe::any_t extra_data;
|
||||||
struct cache_t
|
struct cache_t
|
||||||
{
|
{
|
||||||
blt::size_t depth = 0;
|
blt::size_t depth = 0;
|
||||||
|
|
2
libs/BLT
2
libs/BLT
|
@ -1 +1 @@
|
||||||
Subproject commit 1256ec201cdb3a8d3d9bb4ebe9c25637a718465e
|
Subproject commit 26e606afb18c44ad2fbe982edbdf2811da4d8ad1
|
|
@ -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<type_id> 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()
|
void fb::gp_population_t::breed_new_pop()
|
||||||
|
|
|
@ -25,7 +25,9 @@ namespace fb
|
||||||
{}
|
{}
|
||||||
|
|
||||||
tree_t::tree_t(type_engine_t& types): alloc(), types(types)
|
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,
|
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<type_id> starting_type)
|
blt::size_t min_depth, blt::size_t max_depth, std::optional<type_id> starting_type)
|
||||||
|
|
|
@ -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<blt::u8>() | args.arguments[1]->value().any_cast<blt::u8>());
|
args.self.setValue(args.arguments[0]->value().any_cast<blt::u8>() | args.arguments[1]->value().any_cast<blt::u8>());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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)
|
int main(int argc, const char** argv)
|
||||||
{
|
{
|
||||||
size_t size = 32;
|
size_t size = 32;
|
||||||
|
|
Loading…
Reference in New Issue