From 4729e94d4fa662f7324b911a14830940fe076ae0 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 13 Jul 2025 13:51:07 -0400 Subject: [PATCH] i have no idea just some tests --- .gitmodules | 3 +++ CMakeLists.txt | 4 +++- include/gp_system.h | 2 ++ lib/implot | 1 + src/gp_system.cpp | 44 +++++++++++++++++++++++++++++++++++++------- src/main.cpp | 33 ++++++++++++++++++++++++++++++--- 6 files changed, 76 insertions(+), 11 deletions(-) create mode 160000 lib/implot diff --git a/.gitmodules b/.gitmodules index 47d86f4..781a93f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "lib/lockfree"] path = lib/lockfree url = https://github.com/bhhbazinga/LockFreeLinkedList.git +[submodule "lib/implot"] + path = lib/implot + url = https://github.com/epezent/implot.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 73ac60f..bf9501c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,9 +18,11 @@ include_directories( ${OpenCV_INCLUDE_DIRS} ) include_directories(lib/stb) include_directories(include/) +include_directories(lib/implot) file(GLOB_RECURSE PROJECT_BUILD_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp") +file(GLOB IMPLOT_BUILD_FILES "${CMAKE_CURRENT_SOURCE_DIR}/lib/implot/*.cpp") -add_executable(image-gp-2 ${PROJECT_BUILD_FILES}) +add_executable(image-gp-2 ${PROJECT_BUILD_FILES} ${IMPLOT_BUILD_FILES}) target_compile_options(image-gp-2 PRIVATE -Wall -Wextra -Wpedantic -Wno-comment) target_link_options(image-gp-2 PRIVATE -Wall -Wextra -Wpedantic -Wno-comment) diff --git a/include/gp_system.h b/include/gp_system.h index ad8a3f4..e3a42ee 100644 --- a/include/gp_system.h +++ b/include/gp_system.h @@ -43,4 +43,6 @@ std::array& get_reference_image(); std::array to_gl_image(const std::array& image); +std::tuple&, const std::vector&, const std::vector&, const std::vector&> get_fitness_history(); + #endif //GP_SYSTEM_H diff --git a/lib/implot b/lib/implot new file mode 160000 index 0000000..3da8bd3 --- /dev/null +++ b/lib/implot @@ -0,0 +1 @@ +Subproject commit 3da8bd34299965d3b0ab124df743fe3e076fa222 diff --git a/src/gp_system.cpp b/src/gp_system.cpp index 5d5dc3f..212eccb 100644 --- a/src/gp_system.cpp +++ b/src/gp_system.cpp @@ -25,10 +25,15 @@ using namespace blt::gp; -float filter_nan(const float f) +bool is_nan(const float f) { - if (std::isnan(f) || std::isinf(f) || std::isinf(-f)) - return 0.0f; + return std::isnan(f) || std::isinf(f) || std::isinf(-f); +} + +float filter_nan(const float f, const float failure = 0.0f) +{ + if (is_nan(f)) + return failure; return f; } @@ -38,6 +43,11 @@ prog_config_t config{}; std::vector> images; auto reference_image = image_storage_t::from_file("../silly.png"); +std::vector average_fitness; +std::vector best_fitness; +std::vector worst_fitness; +std::vector overall_fitness; + template void fitness_func(const tree_t& tree, fitness_t& fitness, const blt::size_t index) { @@ -54,13 +64,18 @@ void fitness_func(const tree_t& tree, fitness_t& fitness, const blt::size_t inde auto multiplier = (1 - std::abs((static_cast(x) / (static_cast(IMAGE_DIMENSIONS) / 2)) - 1)) + (1 - std::abs( (static_cast(y) / (static_cast(IMAGE_DIMENSIONS) / 2)) - 1)); - const auto diff = filter_nan(data.get(x, y)) - reference_image[Channel].get(x, y); - fitness.raw_fitness += diff * diff * multiplier; + auto our = data.get(x, y); + auto our2 = blt::mem::type_cast(our); + auto our3 = static_cast(our2) / static_cast(std::numeric_limits::max()); + auto theirs = reference_image[Channel].get(x, y); + const auto diff = std::pow(our3, 1.0f/2.2f) - std::pow(theirs, 1.0f/2.2f); + fitness.raw_fitness += static_cast(diff * multiplier); } } - fitness.raw_fitness /= static_cast(IMAGE_SIZE_CHANNELS); + // fitness.raw_fitness /= static_cast(IMAGE_SIZE_CHANNELS); + fitness.raw_fitness = static_cast(std::sqrt(fitness.raw_fitness)); fitness.standardized_fitness = fitness.raw_fitness; - fitness.adjusted_fitness = -fitness.standardized_fitness; + fitness.adjusted_fitness = 1 - 1 / (1 + fitness.standardized_fitness); } template @@ -271,6 +286,16 @@ void run_step() BLT_TRACE("Channel Green"); else BLT_TRACE("Channel Blue"); + const auto avg = stats.average_fitness.load(std::memory_order_relaxed); + const auto best = stats.best_fitness.load(std::memory_order_relaxed); + const auto worst = stats.worst_fitness.load(std::memory_order_relaxed); + const auto overall = stats.overall_fitness.load(std::memory_order_relaxed); + + average_fitness.push_back(static_cast(avg)); + best_fitness.push_back(static_cast(best)); + worst_fitness.push_back(static_cast(worst)); + overall_fitness.push_back(static_cast(overall)); + BLT_TRACE("\tAvg Fit: {:0.6f}, Best Fit: {:0.6f}, Worst Fit: {:0.6f}, Overall Fit: {:0.6f}", stats.average_fitness.load(std::memory_order_relaxed), stats.best_fitness.load(std::memory_order_relaxed), stats.worst_fitness.load(std::memory_order_relaxed), stats.overall_fitness.load(std::memory_order_relaxed)); @@ -340,3 +365,8 @@ void regenerate_image(blt::size_t index, float& image_storage, blt::i32 width, b { } + +std::tuple&, const std::vector&, const std::vector&, const std::vector&> get_fitness_history() +{ + return {average_fitness, best_fitness, worst_fitness, overall_fitness}; +} diff --git a/src/main.cpp b/src/main.cpp index ed2d058..d6169ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,8 +6,7 @@ #include "blt/gfx/renderer/camera.h" #include #include - -#include "../cmake-build-release-examples/_deps/imgui-src/imgui_internal.h" +#include blt::gfx::matrix_state_manager global_matrices; blt::gfx::resource_manager resources; @@ -58,6 +57,7 @@ std::thread run_gp() void init(const blt::gfx::window_data&) { + ImPlot::CreateContext(); using namespace blt::gfx; for (blt::size_t i = 0; i < population_size; i++) @@ -193,7 +193,7 @@ void update(const blt::gfx::window_data& data) { auto w = data.width; auto h = data.height - top_bar_height - 10; - renderer_2d.drawRectangle({w / 2, h / 2, w, h}, "reference"); + renderer_2d.drawRectangle({static_cast(w / 2), h / 2, w, h}, "reference"); ImGui::EndTabItem(); } @@ -202,6 +202,33 @@ void update(const blt::gfx::window_data& data) ImGui::End(); // MainWindow + // if (ImGui::Begin("Fitness")) + // { + // const auto& [average, best, worst, overall] = get_fitness_history(); + // + // if (ImPlot::BeginPlot("Average Fitness", ImVec2{-1, 0}, ImPlotFlags_NoInputs)) + // { + // ImPlot::PlotLine("Average Fitness", average.data(), static_cast(average.size())); + // ImPlot::EndPlot(); + // } + // if (ImPlot::BeginPlot("Best Fitness", ImVec2{-1, 0}, ImPlotFlags_NoInputs)) + // { + // ImPlot::PlotLine("Best Fitness", best.data(), static_cast(best.size())); + // ImPlot::EndPlot(); + // } + // if (ImPlot::BeginPlot("Worst Fitness", ImVec2{-1, 0}, ImPlotFlags_NoInputs)) + // { + // ImPlot::PlotLine("Worst Fitness", worst.data(), static_cast(worst.size())); + // ImPlot::EndPlot(); + // } + // if (ImPlot::BeginPlot("Overall Fitness", ImVec2{-1, 0}, ImPlotFlags_NoInputs)) + // { + // ImPlot::PlotLine("Overall Fitness", overall.data(), static_cast(overall.size())); + // ImPlot::EndPlot(); + // } + // } + // ImGui::End(); + if (ImGui::Begin("Debug")) { const auto allocated_blocks = g_allocated_blocks.load(std::memory_order_relaxed);