From 0dfa4fea93935f5b28bdcc2b9f16fbff74caa8d8 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 5 Sep 2024 02:20:03 -0400 Subject: [PATCH] need new crossover --- CMakeLists.txt | 2 +- examples/symbolic_regression.cpp | 8 ++++---- include/blt/gp/selection.h | 5 ++++- include/blt/gp/typesystem.h | 8 ++++---- src/transformers.cpp | 12 ++---------- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e3e9fe7..2a27092 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.1.46) +project(blt-gp VERSION 0.1.47) include(CTest) diff --git a/examples/symbolic_regression.cpp b/examples/symbolic_regression.cpp index 206bd8a..5c1c33f 100644 --- a/examples/symbolic_regression.cpp +++ b/examples/symbolic_regression.cpp @@ -39,12 +39,12 @@ blt::gp::mutation_t mut; blt::gp::prog_config_t config = blt::gp::prog_config_t() .set_initial_min_tree_size(2) .set_initial_max_tree_size(6) - .set_elite_count(200) + .set_elite_count(2) .set_crossover_chance(0.9) .set_mutation_chance(0.1) .set_reproduction_chance(0) .set_max_generations(50) - .set_pop_size(20000) + .set_pop_size(500) .set_thread_count(0); blt::gp::gp_program program{SEED, config}; @@ -65,7 +65,7 @@ constexpr auto fitness_function = [](blt::gp::tree_t& current_tree, blt::gp::fit if (diff < value_cutoff) { fitness.raw_fitness += diff; - if (diff < 0.01) + if (diff <= 0.01) fitness.hits++; } else fitness.raw_fitness += value_cutoff; @@ -99,7 +99,7 @@ int main() program.set_operations(builder.build(add, sub, mul, pro_div, op_sin, op_cos, op_exp, op_log, lit, op_x)); BLT_DEBUG("Generate Initial Population"); - auto sel = blt::gp::select_tournament_t{}; + auto sel = blt::gp::select_fitness_proportionate_t{}; program.generate_population(program.get_typesystem().get_type().id(), fitness_function, sel, sel, sel); BLT_DEBUG("Begin Generation Loop"); diff --git a/include/blt/gp/selection.h b/include/blt/gp/selection.h index 7c4c67b..e9c9b28 100644 --- a/include/blt/gp/selection.h +++ b/include/blt/gp/selection.h @@ -100,6 +100,8 @@ namespace blt::gp { p1 = &crossover_selection.select(program, current_pop); p2 = &crossover_selection.select(program, current_pop); + c1.copy_fast(*p1); + c2->copy_fast(*p2); } while (!config.crossover.get().apply(program, *p1, *p2, c1, *c2)); #ifdef BLT_TRACK_ALLOCATIONS tracker.stop_measurement_thread_local(state); @@ -124,6 +126,7 @@ namespace blt::gp do { p = &mutation_selection.select(program, current_pop); + c1.copy_fast(*p); } while (!config.mutator.get().apply(program, *p, c1)); #ifdef BLT_TRACK_ALLOCATIONS tracker.stop_measurement_thread_local(state); @@ -144,7 +147,7 @@ namespace blt::gp auto state = tracker.start_measurement_thread_local(); #endif // reproduction - c1 = reproduction_selection.select(program, current_pop); + c1.copy_fast(reproduction_selection.select(program, current_pop)); #ifdef BLT_TRACK_ALLOCATIONS tracker.stop_measurement_thread_local(state); reproduction_calls.call(); diff --git a/include/blt/gp/typesystem.h b/include/blt/gp/typesystem.h index 7c25ac1..20ec7ba 100644 --- a/include/blt/gp/typesystem.h +++ b/include/blt/gp/typesystem.h @@ -30,14 +30,14 @@ namespace blt::gp { - struct operator_id : integer_type + struct operator_id : integer_type { - using integer_type::integer_type; + using integer_type::integer_type; }; - struct type_id : integer_type + struct type_id : integer_type { - using integer_type::integer_type; + using integer_type::integer_type; }; class type diff --git a/src/transformers.cpp b/src/transformers.cpp index 3f4e270..82693a2 100644 --- a/src/transformers.cpp +++ b/src/transformers.cpp @@ -61,9 +61,6 @@ namespace blt::gp bool crossover_t::apply(gp_program& program, const tree_t& p1, const tree_t& p2, tree_t& c1, tree_t& c2) // NOLINT { - c1.copy_fast(p1); - c2.copy_fast(p2); - auto& c1_ops = c1.get_operations(); auto& c2_ops = c2.get_operations(); @@ -214,12 +211,9 @@ namespace blt::gp return crossover_point_t{static_cast(crossover_point), static_cast(attempted_point)}; } - bool mutation_t::apply(gp_program& program, const tree_t& p, tree_t& c) + bool mutation_t::apply(gp_program& program, const tree_t&, tree_t& c) { - c.copy_fast(p); - mutate_point(program, c, program.get_random().get_size_t(0ul, c.get_operations().size())); - return true; } @@ -307,9 +301,7 @@ namespace blt::gp bool advanced_mutation_t::apply(gp_program& program, const tree_t& p, tree_t& c) { - // child tree - c.copy_fast(p); - + (void) p; auto& ops = c.get_operations(); auto& vals = c.get_values();