45using TreeIter = tree<Node>::pre_order_iterator;
54extern std::unordered_map<std::size_t, std::string>
ArgsName;
148 template<
typename PT>
196 auto msg = fmt::format(
"{} not in node_map\n",
R);
210 auto msg = fmt::format(
"{} not in node_map.at({})\n", sig_hash,
R);
227 auto msg = fmt::format(
"{} not in node_map[{}][{}]\n",type, sig_hash,
R);
240 template<
typename Iter>
242 return !std::all_of(
start, end, [](
const auto&
w) {
return w<=0.0; });
245 template<
typename F>
Node get(
const string& name);
255 return node_map.at(
R).at(sig_hash).at(type);
338 return std::reduce(tw.second.begin(), tw.second.end()); }
367 [](
const auto&
w){ return w; });
402 [](
const auto&
w){ return w; }
463 vector<float> weights;
475 if ( (weights.size()==0)
505 vector<float> weights;
538 if ( (weights.size()==0)
544 weights.begin(), weights.end()));
584 template<NodeType NT,
typename S>
587 const auto& unique_data_types,
594 for (
auto arg: S::get_arg_types()){
595 if (!
in(unique_data_types,
arg) ){
599 ArgsName[S::hash()] = fmt::format(
"{}", S::get_arg_types());
603 template<NodeType NT,
typename S>
605 const unordered_map<string,float>&
user_ops,
606 const vector<DataType>& unique_data_types
629 const vector<DataType> &unique_data_types,
630 std::index_sequence<Is...>)
635 template<NodeType NT>
637 const vector<DataType>& unique_data_types
650 constexpr auto size = std::tuple_size<signatures>::value;
654 std::make_index_sequence<size>()
658 template<std::size_t...
Is>
660 const vector<DataType>& unique_data_types,
661 std::index_sequence<Is...>
664 auto nt = [](
auto i) {
return static_cast<NodeType>(1UL <<
i); };
674 std::swap(
Q[
loc],
Q[
Q.size()-1]);
699 tree<Node>::iterator
spot;
734 std::optional<Node>
opt=std::nullopt;
736 if (max_size>1 &&
max_d>1)
744 spot = Tree.insert(Tree.begin(),
root);
750 return P(*
this, Tree);
759 template <
typename FormatContext>
761 string output =
"Search Space\n===\n";
765 for (
const auto& [args_type,
v2] :
v) {
766 for (
const auto& [node_type,
node] :
v2) {
767 output += fmt::format(
"node_map[{}][{}][{}] = {}, weight = {}\n",
778 return formatter<string_view>::format(
output,
ctx);
void bind_engine(py::module &m, string name)
holds variable type data.
Iter select_randomly(Iter start, Iter end)
int rnd_int(int lowerLimit, int upperLimit)
#define HANDLE_ERROR_THROW(err)
namespace containing Data structures used in Brush
namespace containing various utility functions
bool in(const V &v, const T &i)
check if element is in vector.
< nsga2 selection operator for getting the front
auto Is(NodeType nt) -> bool
std::unordered_map< std::size_t, std::string > ArgsName
vector< Node > generate_terminals(const Dataset &d, const bool weights_init)
generate terminals from the dataset features and random constants.
T RandomDequeue(std::vector< T > &Q)
queue for make program
tree< Node >::pre_order_iterator Iter
tree< Node >::pre_order_iterator TreeIter
std::map< NodeType, std::string > NodeTypeName
class holding the data for a node in a tree.
An individual program, a.k.a. model.
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...
unordered_map< DataType, unordered_map< ArgsHash, unordered_map< NodeType, T > > > Map
void print() const
prints the search space map.
static constexpr std::optional< Node > CreateNode(const auto &unique_data_types, bool use_all, bool weighted)
Node get(const string &name)
constexpr void AddNode(const unordered_map< string, float > &user_ops, const vector< DataType > &unique_data_types)
Map< Node > node_map
Maps return types to argument types to node types.
unordered_map< DataType, vector< Node > > terminal_map
Maps return types to terminals.
constexpr void AddNodes(const unordered_map< string, float > &user_ops, const vector< DataType > &unique_data_types, std::index_sequence< Is... >)
std::optional< Node > sample_terminal(DataType R, bool force_return=false) const
Get a random terminal with return type R
void init(const Dataset &d, const unordered_map< string, float > &user_ops={}, bool weights_init=true)
Called by the constructor to initialize the search space.
RegressorProgram make_regressor(int max_d=0, int max_size=0, const Parameters ¶ms=Parameters())
Makes a random regressor program. Convenience wrapper for make_program.
bool check(DataType R, size_t sig_hash, NodeType type) const
check if a typed Node is in the search space
Node get(NodeType type, DataType R, S sig)
get a typed node.
bool check(DataType R) const
check if a return type is in the node map
unordered_map< DataType, vector< float > > terminal_weights
A map of weights corresponding to elements in terminal_map, used to weight probabilities of each term...
std::optional< Node > sample_op(NodeType type, DataType R)
Get a specific node type that matches a return value.
vector< float > get_weights() const
get weights of the return types
Node get(NodeType type, DataType R, size_t sig_hash)
get a typed node
vector< float > get_weights(DataType ret) const
get weights of the argument types matching return type ret.
bool check(DataType R, size_t sig_hash) const
check if a function signature is in the search space
void GenerateNodeMap(const unordered_map< string, float > &user_ops, const vector< DataType > &unique_data_types, std::index_sequence< Is... >)
tree< Node > & PTC2(tree< Node > &Tree, tree< Node >::iterator root, int max_d, int max_size) const
std::optional< Node > sample_op(DataType ret) const
get an operator matching return type ret.
SearchSpace(const Dataset &d, const unordered_map< string, float > &user_ops={}, bool weights_init=true)
Construct a search space.
std::optional< Node > get_node_like(Node node) const
get a node with a signature matching node
vector< float > get_weights(DataType ret, ArgsHash sig_hash) const
get the weights of nodes matching a signature.
RepresenterProgram make_representer(int max_d=0, int max_size=0, const Parameters ¶ms=Parameters())
Makes a random representer program. Convenience wrapper for make_program.
std::optional< Node > sample_op_with_arg(DataType ret, DataType arg, bool terminal_compatible=true, int max_args=0) const
get operator with at least one argument matching arg
Map< float > node_map_weights
A map of weights corresponding to elements in node_map, used to weight probabilities of each node bei...
MulticlassClassifierProgram make_multiclass_classifier(int max_d=0, int max_size=0, const Parameters ¶ms=Parameters())
Makes a random multiclass classifier program. Convenience wrapper for make_program.
std::optional< tree< Node > > sample_subtree(Node root, int max_d, int max_size) const
create a subtree with maximum size and depth restrictions and root of type root_type
PT make_program(const Parameters ¶ms, int max_d=0, int max_size=0)
Makes a random program.
vector< DataType > terminal_types
A vector storing the available return types of terminals.
bool has_solution_space(Iter start, Iter end) const
Takes iterators to weight vectors and checks if they have a non-empty solution space....
void MakeNodes(const unordered_map< string, float > &user_ops, const vector< DataType > &unique_data_types)
ClassifierProgram make_classifier(int max_d=0, int max_size=0, const Parameters ¶ms=Parameters())
Makes a random classifier program. Convenience wrapper for make_program.
std::optional< Node > sample_terminal(bool force_return=false) const
Get a random terminal.