Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
evaluation.cpp
Go to the documentation of this file.
1#include "evaluation.h"
2
3namespace Brush{
4namespace Eval{
5
6
7// fitness of population
8template<ProgramType T>
10 int island,
11 const Dataset& data,
12 const Parameters& params,
13 bool fit,
14 bool validation
15 )
16{
17 auto indices = pop.get_island_indexes(island);
18
19 for (unsigned i = 0; i<indices.size(); ++i)
20 {
21 Individual<T>& ind = *pop.individuals.at(indices.at(i)).get(); // we are modifying it, so operator[] wont work
22
23 if (false) // pass
24 {
27
28 ind.error = MAX_FLT*VectorXf::Ones(data.y.size());
29 }
30 else
31 {
32 // assign weights to individual
33 if (fit && ind.get_is_fitted() == false)
34 {
35 ind.program.fit(data.get_training_data());
36 }
37
38 assign_fit(ind, data, params, validation);
39 }
40 }
41}
42
43// assign loss to program
44template<ProgramType T>
46 const Parameters& params, bool val)
47{
48 VectorXf errors;
49 using PT = ProgramType;
50
51 Dataset train = data.get_training_data();
52 float f = S.score(ind, train, errors, params);
53 ind.error = errors;
54
55 float f_v = f;
56 if (data.use_validation) {
57 Dataset validation = data.get_validation_data();
58
59 // when calculating validation score, we should not let
60 // it write in errors vector. That would avoid validation data leakage
61 VectorXf val_errors;
62 f_v = S.score(ind, validation, val_errors, params);
63
64 // if (val) // never use validation data here. This is used in lexicase selection
65 // ind.error = val_errors;
66 }
67
68 float error_weight = Individual<T>::weightsMap[params.scorer];
69 if (std::isnan(f) || std::isinf(f))
70 f = error_weight > 0 ? -MAX_FLT : MAX_FLT;
71 if (std::isnan(f_v) || std::isinf(f_v))
72 f_v = error_weight > 0 ? -MAX_FLT : MAX_FLT;
73
74 // This is what is going to determine the weights for the individual's fitness
75 ind.set_objectives(params.get_objectives());
76
77 // when we use these setters, it updates its previous values references
78 ind.fitness.set_loss(f);
79 ind.fitness.set_loss_v(f_v);
80 ind.fitness.set_size(ind.get_size());
83 ind.fitness.set_depth(ind.get_depth());
84
85 vector<float> values;
86 values.resize(0);
87
88 // TODO: implement a better way of switching between train and val
89 // without the burden of calculating stuff everytime
90 for (const auto& n : ind.get_objectives())
91 {
92 if (n.compare(params.scorer)==0)
93 values.push_back(val ? f_v : f);
94 else if (n.compare("complexity")==0)
95 values.push_back(ind.get_complexity());
96 else if (n.compare("linear_complexity")==0)
97 values.push_back(ind.get_linear_complexity());
98 else if (n.compare("size")==0)
99 values.push_back(ind.get_size());
100 else if (n.compare("depth")==0)
101 values.push_back(ind.get_depth());
102 else
103 HANDLE_ERROR_THROW(n+" is not a known objective");
104 }
105
106 // will use inner attributes to set the fitness object
107 ind.fitness.set_values(values);
108}
109
110} // Pop
111} // Brush
holds variable type data.
Definition data.h:51
Dataset get_validation_data() const
Definition data.cpp:199
bool use_validation
Definition data.h:88
ArrayXf y
length N array, the target label
Definition data.h:80
Dataset get_training_data() const
Definition data.cpp:198
void update_fitness(Population< T > &pop, int island, const Dataset &data, const Parameters &params, bool fit=true, bool validation=true)
Update the fitness of individuals in a population.
Definition evaluation.cpp:9
void assign_fit(Individual< T > &ind, const Dataset &data, const Parameters &params, bool val=false)
Assign fitness to an individual.
static std::map< std::string, float > weightsMap
set parent ids using id values
Definition individual.h:138
unsigned int get_complexity() const
Definition individual.h:100
unsigned int get_size() const
Definition individual.h:98
VectorXf error
training error (used in lexicase selectors)
Definition individual.h:35
Fitness fitness
aggregate fitness score
Definition individual.h:37
vector< string > get_objectives() const
Definition individual.h:151
void set_objectives(vector< string > objs)
Definition individual.h:152
unsigned int get_depth() const
Definition individual.h:99
unsigned int get_linear_complexity() const
Definition individual.h:101
Program< T > program
executable data structure
Definition individual.h:17
bool get_is_fitted() const
Definition individual.h:115
vector< size_t > get_island_indexes(int island)
Definition population.h:39
vector< std::shared_ptr< Individual< T > > > individuals
Definition population.h:19
#define HANDLE_ERROR_THROW(err)
Definition error.h:27
static float MAX_FLT
Definition init.h:61
< nsga2 selection operator for getting the front
Definition bandit.cpp:4
ProgramType PT
Definition program.h:40
ProgramType
Definition types.h:70
Namespace containing scoring functions for evaluation metrics.
void set_linear_complexity(unsigned int new_lc)
Definition fitness.h:84
void set_complexity(unsigned int new_c)
Definition fitness.h:78
void set_loss_v(float f_v)
Definition fitness.h:68
void set_depth(unsigned int new_d)
Definition fitness.h:90
void set_values(vector< float > &v)
Definition fitness.h:134
void set_size(unsigned int new_s)
Definition fitness.h:73
void set_loss(float f)
Definition fitness.h:63
vector< string > get_objectives() const
Definition params.h:150
string scorer
actual loss function used, determined by error
Definition params.h:69
Program< PType > & fit(const Dataset &d)
Definition program.h:150