2024-06-02 13:38:24 -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/>.
|
2024-06-21 22:04:57 -04:00
|
|
|
*/
|
|
|
|
#include <blt/gp/generators.h>
|
2024-06-23 14:13:50 -04:00
|
|
|
#include <blt/gp/program.h>
|
|
|
|
#include <stack>
|
2024-06-21 22:04:57 -04:00
|
|
|
|
|
|
|
namespace blt::gp
|
|
|
|
{
|
|
|
|
|
2024-06-23 14:13:50 -04:00
|
|
|
struct stack
|
|
|
|
{
|
|
|
|
blt::gp::operator_id id;
|
|
|
|
blt::size_t depth;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline std::stack<stack> get_base_generator(gp_program& program)
|
|
|
|
{
|
|
|
|
std::stack<stack> tree_generator;
|
|
|
|
|
|
|
|
auto& system = program.get_typesystem();
|
|
|
|
// select a type which has a non-empty set of non-terminals
|
|
|
|
type base_type;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
base_type = system.select_type(program.get_random());
|
|
|
|
} while (system.get_type_non_terminals(base_type.id()).empty());
|
|
|
|
|
|
|
|
tree_generator.emplace(system.select_non_terminal(program.get_random(), base_type.id()), 0);
|
|
|
|
|
|
|
|
return tree_generator;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Func>
|
|
|
|
tree_t create_tree(Func func, gp_program& program, blt::size_t min_depth, blt::size_t max_depth)
|
2024-06-21 22:04:57 -04:00
|
|
|
{
|
2024-06-23 14:13:50 -04:00
|
|
|
std::stack<stack> tree_generator = get_base_generator(program);
|
2024-06-21 22:04:57 -04:00
|
|
|
tree_t tree;
|
2024-06-23 14:13:50 -04:00
|
|
|
|
|
|
|
while (!tree_generator.empty())
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-21 22:04:57 -04:00
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2024-06-23 14:13:50 -04:00
|
|
|
tree_t grow_generator_t::generate(gp_program& program, blt::size_t min_depth, blt::size_t max_depth)
|
|
|
|
{
|
|
|
|
return create_tree([]() {
|
|
|
|
|
|
|
|
}, program, min_depth, max_depth);
|
|
|
|
}
|
|
|
|
|
2024-06-21 22:04:57 -04:00
|
|
|
tree_t full_generator_t::generate(gp_program& program, blt::size_t min_depth, blt::size_t max_depth)
|
|
|
|
{
|
2024-06-23 14:13:50 -04:00
|
|
|
return create_tree([]() {
|
|
|
|
|
|
|
|
}, program, min_depth, max_depth);
|
2024-06-21 22:04:57 -04:00
|
|
|
}
|
|
|
|
}
|