depth to trees

thread
Brett 2024-07-10 02:54:58 -04:00
parent 5da2af01ce
commit 246f305957
6 changed files with 54 additions and 13 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.0.54)
project(blt-gp VERSION 0.0.55)
include(CTest)

View File

@ -56,11 +56,11 @@ void print_best()
auto best = program.get_best<10>();
for (auto& v : best)
BLT_TRACE(v.get().get_evaluation_value<float>(nullptr));
std::string small("--------------------------");
for (blt::size_t i = 0; i < std::to_string(program.get_current_generation()).size(); i++)
small += "-";
BLT_TRACE(small);
BLT_TRACE("%lf (depth: %ld)", v.get().get_evaluation_value<float>(nullptr), v.get().get_depth(program));
//std::string small("--------------------------");
//for (blt::size_t i = 0; i < std::to_string(program.get_current_generation()).size(); i++)
// small += "-";
//BLT_TRACE(small);
}
/**

View File

@ -36,7 +36,6 @@
#include <blt/std/types.h>
#include <blt/std/utility.h>
#include <blt/std/memory.h>
#include <blt/std/meta.h>
#include <blt/gp/fwdecl.h>
#include <blt/gp/typesystem.h>
#include <blt/gp/operations.h>

View File

@ -74,6 +74,8 @@ namespace blt::gp
evaluation_context evaluate(void* context);
blt::size_t get_depth(gp_program& program);
/**
* Helper template for returning the result of the last evaluation
*/
@ -118,7 +120,7 @@ namespace blt::gp
individual() = default;
explicit individual(tree_t&& tree): tree(tree)
explicit individual(tree_t&& tree): tree(std::move(tree))
{}
explicit individual(const tree_t& tree): tree(tree)

View File

@ -149,4 +149,44 @@ namespace blt::gp
out << '\n';
}
blt::size_t tree_t::get_depth(gp_program& program)
{
blt::size_t depth = 0;
auto operations_stack = operations;
std::vector<blt::size_t> values_process;
std::vector<blt::size_t> value_stack;
for (const auto& op : operations_stack)
{
if (op.is_value)
value_stack.push_back(1);
}
while (!operations_stack.empty())
{
auto operation = operations_stack.back();
// keep the last value in the stack on the process stack stored in the eval context, this way it can be accessed easily.
operations_stack.pop_back();
if (operation.is_value)
{
auto d = value_stack.back();
depth = std::max(depth, d);
values_process.push_back(d);
value_stack.pop_back();
continue;
}
blt::size_t local_depth = 0;
for (blt::size_t i = 0; i < program.get_operator_info(operation.id).argc.argc; i++)
{
local_depth = std::max(local_depth, values_process.back());
values_process.pop_back();
}
value_stack.push_back(local_depth + 1);
operations_stack.emplace_back(empty_callable, operation.transfer, operation.id, true);
}
return depth;
}
}