Compare commits

...

2 Commits

Author SHA1 Message Date
Brett 06078fddc4 maybe stack fixed? 2024-07-22 00:00:51 -04:00
Brett 7810a21226 fix fitness proportinate 2024-07-21 23:23:50 -04:00
4 changed files with 20 additions and 12 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.95) project(blt-gp VERSION 0.0.97)
include(CTest) include(CTest)
@ -87,7 +87,7 @@ endmacro()
if (${BUILD_EXAMPLES}) if (${BUILD_EXAMPLES})
blt_add_project(blt-SR-playground examples/symbolic_regression.cpp example) blt_add_project(blt-symbolic-regression examples/symbolic_regression.cpp example)
endif () endif ()

View File

@ -117,6 +117,8 @@ namespace blt::gp
reinterpret_cast<NO_REF_T*>(head->metadata.offset - TYPE_SIZE)->~NO_REF_T(); reinterpret_cast<NO_REF_T*>(head->metadata.offset - TYPE_SIZE)->~NO_REF_T();
// move offset back // move offset back
head->metadata.offset -= TYPE_SIZE; head->metadata.offset -= TYPE_SIZE;
// moving back allows us to allocate with other data, if there is room.
while (head->used_bytes_in_block() == 0 && move_back());
return t; return t;
} }
@ -189,6 +191,7 @@ namespace blt::gp
break; break;
} }
} }
while (head->used_bytes_in_block() == 0 && move_back());
} }
/** /**
@ -212,6 +215,7 @@ namespace blt::gp
to.head->metadata.offset = static_cast<blt::u8*>(ptr) + type_size; to.head->metadata.offset = static_cast<blt::u8*>(ptr) + type_size;
std::memcpy(ptr, head->metadata.offset - type_size, type_size); std::memcpy(ptr, head->metadata.offset - type_size, type_size);
head->metadata.offset -= type_size; head->metadata.offset -= type_size;
while (head->used_bytes_in_block() == 0 && move_back());
} }
template<typename... Args> template<typename... Args>

View File

@ -80,12 +80,17 @@ namespace blt::gp
tree_t& select_fitness_proportionate_t::select(gp_program& program, population_t& pop, population_stats& stats) tree_t& select_fitness_proportionate_t::select(gp_program& program, population_t& pop, population_stats& stats)
{ {
auto choice = program.get_random().get_double(); auto choice = program.get_random().get_double();
for (const auto& ind : blt::enumerate(pop)) for (const auto& [index, ref] : blt::enumerate(pop))
{ {
if (ind.first == 0 && choice <= stats.normalized_fitness[ind.first]) if (index == 0)
return ind.second.tree; {
if (choice > stats.normalized_fitness[ind.first - 1] && choice <= stats.normalized_fitness[ind.first]) if (choice <= stats.normalized_fitness[index])
return ind.second.tree; return ref.tree;
} else
{
if (choice > stats.normalized_fitness[index - 1] && choice <= stats.normalized_fitness[index])
return ref.tree;
}
} }
BLT_WARN("Unable to find individual with fitness proportionate. This should not be a possible code path! (%lf)", choice); BLT_WARN("Unable to find individual with fitness proportionate. This should not be a possible code path! (%lf)", choice);
return pop.get_individuals()[0].tree; return pop.get_individuals()[0].tree;

View File

@ -52,9 +52,9 @@ large_##SIZE tertiary_##SIZE = make_data(large_##SIZE{}, [](auto index) {
return static_cast<blt::u8>(blt::random::murmur_random64c<blt::size_t>(SEED + index, 0, 256)); \ return static_cast<blt::u8>(blt::random::murmur_random64c<blt::size_t>(SEED + index, 0, 256)); \
}) })
#define RUN_TEST(FAILURE_COND, PASS, ...) do { if (FAILURE_COND) { BLT_ERROR(__VA_ARGS__); } else { BLT_DEBUG(PASS); } } while(false) #define RUN_TEST(FAILURE_COND, STACK, PASS, ...) do { if (FAILURE_COND) { BLT_ERROR(__VA_ARGS__); } else { BLT_DEBUG_STREAM << PASS << " | " << STACK.size() << "\n"; } } while(false)
#define RUN_TEST_SIZE(VALUE, SIZE, STACK) RUN_TEST(auto index = compare(VALUE, STACK.pop<SIZE>()); index >= 0, #SIZE " Test PASSED.", "Failed to pop large value (" #SIZE "), failed at index %ld", index) #define RUN_TEST_SIZE(VALUE, SIZE, STACK) RUN_TEST(auto index = compare(VALUE, STACK.pop<SIZE>()); index >= 0, STACK, #SIZE " test PASSED.", "Failed to pop large value (" #SIZE "), failed at index %ld", index)
#define RUN_TEST_TYPE(TYPE, EXPECTED, STACK) RUN_TEST(auto val = STACK.pop<TYPE>(); val != EXPECTED, #TYPE " test PASSED", "Failed to pop correct " #TYPE " (" #EXPECTED ") found %lf", val); #define RUN_TEST_TYPE(TYPE, EXPECTED, STACK) RUN_TEST(auto val = STACK.pop<TYPE>(); val != EXPECTED, STACK, #TYPE " test PASSED", "Failed to pop correct " #TYPE " (" #EXPECTED ") found %lf", val);
const blt::u64 SEED = std::random_device()(); const blt::u64 SEED = std::random_device()();
@ -189,14 +189,13 @@ void test_basic_types()
BLT_TRACE_STREAM << stack.size() << "\n"; BLT_TRACE_STREAM << stack.size() << "\n";
std::cout << std::endl; std::cout << std::endl;
BLT_INFO("Some fishy numbers in the last reported size. Let's try modifying the stack."); BLT_INFO("Some fishy numbers in the last reported size. Let's try modifying the stack."); // fixed by moving back in pop
stack.push(88.9f); stack.push(88.9f);
BLT_TRACE_STREAM << "Pushed float: " << stack.size() << "\n"; BLT_TRACE_STREAM << "Pushed float: " << stack.size() << "\n";
{ {
BLT_INFO("Popping a few values."); BLT_INFO("Popping a few values.");
RUN_TEST_TYPE(float, 88.9f, stack); RUN_TEST_TYPE(float, 88.9f, stack);
BLT_TRACE_STREAM << "popped float: " << stack.size() << "\n";
RUN_TEST_SIZE(secondary_256, large_256, stack); RUN_TEST_SIZE(secondary_256, large_256, stack);
} }
BLT_TRACE_STREAM << stack.size() << "\n"; BLT_TRACE_STREAM << stack.size() << "\n";