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 // OBS: always use get_objectives, as it will replace the "scorer" string with
76 // the actual scorer function name
77 ind.set_objectives(params.get_objectives());
78
79 // when we use these setters, it updates its previous values references
80 ind.fitness.set_loss(f);
81 ind.fitness.set_loss_v(f_v);
82 ind.fitness.set_size(ind.get_size());
85 ind.fitness.set_depth(ind.get_depth());
86
87 vector<float> values;
88 values.resize(0);
89
90 // TODO: implement a better way of switching between train and val
91 // without the burden of calculating stuff everytime
92 for (const auto& n : ind.get_objectives())
93 {
94 // TODO: this should be done in the fitness class when calling wvalues() or values()
95 if (n.compare(params.scorer)==0)
96 values.push_back(val ? f_v : f);
97 else if (n.compare("complexity")==0)
98 values.push_back(ind.get_complexity());
99 else if (n.compare("linear_complexity")==0)
100 values.push_back(ind.get_linear_complexity());
101 else if (n.compare("size")==0)
102 values.push_back(ind.get_size());
103 else if (n.compare("depth")==0)
104 values.push_back(ind.get_depth());
105 else
106 HANDLE_ERROR_THROW(n+" is not a known objective");
107 }
108
109 // will use inner attributes to set the fitness object
110 ind.fitness.set_values(values);
111}
112
113} // Pop
114} // Brush
holds variable type data.
Definition data.h:51
Dataset get_validation_data() const
Definition data.cpp:198
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:197
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:139
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:152
void set_objectives(vector< string > objs)
Definition individual.h:153
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:80
void set_complexity(unsigned int new_c)
Definition fitness.h:75
void set_loss_v(float f_v)
Definition fitness.h:67
void set_depth(unsigned int new_d)
Definition fitness.h:85
void set_values(vector< float > &v)
Definition fitness.h:129
void set_size(unsigned int new_s)
Definition fitness.h:71
void set_loss(float f)
Definition fitness.h:63
vector< string > get_objectives() const
Definition params.h:145
string scorer
actual loss function used, determined by error
Definition params.h:63
Program< PType > & fit(const Dataset &d)
Definition program.h:150