From 2bfbc59dc8988e3d0e6e3dbca586c2df00c95c5b Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 15 Jul 2024 18:59:27 -0400 Subject: [PATCH] spooky --- CMakeLists.txt | 2 +- lib/blt-gp | 2 +- lib/blt-graphics | 2 +- src/main.cpp | 91 ++++++++++++++++++++---------------------------- 4 files changed, 40 insertions(+), 57 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 22ddc4f..2e2729b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(image-gp-6 VERSION 0.0.6) +project(image-gp-6 VERSION 0.0.7) include(FetchContent) diff --git a/lib/blt-gp b/lib/blt-gp index 2081dd3..34a3343 160000 --- a/lib/blt-gp +++ b/lib/blt-gp @@ -1 +1 @@ -Subproject commit 2081dd3e5f19171ca171a1457930845c8b04361b +Subproject commit 34a3343a89e7a4329952d8853212f5a937392294 diff --git a/lib/blt-graphics b/lib/blt-graphics index 672e6b8..8fd5222 160000 --- a/lib/blt-graphics +++ b/lib/blt-graphics @@ -1 +1 @@ -Subproject commit 672e6b8b602dff917e0782bb98b7c6aa361e6945 +Subproject commit 8fd52222939b9e58da8511d9000bb1cf12e4de71 diff --git a/src/main.cpp b/src/main.cpp index c1dc728..f7cf2d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -252,14 +252,9 @@ inline context get_pop_ctx(blt::size_t i) constexpr auto create_fitness_function(blt::size_t channel) { - return [channel](blt::gp::tree_t& current_tree, blt::gp::fitness_t& fitness, blt::size_t in) { - auto& v = generation_images[in]; - for (blt::size_t i = 0; i < DATA_SIZE; i++) - { - context ctx = get_ctx(i); - v.rgb_data[i * CHANNELS + channel] = static_cast(current_tree.get_evaluation_value(&ctx) * 255); - } + return [](blt::gp::tree_t&, blt::gp::fitness_t& fitness, blt::size_t in) { fitness.raw_fitness = fitness_values[in]; + BLT_TRACE("Raw fitness: %lf for %ld", fitness.raw_fitness, in); fitness.standardized_fitness = fitness.raw_fitness; fitness.adjusted_fitness = (1.0 / (1.0 + fitness.standardized_fitness)); // auto& v = fitness_data[in]; @@ -299,26 +294,31 @@ constexpr auto fitness_function_blue = create_fitness_function(2); void execute_generation(blt::gp::gp_program& program) { BLT_TRACE("------------{Begin Generation %ld}------------", program.get_current_generation()); - BLT_START_INTERVAL("Image Test", "Gen"); - auto sel = blt::gp::select_tournament_t{}; - program.create_next_generation(sel, sel, sel, blt::gp::non_deterministic_next_pop_creator); - BLT_END_INTERVAL("Image Test", "Gen"); - BLT_TRACE("Move to next generation"); - BLT_START_INTERVAL("Image Test", "Fitness"); - program.next_generation(); BLT_TRACE("Evaluate Fitness"); + BLT_START_INTERVAL("Image Test", "Fitness"); program.evaluate_fitness(); BLT_END_INTERVAL("Image Test", "Fitness"); + BLT_START_INTERVAL("Image Test", "Gen"); + auto sel = blt::gp::select_tournament_t{}; + program.create_next_generation(sel, sel, sel); + BLT_END_INTERVAL("Image Test", "Gen"); + BLT_TRACE("Move to next generation"); + program.next_generation(); BLT_TRACE("----------------------------------------------"); std::cout << std::endl; } -void evaluate_program(blt::gp::gp_program& program) +void evaluate_generation(blt::gp::gp_program& program, blt::size_t channel) { - BLT_DEBUG("Begin Generation Loop"); - while (!program.should_terminate()) + BLT_DEBUG("Evaluating Images"); + for (auto [index, ind] : blt::enumerate(program.get_current_pop().get_individuals())) { - execute_generation(program); + auto& v = generation_images[index]; + for (blt::size_t i = 0; i < DATA_SIZE; i++) + { + context ctx = get_ctx(i); + v.rgb_data[i * CHANNELS + channel] = static_cast(ind.tree.get_evaluation_value(&ctx) * 255); + } } } @@ -390,34 +390,6 @@ void write_results() print_stats(program_blue); } -template -auto convert_args(context& ctx, Arg&& arg) -{ - if constexpr (std::is_same_v>, context>) - { - return ctx; - } else - { - return std::forward(arg); - } -} - -template -void make_operator_image(T op, Args... args) -{ - auto value = std::unique_ptr(new blt::u8[IMAGE_SIZE * IMAGE_SIZE * CHANNELS]); - - for (blt::size_t i = 0; i < IMAGE_SIZE * IMAGE_SIZE; i++) - { - auto ctx = get_ctx(i); - value.get()[i * CHANNELS] = static_cast(op(convert_args(ctx, args)...) * 255); - value.get()[i * CHANNELS + 1] = static_cast(op(convert_args(ctx, args)...) * 255); - value.get()[i * CHANNELS + 2] = static_cast(op(convert_args(ctx, args)...) * 255); - } - - stbi_write_png((blt::type_string + ".png").c_str(), IMAGE_SIZE, IMAGE_SIZE, CHANNELS, value.get(), 0); -} - void init(const blt::gfx::window_data&) { using namespace blt::gfx; @@ -445,9 +417,13 @@ void init(const blt::gfx::window_data&) create_program(program_blue); BLT_DEBUG("Generate Initial Population"); - program_red.generate_population(type_system.get_type().id(), fitness_function_red); - program_green.generate_population(type_system.get_type().id(), fitness_function_green); - program_blue.generate_population(type_system.get_type().id(), fitness_function_blue); + program_red.generate_population(type_system.get_type().id(), fitness_function_red, false); + program_green.generate_population(type_system.get_type().id(), fitness_function_green, false); + program_blue.generate_population(type_system.get_type().id(), fitness_function_blue, false); + + evaluate_generation(program_red, 0); + evaluate_generation(program_green, 1); + evaluate_generation(program_blue, 2); // evaluate_program(program_red); // evaluate_program(program_green); @@ -474,8 +450,11 @@ void update(const blt::gfx::window_data& data) execute_generation(program_red); execute_generation(program_green); execute_generation(program_blue); - for (auto& f : fitness_values) - f = 0; + + evaluate_generation(program_red, 0); + evaluate_generation(program_green, 1); + evaluate_generation(program_blue, 2); + last_fitness = 0; } ImGui::Button("Write Best"); @@ -490,10 +469,11 @@ void update(const blt::gfx::window_data& data) global_matrices.getOrtho())); double max_fitness = 0; - for (blt::size_t i = 0; i < config.population_size; i++) { + for (blt::size_t i = 0; i < config.population_size; i++) + { if (fitness_values[i] > max_fitness) max_fitness = fitness_values[i]; - } + } for (blt::size_t i = 0; i < config.population_size; i++) { @@ -519,7 +499,8 @@ void update(const blt::gfx::window_data& data) if (blt::gfx::mousePressedLastFrame()) { - if (blt::gfx::isKeyPressed(GLFW_KEY_LEFT_SHIFT)){ + if (blt::gfx::isKeyPressed(GLFW_KEY_LEFT_SHIFT)) + { fitness_values[i] = static_cast(last_fitness); if (blt::gfx::mousePressedLastFrame()) last_fitness++; @@ -548,6 +529,8 @@ void update(const blt::gfx::window_data& data) int main() { + for (auto& v : fitness_values) + v = POP_SIZE + 1; blt::gfx::init(blt::gfx::window_data{"My Sexy Window", init, update, 1440, 720}.setSyncInterval(1)); global_matrices.cleanup(); resources.cleanup();