cleanup and more functions

thread
Brett 2024-08-06 21:34:03 -04:00
parent d5dea3b31e
commit b10b438889
6 changed files with 33 additions and 26 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.123) project(blt-gp VERSION 0.0.124)
include(CTest) include(CTest)

View File

@ -177,6 +177,11 @@ namespace blt::gp
return name; return name;
} }
inline constexpr const auto& get_function() const
{
return func;
}
operator_id id = -1; operator_id id = -1;
private: private:
function_t func; function_t func;

View File

@ -32,8 +32,6 @@ namespace blt::gp
using op_iter = std::vector<blt::gp::op_container_t>::iterator; using op_iter = std::vector<blt::gp::op_container_t>::iterator;
} }
blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, const std::vector<blt::gp::op_container_t>& container, blt::ptrdiff_t start);
class crossover_t class crossover_t
{ {
public: public:
@ -108,7 +106,8 @@ namespace blt::gp
virtual tree_t apply(gp_program& program, const tree_t& p); // NOLINT virtual tree_t apply(gp_program& program, const tree_t& p); // NOLINT
void mutate_point(gp_program& program, tree_t& c, blt::size_t node); // returns the point after the mutation
blt::size_t mutate_point(gp_program& program, tree_t& c, blt::size_t node);
virtual ~mutation_t() = default; virtual ~mutation_t() = default;

View File

@ -112,6 +112,8 @@ namespace blt::gp
} }
void print(gp_program& program, std::ostream& output, bool print_literals = true, bool pretty_indent = false, bool include_types = false) const; void print(gp_program& program, std::ostream& output, bool print_literals = true, bool pretty_indent = false, bool include_types = false) const;
blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, blt::ptrdiff_t start);
private: private:
std::vector<op_container_t> operations; std::vector<op_container_t> operations;

View File

@ -71,10 +71,10 @@ namespace blt::gp
return blt::unexpected(point.error()); return blt::unexpected(point.error());
auto crossover_point_begin_itr = c1_ops.begin() + point->p1_crossover_point; auto crossover_point_begin_itr = c1_ops.begin() + point->p1_crossover_point;
auto crossover_point_end_itr = c1_ops.begin() + find_endpoint(program, c1_ops, point->p1_crossover_point); auto crossover_point_end_itr = c1_ops.begin() + c1.find_endpoint(program, point->p1_crossover_point);
auto found_point_begin_itr = c2_ops.begin() + point->p2_crossover_point; auto found_point_begin_itr = c2_ops.begin() + point->p2_crossover_point;
auto found_point_end_itr = c2_ops.begin() + find_endpoint(program, c2_ops, point->p2_crossover_point); auto found_point_end_itr = c2_ops.begin() + c2.find_endpoint(program, point->p2_crossover_point);
stack_allocator& c1_stack = c1.get_values(); stack_allocator& c1_stack = c1.get_values();
stack_allocator& c2_stack = c2.get_values(); stack_allocator& c2_stack = c2.get_values();
@ -215,13 +215,13 @@ namespace blt::gp
return c; return c;
} }
void mutation_t::mutate_point(gp_program& program, tree_t& c, blt::size_t node) blt::size_t mutation_t::mutate_point(gp_program& program, tree_t& c, blt::size_t node)
{ {
auto& ops_r = c.get_operations(); auto& ops_r = c.get_operations();
auto& vals_r = c.get_values(); auto& vals_r = c.get_values();
auto begin_point = static_cast<blt::ptrdiff_t>(node); auto begin_point = static_cast<blt::ptrdiff_t>(node);
auto end_point = find_endpoint(program, ops_r, begin_point); auto end_point = c.find_endpoint(program, begin_point);
auto begin_operator_id = ops_r[begin_point].id; auto begin_operator_id = ops_r[begin_point].id;
const auto& type_info = program.get_operator_info(begin_operator_id); const auto& type_info = program.get_operator_info(begin_operator_id);
@ -293,23 +293,6 @@ namespace blt::gp
throw std::exception(); throw std::exception();
} }
#endif #endif
} return begin_point + new_ops_r.size();
blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, const std::vector<blt::gp::op_container_t>& container, blt::ptrdiff_t index)
{
blt::i64 children_left = 0;
do
{
const auto& type = program.get_operator_info(container[index].id);
// this is a child to someone
if (children_left != 0)
children_left--;
if (type.argc.argc > 0)
children_left += type.argc.argc;
index++;
} while (children_left > 0);
return index;
} }
} }

View File

@ -201,4 +201,22 @@ namespace blt::gp
return depth; return depth;
} }
blt::ptrdiff_t tree_t::find_endpoint(gp_program& program, blt::ptrdiff_t index)
{
blt::i64 children_left = 0;
do
{
const auto& type = program.get_operator_info(operations[index].id);
// this is a child to someone
if (children_left != 0)
children_left--;
if (type.argc.argc > 0)
children_left += type.argc.argc;
index++;
} while (children_left > 0);
return index;
}
} }