Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
population.h
Go to the documentation of this file.
1#ifndef POPULATION_H
2#define POPULATION_H
3
4#include "../util/utils.h"
5#include "../util/error.h"
6#include "../ind/individual.h"
7
8namespace Brush {
9namespace Pop {
10
11template<ProgramType T>
13public:
14 size_t pop_size;
16 float mig_prob;
17
18 vector<std::shared_ptr<Individual<T>>> individuals;
19 vector<vector<size_t>> island_indexes;
20
21 Population();
23
25 void init(SearchSpace& ss, const Parameters& params);
26
27 // initialize based on list of individuals
28 void init(vector<Individual<T>>& individuals, const Parameters& params);
29
30 // save serialized population
31 void save(string filename);
32 // load serialized population
33 void load(string filename);
34
36 int size() { return individuals.size(); };
37
38 vector<size_t> get_island_indexes(int island){ return island_indexes.at(island); };
39
42
44 void update(vector<vector<size_t>> survivors);
45
47 const Individual<T>& operator [](size_t i) const {return *individuals.at(i);}
48 const Individual<T>& operator [](size_t i) {return *individuals.at(i);}
49
51 string print_models(string sep="\n");
52
54 vector<vector<size_t>> sorted_front(unsigned rank=1);
55
56 // pareto front ignoring island divisions
57 vector<size_t> hall_of_fame(unsigned rank=1);
58
59 // perform a migration in the population. Individuals from sorted front or hall of fame will replace others by the
60 // probability set in parameters. Expects a population without offspring
61 void migrate();
62
65 {
68 bool operator()(size_t i, size_t j)
69 {
70 return pop[i].get_complexity() < pop[j].get_complexity();
71 }
72 };
73
76 {
79 bool operator()(size_t i, size_t j)
80 {
81 return pop[i].get_complexity() == pop[j].get_complexity();
82 }
83 };
84};
85
87 Population<PT::Regressor>, individuals, island_indexes, pop_size, num_islands);
89 Population<PT::BinaryClassifier>, individuals, island_indexes, pop_size, num_islands);
91 Population<PT::MulticlassClassifier>, individuals, island_indexes, pop_size, num_islands);
93 Population<PT::Representer>, individuals, island_indexes, pop_size, num_islands);
94
95}// Pop
96}// Brush
97
98#endif
void bind_engine(py::module &m, string name)
const Individual< T > & operator[](size_t i) const
setting and getting from individuals vector (will ignore islands)
Definition population.h:47
vector< size_t > get_island_indexes(int island)
Definition population.h:38
int size()
returns population size (the effective size of the individuals)
Definition population.h:36
void add_offspring_indexes(int island)
update individual vector size, distributing the expressions in num_islands
void save(string filename)
vector< vector< size_t > > sorted_front(unsigned rank=1)
return complexity-sorted Pareto front indices for each island
vector< std::shared_ptr< Individual< T > > > individuals
Definition population.h:18
void init(SearchSpace &ss, const Parameters &params)
initialize population of programs with a starting model and/or from file
string print_models(string sep="\n")
return population equations.
vector< vector< size_t > > island_indexes
Definition population.h:19
vector< size_t > hall_of_fame(unsigned rank=1)
void load(string filename)
void update(vector< vector< size_t > > survivors)
reduce programs to the indices in survivors. Not thread safe,as it removes elements
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Archive< PT::Regressor >, individuals, sort_complexity)
< nsga2 selection operator for getting the front
Definition data.cpp:12
check for same fitness and complexity to filter uniqueness.
Definition population.h:76
bool operator()(size_t i, size_t j)
Definition population.h:79
Sort each island in increasing complexity. This is not thread safe. I should set complexities of the ...
Definition population.h:65
bool operator()(size_t i, size_t j)
Definition population.h:68
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...