Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
Brush::Var::Variation< T > Class Template Reference

Class representing the variation operators in Brush. More...

#include <variation.h>

Collaboration diagram for Brush::Var::Variation< T >:

Public Member Functions

 Variation ()=default
 Default constructor.
 
 Variation (Parameters &params, 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::tuple< std::optional< Individual< T > >, VectorXf > cross (const Individual< T > &mom, const Individual< T > &dad)
 Performs croearch_spaceover operation on two individuals.
 
std::tuple< std::optional< Individual< T > >, VectorXf > 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)
 Varies a population and updates the selection strategy based on rewards.
 
std::optional< Nodebandit_sample_terminal (DataType R, VectorXf &context)
 
std::optional< Nodebandit_get_node_like (Node node, VectorXf &context)
 
std::optional< Nodebandit_sample_op_with_arg (DataType ret, DataType arg, VectorXf &context, int max_args=0)
 
std::optional< Nodebandit_sample_op (DataType ret, VectorXf &context)
 
VectorXf get_context (const Program< T > &program, Iter spot)
 

Public Attributes

SearchSpace search_space
 
Datasetdata
 
Parameters parameters
 

Private Attributes

Bandit< stringvariation_bandit
 
map< DataType, Bandit< string > > terminal_bandits
 
map< DataType, map< size_t, Bandit< string > > > op_bandits
 
Constants_simplifier constants_simplifier
 
Inexact_simplifier inexact_simplifier
 

Detailed Description

template<ProgramType T>
class Brush::Var::Variation< T >

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.

Constructor & Destructor Documentation

◆ Variation() [1/2]

template<ProgramType T>
Brush::Var::Variation< T >::Variation ( )
default

Default constructor.

◆ Variation() [2/2]

template<ProgramType T>
Brush::Var::Variation< T >::Variation ( Parameters & params,
SearchSpace & ss,
Dataset & d )
inline

Constructor that initializes the Variation object with parameters and search space.

Parameters
paramsThe parameters for the variation operator.
ssThe search space for the variation operator.

Definition at line 57 of file variation.h.

Here is the call graph for this function:

◆ ~Variation()

template<ProgramType T>
Brush::Var::Variation< T >::~Variation ( )
inline

Destructor.

Definition at line 68 of file variation.h.

Member Function Documentation

◆ bandit_get_node_like()

template<ProgramType T>
std::optional< Node > Brush::Var::Variation< T >::bandit_get_node_like ( Node node,
VectorXf & context )
inline

Definition at line 538 of file variation.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bandit_sample_op()

template<ProgramType T>
std::optional< Node > Brush::Var::Variation< T >::bandit_sample_op ( DataType ret,
VectorXf & context )
inline

Definition at line 621 of file variation.h.

◆ bandit_sample_op_with_arg()

template<ProgramType T>
std::optional< Node > Brush::Var::Variation< T >::bandit_sample_op_with_arg ( DataType ret,
DataType arg,
VectorXf & context,
int max_args = 0 )
inline

Definition at line 577 of file variation.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bandit_sample_terminal()

template<ProgramType T>
std::optional< Node > Brush::Var::Variation< T >::bandit_sample_terminal ( DataType R,
VectorXf & context )
inline

Definition at line 509 of file variation.h.

Here is the caller graph for this function:

◆ cross()

template<Brush::ProgramType T>
std::tuple< std::optional< Individual< T > >, VectorXf > 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.

Parameters
momThe first parent individual.
dadThe second parent individual.
Returns
An optional containing the offspring individual if the crossover is successful, or an empty optional otherwise.

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.

Template Parameters
Tthe program type
Parameters
rootthe root parent
otherthe donating parent
Returns
std::optional that may contain the child program of type T

Definition at line 425 of file variation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_context()

template<ProgramType T>
VectorXf Brush::Var::Variation< T >::get_context ( const Program< T > & program,
Iter spot )
inline

Definition at line 648 of file variation.h.

Here is the caller graph for this function:

◆ init()

template<ProgramType T>
void Brush::Var::Variation< T >::init ( )
inline

Initializes the Variation object with parameters and search space.

Parameters
paramsThe parameters for the variation operator.
ssThe search space for the variation operator.

Definition at line 76 of file variation.h.

Here is the caller graph for this function:

◆ mutate()

template<Brush::ProgramType T>
std::tuple< std::optional< Individual< T > >, VectorXf > Brush::Var::Variation< T >::mutate ( const Individual< T > & parent,
string choice = "" )

Performs mutation operation on an individual.

Stochastically mutate a program.

Parameters
parentThe parent individual.
Returns
An optional containing the mutated individual if the mutation is successful, or an empty optional otherwise.

Types of mutation:

  • point mutation changes a single node.
  • insertion mutation inserts a node as the parent of an existing node, and fills in the other arguments.
  • deletion mutation deletes a node.
  • subtree mutation inserts a new subtree into the program.
  • toggle_weight_on mutation turns a node's weight ON.
  • toggle_weight_off mutation turns a node's weight OFF.

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 means that, if you use the mutation as auto opt = mutate(parent, SS), either opt==false or opt.value() contains the child program.

Template Parameters
Tprogram type
Parameters
parentthe program to be mutated
SSa search space
Returns
std::optional that may contain the child program of type T

Definition at line 559 of file variation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_ss()

template<Brush::ProgramType T>
void Brush::Var::Variation< T >::update_ss ( )

Updates the probability distribution sampling for variation and nodes based on the given rewards.

Parameters
popThe population to update the selection strategy for.
rewardsThe rewards obtained from the evaluation of individuals.

Definition at line 772 of file variation.cpp.

Here is the caller graph for this function:

◆ vary()

template<Brush::ProgramType T>
void Brush::Var::Variation< T >::vary ( Population< T > & pop,
int island,
const vector< size_t > & parents )

Handles variation of a population.

Parameters
popThe population to be varied.
islandThe island index.
parentsThe indices of the parent individuals.
pThe parameters for the variation operator.

Definition at line 683 of file variation.cpp.

Here is the call graph for this function:

◆ vary_and_update()

template<ProgramType T>
void Brush::Var::Variation< T >::vary_and_update ( Population< T > & pop,
int island,
const vector< size_t > & parents,
const Dataset & data,
Evaluation< T > & evaluator )
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.

Parameters
popThe population to be varied and updated.
islandThe island index.
parentsThe indices of the parent individuals.

Definition at line 225 of file variation.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ constants_simplifier

template<ProgramType T>
Constants_simplifier Brush::Var::Variation< T >::constants_simplifier
private

Definition at line 664 of file variation.h.

◆ data

template<ProgramType T>
Dataset& Brush::Var::Variation< T >::data

Definition at line 653 of file variation.h.

◆ inexact_simplifier

template<ProgramType T>
Inexact_simplifier Brush::Var::Variation< T >::inexact_simplifier
private

Definition at line 665 of file variation.h.

◆ op_bandits

template<ProgramType T>
map<DataType, map<size_t, Bandit<string> > > Brush::Var::Variation< T >::op_bandits
private

Definition at line 661 of file variation.h.

◆ parameters

template<ProgramType T>
Parameters Brush::Var::Variation< T >::parameters

Definition at line 654 of file variation.h.

◆ search_space

template<ProgramType T>
SearchSpace Brush::Var::Variation< T >::search_space

Definition at line 652 of file variation.h.

◆ terminal_bandits

template<ProgramType T>
map<DataType, Bandit<string> > Brush::Var::Variation< T >::terminal_bandits
private

Definition at line 660 of file variation.h.

◆ variation_bandit

template<ProgramType T>
Bandit<string> Brush::Var::Variation< T >::variation_bandit
private

Definition at line 659 of file variation.h.


The documentation for this class was generated from the following files: