depth to trees
parent
5da2af01ce
commit
246f305957
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.25)
|
cmake_minimum_required(VERSION 3.25)
|
||||||
project(blt-gp VERSION 0.0.54)
|
project(blt-gp VERSION 0.0.55)
|
||||||
|
|
||||||
include(CTest)
|
include(CTest)
|
||||||
|
|
||||||
|
|
|
@ -56,11 +56,11 @@ void print_best()
|
||||||
auto best = program.get_best<10>();
|
auto best = program.get_best<10>();
|
||||||
|
|
||||||
for (auto& v : best)
|
for (auto& v : best)
|
||||||
BLT_TRACE(v.get().get_evaluation_value<float>(nullptr));
|
BLT_TRACE("%lf (depth: %ld)", v.get().get_evaluation_value<float>(nullptr), v.get().get_depth(program));
|
||||||
std::string small("--------------------------");
|
//std::string small("--------------------------");
|
||||||
for (blt::size_t i = 0; i < std::to_string(program.get_current_generation()).size(); i++)
|
//for (blt::size_t i = 0; i < std::to_string(program.get_current_generation()).size(); i++)
|
||||||
small += "-";
|
// small += "-";
|
||||||
BLT_TRACE(small);
|
//BLT_TRACE(small);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -36,7 +36,6 @@
|
||||||
#include <blt/std/types.h>
|
#include <blt/std/types.h>
|
||||||
#include <blt/std/utility.h>
|
#include <blt/std/utility.h>
|
||||||
#include <blt/std/memory.h>
|
#include <blt/std/memory.h>
|
||||||
#include <blt/std/meta.h>
|
|
||||||
#include <blt/gp/fwdecl.h>
|
#include <blt/gp/fwdecl.h>
|
||||||
#include <blt/gp/typesystem.h>
|
#include <blt/gp/typesystem.h>
|
||||||
#include <blt/gp/operations.h>
|
#include <blt/gp/operations.h>
|
||||||
|
@ -280,8 +279,8 @@ namespace blt::gp
|
||||||
template<typename Container, typename Callable>
|
template<typename Container, typename Callable>
|
||||||
void evaluate_fitness(Callable&& fitness_function, Container& result_storage)
|
void evaluate_fitness(Callable&& fitness_function, Container& result_storage)
|
||||||
{
|
{
|
||||||
for (const auto& ind : blt::enumerate(current_pop.get_individuals()))
|
for (const auto& ind : blt::enumerate(current_pop.get_individuals()))
|
||||||
ind.second.raw_fitness = static_cast<double>(fitness_function(ind.second.tree, result_storage, ind.first));
|
ind.second.raw_fitness = static_cast<double>(fitness_function(ind.second.tree, result_storage, ind.first));
|
||||||
double min = 0;
|
double min = 0;
|
||||||
for (auto& ind : current_pop.get_individuals())
|
for (auto& ind : current_pop.get_individuals())
|
||||||
{
|
{
|
||||||
|
|
|
@ -74,6 +74,8 @@ namespace blt::gp
|
||||||
|
|
||||||
evaluation_context evaluate(void* context);
|
evaluation_context evaluate(void* context);
|
||||||
|
|
||||||
|
blt::size_t get_depth(gp_program& program);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper template for returning the result of the last evaluation
|
* Helper template for returning the result of the last evaluation
|
||||||
*/
|
*/
|
||||||
|
@ -118,7 +120,7 @@ namespace blt::gp
|
||||||
|
|
||||||
individual() = default;
|
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)
|
explicit individual(const tree_t& tree): tree(tree)
|
||||||
|
|
40
src/tree.cpp
40
src/tree.cpp
|
@ -149,4 +149,44 @@ namespace blt::gp
|
||||||
|
|
||||||
out << '\n';
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue