remove bitmask, fix the other tests

shared
Brett 2024-08-21 20:40:42 -04:00
parent 46ceaf49dd
commit 7aaad70132
18 changed files with 77 additions and 276 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.18) project(blt-gp VERSION 0.1.19)
include(CTest) include(CTest)

View File

@ -39,7 +39,7 @@ blt::gp::prog_config_t config = blt::gp::prog_config_t()
.set_mutation_chance(0.1) .set_mutation_chance(0.1)
.set_reproduction_chance(0) .set_reproduction_chance(0)
.set_max_generations(50) .set_max_generations(50)
.set_pop_size(50000) .set_pop_size(5000)
.set_thread_count(0); .set_thread_count(0);
blt::gp::type_provider type_system; blt::gp::type_provider type_system;
@ -66,7 +66,7 @@ constexpr auto fitness_function = [](blt::gp::tree_t& current_tree, blt::gp::fit
constexpr double value_cutoff = 1.e15; constexpr double value_cutoff = 1.e15;
for (auto& fitness_case : fitness_cases) for (auto& fitness_case : fitness_cases)
{ {
auto diff = std::abs(fitness_case.y - current_tree.get_evaluation_value<float>(&fitness_case, program.get_eval_func())); auto diff = std::abs(fitness_case.y - current_tree.get_evaluation_value<float>(&fitness_case));
if (diff < value_cutoff) if (diff < value_cutoff)
{ {
fitness.raw_fitness += diff; fitness.raw_fitness += diff;

View File

@ -52,9 +52,6 @@ namespace blt::gp
namespace detail namespace detail
{ {
// requires operator[](bit_index), push_back, clear
using bitmask_t = std::vector<bool>;
class operator_storage_test; class operator_storage_test;
// context*, read stack, write stack // context*, read stack, write stack
using operator_func_t = std::function<void(void*, stack_allocator&, stack_allocator&)>; using operator_func_t = std::function<void(void*, stack_allocator&, stack_allocator&)>;
@ -68,7 +65,7 @@ namespace blt::gp
RETURN RETURN
}; };
using destroy_func_t = std::function<void(destroy_t, bitmask_t* mask, stack_allocator&)>; using destroy_func_t = std::function<void(destroy_t, stack_allocator&)>;
} }
} }

View File

@ -102,16 +102,16 @@ namespace blt::gp
} }
template<typename context = void, typename... NoCtxArgs> template<typename context = void, typename... NoCtxArgs>
void call_destructors_without_first(stack_allocator& read_allocator, detail::bitmask_t* mask) void call_destructors_without_first(stack_allocator& read_allocator)
{ {
if constexpr (sizeof...(NoCtxArgs) > 0) if constexpr (sizeof...(NoCtxArgs) > 0)
{ {
read_allocator.call_destructors<detail::remove_cv_ref<NoCtxArgs>...>(mask); read_allocator.call_destructors<detail::remove_cv_ref<NoCtxArgs>...>();
} }
} }
template<typename Func, typename... ExtraArgs> template<typename Func, typename... ExtraArgs>
Return operator()(bool has_context, detail::bitmask_t* mask, Func&& func, stack_allocator& read_allocator, ExtraArgs&& ... args) Return operator()(bool has_context, Func&& func, stack_allocator& read_allocator, ExtraArgs&& ... args)
{ {
constexpr auto seq = std::make_integer_sequence<blt::u64, sizeof...(Args)>(); constexpr auto seq = std::make_integer_sequence<blt::u64, sizeof...(Args)>();
#if BLT_DEBUG_LEVEL > 0 #if BLT_DEBUG_LEVEL > 0
@ -120,9 +120,9 @@ namespace blt::gp
#endif #endif
Return ret = exec_sequence_to_indices(std::forward<Func>(func), read_allocator, seq, std::forward<ExtraArgs>(args)...); Return ret = exec_sequence_to_indices(std::forward<Func>(func), read_allocator, seq, std::forward<ExtraArgs>(args)...);
if (has_context) if (has_context)
call_destructors_without_first<Args...>(read_allocator, mask); call_destructors_without_first<Args...>(read_allocator);
else else
read_allocator.call_destructors<detail::remove_cv_ref<Args>...>(mask); read_allocator.call_destructors<detail::remove_cv_ref<Args>...>();
read_allocator.pop_bytes((stack_allocator::aligned_size<detail::remove_cv_ref<Args>>() + ...)); read_allocator.pop_bytes((stack_allocator::aligned_size<detail::remove_cv_ref<Args>>() + ...));
return ret; return ret;
#if BLT_DEBUG_LEVEL > 0 #if BLT_DEBUG_LEVEL > 0
@ -159,18 +159,18 @@ namespace blt::gp
constexpr explicit operation_t(const Functor& functor, std::optional<std::string_view> name = {}): func(functor), name(name) constexpr explicit operation_t(const Functor& functor, std::optional<std::string_view> name = {}): func(functor), name(name)
{} {}
[[nodiscard]] constexpr inline Return operator()(stack_allocator& read_allocator, detail::bitmask_t* mask) const [[nodiscard]] constexpr inline Return operator()(stack_allocator& read_allocator) const
{ {
if constexpr (sizeof...(Args) == 0) if constexpr (sizeof...(Args) == 0)
{ {
return func(); return func();
} else } else
{ {
return call_with<Return, Args...>()(false, mask, func, read_allocator); return call_with<Return, Args...>()(false, func, read_allocator);
} }
} }
[[nodiscard]] constexpr inline Return operator()(void* context, stack_allocator& read_allocator, detail::bitmask_t* mask) const [[nodiscard]] constexpr inline Return operator()(void* context, stack_allocator& read_allocator) const
{ {
// should be an impossible state // should be an impossible state
if constexpr (sizeof...(Args) == 0) if constexpr (sizeof...(Args) == 0)
@ -183,7 +183,7 @@ namespace blt::gp
return func(ctx_ref); return func(ctx_ref);
} else } else
{ {
return call_without_first<Return, Args...>()(true, mask, func, read_allocator, ctx_ref); return call_without_first<Return, Args...>()(true, func, read_allocator, ctx_ref);
} }
} }
@ -194,11 +194,11 @@ namespace blt::gp
if constexpr (detail::is_same_v<Context, detail::remove_cv_ref<typename detail::first_arg<Args...>::type>>) if constexpr (detail::is_same_v<Context, detail::remove_cv_ref<typename detail::first_arg<Args...>::type>>)
{ {
// first arg is context // first arg is context
write_allocator.push(this->operator()(context, read_allocator, nullptr)); write_allocator.push(this->operator()(context, read_allocator));
} else } else
{ {
// first arg isn't context // first arg isn't context
write_allocator.push(this->operator()(read_allocator, nullptr)); write_allocator.push(this->operator()(read_allocator));
} }
}; };
} }

View File

@ -122,8 +122,6 @@ namespace blt::gp
evaluation_context results{}; evaluation_context results{};
results.values.reserve(largest); results.values.reserve(largest);
static thread_local detail::bitmask_t bitfield;
bitfield.clear();
blt::size_t total_so_far = 0; blt::size_t total_so_far = 0;
for (const auto& operation : blt::reverse_iterate(ops.begin(), ops.end())) for (const auto& operation : blt::reverse_iterate(ops.begin(), ops.end()))
@ -132,11 +130,9 @@ namespace blt::gp
{ {
total_so_far += stack_allocator::aligned_size(operation.type_size); total_so_far += stack_allocator::aligned_size(operation.type_size);
results.values.copy_from(vals.from(total_so_far), stack_allocator::aligned_size(operation.type_size)); results.values.copy_from(vals.from(total_so_far), stack_allocator::aligned_size(operation.type_size));
bitfield.push_back(false);
continue; continue;
} }
call_jmp_table(operation.id, context, results.values, results.values, &bitfield, operators...); call_jmp_table(operation.id, context, results.values, results.values, operators...);
bitfield.push_back(true);
} }
return results; return results;
@ -248,11 +244,11 @@ namespace blt::gp
out << "[Printing Value on '" << (op.get_name() ? *op.get_name() : "") << "' Not Supported!]"; out << "[Printing Value on '" << (op.get_name() ? *op.get_name() : "") << "' Not Supported!]";
} }
}); });
storage.destroy_funcs.push_back([](detail::destroy_t type, detail::bitmask_t* mask, stack_allocator& alloc) { storage.destroy_funcs.push_back([](detail::destroy_t type, stack_allocator& alloc) {
switch (type) switch (type)
{ {
case detail::destroy_t::ARGS: case detail::destroy_t::ARGS:
alloc.call_destructors<Args...>(mask); alloc.call_destructors<Args...>();
break; break;
case detail::destroy_t::RETURN: case detail::destroy_t::RETURN:
if constexpr (detail::has_func_drop_v<remove_cvref_t<Return>>) if constexpr (detail::has_func_drop_v<remove_cvref_t<Return>>)
@ -277,50 +273,45 @@ namespace blt::gp
} }
} }
template<typename Lambda> template<typename Operator>
static inline void execute(void* context, stack_allocator& write_stack, stack_allocator& read_stack, detail::bitmask_t* mask, static inline void execute(void* context, stack_allocator& write_stack, stack_allocator& read_stack, Operator& operation)
Lambda& lambda)
{ {
if constexpr (std::is_same_v<detail::remove_cv_ref<typename Lambda::First_Arg>, Context>) if constexpr (std::is_same_v<detail::remove_cv_ref<typename Operator::First_Arg>, Context>)
{ {
write_stack.push(lambda(context, read_stack, mask)); write_stack.push(operation(context, read_stack));
} else } else
{ {
write_stack.push(lambda(read_stack, mask)); write_stack.push(operation(read_stack));
} }
} }
template<blt::size_t id, typename Lambda> template<blt::size_t id, typename Operator>
static inline bool call(blt::size_t op, void* context, stack_allocator& write_stack, stack_allocator& read_stack, detail::bitmask_t* mask, static inline bool call(blt::size_t op, void* context, stack_allocator& write_stack, stack_allocator& read_stack, Operator& operation)
Lambda& lambda)
{ {
if (id == op) if (id == op)
{ {
execute(context, write_stack, read_stack, mask, lambda); execute(context, write_stack, read_stack, operation);
return false; return false;
} }
return true; return true;
} }
template<typename... Lambdas, size_t... operator_ids> template<typename... Operators, size_t... operator_ids>
static inline void call_jmp_table_internal(size_t op, void* context, stack_allocator& write_stack, stack_allocator& read_stack, static inline void call_jmp_table_internal(size_t op, void* context, stack_allocator& write_stack, stack_allocator& read_stack,
detail::bitmask_t* mask, std::integer_sequence<size_t, operator_ids...>, Lambdas& ... lambdas) std::integer_sequence<size_t, operator_ids...>, Operators&... operators)
{ {
if (op >= sizeof...(operator_ids)) if (op >= sizeof...(operator_ids))
{ {
BLT_UNREACHABLE; BLT_UNREACHABLE;
} }
(call<operator_ids>(op, context, write_stack, read_stack, mask, lambdas) && ...); (call<operator_ids>(op, context, write_stack, read_stack, operators) && ...);
// std::initializer_list<int>{((op == operator_ids) ? (execute(context, write_stack, read_stack, mask, lambdas), 0) : 0)...};
} }
template<typename... Lambdas> template<typename... Operators>
static inline void call_jmp_table(size_t op, void* context, stack_allocator& write_stack, stack_allocator& read_stack, static inline void call_jmp_table(size_t op, void* context, stack_allocator& write_stack, stack_allocator& read_stack,
detail::bitmask_t* mask, Lambdas& ... lambdas) Operators& ... operators)
{ {
call_jmp_table_internal(op, context, write_stack, read_stack, mask, std::index_sequence_for<Lambdas...>(), call_jmp_table_internal(op, context, write_stack, read_stack, std::index_sequence_for<Operators...>(), operators...);
lambdas...);
} }
type_provider& system; type_provider& system;
@ -542,8 +533,6 @@ namespace blt::gp
void reset_program(type_id root_type, bool eval_fitness_now = true) void reset_program(type_id root_type, bool eval_fitness_now = true)
{ {
current_generation = 0; current_generation = 0;
for (auto& pop : current_pop)
pop.tree.drop(*this);
current_pop = config.pop_initializer.get().generate( current_pop = config.pop_initializer.get().generate(
{*this, root_type, config.population_size, config.initial_min_tree_size, config.initial_max_tree_size}); {*this, root_type, config.population_size, config.initial_min_tree_size, config.initial_max_tree_size});
if (eval_fitness_now) if (eval_fitness_now)
@ -552,7 +541,6 @@ namespace blt::gp
void next_generation() void next_generation()
{ {
current_pop.drop(*this);
current_pop = std::move(next_pop); current_pop = std::move(next_pop);
current_generation++; current_generation++;
} }
@ -702,7 +690,6 @@ namespace blt::gp
~gp_program() ~gp_program()
{ {
current_pop.drop(*this);
thread_helper.lifetime_over = true; thread_helper.lifetime_over = true;
thread_helper.barrier.notify_all(); thread_helper.barrier.notify_all();
thread_helper.thread_function_condition.notify_all(); thread_helper.thread_function_condition.notify_all();

View File

@ -227,22 +227,13 @@ namespace blt::gp
} }
template<typename... Args> template<typename... Args>
void call_destructors(detail::bitmask_t* mask) void call_destructors()
{ {
if constexpr (sizeof...(Args) > 0) if constexpr (sizeof...(Args) > 0)
{ {
blt::size_t offset = (stack_allocator::aligned_size(sizeof(NO_REF_T<Args>)) + ...) - blt::size_t offset = (stack_allocator::aligned_size(sizeof(NO_REF_T<Args>)) + ...) -
stack_allocator::aligned_size(sizeof(NO_REF_T<typename blt::meta::arg_helper<Args...>::First>)); stack_allocator::aligned_size(sizeof(NO_REF_T<typename blt::meta::arg_helper<Args...>::First>));
blt::size_t index = 0; ((call_drop<Args>(offset), offset -= stack_allocator::aligned_size(sizeof(NO_REF_T<Args>))), ...);
if (mask != nullptr)
index = mask->size() - sizeof...(Args);
((call_drop<Args>(offset, index, mask), offset -= stack_allocator::aligned_size(sizeof(NO_REF_T<Args>)), ++index), ...);
if (mask != nullptr)
{
auto& mask_r = *mask;
for (blt::size_t i = 0; i < sizeof...(Args); i++)
mask_r.pop_back();
}
} }
} }
@ -282,11 +273,9 @@ namespace blt::gp
void expand(blt::size_t bytes) void expand(blt::size_t bytes)
{ {
bytes = to_nearest_page_size(bytes); bytes = to_nearest_page_size(bytes);
// auto new_data = static_cast<blt::u8*>(std::malloc(bytes));
auto new_data = static_cast<blt::u8*>(get_allocator().allocate(bytes)); auto new_data = static_cast<blt::u8*>(get_allocator().allocate(bytes));
if (bytes_stored > 0) if (bytes_stored > 0)
std::memcpy(new_data, data_, bytes_stored); std::memcpy(new_data, data_, bytes_stored);
// std::free(data_);
get_allocator().deallocate(data_, size_); get_allocator().deallocate(data_, size_);
data_ = new_data; data_ = new_data;
size_ = bytes; size_ = bytes;
@ -323,16 +312,10 @@ namespace blt::gp
} }
template<typename T> template<typename T>
inline void call_drop(blt::size_t offset, blt::size_t index, detail::bitmask_t* mask) inline void call_drop(blt::size_t offset)
{ {
if constexpr (detail::has_func_drop_v<T>) if constexpr (detail::has_func_drop_v<T>)
{ {
if (mask != nullptr)
{
auto& mask_r = *mask;
if (!mask_r[index])
return;
}
from<NO_REF_T<T >>(offset).drop(); from<NO_REF_T<T >>(offset).drop();
} }
} }

View File

@ -55,6 +55,8 @@ namespace blt::gp
{ {
using iter_type = std::vector<op_container_t>::const_iterator; using iter_type = std::vector<op_container_t>::const_iterator;
public: public:
explicit tree_t(gp_program& program);
struct child_t struct child_t
{ {
blt::ptrdiff_t start; blt::ptrdiff_t start;
@ -82,7 +84,10 @@ namespace blt::gp
return values; return values;
} }
evaluation_context evaluate(void* context, detail::eval_func_t& func) const; evaluation_context evaluate(void* context) const
{
return (*func)(*this, context);
}
blt::size_t get_depth(gp_program& program); blt::size_t get_depth(gp_program& program);
@ -108,9 +113,9 @@ namespace blt::gp
* Helper template for returning the result of evaluation (this calls it) * Helper template for returning the result of evaluation (this calls it)
*/ */
template<typename T> template<typename T>
T get_evaluation_value(void* context, detail::eval_func_t& func) T get_evaluation_value(void* context)
{ {
auto results = evaluate(context, func); auto results = evaluate(context);
return results.values.pop<T>(); return results.values.pop<T>();
} }
@ -120,7 +125,9 @@ namespace blt::gp
bool check(gp_program& program, void* context) const; bool check(gp_program& program, void* context) const;
void find_child_extends(gp_program& program, std::vector<child_t>& vec, blt::size_t parent_node, blt::size_t argc) const; void find_child_extends(gp_program& program, std::vector<child_t>& vec, blt::size_t parent_node, blt::size_t argc) const;
blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, blt::ptrdiff_t start) const; blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, blt::ptrdiff_t start) const;
blt::ptrdiff_t find_parent(blt::gp::gp_program& program, blt::ptrdiff_t start) const; blt::ptrdiff_t find_parent(blt::gp::gp_program& program, blt::ptrdiff_t start) const;
// valid for [begin, end) // valid for [begin, end)
@ -151,11 +158,10 @@ namespace blt::gp
return total_value_bytes(operations.begin(), operations.end()); return total_value_bytes(operations.begin(), operations.end());
} }
void drop(gp_program& program);
private: private:
std::vector<op_container_t> operations; std::vector<op_container_t> operations;
blt::gp::stack_allocator values; blt::gp::stack_allocator values;
std::atomic_int64_t* reference_counter; detail::eval_func_t* func;
}; };
struct fitness_t struct fitness_t
@ -306,7 +312,6 @@ namespace blt::gp
population_t& operator=(population_t&&) = default; population_t& operator=(population_t&&) = default;
void drop(gp_program& program);
private: private:
std::vector<individual> individuals; std::vector<individual> individuals;
}; };

View File

@ -53,7 +53,7 @@ namespace blt::gp
{ {
std::stack<stack> tree_generator = get_initial_stack(args.program, args.root_type); std::stack<stack> tree_generator = get_initial_stack(args.program, args.root_type);
blt::size_t max_depth = 0; blt::size_t max_depth = 0;
tree_t tree; tree_t tree{args.program};
while (!tree_generator.empty()) while (!tree_generator.empty())
{ {

View File

@ -37,45 +37,6 @@ namespace blt::gp
return buffer; return buffer;
} }
evaluation_context tree_t::evaluate(void* context, detail::eval_func_t& func) const
{
#if BLT_DEBUG_LEVEL >= 2
blt::size_t expected_bytes = 0;
blt::size_t found_bytes = values.size().total_used_bytes;
for (const auto& op : operations)
{
if (op.is_value)
expected_bytes += stack_allocator::aligned_size(op.type_size);
}
if (expected_bytes != found_bytes)
{
BLT_WARN("Bytes found %ld vs bytes expected %ld", found_bytes, expected_bytes);
BLT_ABORT("Amount of bytes in stack doesn't match the number of bytes expected for the operations");
}
#endif
// // copy the initial values
// evaluation_context results{};
//
// auto value_stack = values;
// auto& values_process = results.values;
// static thread_local detail::bitmask_t bitfield;
// bitfield.clear();
//
// for (const auto& operation : blt::reverse_iterate(operations.begin(), operations.end()))
// {
// if (operation.is_value)
// {
// value_stack.transfer_bytes(values_process, operation.type_size);
// bitfield.push_back(false);
// continue;
// }
// operation.func(context, values_process, values_process, &bitfield);
// bitfield.push_back(true);
// }
return func(*this, context);
}
std::ostream& create_indent(std::ostream& out, blt::size_t amount, bool pretty_print) std::ostream& create_indent(std::ostream& out, blt::size_t amount, bool pretty_print)
{ {
if (!pretty_print) if (!pretty_print)
@ -259,8 +220,6 @@ namespace blt::gp
bool tree_t::check(gp_program& program, void* context) const bool tree_t::check(gp_program& program, void* context) const
{ {
static thread_local detail::bitmask_t bitfield;
bitfield.clear();
blt::size_t bytes_expected = 0; blt::size_t bytes_expected = 0;
auto bytes_size = values.size().total_used_bytes; auto bytes_size = values.size().total_used_bytes;
@ -318,40 +277,6 @@ namespace blt::gp
return true; return true;
} }
void tree_t::drop(gp_program& program)
{
return;
if (values.empty())
return;
//std::cout << "---- NEW TREE ---- References " << *reference_counter << " ----" << std::endl;
if (reference_counter->load() > 1)
return;
static blt::hashset_t<blt::size_t> sets;
while (!operations.empty())
{
auto operation = operations.back();
if (operation.is_value)
{
struct hello
{
float* f;
blt::size_t i;
};
//auto h = values.from<hello>(0);
/*if (sets.find(h.i) != sets.end())
std::cout << "HEY ASSHOLE Duplicate Value " << h.i << std::endl;
else
{
std::cout << "Destroying Value " << h.i << std::endl;
sets.insert(h.i);
}*/
program.get_destroy_func(operation.id)(detail::destroy_t::RETURN, nullptr, values);
values.pop_bytes(static_cast<blt::ptrdiff_t>(stack_allocator::aligned_size(operation.type_size)));
}
operations.pop_back();
}
}
void tree_t::find_child_extends(gp_program& program, std::vector<child_t>& vec, blt::size_t parent_node, blt::size_t argc) const void tree_t::find_child_extends(gp_program& program, std::vector<child_t>& vec, blt::size_t parent_node, blt::size_t argc) const
{ {
while (vec.size() < argc) while (vec.size() < argc)
@ -372,9 +297,8 @@ namespace blt::gp
} }
} }
void population_t::drop(gp_program& program) tree_t::tree_t(gp_program& program): func(&program.get_eval_func())
{ {
for (auto& pop : get_individuals())
pop.tree.drop(program);
} }
} }

View File

@ -58,13 +58,12 @@ blt::gp::gp_program program{type_system, SEED};
blt::gp::op_container_t make_container(blt::gp::operator_id id) blt::gp::op_container_t make_container(blt::gp::operator_id id)
{ {
auto& info = program.get_operator_info(id); auto& info = program.get_operator_info(id);
return {info.function, type_system.get_type(info.return_type).size(), id, false}; return {type_system.get_type(info.return_type).size(), id, false};
} }
blt::gp::op_container_t make_value(const blt::gp::type& id) blt::gp::op_container_t make_value(const blt::gp::type& id)
{ {
static blt::gp::detail::callable_t empty([](void*, blt::gp::stack_allocator&, blt::gp::stack_allocator&, blt::gp::detail::bitmask_t*) {}); return {id.size(), 0, true};
return {empty, id.size(), 0, true};
} }
blt::gp::operation_t add([](float a, float b) { blt::gp::operation_t add([](float a, float b) {
@ -105,7 +104,7 @@ blt::gp::operation_t large_literal([]() {
void basic_tree() void basic_tree()
{ {
BLT_INFO("Testing if we can get a basic tree going."); BLT_INFO("Testing if we can get a basic tree going.");
blt::gp::tree_t tree; blt::gp::tree_t tree{program};
tree.get_operations().push_back(make_container(sub.id)); 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>()));
@ -120,7 +119,7 @@ void basic_tree()
void large_cross_type_tree() void large_cross_type_tree()
{ {
blt::gp::tree_t tree; blt::gp::tree_t tree{program};
auto& ops = tree.get_operations(); auto& ops = tree.get_operations();
auto& vals = tree.get_values(); auto& vals = tree.get_values();
@ -149,16 +148,7 @@ int main()
type_system.register_type<large_18290>(); type_system.register_type<large_18290>();
blt::gp::operator_builder builder{type_system}; blt::gp::operator_builder builder{type_system};
program.set_operations(builder.build(f_literal, b_literal, add, basic_2t, sub, large_literal, cross_large_type));
builder.add_operator(f_literal); // 0
builder.add_operator(b_literal); // 1
builder.add_operator(add); // 2
builder.add_operator(basic_2t); // 3
builder.add_operator(sub); // 4
builder.add_operator(large_literal); // 5
builder.add_operator(cross_large_type); // 6
program.set_operations(builder.build());
basic_tree(); basic_tree();
large_cross_type_tree(); large_cross_type_tree();

View File

@ -424,7 +424,7 @@ int main()
return f + g; return f + g;
}); });
std::cout << silly_op(alloc, nullptr) << std::endl; std::cout << silly_op(alloc) << std::endl;
std::cout << "Is empty? " << alloc.empty() << std::endl; std::cout << "Is empty? " << alloc.empty() << std::endl;
@ -479,7 +479,7 @@ int main()
//blt::span<void*, 3> spv{arr}; //blt::span<void*, 3> spv{arr};
std::cout << silly_op.operator()(alloc, nullptr) << std::endl; std::cout << silly_op.operator()(alloc) << std::endl;
std::cout << "Hello World!" << std::endl; std::cout << "Hello World!" << std::endl;

View File

@ -62,7 +62,7 @@ int main()
blt::gp::grow_generator_t grow; blt::gp::grow_generator_t grow;
auto tree = grow.generate(blt::gp::generator_arguments{program, type_system.get_type<float>().id(), 3, 7}); auto tree = grow.generate(blt::gp::generator_arguments{program, type_system.get_type<float>().id(), 3, 7});
auto value = tree.get_evaluation_value<float>(nullptr, program.get_eval_func()); auto value = tree.get_evaluation_value<float>(nullptr);
BLT_TRACE(value); BLT_TRACE(value);

View File

@ -40,11 +40,11 @@ blt::gp::operation_t op_or([](bool a, bool b) {return a || b; });
blt::gp::operation_t op_xor([](bool a, bool b) {return static_cast<bool>(a ^ b); }); blt::gp::operation_t op_xor([](bool a, bool b) {return static_cast<bool>(a ^ b); });
blt::gp::operation_t op_not([](bool b) {return !b; }); blt::gp::operation_t op_not([](bool b) {return !b; });
blt::gp::operation_t lit([]() { auto lit = blt::gp::operation_t([]() {
//static std::uniform_real_distribution<float> dist(-32000, 32000); //static std::uniform_real_distribution<float> dist(-32000, 32000);
// static std::uniform_real_distribution<float> dist(0.0f, 10.0f); // static std::uniform_real_distribution<float> dist(0.0f, 10.0f);
return program.get_random().get_float(0.0f, 10.f); return program.get_random().get_float(0.0f, 10.0f);
}); }).set_ephemeral();
/** /**
* This is a test using multiple types with blt::gp * This is a test using multiple types with blt::gp
@ -55,24 +55,7 @@ int main()
type_system.register_type<bool>(); type_system.register_type<bool>();
blt::gp::operator_builder silly{type_system}; blt::gp::operator_builder silly{type_system};
silly.add_operator(add); program.set_operations(silly.build(add, sub, mul, pro_div, op_if, eq_f, eq_b, lt, gt, op_and, op_or, op_xor, op_not, lit));
silly.add_operator(sub);
silly.add_operator(mul);
silly.add_operator(pro_div);
silly.add_operator(op_if);
silly.add_operator(eq_f);
silly.add_operator(eq_b);
silly.add_operator(lt);
silly.add_operator(gt);
silly.add_operator(op_and);
silly.add_operator(op_or);
silly.add_operator(op_xor);
silly.add_operator(op_not);
silly.add_operator(lit, true);
program.set_operations(silly.build());
blt::gp::grow_generator_t grow; blt::gp::grow_generator_t grow;
auto tree = grow.generate(blt::gp::generator_arguments{program, type_system.get_type<float>().id(), 3, 7}); auto tree = grow.generate(blt::gp::generator_arguments{program, type_system.get_type<float>().id(), 3, 7});

View File

@ -40,11 +40,11 @@ blt::gp::operation_t op_or([](bool a, bool b) { return a || b; });
blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast<bool>(a ^ b); }); blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast<bool>(a ^ b); });
blt::gp::operation_t op_not([](bool b) { return !b; }); blt::gp::operation_t op_not([](bool b) { return !b; });
blt::gp::operation_t lit([]() { auto lit = blt::gp::operation_t([]() {
//static std::uniform_real_distribution<float> dist(-32000, 32000); //static std::uniform_real_distribution<float> dist(-32000, 32000);
// static std::uniform_real_distribution<float> dist(0.0f, 10.0f); // static std::uniform_real_distribution<float> dist(0.0f, 10.0f);
return program.get_random().get_float(0.0f, 10.0f); return program.get_random().get_float(0.0f, 10.0f);
}); }).set_ephemeral();
/** /**
* This is a test using multiple types with blt::gp * This is a test using multiple types with blt::gp
@ -55,24 +55,7 @@ int main()
type_system.register_type<bool>(); type_system.register_type<bool>();
blt::gp::operator_builder builder{type_system}; blt::gp::operator_builder builder{type_system};
builder.add_operator(add); program.set_operations(builder.build(add, sub, mul, pro_div, op_if, eq_f, eq_b, lt, gt, op_and, op_or, op_xor, op_not, lit));
builder.add_operator(sub);
builder.add_operator(mul);
builder.add_operator(pro_div);
builder.add_operator(op_if);
builder.add_operator(eq_f);
builder.add_operator(eq_b);
builder.add_operator(lt);
builder.add_operator(gt);
builder.add_operator(op_and);
builder.add_operator(op_or);
builder.add_operator(op_xor);
builder.add_operator(op_not);
builder.add_operator(lit, true);
program.set_operations(builder.build());
blt::gp::ramped_half_initializer_t pop_init; blt::gp::ramped_half_initializer_t pop_init;

View File

@ -61,11 +61,11 @@ blt::gp::operation_t op_or([](bool a, bool b) { return a || b; }, "or"); // 10
blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast<bool>(a ^ b); }, "xor"); // 11 blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast<bool>(a ^ b); }, "xor"); // 11
blt::gp::operation_t op_not([](bool b) { return !b; }, "not"); // 12 blt::gp::operation_t op_not([](bool b) { return !b; }, "not"); // 12
blt::gp::operation_t lit([]() { // 13 auto lit = blt::gp::operation_t([]() {
//static std::uniform_real_distribution<float> dist(-32000, 32000); //static std::uniform_real_distribution<float> dist(-32000, 32000);
// static std::uniform_real_distribution<float> dist(0.0f, 10.0f); // static std::uniform_real_distribution<float> dist(0.0f, 10.0f);
return program.get_random().get_float(0.0f, 10.f); return program.get_random().get_float(0.0f, 10.0f);
}, "lit"); }).set_ephemeral();
/** /**
* This is a test using multiple types with blt::gp * This is a test using multiple types with blt::gp
@ -76,24 +76,7 @@ int main()
type_system.register_type<bool>(); type_system.register_type<bool>();
blt::gp::operator_builder builder{type_system}; blt::gp::operator_builder builder{type_system};
builder.add_operator(add); program.set_operations(builder.build(add, sub, mul, pro_div, op_if, eq_f, eq_b, lt, gt, op_and, op_or, op_xor, op_not, lit));
builder.add_operator(sub);
builder.add_operator(mul);
builder.add_operator(pro_div);
builder.add_operator(op_if);
builder.add_operator(eq_f);
builder.add_operator(eq_b);
builder.add_operator(lt);
builder.add_operator(gt);
builder.add_operator(op_and);
builder.add_operator(op_or);
builder.add_operator(op_xor);
builder.add_operator(op_not);
builder.add_operator(lit, true);
program.set_operations(builder.build());
blt::gp::ramped_half_initializer_t pop_init; blt::gp::ramped_half_initializer_t pop_init;

View File

@ -59,11 +59,11 @@ blt::gp::operation_t op_or([](bool a, bool b) { return a || b; }, "or"); // 10
blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast<bool>(a ^ b); }, "xor"); // 11 blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast<bool>(a ^ b); }, "xor"); // 11
blt::gp::operation_t op_not([](bool b) { return !b; }, "not"); // 12 blt::gp::operation_t op_not([](bool b) { return !b; }, "not"); // 12
blt::gp::operation_t lit([]() { // 13 auto lit = blt::gp::operation_t([]() {
//static std::uniform_real_distribution<float> dist(-32000, 32000); //static std::uniform_real_distribution<float> dist(-32000, 32000);
// static std::uniform_real_distribution<float> dist(0.0f, 10.0f); // static std::uniform_real_distribution<float> dist(0.0f, 10.0f);
return program.get_random().get_float(0.0f, 10.0f); return program.get_random().get_float(0.0f, 10.0f);
}, "lit"); }).set_ephemeral();
/** /**
* This is a test using multiple types with blt::gp * This is a test using multiple types with blt::gp
@ -74,24 +74,7 @@ int main()
type_system.register_type<bool>(); type_system.register_type<bool>();
blt::gp::operator_builder builder{type_system}; blt::gp::operator_builder builder{type_system};
builder.add_operator(add); program.set_operations(builder.build(add, sub, mul, pro_div, op_if, eq_f, eq_b, lt, gt, op_and, op_or, op_xor, op_not, lit));
builder.add_operator(sub);
builder.add_operator(mul);
builder.add_operator(pro_div);
builder.add_operator(op_if);
builder.add_operator(eq_f);
builder.add_operator(eq_b);
builder.add_operator(lt);
builder.add_operator(gt);
builder.add_operator(op_and);
builder.add_operator(op_or);
builder.add_operator(op_xor);
builder.add_operator(op_not);
builder.add_operator(lit, true);
program.set_operations(builder.build());
blt::gp::ramped_half_initializer_t pop_init; blt::gp::ramped_half_initializer_t pop_init;

View File

@ -42,11 +42,11 @@ blt::gp::operation_t op_or([](bool a, bool b) { return a || b; }, "or"); // 10
blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast<bool>(a ^ b); }, "xor"); // 11 blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast<bool>(a ^ b); }, "xor"); // 11
blt::gp::operation_t op_not([](bool b) { return !b; }, "not"); // 12 blt::gp::operation_t op_not([](bool b) { return !b; }, "not"); // 12
blt::gp::operation_t lit([]() { // 13 auto lit = blt::gp::operation_t([]() {
//static std::uniform_real_distribution<float> dist(-32000, 32000); //static std::uniform_real_distribution<float> dist(-32000, 32000);
// static std::uniform_real_distribution<float> dist(0.0f, 10.0f); // static std::uniform_real_distribution<float> dist(0.0f, 10.0f);
return program.get_random().get_float(0.0f, 10.0f); return program.get_random().get_float(0.0f, 10.0f);
}, "lit"); }).set_ephemeral();
void print_best() void print_best()
{ {
@ -90,24 +90,7 @@ int main()
type_system.register_type<bool>(); type_system.register_type<bool>();
blt::gp::operator_builder builder{type_system}; blt::gp::operator_builder builder{type_system};
builder.add_operator(add); program.set_operations(builder.build(add, sub, mul, pro_div, op_if, eq_f, eq_b, lt, gt, op_and, op_or, op_xor, op_not, lit));
builder.add_operator(sub);
builder.add_operator(mul);
builder.add_operator(pro_div);
builder.add_operator(op_if);
builder.add_operator(eq_f);
builder.add_operator(eq_b);
builder.add_operator(lt);
builder.add_operator(gt);
builder.add_operator(op_and);
builder.add_operator(op_or);
builder.add_operator(op_xor);
builder.add_operator(op_not);
builder.add_operator(lit, true);
program.set_operations(builder.build());
auto sel = blt::gp::select_tournament_t{}; auto sel = blt::gp::select_tournament_t{};
program.generate_population(type_system.get_type<float>().id(), fitness_function, sel, sel, sel); program.generate_population(type_system.get_type<float>().id(), fitness_function, sel, sel, sel);

View File

@ -93,7 +93,7 @@ void basic_test()
auto tree = gen.generate(args); auto tree = gen.generate(args);
context ctx{&program}; context ctx{&program};
auto result = tree.get_evaluation_value<float>(&ctx, program.get_eval_func()); auto result = tree.get_evaluation_value<float>(&ctx);
BLT_TRACE(result); BLT_TRACE(result);
BLT_ASSERT(result == -5.0f || result == 5.0f || result == 0.0f); BLT_ASSERT(result == -5.0f || result == 5.0f || result == 0.0f);
tree.print(program, std::cout, true, true); tree.print(program, std::cout, true, true);