Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
variation.h
Go to the documentation of this file.
1/* Brush
2
3copyright 2020 William La Cava
4license: GNU/GPL v3
5*/
6#ifndef VARIATION_H
7#define VARIATION_H
8
9#include "../pop/population.h"
10
11#include <map>
12#include <optional>
13
14using namespace Brush::Pop;
15
20namespace Brush {
21namespace Var {
22
24public:
25 using Iter = tree<Node>::pre_order_iterator;
26
27 static auto find_spots(tree<Node>& Tree, const SearchSpace& SS,
28 const Parameters& params)
29 {
30 vector<float> weights(Tree.size());
31
32 // by default, mutation can happen anywhere, based on node weights
33 std::transform(Tree.begin(), Tree.end(), weights.begin(),
34 [&](const auto& n){ return n.get_prob_change();});
35
36 // Should have same size as prog.Tree.size, even if all weights <= 0.0
37 return weights;
38 }
39
40 static auto mutate(tree<Node>& Tree, Iter spot, const SearchSpace& SS,
41 const Parameters& params);
42};
43
52template<ProgramType T>
53class Variation {
54public:
58 Variation() = default;
59
67 : parameters(params)
68 , search_space(ss)
69 {};
70
75
82 void init(Parameters& params, SearchSpace& ss){
83 this->parameters = params;
84 this->search_space = ss;
85 };
86
95 std::optional<Individual<T>> cross(const Individual<T>& mom,
96 const Individual<T>& dad);
97
105 std::optional<Individual<T>> mutate(const Individual<T>& parent);
106
115 void vary(Population<T>& pop, int island, const vector<size_t>& parents);
116
117private:
118 SearchSpace search_space; // The search space for the variation operator.
119 Parameters parameters; // The parameters for the variation operator
120};
121
122} //namespace Var
123} //namespace Brush
124#endif
void bind_engine(py::module &m, string name)
tree< Node >::pre_order_iterator Iter
Definition variation.h:25
static auto find_spots(tree< Node > &Tree, const SearchSpace &SS, const Parameters &params)
Definition variation.h:27
static auto mutate(tree< Node > &Tree, Iter spot, const SearchSpace &SS, const Parameters &params)
Class representing the variation operators in Brush.
Definition variation.h:53
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.
Variation()=default
Default constructor.
SearchSpace search_space
Definition variation.h:118
~Variation()
Destructor.
Definition variation.h:74
void init(Parameters &params, SearchSpace &ss)
Initializes the Variation object with parameters and search space.
Definition variation.h:82
std::optional< Individual< T > > cross(const Individual< T > &mom, const Individual< T > &dad)
Performs crossover operation on two individuals.
Parameters parameters
Definition variation.h:119
Variation(Parameters &params, SearchSpace &ss)
Constructor that initializes the Variation object with parameters and search space.
Definition variation.h:66
< nsga2 selection operator for getting the front
Definition data.cpp:12
SearchSpace SS
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...