44 vector<float> weights;
48 std::transform(Tree.begin(), Tree.end(), std::back_inserter(weights),
50 size_t d = 1+Tree.depth(iter);
51 std::advance(iter, 1);
54 if ((d >= params.get_max_depth())
55 || (SS.node_map.find(n.ret_type) == SS.node_map.end())) {
59 return n.get_prob_change();
65 weights.resize(Tree.size());
66 std::fill(weights.begin(), weights.end(), 0.0f);
95 for (
auto a: (*n).arg_types)
117 Tree.insert(
spot,
opt.value());
144 Tree.erase_children(
spot);
146 Tree.replace(
spot,
opt.value());
164 vector<float> weights(Tree.size());
166 if (Tree.size() < params.
max_size) {
167 std::transform(Tree.begin(), Tree.end(), weights.begin(),
170 if (Is<NodeType::OffsetSum>(n.node_type))
174 if (!n.get_is_weighted()
175 && IsWeighable(n.ret_type))
177 return n.get_prob_change();
185 std::fill(weights.begin(), weights.end(), 0.0f);
194 if (
spot.node->data.get_is_weighted()==
true
198 spot.node->data.set_is_weighted(
true);
215 vector<float> weights(Tree.size());
217 std::transform(Tree.begin(), Tree.end(), weights.begin(),
220 if (Is<NodeType::OffsetSum>(n.node_type))
223 if (n.get_is_weighted()
224 && IsWeighable(n.ret_type))
225 return n.get_prob_change();
238 if (
spot.node->data.get_is_weighted()==
false)
241 spot.node->data.set_is_weighted(
false);
258 vector<float> weights;
262 if (Tree.size() < params.
max_size) {
264 std::transform(Tree.begin(), Tree.end(), std::back_inserter(weights),
266 size_t d = 1+Tree.depth(iter);
267 std::advance(iter, 1);
270 if ((d >= params.max_depth)
271 || (SS.node_map.find(n.ret_type) == SS.node_map.end())
272 || (SS.node_map.find(n.ret_type) == SS.node_map.end()) )
275 return n.get_prob_change();
279 weights.resize(Tree.size());
280 std::fill(weights.begin(), weights.end(), 0.0f);
302 size_t s = params.
max_size - (Tree.size() - Tree.size(
spot));
314 Tree.erase_children(
spot);
344template<Brush::ProgramType T>
358 auto s_at = child.size_at(child_iter);
359 auto d_at = child.depth_to_reach(child_iter);
361 std::advance(child_iter, 1);
363 if (s_at<parameters.max_size && d_at<parameters.max_depth)
364 return n.get_prob_change();
410 std::transform(
other.Tree.begin(),
other.Tree.end(),
415 if (check_and_incrm() && (n.ret_type == child_ret_type))
416 return n.get_prob_change();
442 ind.set_objectives(
mom.get_objectives());
485template<Brush::ProgramType T>
488 auto options = parameters.mutation_probs;
491 for (
auto &
it : parameters.mutation_probs) {
492 if (
it.second > 0.0) {
511 vector<float> weights;
515 weights = PointMutation::find_spots(
child.Tree, search_space, parameters);
516 else if (
choice ==
"insert")
518 else if (
choice ==
"delete")
519 weights = DeleteMutation::find_spots(
child.Tree, search_space, parameters);
520 else if (
choice ==
"subtree")
522 else if (
choice ==
"toggle_weight_on")
524 else if (
choice ==
"toggle_weight_off")
527 std::string
msg = fmt::format(
"{} not a valid mutation choice",
choice);
531 if (std::all_of(weights.begin(), weights.end(), [](
const auto&
w) {
540 weights.begin(), weights.end());
549 else if (
choice ==
"insert")
551 else if (
choice ==
"delete")
553 else if (
choice ==
"subtree")
555 else if (
choice ==
"toggle_weight_on")
562 && ( (
child.size() <= parameters.max_size)
563 && (
child.depth() <= parameters.max_depth) )){
577template <Brush::ProgramType T>
591 std::optional<Individual<T>>
opt=std::nullopt;
597 if (
r() < parameters.cx_prob)
612 unsigned id = parameters.current_gen*parameters.pop_size+
indices.at(
i);
void bind_engine(py::module &m, string name)
vector< string > get_objectives() const
void init(SearchSpace &ss, const Parameters ¶ms)
Program< T > program
executable data structure
vector< size_t > get_island_indexes(int island)
vector< std::shared_ptr< Individual< T > > > individuals
T random_choice(const map< T, float > &m)
Iter select_randomly(Iter start, Iter end)
int rnd_int(int lowerLimit, int upperLimit)
delete subtree and replace it with a terminal of the same return type
static auto mutate(tree< Node > &Tree, Iter spot, const SearchSpace &SS, const Parameters ¶ms)
insert a node with spot as a child
static auto find_spots(tree< Node > &Tree, const SearchSpace &SS, const Parameters ¶ms)
static auto mutate(tree< Node > &Tree, Iter spot, const SearchSpace &SS, const Parameters ¶ms)
tree< Node >::pre_order_iterator Iter
replace node with same typed node
static auto mutate(tree< Node > &Tree, Iter spot, const SearchSpace &SS, const Parameters ¶ms)
replaces the subtree rooted in spot
static auto find_spots(tree< Node > &Tree, const SearchSpace &SS, const Parameters ¶ms)
static auto mutate(tree< Node > &Tree, Iter spot, const SearchSpace &SS, const Parameters ¶ms)
toggle the node's weight OFF
static auto find_spots(tree< Node > &Tree, const SearchSpace &SS, const Parameters ¶ms)
static auto mutate(tree< Node > &Tree, Iter spot, const SearchSpace &SS, const Parameters ¶ms)
toggle the node's weight ON
static auto mutate(tree< Node > &Tree, Iter spot, const SearchSpace &SS, const Parameters ¶ms)
static auto find_spots(tree< Node > &Tree, const SearchSpace &SS, const Parameters ¶ms)
std::optional< Individual< T > > mutate(const Individual< T > &parent)
Performs mutation operation on an individual.
void vary(Population< T > &pop, int island, const vector< size_t > &parents)
Handles variation of a population.
std::optional< Individual< T > > cross(const Individual< T > &mom, const Individual< T > &dad)
Performs crossover operation on two individuals.
#define HANDLE_ERROR_THROW(err)
< nsga2 selection operator for getting the front
auto IsWeighable() noexcept -> bool
unsigned get_max_size() const
An individual program, a.k.a. model.
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...
Map< Node > node_map
Maps return types to argument types to node types.
std::optional< Node > get_node_like(Node node) const
get a node with a signature matching node
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
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
std::optional< Node > sample_terminal(bool force_return=false) const
Get a random terminal.