Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
engine.h
Go to the documentation of this file.
1/* Brush
2copyright 2020 William La Cava
3license: GNU/GPL v3
4*/
5
6#ifndef Engine_H
7#define Engine_H
8
9#include "util/rnd.h"
10#include "init.h"
11#include "params.h"
12#include "eval/evaluation.h"
13#include "vary/variation.h"
14#include "pop/population.h"
15#include "pop/archive.h"
16#include "selection/selection.h"
17#include "util/csv.h"
18
19#include "taskflow/taskflow.hpp"
20#include <taskflow/algorithm/for_each.hpp>
21
22namespace Brush
23{
24
25using namespace Pop;
26using namespace Sel;
27using namespace Eval;
28using namespace Var;
29using namespace nlohmann;
30
31template <ProgramType T>
44class Engine{
45public:
47 {
48 this->params = Parameters();
49 this->ss = SearchSpace();
50 };
51
53 {
54 this->params = p;
55 this->ss = s;
56 // TODO: make variation to have a default constructor
57 // this->variator(Variation<T>(params, ss)) ;
58 };
59
61
62 // outputs a progress bar, filled according to @param percentage.
63 void print_progress(float percentage);
65 void print_stats(float fraction);
66
67 // all hyperparameters are controlled by the parameter class. please refer to that to change something
68 inline Parameters& get_params(){return params;}
69 inline void set_params(Parameters& p){params=p;}
70
71 inline SearchSpace& get_search_space() { return ss; }
72 inline void set_search_space(SearchSpace& space) { ss = space; }
73
74 inline bool get_is_fitted(){return is_fitted;}
75
78
80
82 run(data);
83 return *this;
84 };
85 Engine<T> &fit(const Ref<const ArrayXXf>& X, const Ref<const ArrayXf>& y)
86 {
87 // The dataset constructor may shuffle its validation split. Seed before
88 // constructing it so the native X/y entry point is reproducible too.
89 r.set_seed(params.get_random_state());
90
91 // Using constructor 2 to create the dataset
95
96 return fit(d);
97 };
98
99 auto predict(const Dataset& data) { return this->best_ind.predict(data); };
100 auto predict(const Ref<const ArrayXXf>& X)
101 {
102 Dataset d(X);
103 return predict(d);
104 };
105
106 template <ProgramType P = T>
107 requires((P == PT::BinaryClassifier) || (P == PT::MulticlassClassifier))
108 auto predict_proba(const Dataset &d) { return this->best_ind.predict_proba(d); };
109 template <ProgramType P = T>
110 requires((P == PT::BinaryClassifier) || (P == PT::MulticlassClassifier))
111 auto predict_proba(const Ref<const ArrayXXf>& X)
112 {
113 Dataset d(X);
114 return predict_proba(d);
115 };
116
118 int get_archive_size(){ return this->archive.individuals.size(); };
119
121 vector<json> get_archive_as_json();
123 void set_population_from_json(vector<json> pop_vector);
124
126 vector<Individual<T>> get_archive();
127 vector<Individual<T>> get_population();
128 void set_population(vector<Individual<T>> pop_vector);
129
130 // locking and unlocking parts of the solutions
131 void lock_nodes(int end_depth=0, bool keep_leaves_unlocked=true, bool keep_current_weights=false);
132
133 // TODO: predict/predict_proba/archive with longitudinal data
134
136 void run(Dataset &d);
137
140
144
145 bool is_fitted = false;
146private:
150
152
154
156 struct PopSummary {
157 unsigned n_individuals = 0;
158 float best_score = 0.0f;
159 float best_score_v = 0.0f;
160 float med_score = 0.0f;
161 float med_score_v = 0.0f;
162 unsigned med_size = 0;
163 unsigned med_complexity = 0;
164 unsigned max_size = 0;
165 unsigned max_complexity = 0;
166 };
167 PopSummary summarize(const vector<size_t>& indices) const;
168
169 // run logs, written when params.logfile is set
170 string run_id;
174
175 void open_logs();
176 void log_stats(unsigned stall_count, const SimplificationRecords& simplifications);
178
179 void init();
180
182 inline void set_is_fitted(bool f){is_fitted=f;}
183};
184
185// TODO: should I serialize data and search space as well?
186// Only stuff to make new predictions should be serialized
187NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Engine<PT::Regressor>, params, best_ind, archive, pop, ss, is_fitted);
188NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Engine<PT::BinaryClassifier>, params, best_ind, archive, pop, ss, is_fitted);
189NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Engine<PT::MulticlassClassifier>, params, best_ind, archive, pop, ss, is_fitted);
190NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Engine<PT::Representer>, params, best_ind, archive, pop, ss, is_fitted);
191
192extern template class Engine<PT::Regressor>;
193extern template class Engine<PT::BinaryClassifier>;
194extern template class Engine<PT::MulticlassClassifier>;
195extern template class Engine<PT::Representer>;
196
197} // Brush
198#endif
holds variable type data.
Definition data.h:51
The Engine class represents the core engine of the brush library.
Definition engine.h:44
auto predict(const Dataset &data)
Definition engine.h:99
void calculate_stats()
Definition engine.cpp:147
auto predict_proba(const Dataset &d)
Definition engine.h:108
SearchSpace & get_search_space()
Definition engine.h:71
Individual< T > best_ind
Definition engine.h:141
void print_progress(float percentage)
Definition engine.cpp:41
Engine(Parameters &p, SearchSpace &s)
Definition engine.h:52
void close_logs()
Definition engine.cpp:307
Engine< T > & fit(Dataset &data)
Definition engine.h:81
void set_population(vector< Individual< T > > pop_vector)
Definition engine.cpp:440
auto predict(const Ref< const ArrayXXf > &X)
Definition engine.h:100
void set_search_space(SearchSpace &space)
Definition engine.h:72
Util::CsvWriter log_islands
Definition engine.h:172
void print_stats(float fraction)
Definition engine.cpp:315
void log_stats(unsigned stall_count, const SimplificationRecords &simplifications)
Definition engine.cpp:247
Parameters & get_params()
Definition engine.h:68
Util::CsvWriter log_generations
Definition engine.h:171
Individual< T > & get_best_ind()
Definition engine.h:79
void set_params(Parameters &p)
Definition engine.h:69
auto predict_proba(const Ref< const ArrayXXf > &X)
Definition engine.h:111
void set_is_fitted(bool f)
set flag indicating whether fit has been called
Definition engine.h:182
int get_archive_size()
return archive size
Definition engine.h:118
PopSummary summarize(const vector< size_t > &indices) const
Definition engine.cpp:57
vector< json > get_archive_as_json()
return archive/population as strings
Definition engine.cpp:348
Util::CsvWriter log_simplifications
Definition engine.h:173
void lock_nodes(int end_depth=0, bool keep_leaves_unlocked=true, bool keep_current_weights=false)
Definition engine.cpp:459
Evaluation< T > evaluator
Definition engine.h:148
bool get_is_fitted()
Definition engine.h:74
Engine< T > & fit(const Ref< const ArrayXXf > &X, const Ref< const ArrayXf > &y)
Definition engine.h:85
void open_logs()
Definition engine.cpp:211
vector< Individual< T > > get_population()
Definition engine.cpp:396
void init()
initialize Feat object for fitting.
Definition engine.cpp:21
vector< json > get_population_as_json()
Definition engine.cpp:363
vector< Individual< T > > get_archive()
return archive/popularion as individuals ready to use
Definition engine.cpp:385
bool update_best()
updates best score by searching in the population for the individual that best fits the given data
Definition engine.cpp:479
void set_population_from_json(vector< json > pop_vector)
Definition engine.cpp:414
Class for evaluating the fitness of individuals in a population.
Definition evaluation.h:27
Minimal RFC 4180 CSV writer used by the run logs.
Definition csv.h:34
class for timing things.
Definition utils.h:270
std::vector< SimplificationRecord > SimplificationRecords
Definition record.h:22
< nsga2 selection operator for getting the front
Definition bandit.cpp:3
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Engine< PT::Regressor >, params, best_ind, archive, pop, ss, is_fitted)
summary statistics of a subset of the population (e.g. an island)
Definition engine.h:156
float best_score
best train loss
Definition engine.h:158
float best_score_v
best validation loss (may be another individual)
Definition engine.h:159
float batch_size
Definition params.h:83
bool shuffle_split
Definition params.h:79
float validation_size
Definition params.h:80
int get_random_state()
Definition params.h:102
bool classification
Definition params.h:75
vector< string > feature_names
Definition params.h:81
vector< string > feature_types
Definition params.h:82
The Archive struct represents a collection of individual programs.
Definition archive.h:26
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...
SearchSpace()=default
interfaces with selection operators.
Definition selection.h:25