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{
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 bool pass = false;
24
25 if (pass)
26 {
27 ind.fitness.loss = MAX_FLT;
28 ind.fitness.loss_v = MAX_FLT;
29 ind.error = MAX_FLT*VectorXf::Ones(data.y.size());
30 }
31 else
32 {
33 // assign weights to individual
34 if (fit && ind.get_is_fitted() == false)
35 {
36 ind.program.fit(data);
37 }
38
39 assign_fit(ind, data, params, validation);
40 }
41 }
42}
43
44// assign loss to program
45template<ProgramType T>
47 const Parameters& params, bool val)
48{
49 VectorXf errors;
50 using PT = ProgramType;
51
53 float f = S.score(ind, train, errors, params);
54
55 float f_v = f;
56 if (data.use_validation) {
58 f_v = S.score(ind, validation, errors, params);
59 }
60
61 // TODO: implement the class weights and use it here (and on errors)
62
63 ind.set_objectives(params.objectives);
64
65 // we will always set all values for fitness (regardless of being used).
66 // this will make sure the information is calculated and ready to be used
67 // regardless of how the program is set to run.
68 ind.error = errors;
69 ind.fitness.set_loss(f);
70 ind.fitness.set_loss_v(f_v);
71 ind.fitness.set_size(ind.get_size());
72 ind.fitness.set_complexity(ind.get_complexity());
73 ind.fitness.set_depth(ind.get_depth());
74
75 vector<float> values;
76 values.resize(0);
77
78 for (const auto& n : ind.get_objectives())
79 {
80 if (n.compare("error")==0)
81 values.push_back(val ? f_v : f);
82 else if (n.compare("complexity")==0)
83 values.push_back(ind.program.complexity());
84 else if (n.compare("size")==0)
85 values.push_back(ind.program.size());
86 else if (n.compare("depth")==0)
87 values.push_back(ind.program.depth());
88 else
89 HANDLE_ERROR_THROW(n+" is not a known objective");
90 }
91
92 // will use inner attributes to set the fitness object
93 ind.fitness.set_values(values);
94}
95
96} // Pop
97} // Brush
void bind_engine(py::module &m, string name)
holds variable type data.
Definition data.h:51
Dataset get_validation_data() const
Definition data.cpp:174
bool use_validation
Definition data.h:84
ArrayXf y
length N array, the target label
Definition data.h:76
Dataset get_training_data() const
Definition data.cpp:173
void update_fitness(Population< T > &pop, int island, const Dataset &data, const Parameters &params, bool fit=true, bool validation=false)
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.
vector< size_t > get_island_indexes(int island)
Definition population.h:38
vector< std::shared_ptr< Individual< T > > > individuals
Definition population.h:18
#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 data.cpp:12
ProgramType
Definition types.h:70
Namespace containing scoring functions for evaluation metrics.
vector< string > objectives
Definition params.h:38