shared
Brett 2024-08-25 18:08:23 -04:00
parent 95b88aeebf
commit e5966789be
4 changed files with 20 additions and 17 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.25) project(blt-gp VERSION 0.1.26)
include(CTest) include(CTest)

View File

@ -123,7 +123,6 @@ namespace blt::gp
static thread_local evaluation_context results{}; static thread_local evaluation_context results{};
results.values.reset(); results.values.reset();
results.values.reserve(largest); results.values.reserve(largest);
// BLT_DEBUG("%ld stored %ld", largest, results.values.internal_storage_size());
blt::size_t total_so_far = 0; blt::size_t total_so_far = 0;
@ -136,11 +135,6 @@ namespace blt::gp
continue; continue;
} }
call_jmp_table(operation.id, context, results.values, results.values, operators...); call_jmp_table(operation.id, context, results.values, results.values, operators...);
// if (results.values.internal_storage_size() != l)
// {
// BLT_DEBUG("Size %ld is now %ld", l, results.values.internal_storage_size());
// l = results.values.internal_storage_size();
// }
} }
return results; return results;
@ -507,22 +501,16 @@ namespace blt::gp
while (thread_helper.next_gen_left > 0) while (thread_helper.next_gen_left > 0)
{ {
blt::size_t size = 0; blt::size_t size = 0;
blt::size_t begin = 0;
blt::size_t end = thread_helper.next_gen_left.load(std::memory_order_relaxed); blt::size_t end = thread_helper.next_gen_left.load(std::memory_order_relaxed);
do do
{ {
size = std::min(end, config.evaluation_size); size = std::min(end, config.evaluation_size);
begin = end - size;
} while (!thread_helper.next_gen_left.compare_exchange_weak(end, end - size, } while (!thread_helper.next_gen_left.compare_exchange_weak(end, end - size,
std::memory_order::memory_order_relaxed, std::memory_order::memory_order_relaxed,
std::memory_order::memory_order_relaxed)); std::memory_order::memory_order_relaxed));
//auto measure = tracker.start_measurement(); while (new_children.size() < size)
for (blt::size_t i = begin; i < end; i++)
func(args, crossover_selection, mutation_selection, reproduction_selection); func(args, crossover_selection, mutation_selection, reproduction_selection);
//tracker.stop_measurement(measure);
//BLT_TRACE("Allocated %ld times with a total of %s", measure.getAllocationDifference(),
// blt::byte_convert_t(measure.getAllocatedByteDifference()).convert_to_nearest_type().to_pretty_string().c_str());
{ {
std::scoped_lock lock(thread_helper.thread_generation_lock); std::scoped_lock lock(thread_helper.thread_generation_lock);
@ -553,6 +541,7 @@ namespace blt::gp
void next_generation() void next_generation()
{ {
BLT_ASSERT_MSG(next_pop.get_individuals().size() == config.population_size, ("pop size: " + std::to_string(next_pop.get_individuals().size())).c_str());
current_pop = std::move(next_pop); current_pop = std::move(next_pop);
current_generation++; current_generation++;
} }

View File

@ -24,6 +24,7 @@
#include <blt/gp/config.h> #include <blt/gp/config.h>
#include <blt/gp/random.h> #include <blt/gp/random.h>
#include <blt/std/assert.h> #include <blt/std/assert.h>
#include "blt/std/format.h"
namespace blt::gp namespace blt::gp
{ {
@ -84,6 +85,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))
{ {
// auto state = tracker.start_measurement();
// crossover // crossover
auto& p1 = crossover_selection.select(program, current_pop, current_stats); auto& p1 = crossover_selection.select(program, current_pop, current_stats);
auto& p2 = crossover_selection.select(program, current_pop, current_stats); auto& p2 = crossover_selection.select(program, current_pop, current_stats);
@ -94,24 +96,36 @@ namespace blt::gp
if (results) if (results)
{ {
next_pop.push_back(std::move(results->child1)); next_pop.push_back(std::move(results->child1));
if (next_pop.size() != config.population_size)
next_pop.push_back(std::move(results->child2)); next_pop.push_back(std::move(results->child2));
} }
// tracker.stop_measurement(state);
// BLT_TRACE("Crossover Allocated %ld times with a total of %s", state.getAllocationDifference(),
// blt::byte_convert_t(state.getAllocatedByteDifference()).convert_to_nearest_type().to_pretty_string().c_str());
} }
break; break;
case 1: case 1:
if (random.choice(config.mutation_chance)) if (random.choice(config.mutation_chance))
{ {
// auto state = tracker.start_measurement();
// mutation // mutation
auto& p = mutation_selection.select(program, current_pop, current_stats); auto& p = mutation_selection.select(program, current_pop, current_stats);
next_pop.push_back(std::move(config.mutator.get().apply(program, p))); next_pop.push_back(std::move(config.mutator.get().apply(program, p)));
// tracker.stop_measurement(state);
// BLT_TRACE("Mutation Allocated %ld times with a total of %s", state.getAllocationDifference(),
// blt::byte_convert_t(state.getAllocatedByteDifference()).convert_to_nearest_type().to_pretty_string().c_str());
} }
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))
{ {
// auto state = tracker.start_measurement();
// reproduction // reproduction
auto& p = reproduction_selection.select(program, current_pop, current_stats); auto& p = reproduction_selection.select(program, current_pop, current_stats);
next_pop.push_back(p); next_pop.push_back(p);
// tracker.stop_measurement(state);
// BLT_TRACE("Reproduction Allocated %ld times with a total of %s", state.getAllocationDifference(),
// blt::byte_convert_t(state.getAllocatedByteDifference()).convert_to_nearest_type().to_pretty_string().c_str());
} }
break; break;
default: default:

View File

@ -121,7 +121,7 @@ namespace blt::gp
return getAllocatedBytes() - getDeallocatedBytes(); return getAllocatedBytes() - getDeallocatedBytes();
} }
allocation_data_t start_measurement() [[nodiscard]] allocation_data_t start_measurement() const
{ {
allocation_data_t data{}; allocation_data_t data{};
data.start_allocations = allocations; data.start_allocations = allocations;
@ -131,7 +131,7 @@ namespace blt::gp
return data; return data;
} }
void stop_measurement(allocation_data_t& data) void stop_measurement(allocation_data_t& data) const
{ {
data.end_allocations = allocations; data.end_allocations = allocations;
data.end_deallocations = deallocations; data.end_deallocations = deallocations;