From 7aaad7013299f50ee91d4f5015c9ad48b80e23b6 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 21 Aug 2024 20:40:42 -0400 Subject: [PATCH] remove bitmask, fix the other tests --- CMakeLists.txt | 2 +- examples/symbolic_regression.cpp | 4 +- include/blt/gp/fwdecl.h | 5 +- include/blt/gp/operations.h | 22 ++++----- include/blt/gp/program.h | 47 +++++++----------- include/blt/gp/stack.h | 23 ++------- include/blt/gp/tree.h | 19 +++++--- src/generators.cpp | 2 +- src/tree.cpp | 82 ++------------------------------ tests/evaluation_tests.cpp | 20 ++------ tests/gp_test_1.cpp | 4 +- tests/gp_test_2.cpp | 2 +- tests/gp_test_3.cpp | 25 ++-------- tests/gp_test_4.cpp | 23 ++------- tests/gp_test_5.cpp | 25 ++-------- tests/gp_test_6.cpp | 23 ++------- tests/gp_test_7.cpp | 23 ++------- tests/order_tests.cpp | 2 +- 18 files changed, 77 insertions(+), 276 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7bdaad..00f2f30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.1.18) +project(blt-gp VERSION 0.1.19) include(CTest) diff --git a/examples/symbolic_regression.cpp b/examples/symbolic_regression.cpp index bdf65f5..d9c0b05 100644 --- a/examples/symbolic_regression.cpp +++ b/examples/symbolic_regression.cpp @@ -39,7 +39,7 @@ blt::gp::prog_config_t config = blt::gp::prog_config_t() .set_mutation_chance(0.1) .set_reproduction_chance(0) .set_max_generations(50) - .set_pop_size(50000) + .set_pop_size(5000) .set_thread_count(0); 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; for (auto& fitness_case : fitness_cases) { - auto diff = std::abs(fitness_case.y - current_tree.get_evaluation_value(&fitness_case, program.get_eval_func())); + auto diff = std::abs(fitness_case.y - current_tree.get_evaluation_value(&fitness_case)); if (diff < value_cutoff) { fitness.raw_fitness += diff; diff --git a/include/blt/gp/fwdecl.h b/include/blt/gp/fwdecl.h index bc45258..c058d5a 100644 --- a/include/blt/gp/fwdecl.h +++ b/include/blt/gp/fwdecl.h @@ -52,9 +52,6 @@ namespace blt::gp namespace detail { - // requires operator[](bit_index), push_back, clear - using bitmask_t = std::vector; - class operator_storage_test; // context*, read stack, write stack using operator_func_t = std::function; @@ -68,7 +65,7 @@ namespace blt::gp RETURN }; - using destroy_func_t = std::function; + using destroy_func_t = std::function; } } diff --git a/include/blt/gp/operations.h b/include/blt/gp/operations.h index 57f8ffd..9662d6a 100644 --- a/include/blt/gp/operations.h +++ b/include/blt/gp/operations.h @@ -102,16 +102,16 @@ namespace blt::gp } template - 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) { - read_allocator.call_destructors...>(mask); + read_allocator.call_destructors...>(); } } template - 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(); #if BLT_DEBUG_LEVEL > 0 @@ -120,9 +120,9 @@ namespace blt::gp #endif Return ret = exec_sequence_to_indices(std::forward(func), read_allocator, seq, std::forward(args)...); if (has_context) - call_destructors_without_first(read_allocator, mask); + call_destructors_without_first(read_allocator); else - read_allocator.call_destructors...>(mask); + read_allocator.call_destructors...>(); read_allocator.pop_bytes((stack_allocator::aligned_size>() + ...)); return ret; #if BLT_DEBUG_LEVEL > 0 @@ -159,18 +159,18 @@ namespace blt::gp constexpr explicit operation_t(const Functor& functor, std::optional 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) { return func(); } else { - return call_with()(false, mask, func, read_allocator); + return call_with()(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 if constexpr (sizeof...(Args) == 0) @@ -183,7 +183,7 @@ namespace blt::gp return func(ctx_ref); } else { - return call_without_first()(true, mask, func, read_allocator, ctx_ref); + return call_without_first()(true, func, read_allocator, ctx_ref); } } @@ -194,11 +194,11 @@ namespace blt::gp if constexpr (detail::is_same_v::type>>) { // first arg is context - write_allocator.push(this->operator()(context, read_allocator, nullptr)); + write_allocator.push(this->operator()(context, read_allocator)); } else { // first arg isn't context - write_allocator.push(this->operator()(read_allocator, nullptr)); + write_allocator.push(this->operator()(read_allocator)); } }; } diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index 5c03aad..8a87553 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -122,8 +122,6 @@ namespace blt::gp evaluation_context results{}; results.values.reserve(largest); - static thread_local detail::bitmask_t bitfield; - bitfield.clear(); blt::size_t total_so_far = 0; 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); results.values.copy_from(vals.from(total_so_far), stack_allocator::aligned_size(operation.type_size)); - bitfield.push_back(false); continue; } - call_jmp_table(operation.id, context, results.values, results.values, &bitfield, operators...); - bitfield.push_back(true); + call_jmp_table(operation.id, context, results.values, results.values, operators...); } return results; @@ -248,11 +244,11 @@ namespace blt::gp 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) { case detail::destroy_t::ARGS: - alloc.call_destructors(mask); + alloc.call_destructors(); break; case detail::destroy_t::RETURN: if constexpr (detail::has_func_drop_v>) @@ -277,50 +273,45 @@ namespace blt::gp } } - template - static inline void execute(void* context, stack_allocator& write_stack, stack_allocator& read_stack, detail::bitmask_t* mask, - Lambda& lambda) + template + static inline void execute(void* context, stack_allocator& write_stack, stack_allocator& read_stack, Operator& operation) { - if constexpr (std::is_same_v, Context>) + if constexpr (std::is_same_v, Context>) { - write_stack.push(lambda(context, read_stack, mask)); + write_stack.push(operation(context, read_stack)); } else { - write_stack.push(lambda(read_stack, mask)); + write_stack.push(operation(read_stack)); } } - template - static inline bool call(blt::size_t op, void* context, stack_allocator& write_stack, stack_allocator& read_stack, detail::bitmask_t* mask, - Lambda& lambda) + template + static inline bool call(blt::size_t op, void* context, stack_allocator& write_stack, stack_allocator& read_stack, Operator& operation) { if (id == op) { - execute(context, write_stack, read_stack, mask, lambda); + execute(context, write_stack, read_stack, operation); return false; } return true; } - template + template 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, Lambdas& ... lambdas) + std::integer_sequence, Operators&... operators) { if (op >= sizeof...(operator_ids)) { BLT_UNREACHABLE; } - (call(op, context, write_stack, read_stack, mask, lambdas) && ...); -// std::initializer_list{((op == operator_ids) ? (execute(context, write_stack, read_stack, mask, lambdas), 0) : 0)...}; - + (call(op, context, write_stack, read_stack, operators) && ...); } - template + template 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...); } type_provider& system; @@ -542,8 +533,6 @@ namespace blt::gp void reset_program(type_id root_type, bool eval_fitness_now = true) { current_generation = 0; - for (auto& pop : current_pop) - pop.tree.drop(*this); current_pop = config.pop_initializer.get().generate( {*this, root_type, config.population_size, config.initial_min_tree_size, config.initial_max_tree_size}); if (eval_fitness_now) @@ -552,7 +541,6 @@ namespace blt::gp void next_generation() { - current_pop.drop(*this); current_pop = std::move(next_pop); current_generation++; } @@ -702,7 +690,6 @@ namespace blt::gp ~gp_program() { - current_pop.drop(*this); thread_helper.lifetime_over = true; thread_helper.barrier.notify_all(); thread_helper.thread_function_condition.notify_all(); diff --git a/include/blt/gp/stack.h b/include/blt/gp/stack.h index 59971b5..21668eb 100644 --- a/include/blt/gp/stack.h +++ b/include/blt/gp/stack.h @@ -227,22 +227,13 @@ namespace blt::gp } template - void call_destructors(detail::bitmask_t* mask) + void call_destructors() { if constexpr (sizeof...(Args) > 0) { blt::size_t offset = (stack_allocator::aligned_size(sizeof(NO_REF_T)) + ...) - stack_allocator::aligned_size(sizeof(NO_REF_T::First>)); - blt::size_t index = 0; - if (mask != nullptr) - index = mask->size() - sizeof...(Args); - ((call_drop(offset, index, mask), offset -= stack_allocator::aligned_size(sizeof(NO_REF_T)), ++index), ...); - if (mask != nullptr) - { - auto& mask_r = *mask; - for (blt::size_t i = 0; i < sizeof...(Args); i++) - mask_r.pop_back(); - } + ((call_drop(offset), offset -= stack_allocator::aligned_size(sizeof(NO_REF_T))), ...); } } @@ -282,11 +273,9 @@ namespace blt::gp void expand(blt::size_t bytes) { bytes = to_nearest_page_size(bytes); -// auto new_data = static_cast(std::malloc(bytes)); auto new_data = static_cast(get_allocator().allocate(bytes)); if (bytes_stored > 0) std::memcpy(new_data, data_, bytes_stored); -// std::free(data_); get_allocator().deallocate(data_, size_); data_ = new_data; size_ = bytes; @@ -323,16 +312,10 @@ namespace blt::gp } template - 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) { - if (mask != nullptr) - { - auto& mask_r = *mask; - if (!mask_r[index]) - return; - } from>(offset).drop(); } } diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index 1e69dc5..96dedfa 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -55,6 +55,8 @@ namespace blt::gp { using iter_type = std::vector::const_iterator; public: + explicit tree_t(gp_program& program); + struct child_t { blt::ptrdiff_t start; @@ -82,7 +84,10 @@ namespace blt::gp 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); @@ -108,9 +113,9 @@ namespace blt::gp * Helper template for returning the result of evaluation (this calls it) */ template - 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(); } @@ -120,7 +125,9 @@ namespace blt::gp bool check(gp_program& program, void* context) const; void find_child_extends(gp_program& program, std::vector& 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_parent(blt::gp::gp_program& program, blt::ptrdiff_t start) const; // valid for [begin, end) @@ -150,12 +157,11 @@ namespace blt::gp { return total_value_bytes(operations.begin(), operations.end()); } - - void drop(gp_program& program); + private: std::vector operations; blt::gp::stack_allocator values; - std::atomic_int64_t* reference_counter; + detail::eval_func_t* func; }; struct fitness_t @@ -306,7 +312,6 @@ namespace blt::gp population_t& operator=(population_t&&) = default; - void drop(gp_program& program); private: std::vector individuals; }; diff --git a/src/generators.cpp b/src/generators.cpp index 9c5b4eb..1ca796e 100644 --- a/src/generators.cpp +++ b/src/generators.cpp @@ -53,7 +53,7 @@ namespace blt::gp { std::stack tree_generator = get_initial_stack(args.program, args.root_type); blt::size_t max_depth = 0; - tree_t tree; + tree_t tree{args.program}; while (!tree_generator.empty()) { diff --git a/src/tree.cpp b/src/tree.cpp index 73acafa..5d6ac32 100644 --- a/src/tree.cpp +++ b/src/tree.cpp @@ -37,45 +37,6 @@ namespace blt::gp 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) { if (!pretty_print) @@ -259,8 +220,6 @@ namespace blt::gp 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; auto bytes_size = values.size().total_used_bytes; @@ -287,7 +246,7 @@ namespace blt::gp blt::size_t total_produced = 0; blt::size_t total_consumed = 0; - + // for (const auto& operation : blt::reverse_iterate(operations.begin(), operations.end())) // { // if (operation.is_value) @@ -318,40 +277,6 @@ namespace blt::gp 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 sets; - while (!operations.empty()) - { - auto operation = operations.back(); - if (operation.is_value) - { - struct hello - { - float* f; - blt::size_t i; - }; - //auto h = values.from(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(stack_allocator::aligned_size(operation.type_size))); - } - operations.pop_back(); - } - } - void tree_t::find_child_extends(gp_program& program, std::vector& vec, blt::size_t parent_node, blt::size_t argc) const { 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); + } } \ No newline at end of file diff --git a/tests/evaluation_tests.cpp b/tests/evaluation_tests.cpp index 25b8feb..fb6c524 100644 --- a/tests/evaluation_tests.cpp +++ b/tests/evaluation_tests.cpp @@ -58,13 +58,12 @@ blt::gp::gp_program program{type_system, SEED}; blt::gp::op_container_t make_container(blt::gp::operator_id 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) { - static blt::gp::detail::callable_t empty([](void*, blt::gp::stack_allocator&, blt::gp::stack_allocator&, blt::gp::detail::bitmask_t*) {}); - return {empty, id.size(), 0, true}; + return {id.size(), 0, true}; } blt::gp::operation_t add([](float a, float b) { @@ -105,7 +104,7 @@ blt::gp::operation_t large_literal([]() { void basic_tree() { 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_value(type_system.get_type())); @@ -120,7 +119,7 @@ void basic_tree() void large_cross_type_tree() { - blt::gp::tree_t tree; + blt::gp::tree_t tree{program}; auto& ops = tree.get_operations(); auto& vals = tree.get_values(); @@ -149,16 +148,7 @@ int main() type_system.register_type(); blt::gp::operator_builder builder{type_system}; - - 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()); + program.set_operations(builder.build(f_literal, b_literal, add, basic_2t, sub, large_literal, cross_large_type)); basic_tree(); large_cross_type_tree(); diff --git a/tests/gp_test_1.cpp b/tests/gp_test_1.cpp index bf0b295..6d2c7cb 100644 --- a/tests/gp_test_1.cpp +++ b/tests/gp_test_1.cpp @@ -424,7 +424,7 @@ int main() 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; @@ -479,7 +479,7 @@ int main() //blt::span 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; diff --git a/tests/gp_test_2.cpp b/tests/gp_test_2.cpp index 50a68dc..960d414 100644 --- a/tests/gp_test_2.cpp +++ b/tests/gp_test_2.cpp @@ -62,7 +62,7 @@ int main() blt::gp::grow_generator_t grow; auto tree = grow.generate(blt::gp::generator_arguments{program, type_system.get_type().id(), 3, 7}); - auto value = tree.get_evaluation_value(nullptr, program.get_eval_func()); + auto value = tree.get_evaluation_value(nullptr); BLT_TRACE(value); diff --git a/tests/gp_test_3.cpp b/tests/gp_test_3.cpp index 6f026bd..d961b3d 100644 --- a/tests/gp_test_3.cpp +++ b/tests/gp_test_3.cpp @@ -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(a ^ 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 dist(-32000, 32000); // static std::uniform_real_distribution 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 @@ -55,24 +55,7 @@ int main() type_system.register_type(); blt::gp::operator_builder silly{type_system}; - silly.add_operator(add); - 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()); + 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)); blt::gp::grow_generator_t grow; auto tree = grow.generate(blt::gp::generator_arguments{program, type_system.get_type().id(), 3, 7}); diff --git a/tests/gp_test_4.cpp b/tests/gp_test_4.cpp index 16a966d..585585d 100644 --- a/tests/gp_test_4.cpp +++ b/tests/gp_test_4.cpp @@ -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(a ^ 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 dist(-32000, 32000); // static std::uniform_real_distribution dist(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 @@ -55,24 +55,7 @@ int main() type_system.register_type(); blt::gp::operator_builder builder{type_system}; - builder.add_operator(add); - 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()); + 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)); blt::gp::ramped_half_initializer_t pop_init; diff --git a/tests/gp_test_5.cpp b/tests/gp_test_5.cpp index bf9e783..7ef9e7a 100644 --- a/tests/gp_test_5.cpp +++ b/tests/gp_test_5.cpp @@ -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(a ^ b); }, "xor"); // 11 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 dist(-32000, 32000); // static std::uniform_real_distribution dist(0.0f, 10.0f); - return program.get_random().get_float(0.0f, 10.f); -}, "lit"); + return program.get_random().get_float(0.0f, 10.0f); +}).set_ephemeral(); /** * This is a test using multiple types with blt::gp @@ -76,24 +76,7 @@ int main() type_system.register_type(); blt::gp::operator_builder builder{type_system}; - builder.add_operator(add); - 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()); + 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)); blt::gp::ramped_half_initializer_t pop_init; diff --git a/tests/gp_test_6.cpp b/tests/gp_test_6.cpp index 1a1c36a..a3955e6 100644 --- a/tests/gp_test_6.cpp +++ b/tests/gp_test_6.cpp @@ -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(a ^ b); }, "xor"); // 11 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 dist(-32000, 32000); // static std::uniform_real_distribution dist(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 @@ -74,24 +74,7 @@ int main() type_system.register_type(); blt::gp::operator_builder builder{type_system}; - builder.add_operator(add); - 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()); + 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)); blt::gp::ramped_half_initializer_t pop_init; diff --git a/tests/gp_test_7.cpp b/tests/gp_test_7.cpp index 4a71b89..25e9ad1 100644 --- a/tests/gp_test_7.cpp +++ b/tests/gp_test_7.cpp @@ -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(a ^ b); }, "xor"); // 11 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 dist(-32000, 32000); // static std::uniform_real_distribution dist(0.0f, 10.0f); return program.get_random().get_float(0.0f, 10.0f); -}, "lit"); +}).set_ephemeral(); void print_best() { @@ -90,24 +90,7 @@ int main() type_system.register_type(); blt::gp::operator_builder builder{type_system}; - builder.add_operator(add); - 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()); + 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)); auto sel = blt::gp::select_tournament_t{}; program.generate_population(type_system.get_type().id(), fitness_function, sel, sel, sel); diff --git a/tests/order_tests.cpp b/tests/order_tests.cpp index d59ea4e..9db441b 100644 --- a/tests/order_tests.cpp +++ b/tests/order_tests.cpp @@ -93,7 +93,7 @@ void basic_test() auto tree = gen.generate(args); context ctx{&program}; - auto result = tree.get_evaluation_value(&ctx, program.get_eval_func()); + auto result = tree.get_evaluation_value(&ctx); BLT_TRACE(result); BLT_ASSERT(result == -5.0f || result == 5.0f || result == 0.0f); tree.print(program, std::cout, true, true);