Compare commits

..

No commits in common. "dd144a199a8a38d25ecb5c2a04042e39082648d7" and "1789a9e4746beb86a3f6de74ea096bc7c4ca7768" have entirely different histories.

9 changed files with 60 additions and 146 deletions

View File

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

View File

@ -104,15 +104,35 @@ int main()
{ {
BLT_TRACE("------------{Begin Generation %ld}------------", program.get_current_generation()); BLT_TRACE("------------{Begin Generation %ld}------------", program.get_current_generation());
BLT_TRACE("Creating next generation"); BLT_TRACE("Creating next generation");
#ifdef BLT_TRACK_ALLOCATIONS
auto gen_alloc = blt::gp::tracker.start_measurement();
#endif
BLT_START_INTERVAL("Symbolic Regression", "Gen"); BLT_START_INTERVAL("Symbolic Regression", "Gen");
program.create_next_generation(); program.create_next_generation();
BLT_END_INTERVAL("Symbolic Regression", "Gen"); BLT_END_INTERVAL("Symbolic Regression", "Gen");
#ifdef BLT_TRACK_ALLOCATIONS
blt::gp::tracker.stop_measurement(gen_alloc);
BLT_TRACE("Generation Allocated %ld times with a total of %s", gen_alloc.getAllocationDifference(),
blt::byte_convert_t(gen_alloc.getAllocatedByteDifference()).convert_to_nearest_type().to_pretty_string().c_str());
auto fitness_alloc = blt::gp::tracker.start_measurement();
#endif
BLT_TRACE("Move to next generation"); BLT_TRACE("Move to next generation");
BLT_START_INTERVAL("Symbolic Regression", "Fitness"); BLT_START_INTERVAL("Symbolic Regression", "Fitness");
program.next_generation(); program.next_generation();
BLT_TRACE("Evaluate Fitness"); BLT_TRACE("Evaluate Fitness");
program.evaluate_fitness(); program.evaluate_fitness();
BLT_END_INTERVAL("Symbolic Regression", "Fitness"); BLT_END_INTERVAL("Symbolic Regression", "Fitness");
#ifdef BLT_TRACK_ALLOCATIONS
blt::gp::tracker.stop_measurement(fitness_alloc);
BLT_TRACE("Fitness Allocated %ld times with a total of %s", fitness_alloc.getAllocationDifference(),
blt::byte_convert_t(fitness_alloc.getAllocatedByteDifference()).convert_to_nearest_type().to_pretty_string().c_str());
#endif
BLT_TRACE("----------------------------------------------"); BLT_TRACE("----------------------------------------------");
std::cout << std::endl; std::cout << std::endl;
} }
@ -142,25 +162,6 @@ int main()
#ifdef BLT_TRACK_ALLOCATIONS #ifdef BLT_TRACK_ALLOCATIONS
BLT_TRACE("Total Allocations: %ld times with a total of %s", blt::gp::tracker.getAllocations(), BLT_TRACE("Total Allocations: %ld times with a total of %s", blt::gp::tracker.getAllocations(),
blt::byte_convert_t(blt::gp::tracker.getAllocatedBytes()).convert_to_nearest_type().to_pretty_string().c_str()); blt::byte_convert_t(blt::gp::tracker.getAllocatedBytes()).convert_to_nearest_type().to_pretty_string().c_str());
auto crossover_calls = blt::gp::crossover_calls.get_calls();
auto crossover_allocations = blt::gp::crossover_allocations.get_calls();
auto mutation_calls = blt::gp::mutation_calls.get_calls();
auto mutation_allocation = blt::gp::mutation_allocations.get_calls();
auto reproduction_calls = blt::gp::reproduction_calls.get_calls();
auto reproduction_allocation = blt::gp::reproduction_allocations.get_calls();
BLT_TRACE("Total Crossover Calls: %ld", crossover_calls);
BLT_TRACE("Total Mutation Calls: %ld", mutation_calls);
BLT_TRACE("Total Reproduction Calls: %ld", reproduction_calls);
BLT_TRACE("Total Crossover Allocations: %ld", crossover_allocations);
BLT_TRACE("Total Mutation Allocations: %ld", mutation_allocation);
BLT_TRACE("Total Reproduction Allocations: %ld", reproduction_allocation);
BLT_TRACE("Percent Crossover calls allocate? %lf%%",
static_cast<double>(crossover_allocations) / static_cast<double>(crossover_calls == 0 ? 1 : crossover_calls) * 100);
BLT_TRACE("Percent Mutation calls allocate? %lf%%",
static_cast<double>(mutation_allocation) / static_cast<double>(mutation_calls == 0 ? 1 : mutation_calls) * 100);
BLT_TRACE("Percent Reproduction calls allocate? %lf%%",
static_cast<double>(reproduction_allocation) / static_cast<double>(reproduction_calls == 0 ? 1 : reproduction_calls) * 100);
#endif #endif
return 0; return 0;

View File

@ -29,12 +29,6 @@
namespace blt::gp namespace blt::gp
{ {
inline allocation_tracker_t tracker; inline allocation_tracker_t tracker;
inline call_tracker_t crossover_calls;
inline call_tracker_t mutation_calls;
inline call_tracker_t reproduction_calls;
inline call_tracker_t crossover_allocations;
inline call_tracker_t mutation_allocations;
inline call_tracker_t reproduction_allocations;
class gp_program; class gp_program;
@ -143,7 +137,7 @@ namespace blt::gp
template<class U, class... Args> template<class U, class... Args>
void construct(U* p, Args&& ... args) void construct(U* p, Args&& ... args)
{ {
new(p) T(std::forward<Args>(args)...); new(p) T(args...);
} }
template<class U> template<class U>

View File

@ -70,7 +70,7 @@ namespace blt::gp
struct operator_info struct operator_info
{ {
// types of the arguments // types of the arguments
tracked_vector<type_id> argument_types; std::vector<type_id> argument_types;
// return type of this operator // return type of this operator
type_id return_type; type_id return_type;
// number of arguments for this operator // number of arguments for this operator
@ -82,15 +82,15 @@ namespace blt::gp
struct program_operator_storage_t struct program_operator_storage_t
{ {
// indexed from return TYPE ID, returns index of operator // indexed from return TYPE ID, returns index of operator
blt::expanding_buffer<tracked_vector<operator_id>> terminals; blt::expanding_buffer<std::vector<operator_id>> terminals;
blt::expanding_buffer<tracked_vector<operator_id>> non_terminals; blt::expanding_buffer<std::vector<operator_id>> non_terminals;
blt::expanding_buffer<tracked_vector<std::pair<operator_id, blt::size_t>>> operators_ordered_terminals; blt::expanding_buffer<std::vector<std::pair<operator_id, blt::size_t>>> operators_ordered_terminals;
// indexed from OPERATOR ID (operator number) // indexed from OPERATOR ID (operator number)
blt::hashset_t<operator_id> ephemeral_leaf_operators; blt::hashset_t<operator_id> ephemeral_leaf_operators;
tracked_vector<operator_info> operators; std::vector<operator_info> operators;
tracked_vector<detail::print_func_t> print_funcs; std::vector<detail::print_func_t> print_funcs;
tracked_vector<detail::destroy_func_t> destroy_funcs; std::vector<detail::destroy_func_t> destroy_funcs;
tracked_vector<std::optional<std::string_view>> names; std::vector<std::optional<std::string_view>> names;
detail::eval_func_t eval_func; detail::eval_func_t eval_func;
@ -110,7 +110,7 @@ namespace blt::gp
template<typename... Operators> template<typename... Operators>
program_operator_storage_t& build(Operators& ... operators) program_operator_storage_t& build(Operators& ... operators)
{ {
tracked_vector<blt::size_t> sizes; std::vector<blt::size_t> sizes;
(sizes.push_back(add_operator(operators)), ...); (sizes.push_back(add_operator(operators)), ...);
blt::size_t largest = 0; blt::size_t largest = 0;
for (auto v : sizes) for (auto v : sizes)
@ -153,7 +153,7 @@ namespace blt::gp
if (op_r.second.empty()) if (op_r.second.empty())
continue; continue;
auto return_type = op_r.first; auto return_type = op_r.first;
tracked_vector<std::pair<operator_id, blt::size_t>> ordered_terminals; std::vector<std::pair<operator_id, blt::size_t>> ordered_terminals;
for (const auto& op : op_r.second) for (const auto& op : op_r.second)
{ {
// count number of terminals // count number of terminals
@ -675,12 +675,12 @@ namespace blt::gp
return storage.names[id]; return storage.names[id];
} }
[[nodiscard]] inline tracked_vector<operator_id>& get_type_terminals(type_id id) [[nodiscard]] inline std::vector<operator_id>& get_type_terminals(type_id id)
{ {
return storage.terminals[id]; return storage.terminals[id];
} }
[[nodiscard]] inline tracked_vector<operator_id>& get_type_non_terminals(type_id id) [[nodiscard]] inline std::vector<operator_id>& get_type_non_terminals(type_id id)
{ {
return storage.non_terminals[id]; return storage.non_terminals[id];
} }
@ -715,7 +715,7 @@ namespace blt::gp
{ {
std::array<blt::size_t, size> arr; std::array<blt::size_t, size> arr;
tracked_vector<std::pair<blt::size_t, double>> values; std::vector<std::pair<blt::size_t, double>> values;
values.reserve(current_pop.get_individuals().size()); values.reserve(current_pop.get_individuals().size());
for (const auto& ind : blt::enumerate(current_pop.get_individuals())) for (const auto& ind : blt::enumerate(current_pop.get_individuals()))
@ -789,11 +789,11 @@ namespace blt::gp
std::atomic_bool fitness_should_exit = false; std::atomic_bool fitness_should_exit = false;
population_stats current_stats{}; population_stats current_stats{};
tracked_vector<population_stats> statistic_history; std::vector<population_stats> statistic_history;
struct concurrency_storage struct concurrency_storage
{ {
tracked_vector<std::unique_ptr<std::thread>> threads; std::vector<std::unique_ptr<std::thread>> threads;
std::mutex thread_function_control{}; std::mutex thread_function_control{};
std::condition_variable thread_function_condition{}; std::condition_variable thread_function_condition{};

View File

@ -88,9 +88,7 @@ namespace blt::gp
// everyone gets a chance once per loop. // everyone gets a chance once per loop.
if (random.choice(config.crossover_chance)) if (random.choice(config.crossover_chance))
{ {
#ifdef BLT_TRACK_ALLOCATIONS // auto state = tracker.start_measurement();
auto state = tracker.start_measurement();
#endif
// crossover // crossover
const tree_t* p1; const tree_t* p1;
const tree_t* p2; const tree_t* p2;
@ -99,54 +97,37 @@ namespace blt::gp
p1 = &crossover_selection.select(program, current_pop); p1 = &crossover_selection.select(program, current_pop);
p2 = &crossover_selection.select(program, current_pop); p2 = &crossover_selection.select(program, current_pop);
} while (!config.crossover.get().apply(program, *p1, *p2, c1, *c2)); } while (!config.crossover.get().apply(program, *p1, *p2, c1, *c2));
#ifdef BLT_TRACK_ALLOCATIONS // tracker.stop_measurement(state);
tracker.stop_measurement(state); // BLT_TRACE("Crossover Allocated %ld times with a total of %s", state.getAllocationDifference(),
crossover_calls.call(); // blt::byte_convert_t(state.getAllocatedByteDifference()).convert_to_nearest_type().to_pretty_string().c_str());
if (state.getAllocationDifference() != 0)
crossover_allocations.call(state.getAllocatedByteDifference());
#endif
return 2; return 2;
} }
break; break;
case 1: case 1:
if (random.choice(config.mutation_chance)) if (random.choice(config.mutation_chance))
{ {
#ifdef BLT_TRACK_ALLOCATIONS // auto state = tracker.start_measurement();
auto state = tracker.start_measurement();
#endif
// mutation // mutation
const tree_t* p; const tree_t* p;
do do
{ {
p = &mutation_selection.select(program, current_pop); p = &mutation_selection.select(program, current_pop);
} while (!config.mutator.get().apply(program, *p, c1)); } while (!config.mutator.get().apply(program, *p, c1));
#ifdef BLT_TRACK_ALLOCATIONS // tracker.stop_measurement(state);
tracker.stop_measurement(state); // BLT_TRACE("Mutation Allocated %ld times with a total of %s", state.getAllocationDifference(),
mutation_calls.call(); // blt::byte_convert_t(state.getAllocatedByteDifference()).convert_to_nearest_type().to_pretty_string().c_str());
if (state.getAllocationDifference() != 0)
{
mutation_allocations.call(state.getAllocatedByteDifference());
}
#endif
return 1; return 1;
} }
break; break;
case 2: case 2:
if (config.reproduction_chance > 0 && random.choice(config.reproduction_chance)) if (config.reproduction_chance > 0 && random.choice(config.reproduction_chance))
{ {
#ifdef BLT_TRACK_ALLOCATIONS // auto state = tracker.start_measurement();
auto state = tracker.start_measurement();
#endif
// reproduction // reproduction
c1 = reproduction_selection.select(program, current_pop); c1 = reproduction_selection.select(program, current_pop);
#ifdef BLT_TRACK_ALLOCATIONS // tracker.stop_measurement(state);
tracker.stop_measurement(state); // BLT_TRACE("Reproduction Allocated %ld times with a total of %s", state.getAllocationDifference(),
reproduction_calls.call(); // blt::byte_convert_t(state.getAllocatedByteDifference()).convert_to_nearest_type().to_pretty_string().c_str());
if (state.getAllocationDifference() != 0)
{
reproduction_allocations.call(state.getAllocatedByteDifference());
}
#endif
return 1; return 1;
} }
break; break;

View File

@ -129,68 +129,6 @@ namespace blt::gp
std::atomic_uint64_t deallocated_bytes = 0; std::atomic_uint64_t deallocated_bytes = 0;
}; };
class call_tracker_t
{
public:
struct call_data_t
{
blt::u64 start_calls = 0;
blt::u64 start_value = 0;
blt::u64 end_calls = 0;
blt::u64 end_value = 0;
[[nodiscard]] inline auto get_call_difference() const
{
return end_calls - start_calls;
}
[[nodiscard]] inline auto get_value_difference() const
{
return end_value - start_value;
}
};
void value(blt::u64 value)
{
secondary_value += value;
}
void call()
{
primary_calls++;
}
void call(blt::u64 v)
{
primary_calls++;
value(v);
}
[[nodiscard]] auto get_calls() const
{
return primary_calls.load();
}
[[nodiscard]] auto get_value() const
{
return secondary_value.load();
}
call_data_t start_measurement()
{
return {primary_calls.load(), 0};
}
void stop_measurement(call_data_t& data)
{
data.end_calls = primary_calls.load();
}
private:
std::atomic_uint64_t primary_calls = 0;
std::atomic_uint64_t secondary_value = 0;
};
} }
#endif //BLT_GP_STATS_H #endif //BLT_GP_STATS_H

View File

@ -156,7 +156,7 @@ namespace blt::gp
bool check(gp_program& program, void* context) const; bool check(gp_program& program, void* context) const;
void find_child_extends(gp_program& program, tracked_vector<child_t>& vec, blt::size_t parent_node, blt::size_t argc) const; void find_child_extends(gp_program& program, std::vector<child_t>& vec, blt::size_t parent_node, blt::size_t argc) const;
blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, blt::ptrdiff_t start) const; blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, blt::ptrdiff_t start) const;
@ -251,7 +251,7 @@ namespace blt::gp
std::atomic<double> average_fitness = 0; std::atomic<double> average_fitness = 0;
std::atomic<double> best_fitness = 0; std::atomic<double> best_fitness = 0;
std::atomic<double> worst_fitness = 1; std::atomic<double> worst_fitness = 1;
tracked_vector<double> normalized_fitness{}; std::vector<double> normalized_fitness{};
void clear() void clear()
{ {

View File

@ -85,8 +85,8 @@ namespace blt::gp
stack_allocator& c2_stack = c2.get_values(); stack_allocator& c2_stack = c2.get_values();
// we have to make a copy because we will modify the underlying storage. // we have to make a copy because we will modify the underlying storage.
static thread_local tracked_vector<op_container_t> c1_operators; static thread_local std::vector<op_container_t> c1_operators;
static thread_local tracked_vector<op_container_t> c2_operators; static thread_local std::vector<op_container_t> c2_operators;
c1_operators.clear(); c1_operators.clear();
c2_operators.clear(); c2_operators.clear();
@ -359,7 +359,7 @@ namespace blt::gp
auto& replacement_func_info = program.get_operator_info(random_replacement); auto& replacement_func_info = program.get_operator_info(random_replacement);
// cache memory used for offset data. // cache memory used for offset data.
thread_local static tracked_vector<tree_t::child_t> children_data; thread_local static std::vector<tree_t::child_t> children_data;
children_data.clear(); children_data.clear();
c.find_child_extends(program, children_data, c_node, current_func_info.argument_types.size()); c.find_child_extends(program, children_data, c_node, current_func_info.argument_types.size());
@ -586,7 +586,7 @@ namespace blt::gp
if (argument_index == -1ul) if (argument_index == -1ul)
continue; continue;
static thread_local tracked_vector<tree_t::child_t> child_data; static thread_local std::vector<tree_t::child_t> child_data;
child_data.clear(); child_data.clear();
c.find_child_extends(program, child_data, c_node, info.argument_types.size()); c.find_child_extends(program, child_data, c_node, info.argument_types.size());
@ -670,7 +670,7 @@ namespace blt::gp
to = pt; to = pt;
} }
static thread_local tracked_vector<tree_t::child_t> child_data; static thread_local std::vector<tree_t::child_t> child_data;
child_data.clear(); child_data.clear();
c.find_child_extends(program, child_data, c_node, info.argument_types.size()); c.find_child_extends(program, child_data, c_node, info.argument_types.size());
@ -700,7 +700,7 @@ namespace blt::gp
vals.copy_from(from_ptr, from_bytes); vals.copy_from(from_ptr, from_bytes);
vals.copy_from(after_ptr, after_to_bytes); vals.copy_from(after_ptr, after_to_bytes);
static thread_local tracked_vector<op_container_t> op_copy; static thread_local std::vector<op_container_t> op_copy;
op_copy.clear(); op_copy.clear();
op_copy.insert(op_copy.begin(), ops.begin() + from_child.start, ops.begin() + from_child.end); op_copy.insert(op_copy.begin(), ops.begin() + from_child.start, ops.begin() + from_child.end);

View File

@ -145,8 +145,8 @@ namespace blt::gp
blt::size_t depth = 0; blt::size_t depth = 0;
auto operations_stack = operations; auto operations_stack = operations;
tracked_vector<blt::size_t> values_process; std::vector<blt::size_t> values_process;
tracked_vector<blt::size_t> value_stack; std::vector<blt::size_t> value_stack;
for (const auto& op : operations_stack) for (const auto& op : operations_stack)
{ {
@ -275,7 +275,7 @@ namespace blt::gp
return true; return true;
} }
void tree_t::find_child_extends(gp_program& program, tracked_vector<child_t>& vec, blt::size_t parent_node, blt::size_t argc) const void tree_t::find_child_extends(gp_program& program, std::vector<child_t>& vec, blt::size_t parent_node, blt::size_t argc) const
{ {
while (vec.size() < argc) while (vec.size() < argc)
{ {