worked operators, need a terminal engine
parent
ac36a63c5b
commit
bfd70c7f81
|
@ -1,5 +1,5 @@
|
|||
cmake_minimum_required(VERSION 3.25)
|
||||
project(blt-gp VERSION 0.0.21)
|
||||
project(blt-gp VERSION 0.0.22)
|
||||
|
||||
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
|
||||
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)
|
||||
|
|
|
@ -78,39 +78,39 @@ void test()
|
|||
{
|
||||
std::vector<op> operations;
|
||||
std::vector<float> values;
|
||||
|
||||
|
||||
std::stack<op> tree_generator;
|
||||
tree_generator.push(generate_op());
|
||||
|
||||
|
||||
while (!tree_generator.empty())
|
||||
{
|
||||
auto opn = tree_generator.top();
|
||||
tree_generator.pop();
|
||||
|
||||
|
||||
operations.push_back(opn);
|
||||
if (opn == op::LIT)
|
||||
{
|
||||
values.push_back(random_value());
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// child 1
|
||||
if (choice())
|
||||
tree_generator.push(generate_op());
|
||||
else
|
||||
tree_generator.push(op::LIT);
|
||||
|
||||
|
||||
// child 2
|
||||
if (choice())
|
||||
tree_generator.push(generate_op());
|
||||
else
|
||||
tree_generator.push(op::LIT);
|
||||
}
|
||||
|
||||
|
||||
for (const auto& v : operations)
|
||||
std::cout << to_string(v) << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
|
||||
{
|
||||
std::stack<blt::size_t> process;
|
||||
for (const auto& v : operations)
|
||||
|
@ -160,11 +160,11 @@ void test()
|
|||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
for (const auto& v : values)
|
||||
std::cout << v << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
|
||||
{
|
||||
std::stack<blt::size_t> process;
|
||||
blt::size_t index = 0;
|
||||
|
@ -184,7 +184,7 @@ void test()
|
|||
std::cout << values[index++];
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
while (!process.empty())
|
||||
{
|
||||
auto top = process.top();
|
||||
|
@ -217,13 +217,13 @@ void test()
|
|||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::stack<float> process;
|
||||
std::stack<op> operators;
|
||||
|
||||
|
||||
for (const auto& v : operations)
|
||||
operators.push(v);
|
||||
|
||||
|
||||
while (!operators.empty())
|
||||
{
|
||||
auto oper = operators.top();
|
||||
|
@ -265,10 +265,10 @@ void test()
|
|||
std::cout << "\tresult: " << values.back() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::cout << process.size() << std::endl;
|
||||
std::cout << process.top() << std::endl;
|
||||
|
||||
|
||||
}
|
||||
//
|
||||
//struct silly
|
||||
|
@ -293,15 +293,21 @@ void test()
|
|||
// unsigned char data[9582];
|
||||
//};
|
||||
|
||||
blt::gp::stack_allocator alloc;
|
||||
blt::gp::type_system type_system;
|
||||
blt::gp::gp_program program(type_system);
|
||||
|
||||
blt::gp::operation_t<float(float, float)> add([](float a, float b) { return a + b; });
|
||||
blt::gp::operation_t<float(float, float)> sub([](float a, float b) { return a - b; });
|
||||
blt::gp::operation_t<float(float, float)> mul([](float a, float b) { return a * b; });
|
||||
blt::gp::operation_t<float(float, float)> pro_div([](float a, float b) { return b == 0 ? 0.0f : a / b; });
|
||||
blt::gp::operation_t<float()> lit([]() {return 0.0f;});
|
||||
|
||||
float nyah(float f, int i, bool b)
|
||||
{
|
||||
return f + static_cast<float>(i * b);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
type_system.register_type<float>();
|
||||
type_system.register_type<bool>();
|
||||
|
||||
// constexpr blt::size_t MAX_ALIGNMENT = 8;
|
||||
// test();
|
||||
// std::cout << alignof(silly) << " " << sizeof(silly) << std::endl;
|
||||
|
@ -364,18 +370,18 @@ int main()
|
|||
// alloc.push(550.3f);
|
||||
// alloc.push(20.1230345);
|
||||
// alloc.push(std::string("SillyString"));
|
||||
alloc.push(33.22f);
|
||||
alloc.push(120);
|
||||
alloc.push(true);
|
||||
|
||||
blt::gp::operation_t<float(float, int, bool)> silly_op(nyah);
|
||||
blt::gp::operation_t<float(float, float)> silly_op_2([](float f, float g) {
|
||||
return f + g;
|
||||
});
|
||||
|
||||
std::cout << silly_op(alloc) << std::endl;
|
||||
|
||||
std::cout << "Is empty? " << alloc.empty() << std::endl;
|
||||
// alloc.push(33.22f);
|
||||
// alloc.push(120);
|
||||
// alloc.push(true);
|
||||
//
|
||||
// blt::gp::operation_t<float(float, int, bool)> silly_op(nyah);
|
||||
// blt::gp::operation_t<float(float, float)> silly_op_2([](float f, float g) {
|
||||
// return f + g;
|
||||
// });
|
||||
|
||||
// std::cout << silly_op(alloc) << std::endl;
|
||||
//
|
||||
// std::cout << "Is empty? " << alloc.empty() << std::endl;
|
||||
|
||||
// std::cout << std::endl;
|
||||
//
|
||||
|
|
|
@ -19,28 +19,58 @@
|
|||
#ifndef BLT_GP_PROGRAM_H
|
||||
#define BLT_GP_PROGRAM_H
|
||||
|
||||
#include <blt/gp/stack.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <blt/gp/fwdecl.h>
|
||||
#include <functional>
|
||||
#include <blt/std/ranges.h>
|
||||
#include <blt/std/hashmap.h>
|
||||
#include <blt/std/types.h>
|
||||
#include <blt/std/utility.h>
|
||||
#include <type_traits>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <blt/std/ranges.h>
|
||||
#include <blt/std/hashmap.h>
|
||||
#include <blt/std/types.h>
|
||||
#include <blt/std/utility.h>
|
||||
#include <blt/std/memory.h>
|
||||
#include <blt/gp/fwdecl.h>
|
||||
#include <blt/gp/stack.h>
|
||||
|
||||
namespace blt::gp
|
||||
{
|
||||
template<typename T>
|
||||
struct integer_type
|
||||
{
|
||||
T id;
|
||||
|
||||
integer_type() = default;
|
||||
|
||||
integer_type(T id): id(id) // NOLINT
|
||||
{}
|
||||
|
||||
inline operator T() const // NOLINT
|
||||
{
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
struct operator_id : integer_type<blt::size_t>
|
||||
{
|
||||
using integer_type<blt::size_t>::integer_type;
|
||||
};
|
||||
|
||||
struct type_id : integer_type<blt::size_t>
|
||||
{
|
||||
using integer_type<blt::size_t>::integer_type;
|
||||
};
|
||||
|
||||
class type
|
||||
{
|
||||
public:
|
||||
type() = default;
|
||||
|
||||
template<typename T>
|
||||
static type make_type(blt::size_t id)
|
||||
static type make_type(type_id id)
|
||||
{
|
||||
return type(sizeof(T), id, blt::type_string<T>());
|
||||
}
|
||||
|
@ -50,7 +80,7 @@ namespace blt::gp
|
|||
return size_;
|
||||
}
|
||||
|
||||
[[nodiscard]] blt::size_t id() const
|
||||
[[nodiscard]] type_id id() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
@ -61,12 +91,19 @@ namespace blt::gp
|
|||
}
|
||||
|
||||
private:
|
||||
type(size_t size, size_t id, std::string_view name): size_(size), id_(id), name_(name)
|
||||
type(size_t size, type_id id, std::string_view name): size_(size), id_(id), name_(name)
|
||||
{}
|
||||
|
||||
blt::size_t size_;
|
||||
blt::size_t id_;
|
||||
std::string name_;
|
||||
blt::size_t size_{};
|
||||
type_id id_{};
|
||||
std::string name_{};
|
||||
};
|
||||
|
||||
class allowed_types_t
|
||||
{
|
||||
public:
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
class type_system
|
||||
|
@ -77,7 +114,7 @@ namespace blt::gp
|
|||
template<typename T>
|
||||
inline type register_type()
|
||||
{
|
||||
types[blt::type_string_raw<T>()](type::make_type<T>(types.size()));
|
||||
types.insert({blt::type_string_raw<T>(), type::make_type<T>(types.size())});
|
||||
return types[blt::type_string_raw<T>()];
|
||||
}
|
||||
|
||||
|
@ -167,27 +204,24 @@ namespace blt::gp
|
|||
template<typename Return, typename... Args>
|
||||
void add_operator(const operation_t<Return(Args...)>& op)
|
||||
{
|
||||
if (op.get_argc() == 0)
|
||||
terminals.push_back(operators.size());
|
||||
else
|
||||
non_terminals.push_back(operators.size());
|
||||
auto return_type_id = system.get_type<Return>().id();
|
||||
auto& operator_list = op.get_argc() == 0 ? terminals : non_terminals;
|
||||
operator_list[return_type_id].push_back(operators.size());
|
||||
|
||||
auto operator_index = operators.size();
|
||||
(argument_types[operator_index].push_back(system.get_type<Args>()), ...);
|
||||
operators.push_back(op.make_callable());
|
||||
|
||||
std::vector<type> types;
|
||||
(types.push_back(system.get_type<Args>()), ...);
|
||||
arg_types.push_back(std::move(types));
|
||||
|
||||
return_types.push_back(system.get_type<Return>());
|
||||
}
|
||||
|
||||
private:
|
||||
type_system system;
|
||||
blt::gp::stack_allocator alloc;
|
||||
// indexed from return TYPE ID, returns index of operator
|
||||
blt::expanding_buffer<std::vector<blt::size_t>> terminals;
|
||||
blt::expanding_buffer<std::vector<blt::size_t>> non_terminals;
|
||||
// indexed from OPERATOR NUMBER
|
||||
blt::expanding_buffer<std::vector<type>> argument_types;
|
||||
std::vector<std::function<void(stack_allocator&)>> operators;
|
||||
std::vector<blt::size_t> terminals;
|
||||
std::vector<blt::size_t> non_terminals;
|
||||
std::vector<type> return_types;
|
||||
std::vector<std::vector<type>> arg_types;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
2
lib/blt
2
lib/blt
|
@ -1 +1 @@
|
|||
Subproject commit 2266d64f042bc97b54e7e50dcaac730bcc3ab4c3
|
||||
Subproject commit 9ad96191ff91ec7efa6aa142e377201fe81b4c40
|
|
@ -14,4 +14,4 @@
|
|||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
*/
|
Loading…
Reference in New Issue