Brush C++ API
A flexible interpretable machine learning framework
|
Class representing the variation operators in Brush. More...
#include <variation.h>
Public Member Functions | |
Variation ()=default | |
Default constructor. | |
Variation (Parameters ¶ms, SearchSpace &ss, Dataset &d) | |
Constructor that initializes the Variation object with parameters and search space. | |
~Variation () | |
Destructor. | |
void | init () |
Initializes the Variation object with parameters and search space. | |
std::optional< Individual< T > > | cross (const Individual< T > &mom, const Individual< T > &dad) |
Performs croearch_spaceover operation on two individuals. | |
std::optional< Individual< T > > | mutate (const Individual< T > &parent, string choice="") |
Performs mutation operation on an individual. | |
void | vary (Population< T > &pop, int island, const vector< size_t > &parents) |
Handles variation of a population. | |
void | update_ss () |
void | vary_and_update (Population< T > &pop, int island, const vector< size_t > &parents, const Dataset &data, Evaluation< T > &evaluator, bool do_simplification) |
Varies a population and updates the selection strategy based on rewards. | |
std::optional< Node > | bandit_sample_terminal (DataType R) |
std::optional< Node > | bandit_get_node_like (Node node) |
std::optional< Node > | bandit_sample_op_with_arg (DataType ret, DataType arg, int max_args=0) |
std::optional< Node > | bandit_sample_op (DataType ret) |
void | log_simplification_table (std::ofstream &log) |
Public Attributes | |
SearchSpace | search_space |
Dataset & | data |
Parameters | parameters |
Private Attributes | |
Bandit | variation_bandit |
map< DataType, Bandit > | terminal_bandits |
map< DataType, map< size_t, Bandit > > | op_bandits |
Constants_simplifier | constants_simplifier |
Inexact_simplifier | inexact_simplifier |
Class representing the variation operators in Brush.
The Variation class is responsible for performing individual-level variations and handling the variation of a population in Brush. It contains methods for crossing individuals, mutating individuals, and varying a population.
Definition at line 44 of file variation.h.
|
default |
Default constructor.
|
inline |
Constructor that initializes the Variation object with parameters and search space.
params | The parameters for the variation operator. |
ss | The search space for the variation operator. |
Definition at line 57 of file variation.h.
|
inline |
Destructor.
Definition at line 68 of file variation.h.
|
inline |
Definition at line 483 of file variation.h.
|
inline |
Definition at line 559 of file variation.h.
|
inline |
Definition at line 516 of file variation.h.
|
inline |
std::optional< Individual< T > > Brush::Var::Variation< T >::cross | ( | const Individual< T > & | mom, |
const Individual< T > & | dad ) |
Performs croearch_spaceover operation on two individuals.
Stochastically swaps subtrees between root and other, returning a new program.
mom | The first parent individual. |
dad | The second parent individual. |
The spot where the cross will take place in the root
parent is sampled based on attribute get_prob_change
of each node in the tree. After selecting the cross spot, the program will iterate through the other
parent searching for all compatible sub-trees to replace.
Due to the stochastic behavior, it may come to a case where there is no candidate to replace the spot node. In this case, the method returns std::nullopt
(and has overloads so it can be used in a boolean context).
If the cross succeeds, the child program can be accessed through the .value()
attribute of the std::optional
. TODO: update this documentation (it doesnt take the program but the individual. also update mutation documentation) This means that, if you use the cross as auto opt = mutate(parent, SS)
, either opt==false
or opt.value()
contains the child.
T | the program type |
root | the root parent |
other | the donating parent |
std::optional
that may contain the child program of type T
Definition at line 416 of file variation.cpp.
|
inline |
Initializes the Variation object with parameters and search space.
params | The parameters for the variation operator. |
ss | The search space for the variation operator. |
Definition at line 76 of file variation.h.
|
inline |
Definition at line 581 of file variation.h.
std::optional< Individual< T > > Brush::Var::Variation< T >::mutate | ( | const Individual< T > & | parent, |
string | choice = "" ) |
Performs mutation operation on an individual.
Stochastically mutate a program.
parent | The parent individual. |
Types of mutation:
Every mutation has a probability (weight) based on global parameters. The spot where the mutation will take place is sampled based on attribute get_prob_change
of each node in the tree. Inside each type of mutation, when a new node is inserted, it is sampled based on terminal_weights
.
Due to the stochastic behavior, and the several sampling steps, it may come to a case where the search space does not hold any possible modification to do in the program. In this case, the method returns std::nullopt
(and has overloads so it can be used in a boolean context).
If the mutation succeeds, the mutated program can be accessed through the .value()
attribute of the std::optional
. This method will try to generate a child that is within size and depth constraints. If ater three attempts it failed to generate a solution under these conditions, then the last model is returned regardless of it's size and depth. This loosens the size and depth constraints by a small factor (the final program will have at most 3*max_arity
extra nodes), and it is better than the alternatives of just replicating the parent or generating a new random solution to add to the population.
This means that, if you use the mutation as auto opt = mutate(parent, SS)
, either opt==false
or opt.value()
contains the child program.
T | program type |
parent | the program to be mutated |
SS | a search space |
std::optional
that may contain the child program of type T
Definition at line 560 of file variation.cpp.
void Brush::Var::Variation< T >::update_ss | ( | ) |
Updates the probability distribution sampling for variation and nodes based on the given rewards.
pop | The population to update the selection strategy for. |
rewards | The rewards obtained from the evaluation of individuals. |
Definition at line 760 of file variation.cpp.
void Brush::Var::Variation< T >::vary | ( | Population< T > & | pop, |
int | island, | ||
const vector< size_t > & | parents ) |
Handles variation of a population.
pop | The population to be varied. |
island | The island index. |
parents | The indices of the parent individuals. |
p | The parameters for the variation operator. |
Definition at line 674 of file variation.cpp.
|
inline |
Varies a population and updates the selection strategy based on rewards.
This function performs variation on a population, calculates rewards, and updates the selection strategy based on the obtained rewards.
pop | The population to be varied and updated. |
island | The island index. |
parents | The indices of the parent individuals. |
Definition at line 220 of file variation.h.
|
private |
Definition at line 603 of file variation.h.
Dataset& Brush::Var::Variation< T >::data |
Definition at line 592 of file variation.h.
|
private |
Definition at line 604 of file variation.h.
|
private |
Definition at line 600 of file variation.h.
Parameters Brush::Var::Variation< T >::parameters |
Definition at line 593 of file variation.h.
SearchSpace Brush::Var::Variation< T >::search_space |
Definition at line 591 of file variation.h.
|
private |
Definition at line 599 of file variation.h.
|
private |
Definition at line 598 of file variation.h.