Brett 2024-08-10 13:36:49 -04:00
parent 98af35f357
commit f2e51bbbaf
3 changed files with 95 additions and 17 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(image-gp-6 VERSION 0.0.28) project(image-gp-6 VERSION 0.0.29)
include(FetchContent) include(FetchContent)

@ -1 +1 @@
Subproject commit 9f7c647952ab41a59d8b7d0a196fb3dc3147d23f Subproject commit 502ea6ea36246de0a9ad6f67d9085796d3c0cf5b

View File

@ -53,6 +53,10 @@ namespace blt::gp
if (!program.get_random().choice(node_mutation_chance)) if (!program.get_random().choice(node_mutation_chance))
continue; continue;
#if BLT_DEBUG_LEVEL >= 2
tree_t c_copy = c;
#endif
// select an operator to apply // select an operator to apply
auto selected_point = static_cast<blt::i32>(mutation_operator::COPY); auto selected_point = static_cast<blt::i32>(mutation_operator::COPY);
auto choice = program.get_random().get_double(); auto choice = program.get_random().get_double();
@ -109,6 +113,7 @@ namespace blt::gp
child_t prev{}; child_t prev{};
if (current_point == 0) if (current_point == 0)
{ {
BLT_TRACE("Child %ld: %s", current_point, std::string(*program.get_name(ops[c_node + 1].id)).c_str());
// first child. // first child.
prev = {static_cast<blt::ptrdiff_t>(c_node + 1), prev = {static_cast<blt::ptrdiff_t>(c_node + 1),
c.find_endpoint(program, static_cast<blt::ptrdiff_t>(c_node + 1))}; c.find_endpoint(program, static_cast<blt::ptrdiff_t>(c_node + 1))};
@ -116,6 +121,7 @@ namespace blt::gp
continue; continue;
} else } else
prev = children_data[current_point - 1]; prev = children_data[current_point - 1];
BLT_TRACE("Child %ld: %s", current_point, std::string(*program.get_name(ops[prev.end].id)).c_str());
child_t next = {prev.end, c.find_endpoint(program, prev.end)}; child_t next = {prev.end, c.find_endpoint(program, prev.end)};
children_data.push_back(next); children_data.push_back(next);
} }
@ -128,7 +134,8 @@ namespace blt::gp
// need to generate replacement. // need to generate replacement.
if (index < current_func_info.argument_types.size() && val.id != current_func_info.argument_types[index].id) if (index < current_func_info.argument_types.size() && val.id != current_func_info.argument_types[index].id)
{ {
BLT_TRACE_STREAM << "Replacing tree argument from type " BLT_TRACE_STREAM << "Replacing tree argument (index: " << index << " proper: " << (children_data.size() - 1 - index)
<< ") from type "
<< program.get_typesystem().get_type(current_func_info.argument_types[index]).name() << " to type " << program.get_typesystem().get_type(current_func_info.argument_types[index]).name() << " to type "
<< program.get_typesystem().get_type(val).name() << "\n"; << program.get_typesystem().get_type(val).name() << "\n";
// TODO: new config? // TODO: new config?
@ -137,7 +144,7 @@ namespace blt::gp
blt::size_t total_bytes_after = 0; blt::size_t total_bytes_after = 0;
blt::size_t total_bytes_for = 0; blt::size_t total_bytes_for = 0;
auto& child = children_data[index]; auto& child = children_data[children_data.size() - 1 - index];
for (blt::ptrdiff_t i = child.start; i < child.end; i++) for (blt::ptrdiff_t i = child.start; i < child.end; i++)
{ {
if (ops[i].is_value) if (ops[i].is_value)
@ -148,7 +155,7 @@ namespace blt::gp
if (ops[i].is_value) if (ops[i].is_value)
total_bytes_after += stack_allocator::aligned_size(ops[i].type_size); total_bytes_after += stack_allocator::aligned_size(ops[i].type_size);
} }
BLT_TRACE("Size for %ld size after: %ld", total_bytes_for, total_bytes_after); BLT_TRACE("Removing bytes %ld, bytes after that must stay: %ld", total_bytes_for, total_bytes_after);
auto after_ptr = get_thread_pointer_for_size<struct mutation_func>(total_bytes_after); auto after_ptr = get_thread_pointer_for_size<struct mutation_func>(total_bytes_after);
vals.copy_to(after_ptr, total_bytes_after); vals.copy_to(after_ptr, total_bytes_after);
@ -170,14 +177,20 @@ namespace blt::gp
ops.insert(ops.begin() + child.start, tree.get_operations().begin(), tree.get_operations().end()); ops.insert(ops.begin() + child.start, tree.get_operations().begin(), tree.get_operations().end());
// shift over everybody after. // shift over everybody after.
for (auto& new_child : blt::iterate(children_data.begin() + static_cast<blt::ptrdiff_t>(index), children_data.end())) if (index > 0)
{
// don't need to update if the index is the last
for (auto& new_child : blt::iterate(children_data.end() - static_cast<blt::ptrdiff_t>(index),
children_data.end()))
{ {
// remove the old tree size, then add the new tree size to get the correct positions. // remove the old tree size, then add the new tree size to get the correct positions.
new_child.start = new_child.start =
new_child.start - (child.end - child.start) + static_cast<blt::ptrdiff_t>(tree.get_operations().size()); new_child.start - (child.end - child.start) +
static_cast<blt::ptrdiff_t>(tree.get_operations().size());
new_child.end = new_child.end =
new_child.end - (child.end - child.start) + static_cast<blt::ptrdiff_t>(tree.get_operations().size()); new_child.end - (child.end - child.start) + static_cast<blt::ptrdiff_t>(tree.get_operations().size());
} }
}
child.end = static_cast<blt::ptrdiff_t>(child.start + tree.get_operations().size()); child.end = static_cast<blt::ptrdiff_t>(child.start + tree.get_operations().size());
#if BLT_DEBUG_LEVEL >= 2 #if BLT_DEBUG_LEVEL >= 2
@ -198,12 +211,20 @@ namespace blt::gp
} }
} }
BLT_DEBUG("Current:");
for (const auto& [index, val] : blt::enumerate(current_func_info.argument_types))
BLT_DEBUG("%ld: %s", index, std::string(program.get_typesystem().get_type(val).name()).c_str());
BLT_DEBUG("Replacement:");
for (const auto& [index, val] : blt::enumerate(replacement_func_info.argument_types))
BLT_DEBUG("%ld: %s", index, std::string(program.get_typesystem().get_type(val).name()).c_str());
if (current_func_info.argc.argc > replacement_func_info.argc.argc) if (current_func_info.argc.argc > replacement_func_info.argc.argc)
{ {
BLT_TRACE("TOO MANY ARGS"); BLT_TRACE("TOO MANY ARGS");
// too many args // too many args
blt::size_t end_index = children_data.back().end; blt::size_t end_index = children_data[replacement_func_info.argc.argc - 1].end;
blt::size_t start_index = children_data[replacement_func_info.argc.argc].start; blt::size_t start_index = children_data.front().start;
blt::size_t total_bytes_for = 0; blt::size_t total_bytes_for = 0;
blt::size_t total_bytes_after = 0; blt::size_t total_bytes_after = 0;
for (blt::size_t i = start_index; i < end_index; i++) for (blt::size_t i = start_index; i < end_index; i++)
@ -232,20 +253,24 @@ namespace blt::gp
// not enough args // not enough args
blt::size_t total_bytes_after = 0; blt::size_t total_bytes_after = 0;
blt::size_t start_index = c_node + 1; blt::size_t start_index = c_node + 1;
if (current_func_info.argc.argc != 0) blt::size_t insert_index = c_node + 1;
start_index = children_data.back().end; //if (current_func_info.argc.argc != 0)
// start_index = children_data.back().end;
for (blt::size_t i = start_index; i < ops.size(); i++) for (blt::size_t i = start_index; i < ops.size(); i++)
{ {
if (ops[i].is_value) if (ops[i].is_value)
total_bytes_after += stack_allocator::aligned_size(ops[i].type_size); total_bytes_after += stack_allocator::aligned_size(ops[i].type_size);
} }
BLT_TRACE("Total bytes after: %ld", total_bytes_after);
auto* data = get_thread_pointer_for_size<struct mutation_func>(total_bytes_after); auto* data = get_thread_pointer_for_size<struct mutation_func>(total_bytes_after);
vals.copy_to(data, total_bytes_after); vals.copy_to(data, total_bytes_after);
vals.pop_bytes(static_cast<blt::ptrdiff_t>(total_bytes_after)); vals.pop_bytes(static_cast<blt::ptrdiff_t>(total_bytes_after));
for (blt::size_t i = current_func_info.argc.argc; i < replacement_func_info.argc.argc; i++) for (blt::ptrdiff_t i = static_cast<blt::ptrdiff_t>(replacement_func_info.argc.argc) - 1;
i >= current_func_info.argc.argc; i--)
{ {
BLT_TRACE("Generating argument %ld", i); BLT_TRACE("Generating argument %ld of type %s", i,
std::string(program.get_typesystem().get_type(replacement_func_info.argument_types[i].id).name()).c_str());
auto tree = config.generator.get().generate( auto tree = config.generator.get().generate(
{program, replacement_func_info.argument_types[i].id, config.replacement_min_depth, {program, replacement_func_info.argument_types[i].id, config.replacement_min_depth,
config.replacement_max_depth}); config.replacement_max_depth});
@ -256,9 +281,13 @@ namespace blt::gp
total_bytes_for += stack_allocator::aligned_size(op.type_size); total_bytes_for += stack_allocator::aligned_size(op.type_size);
} }
vals.copy_from(tree.get_values(), total_bytes_for); vals.copy_from(tree.get_values(), total_bytes_for);
BLT_TRACE("%ld vs %ld", start_index, ops.size());
if (start_index < ops.size())
BLT_TRACE("Argument %ld insert before this: %s", i, std::string(*program.get_name(ops[start_index].id)).c_str());
ops.insert(ops.begin() + static_cast<blt::ptrdiff_t>(start_index), tree.get_operations().begin(), ops.insert(ops.begin() + static_cast<blt::ptrdiff_t>(start_index), tree.get_operations().begin(),
tree.get_operations().end()); tree.get_operations().end());
start_index += tree.get_operations().size(); start_index += tree.get_operations().size();
insert_index += tree.get_operations().size();
} }
vals.copy_from(data, total_bytes_after); vals.copy_from(data, total_bytes_after);
} }
@ -314,6 +343,48 @@ namespace blt::gp
BLT_ABORT("This type size doesn't exist!"); BLT_ABORT("This type size doesn't exist!");
} }
} }
#if BLT_DEBUG_LEVEL >= 2
blt::size_t bytes_expected = 0;
auto bytes_size = vals.size().total_used_bytes;
for (const auto& op : c.get_operations())
{
if (op.is_value)
bytes_expected += stack_allocator::aligned_size(op.type_size);
}
if (bytes_expected != bytes_size)
{
BLT_WARN_STREAM << "Stack state: " << vals.size() << "\n";
BLT_WARN("Child tree bytes %ld vs expected %ld, difference: %ld", bytes_size, bytes_expected,
static_cast<blt::ptrdiff_t>(bytes_expected) - static_cast<blt::ptrdiff_t>(bytes_size));
std::cout << "Parent: " << std::endl;
c_copy.print(program, std::cout, false, true);
std::cout << "Child:" << std::endl;
c.print(program, std::cout, false, true);
std::cout << std::endl;
c.print(program, std::cout, true, true);
std::cout << std::endl;
BLT_ABORT("Amount of bytes in stack doesn't match the number of bytes expected for the operations");
}
auto copy = c;
try
{
auto result = copy.evaluate(nullptr);
blt::black_box(result);
} catch (const std::exception& e)
{
std::cout << "Parent: " << std::endl;
c_copy.print(program, std::cout, false, true);
std::cout << "Child:" << std::endl;
c.print(program, std::cout, false, true);
std::cout << std::endl;
c.print(program, std::cout, true, true);
std::cout << std::endl;
BLT_WARN(e.what());
throw e;
}
#endif
} }
break; break;
case mutation_operator::SUB_FUNC: case mutation_operator::SUB_FUNC:
@ -347,6 +418,13 @@ namespace blt::gp
BLT_WARN_STREAM << "Stack state: " << vals.size() << "\n"; BLT_WARN_STREAM << "Stack state: " << vals.size() << "\n";
BLT_WARN("Child tree bytes %ld vs expected %ld, difference: %ld", bytes_size, bytes_expected, BLT_WARN("Child tree bytes %ld vs expected %ld, difference: %ld", bytes_size, bytes_expected,
static_cast<blt::ptrdiff_t>(bytes_expected) - static_cast<blt::ptrdiff_t>(bytes_size)); static_cast<blt::ptrdiff_t>(bytes_expected) - static_cast<blt::ptrdiff_t>(bytes_size));
std::cout << "Parent: " << std::endl;
p.print(program, std::cout, false, true);
std::cout << "Child:" << std::endl;
c.print(program, std::cout, false, true);
std::cout << std::endl;
c.print(program, std::cout, true, true);
std::cout << std::endl;
BLT_ABORT("Amount of bytes in stack doesn't match the number of bytes expected for the operations"); BLT_ABORT("Amount of bytes in stack doesn't match the number of bytes expected for the operations");
} }
auto copy = c; auto copy = c;