2024-07-08 22:20:51 -04:00
|
|
|
/*
|
|
|
|
* <Short Description>
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <blt/gp/program.h>
|
|
|
|
#include <blt/gp/tree.h>
|
|
|
|
#include <blt/std/logging.h>
|
|
|
|
|
2024-09-12 15:24:27 -04:00
|
|
|
static const auto SEED_FUNC = [] { return std::random_device()(); };
|
2024-07-08 22:20:51 -04:00
|
|
|
|
2024-07-11 04:11:24 -04:00
|
|
|
blt::gp::prog_config_t config = blt::gp::prog_config_t().set_elite_count(2);
|
2024-07-09 21:57:18 -04:00
|
|
|
|
2024-09-12 15:24:27 -04:00
|
|
|
blt::gp::gp_program program(SEED_FUNC, config); // NOLINT
|
2024-07-09 21:57:18 -04:00
|
|
|
std::array<float, 500> result_container;
|
2024-07-08 22:20:51 -04:00
|
|
|
|
|
|
|
blt::gp::operation_t add([](float a, float b) { return a + b; }, "add"); // 0
|
|
|
|
blt::gp::operation_t sub([](float a, float b) { return a - b; }, "sub"); // 1
|
|
|
|
blt::gp::operation_t mul([](float a, float b) { return a * b; }, "mul"); // 2
|
|
|
|
blt::gp::operation_t pro_div([](float a, float b) { return b == 0 ? 0.0f : a / b; }, "div"); // 3
|
|
|
|
|
|
|
|
blt::gp::operation_t op_if([](bool b, float a, float c) { return b ? a : c; }, "if"); // 4
|
|
|
|
blt::gp::operation_t eq_f([](float a, float b) { return a == b; }, "eq_f"); // 5
|
|
|
|
blt::gp::operation_t eq_b([](bool a, bool b) { return a == b; }, "eq_b"); // 6
|
|
|
|
blt::gp::operation_t lt([](float a, float b) { return a < b; }, "lt"); // 7
|
|
|
|
blt::gp::operation_t gt([](float a, float b) { return a > b; }, "gt"); // 8
|
|
|
|
blt::gp::operation_t op_and([](bool a, bool b) { return a && b; }, "and"); // 9
|
|
|
|
blt::gp::operation_t op_or([](bool a, bool b) { return a || b; }, "or"); // 10
|
|
|
|
blt::gp::operation_t op_xor([](bool a, bool b) { return static_cast<bool>(a ^ b); }, "xor"); // 11
|
|
|
|
blt::gp::operation_t op_not([](bool b) { return !b; }, "not"); // 12
|
|
|
|
|
2024-08-21 20:40:42 -04:00
|
|
|
auto lit = blt::gp::operation_t([]() {
|
2024-07-08 22:20:51 -04:00
|
|
|
//static std::uniform_real_distribution<float> dist(-32000, 32000);
|
2024-07-09 21:57:18 -04:00
|
|
|
// static std::uniform_real_distribution<float> dist(0.0f, 10.0f);
|
|
|
|
return program.get_random().get_float(0.0f, 10.0f);
|
2024-08-21 20:40:42 -04:00
|
|
|
}).set_ephemeral();
|
2024-07-08 22:20:51 -04:00
|
|
|
|
2024-07-09 21:57:18 -04:00
|
|
|
void print_best()
|
|
|
|
{
|
|
|
|
BLT_TRACE("----{Current Gen: %ld}----", program.get_current_generation());
|
2024-07-10 22:00:28 -04:00
|
|
|
auto best = program.get_best_indexes<10>();
|
2024-07-09 21:57:18 -04:00
|
|
|
|
2024-07-10 22:00:28 -04:00
|
|
|
for (auto& i : best)
|
2024-07-10 12:39:01 -04:00
|
|
|
{
|
2024-07-10 22:00:28 -04:00
|
|
|
auto& v = program.get_current_pop().get_individuals()[i];
|
|
|
|
auto& tree = v.tree;
|
|
|
|
auto size = tree.get_values().size();
|
2024-08-19 21:03:18 -04:00
|
|
|
BLT_TRACE("%lf [index %ld] (fitness: %lf, raw: %lf) (depth: %ld) (size: t: %ld u: %ld r: %ld) filled: %f%%",
|
2024-09-12 15:24:27 -04:00
|
|
|
tree.get_evaluation_value<float>(), i, v.fitness.adjusted_fitness, v.fitness.raw_fitness,
|
2024-08-19 21:03:18 -04:00
|
|
|
tree.get_depth(program), size.total_size_bytes, size.total_used_bytes,
|
2024-07-10 22:00:28 -04:00
|
|
|
size.total_remaining_bytes,
|
2024-08-19 21:03:18 -04:00
|
|
|
static_cast<double>(size.total_used_bytes) / (size.total_size_bytes == 0 ? 1 : static_cast<double>(size.total_size_bytes)));
|
2024-07-10 12:39:01 -04:00
|
|
|
}
|
2024-07-10 02:54:58 -04:00
|
|
|
//std::string small("--------------------------");
|
|
|
|
//for (blt::size_t i = 0; i < std::to_string(program.get_current_generation()).size(); i++)
|
|
|
|
// small += "-";
|
|
|
|
//BLT_TRACE(small);
|
2024-07-09 21:57:18 -04:00
|
|
|
}
|
|
|
|
|
2024-07-11 04:11:24 -04:00
|
|
|
constexpr auto fitness_function = [](blt::gp::tree_t& current_tree, blt::gp::fitness_t& fitness, blt::size_t index) {
|
|
|
|
/*auto size = current_tree.get_values().size();
|
|
|
|
BLT_DEBUG("(depth: %ld) (blocks: %ld) (size: t: %ld m: %ld u: %ld r: %ld) filled: %f%%",
|
|
|
|
current_tree.get_depth(program), size.blocks, size.total_size_bytes, size.total_no_meta_bytes, size.total_used_bytes,
|
|
|
|
size.total_remaining_bytes, static_cast<double>(size.total_used_bytes) / static_cast<double>(size.total_no_meta_bytes));*/
|
2024-09-12 15:24:27 -04:00
|
|
|
result_container[index] = current_tree.get_evaluation_value<float>();
|
2024-07-11 04:11:24 -04:00
|
|
|
fitness.raw_fitness = result_container[index] / 1000000000.0;
|
|
|
|
fitness.standardized_fitness = fitness.raw_fitness;
|
|
|
|
fitness.adjusted_fitness = 1.0 - (1.0 / (1.0 + fitness.raw_fitness));
|
|
|
|
};
|
|
|
|
|
2024-07-08 22:20:51 -04:00
|
|
|
/**
|
|
|
|
* This is a test using multiple types with blt::gp
|
|
|
|
*/
|
|
|
|
int main()
|
|
|
|
{
|
2024-09-12 15:24:27 -04:00
|
|
|
blt::gp::operator_builder builder{};
|
2024-08-21 20:40:42 -04:00
|
|
|
program.set_operations(builder.build(add, sub, mul, pro_div, op_if, eq_f, eq_b, lt, gt, op_and, op_or, op_xor, op_not, lit));
|
2024-07-08 22:20:51 -04:00
|
|
|
|
2024-08-19 21:03:18 -04:00
|
|
|
auto sel = blt::gp::select_tournament_t{};
|
2024-09-12 15:24:27 -04:00
|
|
|
program.generate_population(program.get_typesystem().get_type<float>().id(), fitness_function, sel, sel, sel);
|
2024-07-08 22:20:51 -04:00
|
|
|
|
2024-07-09 21:57:18 -04:00
|
|
|
while (!program.should_terminate())
|
2024-07-08 22:20:51 -04:00
|
|
|
{
|
2024-07-09 21:57:18 -04:00
|
|
|
print_best();
|
2024-08-19 21:03:18 -04:00
|
|
|
program.create_next_generation();
|
2024-07-09 21:57:18 -04:00
|
|
|
program.next_generation();
|
2024-07-11 04:11:24 -04:00
|
|
|
program.evaluate_fitness();
|
2024-07-08 22:20:51 -04:00
|
|
|
}
|
|
|
|
|
2024-07-09 21:57:18 -04:00
|
|
|
print_best();
|
2024-07-09 14:11:19 -04:00
|
|
|
|
2024-07-08 22:20:51 -04:00
|
|
|
return 0;
|
|
|
|
}
|