non-fix, this code is wack lol

main
Brett 2024-03-24 17:45:27 -04:00
parent a74eea9a9b
commit ec4d29ab79
8 changed files with 80 additions and 39 deletions

View File

@ -38,8 +38,8 @@ add_executable(gp_image_test ${PROJECT_BUILD_FILES})
add_dependencies(gp_image_test stats_lib_bindings_rust) 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_compile_options(gp_image_test PRIVATE -Wall -Wextra -Wpedantic -Wno-comment -Wno-unused-parameter)
target_link_options(gp_image_test PRIVATE -Wall -Wextra -Werror -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) target_link_libraries(gp_image_test BLT_WITH_GRAPHICS)

View File

@ -35,7 +35,7 @@ template<typename T>
using allowed_funcs = std::vector<T>; using allowed_funcs = std::vector<T>;
template<typename T> template<typename T>
using allowed_funcs_set = HASHSET<T>; using allowed_funcs_set = blt::hashset_t<T>;
template<typename T> template<typename T>
using func_list = std::vector<allowed_funcs<T>>; using func_list = std::vector<allowed_funcs<T>>;
@ -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_BAM(const image& img, const image& compare, float allowed_diff);
float eval_DST(const image& img, const image& compare);
//template<typename F> //template<typename F>
//bool isNan(F f) //bool isNan(F f)
//{ //{

View File

@ -56,6 +56,9 @@ struct node
void reset_children() 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++) for (size_t i = 0; i < argc; i++)
sub_nodes[i]->img.reset(); sub_nodes[i]->img.reset();
} }

View File

@ -30,24 +30,30 @@
#include <config.h> #include <config.h>
using image_data_t = std::array<blt::vec3, width * height>; using image_data_t = std::array<blt::vec3, width * height>;
inline blt::bump_allocator<image_data_t> img_allocator(8192); inline blt::bump_allocator<> img_allocator(8192);
class image class image
{ {
private: private:
mutable std::variant<image_data_t*, blt::vec3, float> data; mutable std::variant<image_data_t*, blt::vec3, float> data = nullptr;
public: public:
image() image()
{ {
data = img_allocator.allocate(1); data = img_allocator.allocate<image_data_t>();
img_allocator.construct(std::get<image_data_t*>(data)); img_allocator.construct(std::get<image_data_t*>(data));
} }
explicit image(const blt::vec3& v): data(v)
{}
explicit image(float f): data(f)
{}
image(const image& copy) image(const image& copy)
{ {
std::visit(blt::lambda_visitor{ std::visit(blt::lambda_visitor{
[&](const image_data_t* d) { [&](const image_data_t* d) {
data = img_allocator.allocate(1); data = img_allocator.allocate<image_data_t>();
img_allocator.construct(std::get<image_data_t*>(data)); img_allocator.construct(std::get<image_data_t*>(data));
for (size_t i = 0; i < d->size(); i++) for (size_t i = 0; i < d->size(); i++)
@ -64,7 +70,18 @@ class image
image(image&& move) noexcept 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; move.data = nullptr;
} }
@ -72,11 +89,14 @@ class image
{ {
if (&copy == this) if (&copy == this)
return *this; return *this;
if (std::holds_alternative<image_data_t*>(data))
{
BLT_INFO("Copy deallocate");
img_allocator.deallocate(std::get<image_data_t*>(data), 1);
}
std::visit(blt::lambda_visitor{ std::visit(blt::lambda_visitor{
[&](const image_data_t* d) { [&](const image_data_t* d) {
if (std::holds_alternative<image_data_t*>(data)) data = img_allocator.allocate<image_data_t>();
img_allocator.deallocate(std::get<image_data_t*>(data), 1);
data = img_allocator.allocate(1);
img_allocator.construct(std::get<image_data_t*>(data)); img_allocator.construct(std::get<image_data_t*>(data));
for (size_t i = 0; i < d->size(); i++) for (size_t i = 0; i < d->size(); i++)
@ -98,12 +118,6 @@ class image
return *this; return *this;
} }
image(const blt::vec3& v): data(v)
{}
image(float f): data(f)
{}
image_data_t& getData() image_data_t& getData()
{ {
if (std::holds_alternative<image_data_t*>(data)) if (std::holds_alternative<image_data_t*>(data))
@ -123,7 +137,7 @@ class image
} }
}, data); }, data);
data = img_allocator.allocate(1); data = img_allocator.allocate<image_data_t>();
img_allocator.construct(std::get<image_data_t*>(data)); img_allocator.construct(std::get<image_data_t*>(data));
for (auto& v : *std::get<image_data_t*>(data)) for (auto& v : *std::get<image_data_t*>(data))
@ -151,7 +165,7 @@ class image
} }
}, data); }, data);
data = img_allocator.allocate(1); data = img_allocator.allocate<image_data_t>();
img_allocator.construct(std::get<image_data_t*>(data)); img_allocator.construct(std::get<image_data_t*>(data));
for (auto& v : *std::get<image_data_t*>(data)) for (auto& v : *std::get<image_data_t*>(data))
@ -181,7 +195,7 @@ class image
{ {
if (!std::holds_alternative<image_data_t*>(data)) if (!std::holds_alternative<image_data_t*>(data))
{ {
data = img_allocator.allocate(1); data = img_allocator.allocate<image_data_t>();
img_allocator.construct(std::get<image_data_t*>(data)); img_allocator.construct(std::get<image_data_t*>(data));
} }
(*std::get<image_data_t*>(data))[y * height + x] = c; (*std::get<image_data_t*>(data))[y * height + x] = c;
@ -190,7 +204,10 @@ class image
~image() ~image()
{ {
if (std::holds_alternative<image_data_t*>(data)) if (std::holds_alternative<image_data_t*>(data))
{
BLT_TRACE("%p", std::get<image_data_t*>(data));
img_allocator.deallocate(std::get<image_data_t*>(data), 1); img_allocator.deallocate(std::get<image_data_t*>(data), 1);
}
} }
}; };

@ -1 +1 @@
Subproject commit fab759dd4c7c1dd02feae2f2d6c3eb28ceac09da Subproject commit 3da2a52953a450176f609b017d7764c726b7ff42

View File

@ -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) 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) 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) 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::random_device dev;
static std::mt19937_64 engine(dev()); static std::mt19937_64 engine(dev());
static std::uniform_real_distribution<float> dist(0, 1); static std::uniform_real_distribution<float> 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) 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) 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) 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) 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; 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<float>(width))));
}

View File

@ -23,11 +23,11 @@
#include <queue> #include <queue>
#include <stack> #include <stack>
blt::bump_allocator<node> node_allocator(32000); blt::bump_allocator node_allocator(32000);
node* createNode(function_t type) node* createNode(function_t type)
{ {
auto* n = node_allocator.allocate(1); auto* n = node_allocator.allocate<node>();
node_allocator.construct(n, type); node_allocator.construct(n, type);
return n; return n;
} }
@ -236,7 +236,7 @@ void node::evaluate()
if (function_t::NAME == function_t::IF) { \ if (function_t::NAME == function_t::IF) { \
/*std::cout << "__:" << function_name_map[to_underlying(this->sub_nodes[0]->type)] << std::endl;*/ \ /*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<const image**>(sub_node_images.data()), data); \ FUNC(img.value(), 0, 0, argc, const_cast<const image**>(sub_node_images.data()), data); \
} else { \ } else { \
for (blt::i32 y = 0; y < height; y++) { \ for (blt::i32 y = 0; y < height; y++) { \
@ -271,7 +271,7 @@ node::node(const node& copy)
node* node::clone() node* node::clone()
{ {
auto np = node_allocator.allocate(1); auto np = node_allocator.allocate<node>(1);
// tee hee // tee hee
::new(np) node(*this); ::new(np) node(*this);
return np; return np;

View File

@ -251,7 +251,8 @@ class tree
float fitness() float fitness()
{ {
auto& img = root->getImage(); auto& img = root->getImage();
return eval_BAM(img, compare, 0.1) * eval_DNF_SW_1(img) * static_cast<float>(std::min(depth(root.get()), 5ul)); //return eval_BAM(img, compare, 0.1) * eval_DNF_SW_1(img) * static_cast<float>(std::min(depth(root.get()), 5ul));
return eval_DST(img, compare);
} }
void printTree() void printTree()
@ -272,11 +273,12 @@ class gp_population
struct gp_i struct gp_i
{ {
std::unique_ptr<tree> t = nullptr; std::unique_ptr<tree> t = nullptr;
float fitness = 0; float r_fitness = 0;
float a_fitness = 0;
[[nodiscard]] inline gp_i clone() const [[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()); } while (n == p[index].t.get());
auto& v = p[index]; auto& v = p[index];
if (v.fitness >= fitness) if (v.r_fitness >= fitness)
{ {
n = v.t.get(); n = v.t.get();
ni = index; ni = index;
fitness = v.fitness; fitness = v.r_fitness;
} }
} }
BLT_ASSERT(n != nullptr); BLT_ASSERT(n != nullptr);
@ -339,7 +341,7 @@ class gp_population
new_pop[0] = {pop[b.first].t->clone(), b.second}; new_pop[0] = {pop[b.first].t->clone(), b.second};
for (blt::size_t i = 1; i < POPULATION_SIZE; i++) 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 crossover_count = 0;
blt::size_t mutation_count = 0; blt::size_t mutation_count = 0;
@ -392,7 +394,7 @@ class gp_population
for (auto& v : pop) for (auto& v : pop)
{ {
v.t->evaluate(); v.t->evaluate();
v.fitness = v.t->fitness(); v.r_fitness = v.t->fitness();
} }
BLT_TRACE("Complete"); BLT_TRACE("Complete");
} }
@ -408,10 +410,10 @@ class gp_population
float fitness = -2 * 8192; float fitness = -2 * 8192;
for (blt::size_t j = 0; j < POPULATION_SIZE; j++) for (blt::size_t j = 0; j < POPULATION_SIZE; j++)
{ {
if (pop[j].fitness > fitness) if (pop[j].r_fitness > fitness)
{ {
i = j; i = j;
fitness = pop[j].fitness; fitness = pop[j].r_fitness;
} }
} }
best = i; best = i;