2024-06-29 14:03:57 -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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* <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/generators.h>
|
|
|
|
#include <blt/gp/tree.h>
|
|
|
|
#include <blt/std/logging.h>
|
2024-07-03 14:04:48 -04:00
|
|
|
#include <blt/gp/transformers.h>
|
2024-07-03 21:27:57 -04:00
|
|
|
#include <string_view>
|
|
|
|
#include <iostream>
|
2024-06-29 14:03:57 -04:00
|
|
|
|
2024-09-12 15:24:27 -04:00
|
|
|
static const auto SEED_FUNC = [] { return std::random_device()(); };
|
2024-06-29 14:03:57 -04:00
|
|
|
|
2024-09-12 15:24:27 -04:00
|
|
|
blt::gp::gp_program program(SEED_FUNC); // NOLINT
|
2024-06-29 14:03:57 -04:00
|
|
|
|
2024-07-03 21:27:57 -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
|
2024-06-29 14:03:57 -04:00
|
|
|
|
2024-07-03 21:27:57 -04:00
|
|
|
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-06-29 14:03:57 -04:00
|
|
|
|
2024-08-21 20:40:42 -04:00
|
|
|
auto lit = blt::gp::operation_t([]() {
|
2024-06-29 14:03:57 -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);
|
2024-08-21 20:40:42 -04:00
|
|
|
return program.get_random().get_float(0.0f, 10.0f);
|
|
|
|
}).set_ephemeral();
|
2024-06-29 14:03:57 -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-06-29 14:03:57 -04:00
|
|
|
|
|
|
|
blt::gp::ramped_half_initializer_t pop_init;
|
|
|
|
|
2024-09-12 15:24:27 -04:00
|
|
|
auto pop = pop_init.generate(blt::gp::initializer_arguments{program, program.get_typesystem().get_type<float>().id(), 500, 3, 10});
|
2024-07-03 14:04:48 -04:00
|
|
|
|
|
|
|
// for (auto& tree : pop.getIndividuals())
|
|
|
|
// {
|
|
|
|
// auto value = tree.get_evaluation_value<float>(nullptr);
|
|
|
|
//
|
|
|
|
// BLT_TRACE(value);
|
|
|
|
// }
|
|
|
|
|
|
|
|
blt::gp::crossover_t crossover;
|
|
|
|
|
2024-07-09 00:49:49 -04:00
|
|
|
auto& ind = pop.get_individuals();
|
2024-06-29 14:03:57 -04:00
|
|
|
|
2024-07-03 21:27:57 -04:00
|
|
|
std::vector<float> pre;
|
|
|
|
std::vector<float> pos;
|
|
|
|
blt::size_t errors = 0;
|
|
|
|
BLT_INFO("Pre-Crossover:");
|
2024-07-09 00:49:49 -04:00
|
|
|
for (auto& tree : pop.get_individuals())
|
2024-06-29 14:03:57 -04:00
|
|
|
{
|
2024-09-12 15:24:27 -04:00
|
|
|
auto f = tree.tree.get_evaluation_value<float>();
|
2024-07-03 21:27:57 -04:00
|
|
|
pre.push_back(f);
|
|
|
|
BLT_TRACE(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
BLT_INFO("Crossover:");
|
|
|
|
blt::gp::population_t new_pop;
|
2024-07-09 00:49:49 -04:00
|
|
|
while (new_pop.get_individuals().size() < pop.get_individuals().size())
|
2024-07-03 14:04:48 -04:00
|
|
|
{
|
2024-07-03 21:27:57 -04:00
|
|
|
auto& random = program.get_random();
|
2024-07-09 21:57:18 -04:00
|
|
|
blt::size_t first = random.get_size_t(0ul, pop.get_individuals().size());
|
2024-07-03 21:27:57 -04:00
|
|
|
blt::size_t second;
|
|
|
|
do
|
2024-07-03 14:04:48 -04:00
|
|
|
{
|
2024-07-09 21:57:18 -04:00
|
|
|
second = random.get_size_t(0ul, pop.get_individuals().size());
|
2024-07-03 21:27:57 -04:00
|
|
|
} while (second == first);
|
|
|
|
|
2024-09-12 15:24:27 -04:00
|
|
|
blt::gp::tree_t child1{program};
|
|
|
|
blt::gp::tree_t child2{program};
|
|
|
|
// crossover function assumes that children have been copied from parents
|
|
|
|
child1.copy_fast(ind[first].tree);
|
|
|
|
child2.copy_fast(ind[second].tree);
|
|
|
|
auto results = crossover.apply(program, ind[first].tree, ind[second].tree, child1, child2);
|
|
|
|
if (results)
|
2024-07-03 21:27:57 -04:00
|
|
|
{
|
|
|
|
// bool print_literals = true;
|
|
|
|
// bool pretty_print = false;
|
|
|
|
// bool print_returns = false;
|
2024-09-12 15:24:27 -04:00
|
|
|
// BLT_TRACE("Parent 1: %f", ind[0].get_evaluation_value<float>());
|
2024-07-03 21:27:57 -04:00
|
|
|
// ind[0].print(program, std::cout, print_literals, pretty_print, print_returns);
|
2024-09-12 15:24:27 -04:00
|
|
|
// BLT_TRACE("Parent 2: %f", ind[1].get_evaluation_value<float>());
|
2024-07-03 21:27:57 -04:00
|
|
|
// ind[1].print(program, std::cout, print_literals, pretty_print, print_returns);
|
|
|
|
// BLT_TRACE("------------");
|
2024-09-12 15:24:27 -04:00
|
|
|
// BLT_TRACE("Child 1: %f", results->child1.get_evaluation_value<float>());
|
2024-07-03 21:27:57 -04:00
|
|
|
// results->child1.print(program, std::cout, print_literals, pretty_print, print_returns);
|
2024-09-12 15:24:27 -04:00
|
|
|
// BLT_TRACE("Child 2: %f", results->child2.get_evaluation_value<float>());
|
2024-07-03 21:27:57 -04:00
|
|
|
// results->child2.print(program, std::cout, print_literals, pretty_print, print_returns);
|
2024-09-12 15:24:27 -04:00
|
|
|
new_pop.get_individuals().emplace_back(std::move(child1));
|
|
|
|
new_pop.get_individuals().emplace_back(std::move(child2));
|
2024-07-03 21:27:57 -04:00
|
|
|
} else
|
|
|
|
{
|
2024-09-12 15:24:27 -04:00
|
|
|
BLT_DEBUG("Crossover Failed.");
|
2024-07-03 21:27:57 -04:00
|
|
|
errors++;
|
2024-07-09 00:49:49 -04:00
|
|
|
new_pop.get_individuals().push_back(ind[first]);
|
|
|
|
new_pop.get_individuals().push_back(ind[second]);
|
2024-07-03 14:04:48 -04:00
|
|
|
}
|
2024-06-29 14:03:57 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 21:27:57 -04:00
|
|
|
BLT_INFO("Post-Crossover:");
|
2024-07-08 22:20:51 -04:00
|
|
|
for (auto& tree : new_pop.for_each_tree())
|
2024-07-03 21:27:57 -04:00
|
|
|
{
|
2024-09-12 15:24:27 -04:00
|
|
|
auto f = tree.get_evaluation_value<float>();
|
2024-07-03 21:27:57 -04:00
|
|
|
pos.push_back(f);
|
|
|
|
BLT_TRACE(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
BLT_INFO("Stats:");
|
|
|
|
blt::size_t eq = 0;
|
|
|
|
for (const auto& v : pos)
|
|
|
|
{
|
|
|
|
for (const auto m : pre)
|
|
|
|
{
|
|
|
|
if (v == m)
|
|
|
|
{
|
|
|
|
eq++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BLT_INFO("Equal values: %ld", eq);
|
|
|
|
BLT_INFO("Error times: %ld", errors);
|
2024-07-03 14:04:48 -04:00
|
|
|
|
2024-06-29 14:03:57 -04:00
|
|
|
return 0;
|
|
|
|
}
|