crossover
parent
1fa12da477
commit
d3124f8516
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.25)
|
cmake_minimum_required(VERSION 3.25)
|
||||||
project(blt-gp VERSION 0.0.42)
|
project(blt-gp VERSION 0.0.43)
|
||||||
|
|
||||||
include(CTest)
|
include(CTest)
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,11 @@ namespace blt::gp
|
||||||
blt::size_t argc_context = 0;
|
blt::size_t argc_context = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct operator_info
|
||||||
|
{
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
struct operator_storage
|
struct operator_storage
|
||||||
{
|
{
|
||||||
// indexed from return TYPE ID, returns index of operator
|
// indexed from return TYPE ID, returns index of operator
|
||||||
|
|
|
@ -21,11 +21,13 @@
|
||||||
|
|
||||||
#include <blt/std/types.h>
|
#include <blt/std/types.h>
|
||||||
#include <blt/std/logging.h>
|
#include <blt/std/logging.h>
|
||||||
|
#include <blt/gp/fwdecl.h>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
namespace blt::gp
|
namespace blt::gp
|
||||||
{
|
{
|
||||||
|
@ -52,7 +54,8 @@ namespace blt::gp
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T pop()
|
T pop()
|
||||||
{
|
{
|
||||||
static_assert(std::is_trivially_copyable_v<std::remove_reference_t<T>> && "Type must be bitwise copyable!");
|
using NO_REF_T = std::remove_reference_t<T>;
|
||||||
|
static_assert(std::is_trivially_copyable_v<NO_REF_T> && "Type must be bitwise copyable!");
|
||||||
constexpr static auto TYPE_SIZE = aligned_size<T>();
|
constexpr static auto TYPE_SIZE = aligned_size<T>();
|
||||||
if (head == nullptr)
|
if (head == nullptr)
|
||||||
throw std::runtime_error("Silly boi the stack is empty!");
|
throw std::runtime_error("Silly boi the stack is empty!");
|
||||||
|
@ -116,7 +119,8 @@ namespace blt::gp
|
||||||
free(old);
|
free(old);
|
||||||
if (diff == 0)
|
if (diff == 0)
|
||||||
break;
|
break;
|
||||||
} else {
|
} else
|
||||||
|
{
|
||||||
// otherwise update the offset pointer
|
// otherwise update the offset pointer
|
||||||
head->metadata.offset -= bytes;
|
head->metadata.offset -= bytes;
|
||||||
break;
|
break;
|
||||||
|
@ -185,8 +189,7 @@ namespace blt::gp
|
||||||
|
|
||||||
stack_allocator& operator=(stack_allocator&& move) noexcept
|
stack_allocator& operator=(stack_allocator&& move) noexcept
|
||||||
{
|
{
|
||||||
head = move.head;
|
move.head = std::exchange(head, move.head);
|
||||||
move.head = nullptr;
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include <blt/std/utility.h>
|
#include <blt/std/utility.h>
|
||||||
#include <blt/gp/fwdecl.h>
|
#include <blt/gp/fwdecl.h>
|
||||||
#include <blt/gp/tree.h>
|
#include <blt/gp/tree.h>
|
||||||
|
#include <blt/std/expected.h>
|
||||||
|
|
||||||
namespace blt::gp
|
namespace blt::gp
|
||||||
{
|
{
|
||||||
|
@ -29,8 +30,25 @@ namespace blt::gp
|
||||||
class crossover_t
|
class crossover_t
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BLT_ATTRIB_CONST virtual std::pair<tree_t, tree_t> apply(gp_program& program, const tree_t& p1, const tree_t& p2);
|
enum class error_t
|
||||||
virtual void apply_in_place(gp_program& program, tree_t& p1, tree_t& p2);
|
{
|
||||||
|
NO_VALID_TYPE
|
||||||
|
};
|
||||||
|
struct result_t
|
||||||
|
{
|
||||||
|
tree_t child1;
|
||||||
|
tree_t child2;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* child1 and child2 are copies of the parents, the result of selecting a crossover point and performing standard subtree crossover.
|
||||||
|
* the parents are not modified during this process
|
||||||
|
* @param program reference to the global program container responsible for managing these trees
|
||||||
|
* @param p1 reference to the first parent
|
||||||
|
* @param p2 reference to the second parent
|
||||||
|
* @return expected pair of child otherwise returns error enum
|
||||||
|
*/
|
||||||
|
virtual blt::expected<result_t, error_t> apply(gp_program& program, const tree_t& p1, const tree_t& p2); // NOLINT
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,6 @@ namespace blt::gp
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] inline std::vector<op_container_t>& get_operations()
|
[[nodiscard]] inline std::vector<op_container_t>& get_operations()
|
||||||
{
|
{
|
||||||
valid = false;
|
|
||||||
return operations;
|
return operations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,20 +70,6 @@ namespace blt::gp
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDepth(blt::size_t d)
|
|
||||||
{
|
|
||||||
depth = d;
|
|
||||||
valid = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
blt::size_t getDepth()
|
|
||||||
{
|
|
||||||
if (valid)
|
|
||||||
return depth;
|
|
||||||
valid = true;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
evaluation_context evaluate(void* context);
|
evaluation_context evaluate(void* context);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -116,7 +101,6 @@ namespace blt::gp
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool valid = false;
|
|
||||||
std::vector<op_container_t> operations;
|
std::vector<op_container_t> operations;
|
||||||
blt::gp::stack_allocator values;
|
blt::gp::stack_allocator values;
|
||||||
blt::size_t depth;
|
blt::size_t depth;
|
||||||
|
|
2
lib/blt
2
lib/blt
|
@ -1 +1 @@
|
||||||
Subproject commit cdb91d800781d2c2a8ad3b9a829ca6d52abaa6ea
|
Subproject commit f3451b57ab249fdcf5ccedd64cbd56b2a64e2de2
|
|
@ -59,7 +59,6 @@ namespace blt::gp
|
||||||
{
|
{
|
||||||
auto top = tree_generator.top();
|
auto top = tree_generator.top();
|
||||||
tree_generator.pop();
|
tree_generator.pop();
|
||||||
//BLT_INFO("%ld D: %ld (%ld left)", top.id, top.depth, tree_generator.size());
|
|
||||||
|
|
||||||
tree.get_operations().emplace_back(
|
tree.get_operations().emplace_back(
|
||||||
args.program.get_operation(top.id),
|
args.program.get_operation(top.id),
|
||||||
|
@ -74,13 +73,9 @@ namespace blt::gp
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& child : args.program.get_argument_types(top.id))
|
for (const auto& child : args.program.get_argument_types(top.id))
|
||||||
{
|
|
||||||
std::forward<Func>(perChild)(args.program, tree_generator, child, top.depth + 1);
|
std::forward<Func>(perChild)(args.program, tree_generator, child, top.depth + 1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tree.setDepth(max_depth);
|
|
||||||
|
|
||||||
return tree;
|
return tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,5 +19,12 @@
|
||||||
|
|
||||||
namespace blt::gp
|
namespace blt::gp
|
||||||
{
|
{
|
||||||
|
blt::expected<crossover_t::result_t, crossover_t::error_t> crossover_t::apply(gp_program& program, const tree_t& p1, const tree_t& p2) // NOLINT
|
||||||
|
{
|
||||||
|
result_t result{p1, p2};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue