1 // Copyright 2020, 2021 Francesco Biscani (bluescarni@gmail.com), Dario Izzo (dario.izzo@gmail.com)
2 //
3 // This file is part of the heyoka library.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla
6 // Public License v. 2.0. If a copy of the MPL was not distributed
7 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #ifndef HEYOKA_GP_HPP
10 #define HEYOKA_GP_HPP
11 
12 #include <cstddef>
13 #include <ostream>
14 #include <string>
15 #include <vector>
16 
17 #include <heyoka/detail/visibility.hpp>
18 #include <heyoka/expression.hpp>
19 #include <heyoka/splitmix64.hpp>
20 
21 namespace heyoka
22 {
23 
24 class HEYOKA_DLL_PUBLIC expression_generator
25 {
26 public:
27     enum class node_type { num, var, bo, u_fun, b_fun };
28 
29 private:
30     std::vector<std::string> m_vars;
31     std::vector<expression (*)(expression)> m_u_funcs;
32     std::vector<expression (*)(expression, expression)> m_b_funcs;
33     std::vector<double> m_weights;
34     double m_range_dbl;
35     mutable splitmix64 m_e;
36 
37 public:
38     explicit expression_generator(const std::vector<std::string> &, splitmix64 &);
39     expression operator()(unsigned, unsigned, unsigned = 0u) const;
40 
41     // getters
42     const std::vector<expression (*)(expression)> &get_u_funcs() const;
43     const std::vector<expression (*)(expression, expression)> &get_b_funcs() const;
44     const std::vector<std::string> &get_vars() const;
45     const double &get_range_dbl() const;
46     const std::vector<double> &get_weights() const;
47 
48     // setters
49     void set_u_funcs(const std::vector<expression (*)(expression)> &);
50     void set_b_funcs(const std::vector<expression (*)(expression, expression)> &);
51     void set_vars(const std::vector<std::string> &);
52     void set_range_dbl(const double &);
53     void set_weights(const std::vector<double> &);
54 };
55 
56 // Streaming operators
57 HEYOKA_DLL_PUBLIC std::ostream &operator<<(std::ostream &, const expression_generator &);
58 
59 // expression manipulators
60 HEYOKA_DLL_PUBLIC std::size_t count_nodes(const expression &);
61 HEYOKA_DLL_PUBLIC expression *fetch_from_node_id(expression &, std::size_t);
62 HEYOKA_DLL_PUBLIC void mutate(expression &, const expression_generator &, const double, splitmix64 &, const unsigned,
63                               const unsigned, const unsigned = 0u);
64 HEYOKA_DLL_PUBLIC void mutate(expression &, std::size_t, const expression_generator &, const unsigned, const unsigned);
65 HEYOKA_DLL_PUBLIC void crossover(expression &, expression &, splitmix64 &);
66 HEYOKA_DLL_PUBLIC void crossover(expression &, expression &, std::size_t, std::size_t);
67 
68 } // namespace heyoka
69 
70 #endif
71