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 {
49 auto& ind_ptr = individuals.at(i);
50 if (!ind_ptr) {
51 HANDLE_ERROR_THROW(fmt::format("Attempted to access null individual at index {}", i));
52 }
53 return *ind_ptr;
54 }
55 const Individual<T>& operator [](size_t i) {
56 auto& ind_ptr = individuals.at(i);
57 if (!ind_ptr) {
58 HANDLE_ERROR_THROW(fmt::format("Attempted to access null individual at index {}", i));
59 }
60 return *ind_ptr;
61 }
62
64 string print_models(string sep="\n");
65
67 vector<vector<size_t>> sorted_front(unsigned rank=1);
68
69 // pareto front ignoring island divisions
70 vector<size_t> hall_of_fame(unsigned rank=1);
71
72 // perform a migration in the population. Individuals from sorted front or hall of fame will replace others by the
73 // probability set in parameters. Expects a population without offspring
74 void migrate();
75
78 {
81 bool operator()(size_t i, size_t j)
82 {
83 return pop[i].fitness.complexity < pop[j].fitness.complexity;
84 }
85 };
86
88 {
91 bool operator()(size_t i, size_t j)
92 {
93 return pop[i].fitness.linear_complexity < pop[j].fitness.linear_complexity;
94 }
95 };
96
99 {
102 bool operator()(size_t i, size_t j)
103 {
104 return (pop[i].fitness == pop[j].fitness);
105
106 // return (pop[i].fitness == pop[j].fitness
107 // && pop[i].fitness.complexity == pop[j].fitness.complexity);
108 }
109 };
110};
111
113 individuals, island_indexes, pop_size, num_islands, mig_prob, linear_complexity);
115 individuals, island_indexes, pop_size, num_islands, mig_prob, linear_complexity);
117 individuals, island_indexes, pop_size, num_islands, mig_prob, linear_complexity);
119 individuals, island_indexes, pop_size, num_islands, mig_prob, linear_complexity);
120
121}// Pop
122}// Brush
123
124#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
#define HANDLE_ERROR_THROW(err)
Definition error.h:27
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:102
bool operator()(size_t i, size_t j)
Definition population.h:81
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...