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;
18
19 vector<std::shared_ptr<Individual<T>>> individuals;
20 vector<vector<size_t>> island_indexes;
21
22 Population();
24
26 void init(SearchSpace& ss, const Parameters& params);
27
28 // initialize based on list of individuals
29 void init(vector<Individual<T>>& individuals, const Parameters& params);
30
31 // save serialized population
32 void save(string filename);
33 // load serialized population
34 void load(string filename);
35
37 int size() { return individuals.size(); };
38
39 vector<size_t> get_island_indexes(int island){ return island_indexes.at(island); };
40
42 void add_offspring_indexes(int island);
43
45 void update(vector<vector<size_t>> survivors);
46
48 const Individual<T>& operator [](size_t i) const {return *individuals.at(i);}
49 const Individual<T>& operator [](size_t i) {return *individuals.at(i);}
50
52 string print_models(string sep="\n");
53
55 vector<vector<size_t>> sorted_front(unsigned rank=1);
56
57 // pareto front ignoring island divisions
58 vector<size_t> hall_of_fame(unsigned rank=1);
59
60 // perform a migration in the population. Individuals from sorted front or hall of fame will replace others by the
61 // probability set in parameters. Expects a population without offspring
62 void migrate();
63
66 {
69 bool operator()(size_t i, size_t j)
70 {
71 return pop[i].fitness.complexity < pop[j].fitness.complexity;
72 }
73 };
74
76 {
79 bool operator()(size_t i, size_t j)
80 {
81 return pop[i].fitness.linear_complexity < pop[j].fitness.linear_complexity;
82 }
83 };
84
87 {
90 bool operator()(size_t i, size_t j)
91 {
92 return (pop[i].fitness == pop[j].fitness);
93
94 // return (pop[i].fitness == pop[j].fitness
95 // && pop[i].fitness.complexity == pop[j].fitness.complexity);
96 }
97 };
98};
99
101 individuals, island_indexes, pop_size, num_islands, mig_prob, linear_complexity);
103 individuals, island_indexes, pop_size, num_islands, mig_prob, linear_complexity);
105 individuals, island_indexes, pop_size, num_islands, mig_prob, linear_complexity);
107 individuals, island_indexes, pop_size, num_islands, mig_prob, linear_complexity);
108
109}// Pop
110}// Brush
111
112#endif
const Individual< T > & operator[](size_t i) const
setting and getting from individuals vector (will ignore islands)
Definition population.h:48
vector< size_t > get_island_indexes(int island)
Definition population.h:39
bool linear_complexity
Indicates if the user set linear_complexity instead of recursive complexity.
Definition population.h:17
int size()
returns population size (the effective size of the individuals)
Definition population.h:37
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:19
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:20
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, linear_complexity)
< nsga2 selection operator for getting the front
Definition bandit.cpp:4
bool operator()(size_t i, size_t j)
Definition population.h:90
bool operator()(size_t i, size_t j)
Definition population.h:69
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...