diff --git a/CMakeLists.txt b/CMakeLists.txt index 2516c13..0017869 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,8 +38,8 @@ add_executable(gp_image_test ${PROJECT_BUILD_FILES}) add_dependencies(gp_image_test stats_lib_bindings_rust) -target_compile_options(gp_image_test PRIVATE -Wall -Wextra -Werror -Wpedantic -Wno-comment -Wno-unused-parameter) -target_link_options(gp_image_test PRIVATE -Wall -Wextra -Werror -Wpedantic -Wno-comment -Wno-unused-parameter) +target_compile_options(gp_image_test PRIVATE -Wall -Wextra -Wpedantic -Wno-comment -Wno-unused-parameter) +target_link_options(gp_image_test PRIVATE -Wall -Wextra -Wpedantic -Wno-comment -Wno-unused-parameter) target_link_libraries(gp_image_test BLT_WITH_GRAPHICS) diff --git a/include/functions.h b/include/functions.h index cff6082..de652ab 100644 --- a/include/functions.h +++ b/include/functions.h @@ -35,7 +35,7 @@ template using allowed_funcs = std::vector; template -using allowed_funcs_set = HASHSET; +using allowed_funcs_set = blt::hashset_t; template using func_list = std::vector>; @@ -266,6 +266,8 @@ float eval_DNF_SW_1(const image& img); float eval_BAM(const image& img, const image& compare, float allowed_diff); +float eval_DST(const image& img, const image& compare); + //template //bool isNan(F f) //{ diff --git a/include/gp.h b/include/gp.h index fbeb29e..316156a 100644 --- a/include/gp.h +++ b/include/gp.h @@ -56,6 +56,9 @@ struct node void reset_children() { + for (size_t i = 0; i < argc; i++) + BLT_INFO("Child node %p with img %p", sub_nodes[i], &sub_nodes[i]->img.value().getData()); + blt::logging::flush(); for (size_t i = 0; i < argc; i++) sub_nodes[i]->img.reset(); } diff --git a/include/image.h b/include/image.h index ad73a28..f272366 100644 --- a/include/image.h +++ b/include/image.h @@ -30,24 +30,30 @@ #include using image_data_t = std::array; -inline blt::bump_allocator img_allocator(8192); +inline blt::bump_allocator<> img_allocator(8192); class image { private: - mutable std::variant data; + mutable std::variant data = nullptr; public: image() { - data = img_allocator.allocate(1); + data = img_allocator.allocate(); img_allocator.construct(std::get(data)); } + explicit image(const blt::vec3& v): data(v) + {} + + explicit image(float f): data(f) + {} + image(const image& copy) { std::visit(blt::lambda_visitor{ [&](const image_data_t* d) { - data = img_allocator.allocate(1); + data = img_allocator.allocate(); img_allocator.construct(std::get(data)); for (size_t i = 0; i < d->size(); i++) @@ -64,7 +70,18 @@ class image image(image&& move) noexcept { - data = std::move(move.data); + std::visit(blt::lambda_visitor{ + [&](image_data_t* d) { + this->data = d; + }, + [&](const blt::vec3& v) { + this->data = v; + }, + [&](float f) { + this->data = f; + } + }, move.data); + move.data = nullptr; } @@ -72,11 +89,14 @@ class image { if (© == this) return *this; + if (std::holds_alternative(data)) + { + BLT_INFO("Copy deallocate"); + img_allocator.deallocate(std::get(data), 1); + } std::visit(blt::lambda_visitor{ [&](const image_data_t* d) { - if (std::holds_alternative(data)) - img_allocator.deallocate(std::get(data), 1); - data = img_allocator.allocate(1); + data = img_allocator.allocate(); img_allocator.construct(std::get(data)); for (size_t i = 0; i < d->size(); i++) @@ -98,12 +118,6 @@ class image return *this; } - image(const blt::vec3& v): data(v) - {} - - image(float f): data(f) - {} - image_data_t& getData() { if (std::holds_alternative(data)) @@ -123,7 +137,7 @@ class image } }, data); - data = img_allocator.allocate(1); + data = img_allocator.allocate(); img_allocator.construct(std::get(data)); for (auto& v : *std::get(data)) @@ -151,7 +165,7 @@ class image } }, data); - data = img_allocator.allocate(1); + data = img_allocator.allocate(); img_allocator.construct(std::get(data)); for (auto& v : *std::get(data)) @@ -181,7 +195,7 @@ class image { if (!std::holds_alternative(data)) { - data = img_allocator.allocate(1); + data = img_allocator.allocate(); img_allocator.construct(std::get(data)); } (*std::get(data))[y * height + x] = c; @@ -190,7 +204,10 @@ class image ~image() { if (std::holds_alternative(data)) + { + BLT_TRACE("%p", std::get(data)); img_allocator.deallocate(std::get(data), 1); + } } }; diff --git a/libraries/BLT-With-Graphics-Template b/libraries/BLT-With-Graphics-Template index fab759d..3da2a52 160000 --- a/libraries/BLT-With-Graphics-Template +++ b/libraries/BLT-With-Graphics-Template @@ -1 +1 @@ -Subproject commit fab759dd4c7c1dd02feae2f2d6c3eb28ceac09da +Subproject commit 3da2a52953a450176f609b017d7764c726b7ff42 diff --git a/src/functions.cpp b/src/functions.cpp index 56fb804..1db6bcd 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -71,12 +71,12 @@ inline float protect_div(float x, float y, float d = 0) void f_x(image& img, float x, float y, blt::size_t argc, const image** argv, const data_t& extra_data) { - img = x; + img = image(x); } void f_y(image& img, float x, float y, blt::size_t argc, const image** argv, const data_t& extra_data) { - img = y; + img = image(y); } void f_random(image& img, float x, float y, blt::size_t argc, const image** argv, const data_t& extra_data) @@ -84,7 +84,7 @@ void f_random(image& img, float x, float y, blt::size_t argc, const image** argv static std::random_device dev; static std::mt19937_64 engine(dev()); static std::uniform_real_distribution dist(0, 1); - img = dist(engine); + img = image(dist(engine)); } void f_noise(image& img, float x, float y, blt::size_t argc, const image** argv, const data_t& extra_data) @@ -141,12 +141,12 @@ void f_cnoise(image& img, float x, float y, blt::size_t argc, const image** argv void f_scalar(image& img, float x, float y, blt::size_t argc, const image** argv, const data_t& extra_data) { - img = extra_data[0]; + img = image(extra_data[0]); } void f_color(image& img, float x, float y, blt::size_t argc, const image** argv, const data_t& extra_data) { - img = blt::vec3{extra_data[0], extra_data[1], extra_data[2]}; + img = image(blt::vec3{extra_data[0], extra_data[1], extra_data[2]}); } void f_log(image& img, float x, float y, blt::size_t argc, const image** argv, const data_t& extra_data) @@ -399,3 +399,20 @@ float eval_BAM(const image& img, const image& compare, float allowed_diff) return values; } +float eval_DST(const image& img, const image& compare) +{ + float dist = 0; + for (blt::i32 i = 0; i < width; i++) + { + for (blt::i32 j = 0; j < height; j++) + { + auto px_img = img.get(i, j); + auto px_cmp = compare.get(i, j); + auto dist_v = px_img - px_cmp; + auto dist_srd = dist_v * dist_v; + dist += std::sqrt(dist_srd.x() + dist_srd.y() + dist_srd.z()); + } + } + return 1 - (1 / (1 + (dist / static_cast(width)))); +} + diff --git a/src/gp.cpp b/src/gp.cpp index 148f431..edda7b6 100644 --- a/src/gp.cpp +++ b/src/gp.cpp @@ -23,11 +23,11 @@ #include #include -blt::bump_allocator node_allocator(32000); +blt::bump_allocator node_allocator(32000); node* createNode(function_t type) { - auto* n = node_allocator.allocate(1); + auto* n = node_allocator.allocate(); node_allocator.construct(n, type); return n; } @@ -236,7 +236,7 @@ void node::evaluate() if (function_t::NAME == function_t::IF) { \ /*std::cout << "__:" << function_name_map[to_underlying(this->sub_nodes[0]->type)] << std::endl;*/ \ }\ - if (FUNC_ALLOW_TERMINALS_SET.contains(function_t::NAME)){ \ + if (FUNC_ALLOW_TERMINALS_SET.contains(function_t::NAME) && false){ \ FUNC(img.value(), 0, 0, argc, const_cast(sub_node_images.data()), data); \ } else { \ for (blt::i32 y = 0; y < height; y++) { \ @@ -271,7 +271,7 @@ node::node(const node& copy) node* node::clone() { - auto np = node_allocator.allocate(1); + auto np = node_allocator.allocate(1); // tee hee ::new(np) node(*this); return np; diff --git a/src/main.cpp b/src/main.cpp index d080927..b17abd1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -251,7 +251,8 @@ class tree float fitness() { auto& img = root->getImage(); - return eval_BAM(img, compare, 0.1) * eval_DNF_SW_1(img) * static_cast(std::min(depth(root.get()), 5ul)); + //return eval_BAM(img, compare, 0.1) * eval_DNF_SW_1(img) * static_cast(std::min(depth(root.get()), 5ul)); + return eval_DST(img, compare); } void printTree() @@ -272,11 +273,12 @@ class gp_population struct gp_i { std::unique_ptr t = nullptr; - float fitness = 0; + float r_fitness = 0; + float a_fitness = 0; [[nodiscard]] inline gp_i clone() const { - return {t->clone(), fitness}; + return {t->clone(), r_fitness}; } }; @@ -317,11 +319,11 @@ class gp_population } while (n == p[index].t.get()); auto& v = p[index]; - if (v.fitness >= fitness) + if (v.r_fitness >= fitness) { n = v.t.get(); ni = index; - fitness = v.fitness; + fitness = v.r_fitness; } } BLT_ASSERT(n != nullptr); @@ -339,7 +341,7 @@ class gp_population new_pop[0] = {pop[b.first].t->clone(), b.second}; for (blt::size_t i = 1; i < POPULATION_SIZE; i++) - new_pop[i] = {pop[i].t->clone(), pop[i].fitness}; + new_pop[i] = {pop[i].t->clone(), pop[i].r_fitness}; blt::size_t crossover_count = 0; blt::size_t mutation_count = 0; @@ -392,7 +394,7 @@ class gp_population for (auto& v : pop) { v.t->evaluate(); - v.fitness = v.t->fitness(); + v.r_fitness = v.t->fitness(); } BLT_TRACE("Complete"); } @@ -408,10 +410,10 @@ class gp_population float fitness = -2 * 8192; for (blt::size_t j = 0; j < POPULATION_SIZE; j++) { - if (pop[j].fitness > fitness) + if (pop[j].r_fitness > fitness) { i = j; - fitness = pop[j].fitness; + fitness = pop[j].r_fitness; } } best = i;