Compare commits

..

3 Commits

6 changed files with 151 additions and 32 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.0.113)
project(blt-gp VERSION 0.0.114)
include(CTest)
@ -95,6 +95,7 @@ if (${BUILD_GP_TESTS})
blt_add_project(blt-stack tests/stack_tests.cpp test)
blt_add_project(blt-eval tests/evaluation_tests.cpp test)
blt_add_project(blt-order tests/order_tests.cpp test)
blt_add_project(blt-gp1 tests/gp_test_1.cpp test)
blt_add_project(blt-gp2 tests/gp_test_2.cpp test)
blt_add_project(blt-gp3 tests/gp_test_3.cpp test)

View File

@ -78,7 +78,7 @@ namespace blt::gp
{
blt::size_t offset = 0;
blt::size_t current_index = 0;
((offset += (current_index++ < index ? stack_allocator::aligned_size<detail::remove_cv_ref<Args>>() : 0)), ...);
((offset += (current_index++ > index ? stack_allocator::aligned_size<detail::remove_cv_ref<Args>>() : 0)), ...);
//BLT_INFO("offset %ld for index %ld", offset, index);
return offset;
}

View File

@ -183,7 +183,8 @@ namespace blt::gp
auto begin_point = static_cast<blt::ptrdiff_t>(program.get_random().get_size_t(0ul, ops_r.size()));
auto end_point = find_endpoint(program, ops_r, begin_point);
const auto& type_info = program.get_operator_info(ops_r[begin_point].id);
auto begin_operator_id = ops_r[begin_point].id;
const auto& type_info = program.get_operator_info(begin_operator_id);
auto begin_itr = ops_r.begin() + begin_point;
auto end_itr = ops_r.begin() + end_point;
@ -194,6 +195,7 @@ namespace blt::gp
auto& new_vals_r = new_tree.get_values();
stack_allocator stack_after;
stack_allocator new_vals_flip; // this is annoying.
transfer_backward(vals_r, stack_after, ops_r.end() - 1, end_itr - 1);
for (auto it = end_itr - 1; it != begin_itr - 1; it--)
{
@ -201,7 +203,8 @@ namespace blt::gp
vals_r.pop_bytes(static_cast<blt::ptrdiff_t>(stack_allocator::aligned_size(it->type_size)));
}
transfer_backward(new_vals_r, vals_r, new_ops_r.end() - 1, new_ops_r.begin() - 1);
transfer_backward(new_vals_r, new_vals_flip, new_ops_r.end() - 1, new_ops_r.begin() - 1);
transfer_forward(new_vals_flip, vals_r, new_ops_r.begin(), new_ops_r.end());
transfer_forward(stack_after, vals_r, end_itr, ops_r.end());
auto before = begin_itr - 1;
@ -235,6 +238,7 @@ namespace blt::gp
std::cout << "This occurred at point " << begin_point << " ending at (old) " << end_point << "\n";
std::cout << "our root type is " << ops_r[begin_point].id << " with size " << stack_allocator::aligned_size(ops_r[begin_point].type_size) << "\n";
std::cout << "now Named: " << (program.get_name(ops_r[begin_point].id) ? *program.get_name(ops_r[begin_point].id) : "Unnamed") << "\n";
std::cout << "Was named: " << (program.get_name(begin_operator_id) ? *program.get_name(begin_operator_id) : "Unnamed") << "\n";
std::cout << "Parent:" << std::endl;
p.print(program, std::cout, false, true);
std::cout << "Child:" << std::endl;
@ -244,7 +248,6 @@ namespace blt::gp
std::cout << std::endl;
throw std::exception();
}
#endif
return c;

View File

@ -91,7 +91,7 @@ blt::gp::operation_t cross_large_type([](const large_18290& input, const float a
});
blt::gp::operation_t f_literal([]() {
return 0.0f;
return 555.0f;
});
blt::gp::operation_t b_literal([]() {
@ -110,8 +110,8 @@ void basic_tree()
tree.get_operations().push_back(make_container(sub.id));
tree.get_operations().push_back(make_value(type_system.get_type<float>()));
tree.get_operations().push_back(make_value(type_system.get_type<float>()));
tree.get_values().push(120.0f);
tree.get_values().push(50.0f);
tree.get_values().push(120.0f);
auto val = tree.get_evaluation_value<float>(nullptr);
BLT_TRACE(val);
@ -125,11 +125,11 @@ void large_cross_type_tree()
auto& vals = tree.get_values();
ops.push_back(make_container(cross_large_type.id));
ops.push_back(make_container(large_literal.id));
ops.push_back(make_container(sub.id));
ops.push_back(make_value(type_system.get_type<float>()));
ops.push_back(make_value(type_system.get_type<float>()));
ops.push_back(make_value(type_system.get_type<float>()));
ops.push_back(make_container(large_literal.id));
vals.push(50.0f);
vals.push(120.0f);

115
tests/order_tests.cpp Normal file
View File

@ -0,0 +1,115 @@
/*
* <Short Description>
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <blt/std/logging.h>
#include <blt/gp/tree.h>
#include <blt/gp/operations.h>
#include <blt/gp/generators.h>
#include <blt/gp/transformers.h>
#include <blt/gp/program.h>
#include <random>
const blt::u64 SEED = std::random_device()();
//const blt::u64 SEED = 3495535167;
blt::gp::random_t b_rand {SEED};
blt::gp::type_provider type_system;
struct context
{
blt::gp::gp_program* program;
};
struct large_256
{
blt::u8 data[256];
};
struct large_2048
{
blt::u8 data[2048];
};
// not actually 4096 but will fill the whole page (4096)
struct large_4096
{
blt::u8 data[blt::gp::stack_allocator::page_size_no_block()];
};
struct large_6123
{
blt::u8 data[6123];
};
struct large_18290
{
blt::u8 data[18290];
};
blt::gp::operation_t basic_sub([](float a, float b, bool choice) {
if (choice)
{
BLT_TRACE("Choice Taken! a: %lf b: %lf", a, b);
return b - a;
}else
{
BLT_TRACE("Choice Not Taken! a: %lf b: %lf", a, b);
return a - b;
}
}, "sub");
blt::gp::operation_t basic_lit_f([]() {
return b_rand.choice() ? 5.0f : 10.0f;
});
blt::gp::operation_t basic_lit_b([]() {
return false;
});
void basic_test()
{
blt::gp::gp_program program{type_system, SEED};
blt::gp::operator_builder<context> builder{type_system};
builder.add_operator(basic_sub);
builder.add_operator(basic_lit_f, true);
builder.add_operator(basic_lit_b, true);
program.set_operations(builder.build());
blt::gp::grow_generator_t gen;
blt::gp::generator_arguments args{program, type_system.get_type<float>().id(), 1, 1};
auto tree = gen.generate(args);
context ctx{&program};
auto result = tree.get_evaluation_value<float>(&ctx);
BLT_TRACE(result);
BLT_ASSERT(result == -5.0f || result == 5.0f || result == 0.0f);
tree.print(program, std::cout, true, true);
}
int main()
{
BLT_INFO("Starting with seed %ld", SEED);
type_system.register_type<float>();
type_system.register_type<bool>();
type_system.register_type<large_18290>();
basic_test();
}

View File

@ -298,8 +298,8 @@ void test_basic()
BLT_INFO("Testing basic with stack");
{
blt::gp::stack_allocator stack;
stack.push(10.0f);
stack.push(50.0f);
stack.push(10.0f);
BLT_TRACE_STREAM << stack.size() << "\n";
basic_2.make_callable<blt::gp::detail::empty_t>()(nullptr, stack, stack);
BLT_TRACE_STREAM << stack.size() << "\n";
@ -312,8 +312,8 @@ void test_basic()
{
blt::gp::stack_allocator stack;
stack.push(std::array<blt::u8, blt::gp::stack_allocator::page_size_no_block() - sizeof(float)>{});
stack.push(10.0f);
stack.push(50.0f);
stack.push(10.0f);
auto size = stack.size();
BLT_TRACE_STREAM << size << "\n";
BLT_ASSERT(size.blocks > 1 && "Stack doesn't have more than one block!");
@ -332,10 +332,10 @@ void test_mixed()
BLT_INFO("Testing mixed with stack");
{
blt::gp::stack_allocator stack;
stack.push(false);
stack.push(true);
stack.push(10.0f);
stack.push(50.0f);
stack.push(10.0f);
stack.push(true);
stack.push(false);
BLT_TRACE_STREAM << stack.size() << "\n";
basic_mixed_4.make_callable<blt::gp::detail::empty_t>()(nullptr, stack, stack);
@ -349,10 +349,10 @@ void test_mixed()
{
blt::gp::stack_allocator stack;
stack.push(std::array<blt::u8, blt::gp::stack_allocator::page_size_no_block() - sizeof(float)>{});
stack.push(false);
stack.push(true);
stack.push(10.0f);
stack.push(50.0f);
stack.push(10.0f);
stack.push(true);
stack.push(false);
auto size = stack.size();
BLT_TRACE_STREAM << size << "\n";
BLT_ASSERT(size.blocks > 1 && "Stack doesn't have more than one block!");
@ -371,9 +371,9 @@ void test_large_256()
BLT_INFO("Testing large 256 with stack");
{
blt::gp::stack_allocator stack;
stack.push(69.420f);
stack.push(691.0f);
stack.push(base_256);
stack.push(691.0f);
stack.push(69.420f);
BLT_TRACE_STREAM << stack.size() << "\n";
large_256_basic_3.make_callable<blt::gp::detail::empty_t>()(nullptr, stack, stack);
@ -387,9 +387,9 @@ void test_large_256()
{
blt::gp::stack_allocator stack;
stack.push(std::array<blt::u8, blt::gp::stack_allocator::page_size_no_block() - sizeof(large_256)>{});
stack.push(69.420f);
stack.push(691.0f);
stack.push(base_256);
stack.push(691.0f);
stack.push(69.420f);
auto size = stack.size();
BLT_TRACE_STREAM << size << "\n";
BLT_ASSERT(size.blocks > 1 && "Stack doesn't have more than one block!");
@ -408,9 +408,9 @@ void test_large_4096()
BLT_INFO("Testing large 4096 with stack");
{
blt::gp::stack_allocator stack;
stack.push(true);
stack.push(33.0f);
stack.push(base_4096);
stack.push(33.0f);
stack.push(true);
BLT_TRACE_STREAM << stack.size() << "\n";
large_4096_basic_3b.make_callable<blt::gp::detail::empty_t>()(nullptr, stack, stack);
BLT_TRACE_STREAM << stack.size() << "\n";
@ -422,9 +422,9 @@ void test_large_4096()
BLT_INFO("Testing large 4096 with stack over boundary");
{
blt::gp::stack_allocator stack;
stack.push(true);
stack.push(33.0f);
stack.push(base_4096);
stack.push(33.0f);
stack.push(true);
auto size = stack.size();
BLT_TRACE_STREAM << size << "\n";
BLT_ASSERT(size.blocks > 1 && "Stack doesn't have more than one block!");
@ -442,9 +442,9 @@ void test_large_18290()
BLT_INFO("Testing large 18290 with stack");
{
blt::gp::stack_allocator stack;
stack.push(true);
stack.push(-2543.0f);
stack.push(base_18290);
stack.push(-2543.0f);
stack.push(true);
BLT_TRACE_STREAM << stack.size() << "\n";
large_18290_basic_3b.make_callable<blt::gp::detail::empty_t>()(nullptr, stack, stack);
BLT_TRACE_STREAM << stack.size() << "\n";
@ -457,9 +457,9 @@ void test_large_18290()
{
blt::gp::stack_allocator stack;
stack.push(std::array<blt::u8, 20480 - 18290 - blt::gp::stack_allocator::block_size()>());
stack.push(true);
stack.push(-2543.0f);
stack.push(base_18290);
stack.push(-2543.0f);
stack.push(true);
auto size = stack.size();
BLT_TRACE_STREAM << size << "\n";
BLT_ASSERT(size.blocks > 1 && "Stack doesn't have more than one block!");
@ -474,15 +474,15 @@ void test_large_18290()
BLT_INFO("Testing large 18290 with stack over multiple boundaries");
{
blt::gp::stack_allocator stack;
stack.push(true);
stack.push(-2543.0f);
stack.push(true);
stack.push(-2543.0f);
stack.push(base_18290);
stack.push(-2543.0f);
stack.push(true);
auto size = stack.size();
BLT_TRACE_STREAM << size << "\n";
large_18290_basic_3b.make_callable<blt::gp::detail::empty_t>()(nullptr, stack, stack);
BLT_TRACE_STREAM << stack.size() << "\n";
stack.push(-2543.0f);
stack.push(true);
BLT_TRACE_STREAM << stack.size() << "\n";
large_18290_basic_3b.make_callable<blt::gp::detail::empty_t>()(nullptr, stack, stack);
BLT_TRACE_STREAM << stack.size() << "\n";